Skip to content

Commit

Permalink
Merge branch 'branches/rudder/8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Félix Dallidet committed Aug 25, 2023
2 parents 5116a30 + 7d8925d commit 2f5efdd
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 4 deletions.
32 changes: 29 additions & 3 deletions policies/rudderc/src/backends/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ pub mod filters {
.join("-"))
}

/// `my_method` -> `MyMethod`
/// `my_test-method` -> `MyTestMethod`
pub fn camel_case<T: Display>(s: T) -> askama::Result<String> {
Ok(s.to_string()
.split('_')
.split(['-', '_'])
.map(uppercase_first_letter)
.collect::<Vec<String>>()
.join(""))
Expand Down Expand Up @@ -156,7 +156,8 @@ fn method_call(m: Method, condition: Condition) -> Result<WindowsMethod> {
.find(|p| p.name == n)
.unwrap()
.escaping;
(n, v, p_type)
let n_formatted = filters::camel_case(n).unwrap();
(n_formatted, v, p_type)
})
.collect();

Expand Down Expand Up @@ -253,4 +254,29 @@ mod tests {
let res = canonify_condition(c).unwrap();
assert_eq!(res, r);
}

use crate::backends::windows::filters::camel_case;

#[test]
fn it_camelcase_method_params() {
let p = "packageName";
let r = "PackageName";
let res = camel_case(p).unwrap();
assert_eq!(res, r);

let p = "package-name";
let r = "PackageName";
let res = camel_case(p).unwrap();
assert_eq!(res, r);

let p = "package_name";
let r = "PackageName";
let res = camel_case(p).unwrap();
assert_eq!(res, r);

let p = "Report-Message";
let r = "ReportMessage";
let res = camel_case(p).unwrap();
assert_eq!(res, r);
}
}
2 changes: 1 addition & 1 deletion policies/rudderc/templates/technique.ps1.askama
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function {{ id|dsc_case }} {
if ($localContext.Evaluate($class)) {
$methodParams = @{
{% for arg in m.args %}
{{- arg.0|dsc_case }} = {{ arg|parameter_fmt }}
{{- arg.0 }} = {{ arg|parameter_fmt }}
{% endfor %}
}
$call = {{ m.name|dsc_case }} @methodParams -PolicyMode $policyMode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<TECHNIQUE name="Windows long param names">
<DESCRIPTION>Windows long param names</DESCRIPTION>
<USEMETHODREPORTING>true</USEMETHODREPORTING>
<MULTIINSTANCE>false</MULTIINSTANCE>
<POLICYGENERATION>separated</POLICYGENERATION>
<AGENT type="cfengine-community">
<BUNDLES>
<NAME>Windows_long_param_names</NAME>
</BUNDLES>
<FILES>
<FILE name="technique.cf">
<INCLUDED>true</INCLUDED>
</FILE>
</FILES>
</AGENT>
<AGENT type="dsc">
<BUNDLES>
<NAME>Windows-Long-Param-Names</NAME>
</BUNDLES>
<FILES>
<FILE name="technique.ps1">
<INCLUDED>true</INCLUDED>
</FILE>
</FILES>
</AGENT>
<SECTIONS>
<SECTION name="Report if condition" id="d86ce2e5-d5b6-45cc-87e8-c11cca71d907" component="true" multivalued="true">
<REPORTKEYS>
<VALUE id="d86ce2e5-d5b6-45cc-87e8-c11cca71d907">
This should be ReportMessage
</VALUE>
</REPORTKEYS>
</SECTION>
</SECTIONS>
</TECHNIQUE>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @name Windows long param names
# @version 0.1

bundle agent Windows_long_param_names {

vars:
"args" slist => {};
"report_param" string => join("_", args);
"full_class_prefix" string => canonify("Windows_long_param_names_${report_param}");
"class_prefix" string => string_head("${full_class_prefix}", "1000");

methods:
"d86ce2e5-d5b6-45cc-87e8-c11cca71d907_${report_data.directive_id}" usebundle => call_d86ce2e5_d5b6_45cc_87e8_c11cca71d907("Report if condition", "This should be ReportMessage", "d86ce2e5-d5b6-45cc-87e8-c11cca71d907", @{args}, "${class_prefix}", "This should be ReportMessage", "it_generates_properly");

}
bundle agent call_d86ce2e5_d5b6_45cc_87e8_c11cca71d907(c_name, c_key, report_id, args, class_prefix, report_message, condition) {

methods:
"d86ce2e5-d5b6-45cc-87e8-c11cca71d907_${report_data.directive_id}" usebundle => _method_reporting_context_v4("${c_name}", "${c_key}", "${report_id}");
"d86ce2e5-d5b6-45cc-87e8-c11cca71d907_${report_data.directive_id}" usebundle => report_if_condition("${report_message}", "${condition}");

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Windows-Long-Param-Names {
[CmdletBinding()]
param (
[parameter(Mandatory = $true)]
[string]$reportId,
[parameter(Mandatory = $true)]
[string]$techniqueName,

[Rudder.PolicyMode]$policyMode
)
$techniqueParams = @{

}
BeginTechniqueCall -Name $techniqueName -Parameters $techniqueParams
$reportIdBase = $reportId.Substring(0, $reportId.Length - 1)
$localContext = New-Object -TypeName "Rudder.Context" -ArgumentList @($techniqueName)
$localContext.Merge($system_classes)



$reportId=$reportIdBase + "d86ce2e5-d5b6-45cc-87e8-c11cca71d907"
$componentKey = "This should be ReportMessage"
$reportParams = @{
ClassPrefix = ([Rudder.Condition]::canonify(("report_if_condition_" + $componentKey)))
ComponentKey = $componentKey
ComponentName = "Report if condition"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = $false
TechniqueName = $techniqueName
}

$methodParams = @{
Condition = "it_generates_properly"
ReportMessage = "This should be ReportMessage"

}
$call = Report-If-Condition @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)



EndTechniqueCall -Name $techniqueName
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
id: Windows_long_param_names
name: Windows long param names
version: "0.1"
items:
- id: d86ce2e5-d5b6-45cc-87e8-c11cca71d907
method: report_if_condition
params:
condition: "it_generates_properly"
report_message: "This should be ReportMessage"
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#####################################################################################
# Copyright 2021 Normation SAS
#####################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#####################################################################################

# @name Report if condition
# @description Report a Rudder report based on a condition.
# @action
# @documentation
#
# This method will only send a Rudder report:
#
# If the **condition** is met, it will report a compliant report, with the following message:
# `**<report_message>** was correct.`
#
# Otherwise, it will report an error, with the following message:
# `**report_message** was incorrect`
#
# This method will never be in a repaired state.
#
# @parameter report_message Message subject, will be extended based on the report status
# @parameter condition Condition to report a success
#
# @class_prefix report_if_condition
# @class_parameter report_message
# @agent_support = ["cfengine-community"]

bundle agent report_if_condition(report_message, condition)
{
vars:
"old_class_prefix" string => canonify("report_if_condition_${report_message}");
"args" slist => { "${report_message}", "${condition}" };
"report_param" string => join("_", args);
"full_class_prefix" string => canonify("report_if_condition_${report_param}");
"class_prefix" string => string_head("${full_class_prefix}", "1000");

classes:
"pass2" expression => "pass1";
"pass1" expression => "any";
"local_expression" expression => "${condition}";

methods:
pass2::
"success old class class" usebundle => _classes_success("${old_class_prefix}"),
if => "local_expression";
"success class prefix" usebundle => _classes_success("${class_prefix}"),
if => "local_expression";

"error old class class" usebundle => _classes_failure("${old_class_prefix}"),
if => not("local_expression");
"error class prefix" usebundle => _classes_failure("${class_prefix}"),
if => not("local_expression");

"report" usebundle => _log_v3("${report_message}", "${report_message}", "${old_class_prefix}", "${class_prefix}", @{args}),
if => "${old_class_prefix}_reached|${class_prefix}_reached";
reports:
pass2.local_expression::
"Condition ${condition} evaluates to true";
pass2.!local_expression::
"Condition ${condition} evaluates to false";

}

0 comments on commit 2f5efdd

Please sign in to comment.