Skip to content

Commit ef7da8b

Browse files
committed
Added new hostswithgroup policy function
Changelog: Added new hostswithgroup function to get hosts from a group, selecting a specific field. This function is enterprise only. Ticket: ENT-11325 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 7b73e78 commit ef7da8b

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

examples/hostswithgroup.cf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
body common control
2+
{
3+
bundlesequence => { "example" };
4+
inputs => { "$(sys.libdir)/stdlib.cf" };
5+
}
6+
7+
8+
bundle agent example
9+
{
10+
vars:
11+
12+
am_policy_hub::
13+
"host_list" slist => hostswithgroup( "Linux", "name" );
14+
15+
files:
16+
am_policy_hub::
17+
"/tmp/master_config.cfg"
18+
edit_line => insert_lines("host=$(host_list)"),
19+
create => "true";
20+
}

libpromises/cf3.defs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,11 @@ enum cfopaction
722722
cfa_warn,
723723
};
724724

725+
enum hostswithfieldenum {
726+
HOSTS_WITH_CLASS,
727+
HOSTS_WITH_GROUP
728+
};
729+
725730
typedef enum
726731
{
727732
BACKUP_OPTION_BACKUP,

libpromises/enterprise_stubs.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ ENTERPRISE_FUNC_8ARG_DEFINE_STUB(void *, CfRegLDAP, EvalContext *, ctx, char *,
175175
return NULL;
176176
}
177177

178-
ENTERPRISE_FUNC_4ARG_DEFINE_STUB(bool, ListHostsWithClass, EvalContext *, ctx, Rlist **, return_list, char *, class_name, char *, return_format)
178+
ENTERPRISE_FUNC_5ARG_DEFINE_STUB(bool, ListHostsWithField, EvalContext *, ctx, Rlist **, return_list, char *, field_value, char *, return_format, int, function_type)
179179
{
180180
Log(LOG_LEVEL_ERR, "Host class counting is only available in CFEngine Enterprise");
181181
return false;
182182
}
183-
184183
/* cf-serverd: server_transform.c, cf-serverd.c */
185184

186185
ENTERPRISE_FUNC_3ARG_DEFINE_STUB(bool, TranslatePath, const char *, from, char *, to, size_t, to_size)

libpromises/evalfunction.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,30 @@ static FnCallResult FnCallHostsSeen(ARG_UNUSED EvalContext *ctx, ARG_UNUSED cons
737737

738738
/*********************************************************************/
739739

740-
static FnCallResult FnCallHostsWithClass(EvalContext *ctx, ARG_UNUSED const Policy *policy, ARG_UNUSED const FnCall *fp, const Rlist *finalargs)
740+
static FnCallResult FnCallHostsWithField(EvalContext *ctx, ARG_UNUSED const Policy *policy, ARG_UNUSED const FnCall *fp, const Rlist *finalargs)
741741
{
742+
assert(fp != NULL);
742743
Rlist *returnlist = NULL;
743744

744-
char *class_name = RlistScalarValue(finalargs);
745+
char *field_value = RlistScalarValue(finalargs);
745746
char *return_format = RlistScalarValue(finalargs->next);
746-
747-
if (!ListHostsWithClass(ctx, &returnlist, class_name, return_format))
747+
if (StringEqual(fp->name, "hostswithclass"))
748+
{
749+
if (!ListHostsWithField(ctx, &returnlist, field_value, return_format, HOSTS_WITH_CLASS))
750+
{
751+
return FnFailure();
752+
}
753+
}
754+
else if (StringEqual(fp->name, "hostswithgroup"))
748755
{
756+
if (!ListHostsWithField(ctx, &returnlist, field_value, return_format, HOSTS_WITH_GROUP))
757+
{
758+
return FnFailure();
759+
}
760+
}
761+
else
762+
{
763+
ProgrammingError("HostsWithField with unknown call function %s, aborting", fp->name);
749764
return FnFailure();
750765
}
751766

@@ -9754,6 +9769,13 @@ static const FnCallArg HOSTSWITHCLASS_ARGS[] =
97549769
{NULL, CF_DATA_TYPE_NONE, NULL}
97559770
};
97569771

9772+
static const FnCallArg HOSTSWITHGROUP_ARGS[] =
9773+
{
9774+
{"[a-zA-Z0-9_]+", CF_DATA_TYPE_STRING, "Group name to look for"},
9775+
{"name,address,hostkey", CF_DATA_TYPE_OPTION, "Type of return value desired"},
9776+
{NULL, CF_DATA_TYPE_NONE, NULL}
9777+
};
9778+
97579779
static const FnCallArg IFELSE_ARGS[] =
97589780
{
97599781
{NULL, CF_DATA_TYPE_NONE, NULL}
@@ -10606,7 +10628,9 @@ const FnCallType CF_FNCALL_TYPES[] =
1060610628
FNCALL_OPTION_NONE, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),
1060710629
FnCallTypeNew("hostsseen", CF_DATA_TYPE_STRING_LIST, HOSTSSEEN_ARGS, &FnCallHostsSeen, "Extract the list of hosts last seen/not seen within the last arg1 hours",
1060810630
FNCALL_OPTION_NONE, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),
10609-
FnCallTypeNew("hostswithclass", CF_DATA_TYPE_STRING_LIST, HOSTSWITHCLASS_ARGS, &FnCallHostsWithClass, "Extract the list of hosts with the given class set from the hub database (enterprise extension)",
10631+
FnCallTypeNew("hostswithclass", CF_DATA_TYPE_STRING_LIST, HOSTSWITHCLASS_ARGS, &FnCallHostsWithField, "Extract the list of hosts with the given class set from the hub database (enterprise extension)",
10632+
FNCALL_OPTION_NONE, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),
10633+
FnCallTypeNew("hostswithgroup", CF_DATA_TYPE_STRING_LIST, HOSTSWITHGROUP_ARGS, &FnCallHostsWithField, "Extract the list of hosts of a given group from the hub database (enterprise extension)",
1061010634
FNCALL_OPTION_NONE, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),
1061110635
FnCallTypeNew("hubknowledge", CF_DATA_TYPE_STRING, HUB_KNOWLEDGE_ARGS, &FnCallHubKnowledge, "Read global knowledge from the hub host by id (enterprise extension)",
1061210636
FNCALL_OPTION_CACHED, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),

libpromises/prototypes3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ENTERPRISE_FUNC_8ARG_DECLARE(void *, CfRegLDAP, EvalContext *, ctx, char *, uri,
7474
ENTERPRISE_VOID_FUNC_3ARG_DECLARE(void, CacheUnreliableValue, char *, caller, char *, handle, char *, buffer);
7575
ENTERPRISE_FUNC_3ARG_DECLARE(int, RetrieveUnreliableValue, char *, caller, char *, handle, char *, buffer);
7676
ENTERPRISE_FUNC_3ARG_DECLARE(bool, TranslatePath, const char *, from, char *, to, size_t, to_size);
77-
ENTERPRISE_FUNC_4ARG_DECLARE(bool, ListHostsWithClass, EvalContext *, ctx, Rlist **, return_list, char *, class_name, char *, return_format);
77+
ENTERPRISE_FUNC_5ARG_DECLARE(bool, ListHostsWithField, EvalContext *, ctx, Rlist **, return_list, char *, field_value, char *, return_format, int, function_type);
7878

7979
ENTERPRISE_VOID_FUNC_2ARG_DECLARE(void, CheckAndSetHAState, const char *, workdir, EvalContext *, ctx);
8080
ENTERPRISE_VOID_FUNC_0ARG_DECLARE(void, ReloadHAConfig);

0 commit comments

Comments
 (0)