diff --git a/generated/kbapi/kibana.gen.go b/generated/kbapi/kibana.gen.go index 28bad859d..69302a24b 100644 --- a/generated/kbapi/kibana.gen.go +++ b/generated/kbapi/kibana.gen.go @@ -2260,9 +2260,9 @@ const ( // Defines values for PackageListItemRelease. const ( - Beta PackageListItemRelease = "beta" - Experimental PackageListItemRelease = "experimental" - Ga PackageListItemRelease = "ga" + PackageListItemReleaseBeta PackageListItemRelease = "beta" + PackageListItemReleaseExperimental PackageListItemRelease = "experimental" + PackageListItemReleaseGa PackageListItemRelease = "ga" ) // Defines values for PackageListItemType0. @@ -2280,6 +2280,20 @@ const ( PackageListItemType2Content PackageListItemType2 = "content" ) +// Defines values for PackagePolicyRequestTypedInputStreamRelease. +const ( + PackagePolicyRequestTypedInputStreamReleaseBeta PackagePolicyRequestTypedInputStreamRelease = "beta" + PackagePolicyRequestTypedInputStreamReleaseExperimental PackagePolicyRequestTypedInputStreamRelease = "experimental" + PackagePolicyRequestTypedInputStreamReleaseGa PackagePolicyRequestTypedInputStreamRelease = "ga" +) + +// Defines values for PackagePolicyTypedInputStreamRelease. +const ( + Beta PackagePolicyTypedInputStreamRelease = "beta" + Experimental PackagePolicyTypedInputStreamRelease = "experimental" + Ga PackagePolicyTypedInputStreamRelease = "ga" +) + // Defines values for ParamsEsQueryDslRuleSearchType. const ( EsQuery ParamsEsQueryDslRuleSearchType = "esQuery" @@ -23834,9 +23848,9 @@ type PackagePolicy struct { // Id Package policy unique identifier. Id string `json:"id"` - // Inputs Package policy inputs. Refer to the integration documentation to know which inputs are available. - Inputs map[string]PackagePolicyInput `json:"inputs"` - IsManaged *bool `json:"is_managed,omitempty"` + // Inputs Package policy inputs. + Inputs PackagePolicy_Inputs `json:"inputs"` + IsManaged *bool `json:"is_managed,omitempty"` // Name Unique name for the package policy. Name string `json:"name"` @@ -23905,25 +23919,55 @@ type PackagePolicy_Elasticsearch struct { AdditionalProperties map[string]interface{} `json:"-"` } -// PackagePolicyInput defines model for package_policy_input. -type PackagePolicyInput struct { +// PackagePolicy_Inputs Package policy inputs. +type PackagePolicy_Inputs struct { + union json.RawMessage +} + +// PackagePolicyMappedInput defines model for package_policy_mapped_input. +type PackagePolicyMappedInput struct { // Enabled Enable or disable that input. Defaults to `true` (enabled). Enabled *bool `json:"enabled,omitempty"` // Streams Input streams. Refer to the integration documentation to know which streams are available. - Streams *map[string]PackagePolicyInputStream `json:"streams,omitempty"` - Vars *map[string]interface{} `json:"vars,omitempty"` + Streams *map[string]PackagePolicyMappedInputStream `json:"streams,omitempty"` + Vars *map[string]interface{} `json:"vars,omitempty"` } -// PackagePolicyInputStream defines model for package_policy_input_stream. -type PackagePolicyInputStream struct { +// PackagePolicyMappedInputStream defines model for package_policy_mapped_input_stream. +type PackagePolicyMappedInputStream struct { // Enabled Enable or disable that stream. Defaults to `true` (enabled). Enabled *bool `json:"enabled,omitempty"` Vars *map[string]interface{} `json:"vars,omitempty"` } +// PackagePolicyMappedInputs Package policy inputs. Refer to the integration documentation to know which inputs are available. +type PackagePolicyMappedInputs map[string]PackagePolicyMappedInput + // PackagePolicyRequest defines model for package_policy_request. type PackagePolicyRequest struct { + union json.RawMessage +} + +// PackagePolicyRequestMappedInput defines model for package_policy_request_mapped_input. +type PackagePolicyRequestMappedInput struct { + // Enabled Enable or disable that input. Defaults to `true` (enabled). + Enabled *bool `json:"enabled,omitempty"` + + // Streams Input streams. Refer to the integration documentation to know which streams are available. + Streams *map[string]PackagePolicyRequestMappedInputStream `json:"streams,omitempty"` + Vars *map[string]interface{} `json:"vars,omitempty"` +} + +// PackagePolicyRequestMappedInputStream defines model for package_policy_request_mapped_input_stream. +type PackagePolicyRequestMappedInputStream struct { + // Enabled Enable or disable that stream. Defaults to `true` (enabled). + Enabled *bool `json:"enabled,omitempty"` + Vars *map[string]interface{} `json:"vars,omitempty"` +} + +// PackagePolicyRequestMappedInputs defines model for package_policy_request_mapped_inputs. +type PackagePolicyRequestMappedInputs struct { // AdditionalDatastreamsPermissions Additional datastream permissions, that will be added to the agent policy. AdditionalDatastreamsPermissions *[]string `json:"additional_datastreams_permissions,omitempty"` @@ -23937,7 +23981,7 @@ type PackagePolicyRequest struct { Id *string `json:"id,omitempty"` // Inputs Package policy inputs. Refer to the integration documentation to know which inputs are available. - Inputs *map[string]PackagePolicyRequestInput `json:"inputs,omitempty"` + Inputs *map[string]PackagePolicyRequestMappedInput `json:"inputs,omitempty"` // Name Unique name for the policy. Name string `json:"name"` @@ -23960,23 +24004,6 @@ type PackagePolicyRequest struct { Vars *map[string]interface{} `json:"vars,omitempty"` } -// PackagePolicyRequestInput defines model for package_policy_request_input. -type PackagePolicyRequestInput struct { - // Enabled Enable or disable that input. Defaults to `true` (enabled). - Enabled *bool `json:"enabled,omitempty"` - - // Streams Input streams. Refer to the integration documentation to know which streams are available. - Streams *map[string]PackagePolicyRequestInputStream `json:"streams,omitempty"` - Vars *map[string]interface{} `json:"vars,omitempty"` -} - -// PackagePolicyRequestInputStream defines model for package_policy_request_input_stream. -type PackagePolicyRequestInputStream struct { - // Enabled Enable or disable that stream. Defaults to `true` (enabled). - Enabled *bool `json:"enabled,omitempty"` - Vars *map[string]interface{} `json:"vars,omitempty"` -} - // PackagePolicyRequestPackage defines model for package_policy_request_package. type PackagePolicyRequestPackage struct { ExperimentalDataStreamFeatures *[]struct { @@ -23999,11 +24026,142 @@ type PackagePolicyRequestPackage struct { Version string `json:"version"` } +// PackagePolicyRequestTypedInput defines model for package_policy_request_typed_input. +type PackagePolicyRequestTypedInput struct { + Config *map[string]interface{} `json:"config,omitempty"` + Enabled bool `json:"enabled"` + Id *string `json:"id,omitempty"` + KeepEnabled *bool `json:"keep_enabled,omitempty"` + PolicyTemplate *string `json:"policy_template,omitempty"` + Streams *[]PackagePolicyRequestTypedInputStream `json:"streams,omitempty"` + Type string `json:"type"` + Vars *map[string]interface{} `json:"vars,omitempty"` +} + +// PackagePolicyRequestTypedInputStream defines model for package_policy_request_typed_input_stream. +type PackagePolicyRequestTypedInputStream struct { + CompiledStream interface{} `json:"compiled_stream"` + Config *map[string]interface{} `json:"config,omitempty"` + DataStream struct { + Dataset string `json:"dataset"` + Elasticsearch *struct { + DynamicDataset *bool `json:"dynamic_dataset,omitempty"` + DynamicNamespace *bool `json:"dynamic_namespace,omitempty"` + Privileges *struct { + Indices *[]string `json:"indices,omitempty"` + } `json:"privileges,omitempty"` + } `json:"elasticsearch,omitempty"` + Type string `json:"type"` + } `json:"data_stream"` + Enabled bool `json:"enabled"` + Id *string `json:"id,omitempty"` + KeepEnabled *bool `json:"keep_enabled,omitempty"` + Release *PackagePolicyRequestTypedInputStreamRelease `json:"release,omitempty"` + Vars *map[string]interface{} `json:"vars,omitempty"` +} + +// PackagePolicyRequestTypedInputStreamRelease defines model for PackagePolicyRequestTypedInputStream.Release. +type PackagePolicyRequestTypedInputStreamRelease string + +// PackagePolicyRequestTypedInputs defines model for package_policy_request_typed_inputs. +type PackagePolicyRequestTypedInputs struct { + // AdditionalDatastreamsPermissions Additional datastream permissions, that will be added to the agent policy. + AdditionalDatastreamsPermissions *[]string `json:"additional_datastreams_permissions,omitempty"` + + // CloudConnectorId ID of the cloud connector associated with this package policy. + CloudConnectorId *string `json:"cloud_connector_id,omitempty"` + + // Description Package policy description + Description *string `json:"description,omitempty"` + Enabled *bool `json:"enabled,omitempty"` + Force *bool `json:"force,omitempty"` + Inputs *[]PackagePolicyRequestTypedInput `json:"inputs,omitempty"` + IsManaged *bool `json:"is_managed,omitempty"` + Name *string `json:"name,omitempty"` + + // Namespace The package policy namespace. Leave blank to inherit the agent policy's namespace. + Namespace *string `json:"namespace,omitempty"` + OutputId *string `json:"output_id,omitempty"` + + // Overrides Override settings that are defined in the package policy. The override option should be used only in unusual circumstances and not as a routine procedure. + Overrides *struct { + Inputs *map[string]interface{} `json:"inputs,omitempty"` + } `json:"overrides,omitempty"` + Package *PackagePolicyRequestPackage `json:"package,omitempty"` + + // PolicyId ID of the agent policy which the package policy will be added to. + // Deprecated: this property has been marked as deprecated upstream, but no `x-deprecated-reason` was set + PolicyId *string `json:"policy_id,omitempty"` + PolicyIds *[]string `json:"policy_ids,omitempty"` + SpaceIds *[]string `json:"spaceIds,omitempty"` + + // SupportsAgentless Indicates whether the package policy belongs to an agentless agent policy. + SupportsAgentless *bool `json:"supports_agentless,omitempty"` + + // SupportsCloudConnector Indicates whether the package policy supports cloud connectors. + SupportsCloudConnector *bool `json:"supports_cloud_connector,omitempty"` + Vars *map[string]interface{} `json:"vars,omitempty"` + Version *string `json:"version,omitempty"` +} + // PackagePolicySecretRef defines model for package_policy_secret_ref. type PackagePolicySecretRef struct { Id string `json:"id"` } +// PackagePolicyTypedInput defines model for package_policy_typed_input. +type PackagePolicyTypedInput struct { + CompiledInput interface{} `json:"compiled_input"` + + // Config Package variable (see integration documentation for more information) + Config *map[string]struct { + Frozen *bool `json:"frozen,omitempty"` + Type *string `json:"type,omitempty"` + Value interface{} `json:"value"` + } `json:"config,omitempty"` + Enabled bool `json:"enabled"` + Id *string `json:"id,omitempty"` + KeepEnabled *bool `json:"keep_enabled,omitempty"` + PolicyTemplate *string `json:"policy_template,omitempty"` + Streams []PackagePolicyTypedInputStream `json:"streams"` + Type string `json:"type"` + Vars *map[string]interface{} `json:"vars,omitempty"` +} + +// PackagePolicyTypedInputStream defines model for package_policy_typed_input_stream. +type PackagePolicyTypedInputStream struct { + CompiledStream interface{} `json:"compiled_stream"` + + // Config Package variable (see integration documentation for more information) + Config *map[string]struct { + Frozen *bool `json:"frozen,omitempty"` + Type *string `json:"type,omitempty"` + Value interface{} `json:"value"` + } `json:"config,omitempty"` + DataStream struct { + Dataset string `json:"dataset"` + Elasticsearch *struct { + DynamicDataset *bool `json:"dynamic_dataset,omitempty"` + DynamicNamespace *bool `json:"dynamic_namespace,omitempty"` + Privileges *struct { + Indices *[]string `json:"indices,omitempty"` + } `json:"privileges,omitempty"` + } `json:"elasticsearch,omitempty"` + Type string `json:"type"` + } `json:"data_stream"` + Enabled bool `json:"enabled"` + Id *string `json:"id,omitempty"` + KeepEnabled *bool `json:"keep_enabled,omitempty"` + Release *PackagePolicyTypedInputStreamRelease `json:"release,omitempty"` + Vars *map[string]interface{} `json:"vars,omitempty"` +} + +// PackagePolicyTypedInputStreamRelease defines model for PackagePolicyTypedInputStream.Release. +type PackagePolicyTypedInputStreamRelease string + +// PackagePolicyTypedInputs defines model for package_policy_typed_inputs. +type PackagePolicyTypedInputs = []PackagePolicyTypedInput + // PagerdutyConfig Defines properties for connectors when type is `.pagerduty`. type PagerdutyConfig struct { // ApiUrl The PagerDuty event URL. @@ -60869,7 +61027,7 @@ func (t SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item) AsSLO // FromSLOsTimesliceMetricBasicMetricWithField overwrites any union data inside the SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item as the provided SLOsTimesliceMetricBasicMetricWithField func (t *SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item) FromSLOsTimesliceMetricBasicMetricWithField(v SLOsTimesliceMetricBasicMetricWithField) error { - v.Aggregation = "last_value" + v.Aggregation = "cardinality" b, err := json.Marshal(v) t.union = b return err @@ -60877,7 +61035,7 @@ func (t *SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item) From // MergeSLOsTimesliceMetricBasicMetricWithField performs a merge with any union data inside the SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item, using the provided SLOsTimesliceMetricBasicMetricWithField func (t *SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item) MergeSLOsTimesliceMetricBasicMetricWithField(v SLOsTimesliceMetricBasicMetricWithField) error { - v.Aggregation = "last_value" + v.Aggregation = "cardinality" b, err := json.Marshal(v) if err != nil { return err @@ -60958,10 +61116,10 @@ func (t SLOsIndicatorPropertiesTimesliceMetric_Params_Metric_Metrics_Item) Value return nil, err } switch discriminator { + case "cardinality": + return t.AsSLOsTimesliceMetricBasicMetricWithField() case "doc_count": return t.AsSLOsTimesliceMetricDocCountMetric() - case "last_value": - return t.AsSLOsTimesliceMetricBasicMetricWithField() case "percentile": return t.AsSLOsTimesliceMetricPercentileMetric() default: @@ -71422,6 +71580,130 @@ func (t *PackageListItem_Type) UnmarshalJSON(b []byte) error { return err } +// AsPackagePolicyTypedInputs returns the union data inside the PackagePolicy_Inputs as a PackagePolicyTypedInputs +func (t PackagePolicy_Inputs) AsPackagePolicyTypedInputs() (PackagePolicyTypedInputs, error) { + var body PackagePolicyTypedInputs + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromPackagePolicyTypedInputs overwrites any union data inside the PackagePolicy_Inputs as the provided PackagePolicyTypedInputs +func (t *PackagePolicy_Inputs) FromPackagePolicyTypedInputs(v PackagePolicyTypedInputs) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergePackagePolicyTypedInputs performs a merge with any union data inside the PackagePolicy_Inputs, using the provided PackagePolicyTypedInputs +func (t *PackagePolicy_Inputs) MergePackagePolicyTypedInputs(v PackagePolicyTypedInputs) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +// AsPackagePolicyMappedInputs returns the union data inside the PackagePolicy_Inputs as a PackagePolicyMappedInputs +func (t PackagePolicy_Inputs) AsPackagePolicyMappedInputs() (PackagePolicyMappedInputs, error) { + var body PackagePolicyMappedInputs + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromPackagePolicyMappedInputs overwrites any union data inside the PackagePolicy_Inputs as the provided PackagePolicyMappedInputs +func (t *PackagePolicy_Inputs) FromPackagePolicyMappedInputs(v PackagePolicyMappedInputs) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergePackagePolicyMappedInputs performs a merge with any union data inside the PackagePolicy_Inputs, using the provided PackagePolicyMappedInputs +func (t *PackagePolicy_Inputs) MergePackagePolicyMappedInputs(v PackagePolicyMappedInputs) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +func (t PackagePolicy_Inputs) MarshalJSON() ([]byte, error) { + b, err := t.union.MarshalJSON() + return b, err +} + +func (t *PackagePolicy_Inputs) UnmarshalJSON(b []byte) error { + err := t.union.UnmarshalJSON(b) + return err +} + +// AsPackagePolicyRequestTypedInputs returns the union data inside the PackagePolicyRequest as a PackagePolicyRequestTypedInputs +func (t PackagePolicyRequest) AsPackagePolicyRequestTypedInputs() (PackagePolicyRequestTypedInputs, error) { + var body PackagePolicyRequestTypedInputs + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromPackagePolicyRequestTypedInputs overwrites any union data inside the PackagePolicyRequest as the provided PackagePolicyRequestTypedInputs +func (t *PackagePolicyRequest) FromPackagePolicyRequestTypedInputs(v PackagePolicyRequestTypedInputs) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergePackagePolicyRequestTypedInputs performs a merge with any union data inside the PackagePolicyRequest, using the provided PackagePolicyRequestTypedInputs +func (t *PackagePolicyRequest) MergePackagePolicyRequestTypedInputs(v PackagePolicyRequestTypedInputs) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +// AsPackagePolicyRequestMappedInputs returns the union data inside the PackagePolicyRequest as a PackagePolicyRequestMappedInputs +func (t PackagePolicyRequest) AsPackagePolicyRequestMappedInputs() (PackagePolicyRequestMappedInputs, error) { + var body PackagePolicyRequestMappedInputs + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromPackagePolicyRequestMappedInputs overwrites any union data inside the PackagePolicyRequest as the provided PackagePolicyRequestMappedInputs +func (t *PackagePolicyRequest) FromPackagePolicyRequestMappedInputs(v PackagePolicyRequestMappedInputs) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergePackagePolicyRequestMappedInputs performs a merge with any union data inside the PackagePolicyRequest, using the provided PackagePolicyRequestMappedInputs +func (t *PackagePolicyRequest) MergePackagePolicyRequestMappedInputs(v PackagePolicyRequestMappedInputs) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +func (t PackagePolicyRequest) MarshalJSON() ([]byte, error) { + b, err := t.union.MarshalJSON() + return b, err +} + +func (t *PackagePolicyRequest) UnmarshalJSON(b []byte) error { + err := t.union.UnmarshalJSON(b) + return err +} + // AsParamsEsQueryDslRuleIndex0 returns the union data inside the ParamsEsQueryDslRule_Index as a ParamsEsQueryDslRuleIndex0 func (t ParamsEsQueryDslRule_Index) AsParamsEsQueryDslRuleIndex0() (ParamsEsQueryDslRuleIndex0, error) { var body ParamsEsQueryDslRuleIndex0 diff --git a/generated/kbapi/transform_schema.go b/generated/kbapi/transform_schema.go index 05fbf0bca..f5962c2c5 100644 --- a/generated/kbapi/transform_schema.go +++ b/generated/kbapi/transform_schema.go @@ -17,6 +17,7 @@ import ( "strconv" "strings" + "github.com/google/go-cmp/cmp" "gopkg.in/yaml.v3" ) @@ -348,7 +349,11 @@ func (m Map) CreateRef(schema *Schema, name string, key string) Map { if reflect.DeepEqual(refTarget, existing) { writeComponent = false } else { - log.Panicf("Component schema key already in use and not an exact duplicate: %q", refPath) + log.Panicf( + "Component schema key already in use and not an exact duplicate: %q\n\n%s", + refPath, + cmp.Diff(existing, refTarget), + ) return nil } } @@ -1108,31 +1113,46 @@ func transformFleetPaths(schema *Schema) { epmPoliciesPath.Get.CreateRef(schema, "package_policy", "responses.200.content.application/json.schema.properties.items.items") epmPoliciesPath.Post.CreateRef(schema, "package_policy", "responses.200.content.application/json.schema.properties.item") - epmPoliciesPath.Post.Move("requestBody.content.application/json.schema.anyOf.1", "requestBody.content.application/json.schema") // anyOf.0 is the deprecated array format - epmPolicyPath.Put.Move("requestBody.content.application/json.schema.anyOf.1", "requestBody.content.application/json.schema") // anyOf.0 is the deprecated array format - epmPoliciesPath.Post.CreateRef(schema, "package_policy_request", "requestBody.content.application/json.schema") + epmPolicyPath.Put.CreateRef(schema, "package_policy_request_typed_inputs", "requestBody.content.application/json.schema.anyOf.0") + epmPolicyPath.Put.CreateRef(schema, "package_policy_request_mapped_inputs", "requestBody.content.application/json.schema.anyOf.1") epmPolicyPath.Put.CreateRef(schema, "package_policy_request", "requestBody.content.application/json.schema") + epmPoliciesPath.Post.Set("requestBody.content.application/json.schema", epmPolicyPath.Put.MustGetMap("requestBody.content.application/json.schema")) + epmPolicyPath.Get.CreateRef(schema, "package_policy", "responses.200.content.application/json.schema.properties.item") epmPolicyPath.Put.CreateRef(schema, "package_policy", "responses.200.content.application/json.schema.properties.item") schema.Components.CreateRef(schema, "package_policy_secret_ref", "schemas.package_policy.properties.secret_references.items") - schema.Components.Move("schemas.package_policy.properties.inputs.anyOf.1", "schemas.package_policy.properties.inputs") // anyOf.0 is the deprecated array format + schema.Components.CreateRef(schema, "package_policy_typed_inputs", "schemas.package_policy.properties.inputs.anyOf.0") + schema.Components.CreateRef(schema, "package_policy_mapped_inputs", "schemas.package_policy.properties.inputs.anyOf.1") + schema.Components.CreateRef(schema, "package_policy_typed_input", "schemas.package_policy_typed_inputs.items") + schema.Components.CreateRef(schema, "package_policy_mapped_input", "schemas.package_policy_mapped_inputs.additionalProperties") + schema.Components.CreateRef(schema, "package_policy_typed_input_stream", "schemas.package_policy_typed_input.properties.streams.items") + schema.Components.CreateRef(schema, "package_policy_mapped_input_stream", "schemas.package_policy_mapped_input.properties.streams.additionalProperties") + + schema.Components.CreateRef(schema, "package_policy_request_package", "schemas.package_policy_request_mapped_inputs.properties.package") + schema.Components.CreateRef(schema, "package_policy_request_package", "schemas.package_policy_request_typed_inputs.properties.package") - schema.Components.CreateRef(schema, "package_policy_input", "schemas.package_policy.properties.inputs.additionalProperties") - schema.Components.CreateRef(schema, "package_policy_input_stream", "schemas.package_policy_input.properties.streams.additionalProperties") + schema.Components.CreateRef(schema, "package_policy_request_mapped_input", "schemas.package_policy_request_mapped_inputs.properties.inputs.additionalProperties") + schema.Components.CreateRef(schema, "package_policy_request_mapped_input_stream", "schemas.package_policy_request_mapped_input.properties.streams.additionalProperties") - schema.Components.CreateRef(schema, "package_policy_request_package", "schemas.package_policy_request.properties.package") - schema.Components.CreateRef(schema, "package_policy_request_input", "schemas.package_policy_request.properties.inputs.additionalProperties") - schema.Components.CreateRef(schema, "package_policy_request_input_stream", "schemas.package_policy_request_input.properties.streams.additionalProperties") + schema.Components.CreateRef(schema, "package_policy_request_typed_input", "schemas.package_policy_request_typed_inputs.properties.inputs.items") + schema.Components.CreateRef(schema, "package_policy_request_typed_input_stream", "schemas.package_policy_request_typed_input.properties.streams.items") // Simplify all of the vars schema.Components.Set("schemas.package_policy.properties.vars", Map{"type": "object"}) - schema.Components.Set("schemas.package_policy_input.properties.vars", Map{"type": "object"}) - schema.Components.Set("schemas.package_policy_input_stream.properties.vars", Map{"type": "object"}) - schema.Components.Set("schemas.package_policy_request.properties.vars", Map{"type": "object"}) - schema.Components.Set("schemas.package_policy_request_input.properties.vars", Map{"type": "object"}) - schema.Components.Set("schemas.package_policy_request_input_stream.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_typed_input.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_mapped_input.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_typed_input_stream.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_mapped_input_stream.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_mapped_inputs.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_mapped_input.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_mapped_input_stream.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_typed_inputs.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_typed_input.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_typed_input.properties.config", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_typed_input_stream.properties.vars", Map{"type": "object"}) + schema.Components.Set("schemas.package_policy_request_typed_input_stream.properties.config", Map{"type": "object"}) } func setAllXOmitEmpty(key string, node Map) { diff --git a/go.mod b/go.mod index 0d3ec6a40..4162eeaee 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/disaster37/go-kibana-rest/v8 v8.5.0 github.com/elastic/elastic-transport-go/v8 v8.8.0 github.com/elastic/go-elasticsearch/v8 v8.19.0 + github.com/google/go-cmp v0.7.0 github.com/google/gofuzz v1.2.0 github.com/google/uuid v1.6.0 github.com/hashicorp/go-cty v1.5.0 @@ -25,6 +26,7 @@ require ( github.com/oapi-codegen/runtime v1.1.2 github.com/stretchr/testify v1.11.1 go.uber.org/mock v0.6.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -205,7 +207,6 @@ require ( github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/certificate-transparency-go v1.3.1 // indirect - github.com/google/go-cmp v0.7.0 // indirect github.com/google/go-containerregistry v0.20.6 // indirect github.com/google/go-github/v74 v74.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect @@ -409,7 +410,6 @@ require ( gopkg.in/mail.v2 v2.3.1 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect gotest.tools/gotestsum v1.13.0 // indirect k8s.io/klog/v2 v2.130.1 // indirect lukechampine.com/blake3 v1.2.1 // indirect diff --git a/internal/fleet/integration_policy/models.go b/internal/fleet/integration_policy/models.go index 22642da74..6192409ee 100644 --- a/internal/fleet/integration_policy/models.go +++ b/internal/fleet/integration_policy/models.go @@ -107,13 +107,20 @@ func (model *integrationPolicyModel) populateFromAPI(ctx context.Context, data * model.SpaceIds = types.SetNull(types.StringType) } // If originally set but API didn't return it, keep the original value - - model.populateInputFromAPI(ctx, data.Inputs, &diags) + mappedInputs, err := data.Inputs.AsPackagePolicyMappedInputs() + if err != nil { + diags.AddError( + "Error reading integration policy inputs", + "Could not parse integration policy inputs from API response: "+err.Error(), + ) + return diags + } + model.populateInputFromAPI(ctx, mappedInputs, &diags) return diags } -func (model *integrationPolicyModel) populateInputFromAPI(ctx context.Context, inputs map[string]kbapi.PackagePolicyInput, diags *diag.Diagnostics) { +func (model *integrationPolicyModel) populateInputFromAPI(ctx context.Context, inputs map[string]kbapi.PackagePolicyMappedInput, diags *diag.Diagnostics) { // Handle input population based on context: // 1. If model.Input is unknown: we're importing or reading fresh state → populate from API // 2. If model.Input is known and null/empty: user explicitly didn't configure inputs → don't populate (avoid inconsistent state) @@ -135,7 +142,7 @@ func (model *integrationPolicyModel) populateInputFromAPI(ctx context.Context, i // Case 3: Known and not null/empty - user configured inputs, populate from API (continue below) newInputs := utils.TransformMapToSlice(ctx, inputs, path.Root("input"), diags, - func(inputData kbapi.PackagePolicyInput, meta utils.MapMeta) integrationPolicyInputModel { + func(inputData kbapi.PackagePolicyMappedInput, meta utils.MapMeta) integrationPolicyInputModel { return integrationPolicyInputModel{ InputID: types.StringValue(meta.Key), Enabled: types.BoolPointerValue(inputData.Enabled), @@ -186,7 +193,7 @@ func (model integrationPolicyModel) toAPIModel(ctx context.Context, isUpdate boo } } - body := kbapi.PackagePolicyRequest{ + body := kbapi.PackagePolicyRequestMappedInputs{ Description: model.Description.ValueStringPointer(), Force: model.Force.ValueBoolPointer(), Name: model.Name.ValueString(), @@ -219,17 +226,27 @@ func (model integrationPolicyModel) toAPIModel(ctx context.Context, isUpdate boo } body.Inputs = utils.MapRef(utils.ListTypeToMap(ctx, model.Input, path.Root("input"), &diags, - func(inputModel integrationPolicyInputModel, meta utils.ListMeta) (string, kbapi.PackagePolicyRequestInput) { - return inputModel.InputID.ValueString(), kbapi.PackagePolicyRequestInput{ + func(inputModel integrationPolicyInputModel, meta utils.ListMeta) (string, kbapi.PackagePolicyRequestMappedInput) { + return inputModel.InputID.ValueString(), kbapi.PackagePolicyRequestMappedInput{ Enabled: inputModel.Enabled.ValueBoolPointer(), - Streams: utils.MapRef(utils.NormalizedTypeToMap[kbapi.PackagePolicyRequestInputStream](inputModel.StreamsJson, meta.Path.AtName("streams_json"), &diags)), + Streams: utils.MapRef(utils.NormalizedTypeToMap[kbapi.PackagePolicyRequestMappedInputStream](inputModel.StreamsJson, meta.Path.AtName("streams_json"), &diags)), Vars: utils.MapRef(utils.NormalizedTypeToMap[any](inputModel.VarsJson, meta.Path.AtName("vars_json"), &diags)), } })) // Note: space_ids is read-only for integration policies and inherited from the agent policy - return body, diags + var req kbapi.PackagePolicyRequest + err := req.FromPackagePolicyRequestMappedInputs(body) + if err != nil { + diags.AddError( + "Error constructing integration policy request", + "Could not convert integration policy to API request: "+err.Error(), + ) + return kbapi.PackagePolicyRequest{}, diags + } + + return req, diags } // sortInputs will sort the 'incoming' list of input definitions based on diff --git a/internal/fleet/integration_policy/models_test.go b/internal/fleet/integration_policy/models_test.go index 33c3c02f8..0f4839ee9 100644 --- a/internal/fleet/integration_policy/models_test.go +++ b/internal/fleet/integration_policy/models_test.go @@ -95,6 +95,9 @@ func TestOutputIdHandling(t *testing.T) { OutputId: &outputId, } + // Initialize inputs union to an empty mapped set to avoid JSON parse errors + _ = data.Inputs.FromPackagePolicyMappedInputs(map[string]kbapi.PackagePolicyMappedInput{}) + diags := model.populateFromAPI(context.Background(), data) require.Empty(t, diags) require.Equal(t, "test-output-id", model.OutputID.ValueString()) @@ -115,8 +118,11 @@ func TestOutputIdHandling(t *testing.T) { result, diags := model.toAPIModel(context.Background(), false, feat) require.Empty(t, diags) - require.NotNil(t, result.OutputId) - require.Equal(t, "test-output-id", *result.OutputId) + + mappedResult, err := result.AsPackagePolicyRequestMappedInputs() + require.NoError(t, err) + require.NotNil(t, mappedResult.OutputId) + require.Equal(t, "test-output-id", *mappedResult.OutputId) }) t.Run("toAPIModel_unsupported_version", func(t *testing.T) { diff --git a/internal/fleet/integration_policy/secrets.go b/internal/fleet/integration_policy/secrets.go index 0423a3798..efdf53b0a 100644 --- a/internal/fleet/integration_policy/secrets.go +++ b/internal/fleet/integration_policy/secrets.go @@ -3,6 +3,7 @@ package integration_policy import ( "context" "encoding/json" + "maps" "github.com/elastic/terraform-provider-elasticstack/generated/kbapi" "github.com/elastic/terraform-provider-elasticstack/internal/utils" @@ -113,15 +114,46 @@ func HandleRespSecrets(ctx context.Context, resp *kbapi.PackagePolicy, private p } handleVars(utils.Deref(resp.Vars)) - for _, input := range resp.Inputs { - handleVars(utils.Deref(input.Vars)) - for _, stream := range utils.Deref(input.Streams) { - handleVars(utils.Deref(stream.Vars)) + + // Mapped inputs: extract, mutate, and write back + mappedInputs, _ := resp.Inputs.AsPackagePolicyMappedInputs() + typedInputs, _ := resp.Inputs.AsPackagePolicyTypedInputs() + if mappedInputs != nil { + for _, input := range mappedInputs { + if input.Vars != nil { + handleVars(*input.Vars) + } + + if input.Streams != nil { + for _, stream := range *input.Streams { + if stream.Vars != nil { + handleVars(*stream.Vars) + } + } + } + } + // Write back the mutated data + if err := resp.Inputs.FromPackagePolicyMappedInputs(mappedInputs); err != nil { + diags.AddError("could not write back mapped inputs", err.Error()) + } + } else if typedInputs != nil { + // Typed inputs: mutate in place through pointers + for i := range typedInputs { + if typedInputs[i].Vars != nil { + handleVars(*typedInputs[i].Vars) + } + for j := range typedInputs[i].Streams { + if typedInputs[i].Streams[j].Vars != nil { + handleVars(*typedInputs[i].Streams[j].Vars) + } + } + } + if err := resp.Inputs.FromPackagePolicyTypedInputs(typedInputs); err != nil { + diags.AddError("could not write back typed inputs", err.Error()) } } - nd = secrets.Save(ctx, private) - diags.Append(nd...) + diags.Append(secrets.Save(ctx, private)...) return } @@ -188,19 +220,46 @@ func HandleReqRespSecrets(ctx context.Context, req kbapi.PackagePolicyRequest, r } } - handleVars(utils.Deref(req.Vars), utils.Deref(resp.Vars)) - for inputID, inputReq := range utils.Deref(req.Inputs) { - inputResp := resp.Inputs[inputID] - handleVars(utils.Deref(inputReq.Vars), utils.Deref(inputResp.Vars)) - streamsResp := utils.Deref(inputResp.Streams) - for streamID, streamReq := range utils.Deref(inputReq.Streams) { - streamResp := streamsResp[streamID] - handleVars(utils.Deref(streamReq.Vars), utils.Deref(streamResp.Vars)) + mappedReq, _ := req.AsPackagePolicyRequestMappedInputs() + typedReq, _ := req.AsPackagePolicyRequestTypedInputs() + + reqVars := map[string]any{} + maps.Copy(reqVars, utils.Deref(mappedReq.Vars)) + maps.Copy(reqVars, utils.Deref(typedReq.Vars)) + // Policy-level vars + if resp.Vars != nil { + handleVars(reqVars, utils.Deref(resp.Vars)) + } + + respMappedInputs, _ := resp.Inputs.AsPackagePolicyMappedInputs() + respTypedInputs, _ := resp.Inputs.AsPackagePolicyTypedInputs() + if respMappedInputs != nil { + for inputID, inputReq := range utils.Deref(mappedReq.Inputs) { + inputResp := respMappedInputs[inputID] + handleVars(utils.Deref(inputReq.Vars), utils.Deref(inputResp.Vars)) + + streamsResp := utils.Deref(inputResp.Streams) + for streamID, streamReq := range utils.Deref(inputReq.Streams) { + handleVars(utils.Deref(streamReq.Vars), utils.Deref(streamsResp[streamID].Vars)) + } + } + if err := resp.Inputs.FromPackagePolicyMappedInputs(respMappedInputs); err != nil { + diags.AddError("failed to update mapped inputs", err.Error()) + } + } else if respTypedInputs != nil { + for inputIdx, inputReq := range utils.Deref(typedReq.Inputs) { + inputResp := respTypedInputs[inputIdx] + handleVars(utils.Deref(inputReq.Vars), utils.Deref(inputResp.Vars)) + for streamIdx, streamReq := range utils.Deref(inputReq.Streams) { + handleVars(utils.Deref(streamReq.Vars), utils.Deref(inputResp.Streams[streamIdx].Vars)) + } + } + if err := resp.Inputs.FromPackagePolicyTypedInputs(respTypedInputs); err != nil { + diags.AddError("failed to update typed inputs", err.Error()) } } - nd = secrets.Save(ctx, private) - diags.Append(nd...) + diags.Append(secrets.Save(ctx, private)...) return } diff --git a/internal/fleet/integration_policy/secrets_test.go b/internal/fleet/integration_policy/secrets_test.go index 811ce25e6..aa2af6b9e 100644 --- a/internal/fleet/integration_policy/secrets_test.go +++ b/internal/fleet/integration_policy/secrets_test.go @@ -2,6 +2,7 @@ package integration_policy_test import ( "context" + "fmt" "maps" "testing" @@ -89,25 +90,32 @@ func TestHandleRespSecrets(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(fmt.Sprintf("%s - mapped inputs", tt.name), func(t *testing.T) { + var respInputs kbapi.PackagePolicy_Inputs + require.NoError(t, respInputs.FromPackagePolicyMappedInputs(kbapi.PackagePolicyMappedInputs{ + "input1": { + Streams: &map[string]kbapi.PackagePolicyMappedInputStream{"stream1": {Vars: utils.Pointer(maps.Clone(tt.input))}}, + Vars: utils.Pointer(maps.Clone(tt.input)), + }, + })) + + wantsMappedInputs := kbapi.PackagePolicyMappedInputs{ + "input1": { + Streams: &map[string]kbapi.PackagePolicyMappedInputStream{"stream1": {Vars: utils.Pointer(tt.want)}}, + Vars: &tt.want, + }, + } + var wantsInputs kbapi.PackagePolicy_Inputs + require.NoError(t, wantsInputs.FromPackagePolicyMappedInputs(wantsMappedInputs)) + resp := kbapi.PackagePolicy{ SecretReferences: secretRefs, - Inputs: map[string]kbapi.PackagePolicyInput{ - "input1": { - Streams: &map[string]kbapi.PackagePolicyInputStream{"stream1": {Vars: utils.Pointer(maps.Clone(tt.input))}}, - Vars: utils.Pointer(maps.Clone(tt.input)), - }, - }, - Vars: utils.Pointer(maps.Clone(tt.input)), + Inputs: respInputs, + Vars: utils.Pointer(maps.Clone(tt.input)), } wants := kbapi.PackagePolicy{ - Inputs: map[string]kbapi.PackagePolicyInput{ - "input1": { - Streams: &map[string]kbapi.PackagePolicyInputStream{"stream1": {Vars: utils.Pointer(tt.want)}}, - Vars: &tt.want, - }, - }, - Vars: &tt.want, + Inputs: wantsInputs, + Vars: &tt.want, } diags := integration_policy.HandleRespSecrets(ctx, &resp, &private) @@ -117,17 +125,72 @@ func TestHandleRespSecrets(t *testing.T) { want := *wants.Vars require.Equal(t, want, got) + // Re-extract mapped inputs from union to get the modified values + finalMappedInputs, err := resp.Inputs.AsPackagePolicyMappedInputs() + require.NoError(t, err) + // Input vars - got = *resp.Inputs["input1"].Vars - want = *wants.Inputs["input1"].Vars + got = *finalMappedInputs["input1"].Vars + want = *wantsMappedInputs["input1"].Vars require.Equal(t, want, got) // Stream vars - got = *(*resp.Inputs["input1"].Streams)["stream1"].Vars - want = *(*wants.Inputs["input1"].Streams)["stream1"].Vars + got = *(*finalMappedInputs["input1"].Streams)["stream1"].Vars + want = *(*wantsMappedInputs["input1"].Streams)["stream1"].Vars + require.Equal(t, want, got) // privateData + privateWants := privateData{"secrets": `{"known-secret":"secret","known-secret-1":"secret1","known-secret-2":"secret2"}`} + require.Equal(t, privateWants, private) + }) + t.Run(fmt.Sprintf("%s - typed inputs", tt.name), func(t *testing.T) { + var respInputs kbapi.PackagePolicy_Inputs + require.NoError(t, respInputs.FromPackagePolicyTypedInputs(kbapi.PackagePolicyTypedInputs{ + { + Type: "input1", + Streams: []kbapi.PackagePolicyTypedInputStream{{Vars: utils.Pointer(maps.Clone(tt.input))}}, + Vars: utils.Pointer(maps.Clone(tt.input)), + }, + })) + + wantsTypedInputs := kbapi.PackagePolicyTypedInputs{ + { + Type: "input1", + Streams: []kbapi.PackagePolicyTypedInputStream{{Vars: utils.Pointer(tt.want)}}, + Vars: &tt.want, + }, + } + var wantsInputs kbapi.PackagePolicy_Inputs + require.NoError(t, wantsInputs.FromPackagePolicyTypedInputs(wantsTypedInputs)) + + resp := kbapi.PackagePolicy{ + SecretReferences: secretRefs, + Inputs: respInputs, + Vars: utils.Pointer(maps.Clone(tt.input)), + } + wants := kbapi.PackagePolicy{ + Inputs: wantsInputs, + Vars: &tt.want, + } + + diags := integration_policy.HandleRespSecrets(ctx, &resp, &private) + require.Empty(t, diags) + // Policy vars + got := *resp.Vars + want := *wants.Vars + require.Equal(t, want, got) + + // Re-extract mapped inputs from union to get the modified values + finalTypedInputs, err := resp.Inputs.AsPackagePolicyTypedInputs() + require.NoError(t, err) + + // Input vars + got = *finalTypedInputs[0].Vars + want = *wantsTypedInputs[0].Vars require.Equal(t, want, got) - // privateData + // Stream vars + got = *finalTypedInputs[0].Streams[0].Vars + want = *wantsTypedInputs[0].Streams[0].Vars + require.Equal(t, want, got) // privateData privateWants := privateData{"secrets": `{"known-secret":"secret","known-secret-1":"secret1","known-secret-2":"secret2"}`} require.Equal(t, privateWants, private) }) @@ -202,34 +265,126 @@ func TestHandleReqRespSecrets(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - req := kbapi.PackagePolicyRequest{ - Inputs: &map[string]kbapi.PackagePolicyRequestInput{ - "input1": { - Streams: &map[string]kbapi.PackagePolicyRequestInputStream{"stream1": {Vars: utils.Pointer(maps.Clone(tt.reqInput))}}, - Vars: utils.Pointer(maps.Clone(tt.reqInput)), - }, + t.Run(fmt.Sprintf("%s - mapped inputs", tt.name), func(t *testing.T) { + // Build mapped request inputs union + mappedReqInputs := map[string]kbapi.PackagePolicyRequestMappedInput{ + "input1": { + Streams: &map[string]kbapi.PackagePolicyRequestMappedInputStream{"stream1": {Vars: utils.Pointer(maps.Clone(tt.reqInput))}}, + Vars: utils.Pointer(maps.Clone(tt.reqInput)), + }, + } + mappedReq := kbapi.PackagePolicyRequestMappedInputs{ + Inputs: &mappedReqInputs, + Vars: utils.Pointer(maps.Clone(tt.reqInput)), + } + + var req kbapi.PackagePolicyRequest + require.NoError(t, req.FromPackagePolicyRequestMappedInputs(mappedReq)) + + // Build mapped response inputs union + var respInputs kbapi.PackagePolicy_Inputs + require.NoError(t, respInputs.FromPackagePolicyMappedInputs(kbapi.PackagePolicyMappedInputs{ + "input1": { + Streams: &map[string]kbapi.PackagePolicyMappedInputStream{"stream1": {Vars: utils.Pointer(maps.Clone(tt.respInput))}}, + Vars: utils.Pointer(maps.Clone(tt.respInput)), + }, + })) + + wantsMappedInputs := kbapi.PackagePolicyMappedInputs{ + "input1": { + Streams: &map[string]kbapi.PackagePolicyMappedInputStream{"stream1": {Vars: utils.Pointer(tt.want)}}, + Vars: &tt.want, }, - Vars: utils.Pointer(maps.Clone(tt.reqInput)), } + var wantsInputs kbapi.PackagePolicy_Inputs + require.NoError(t, wantsInputs.FromPackagePolicyMappedInputs(wantsMappedInputs)) + resp := kbapi.PackagePolicy{ SecretReferences: secretRefs, - Inputs: map[string]kbapi.PackagePolicyInput{ - "input1": { - Streams: &map[string]kbapi.PackagePolicyInputStream{"stream1": {Vars: utils.Pointer(maps.Clone(tt.respInput))}}, - Vars: utils.Pointer(maps.Clone(tt.respInput)), - }, - }, - Vars: utils.Pointer(maps.Clone(tt.respInput)), + Inputs: respInputs, + Vars: utils.Pointer(maps.Clone(tt.respInput)), } wants := kbapi.PackagePolicy{ - Inputs: map[string]kbapi.PackagePolicyInput{ - "input1": { - Streams: &map[string]kbapi.PackagePolicyInputStream{"stream1": {Vars: utils.Pointer(tt.want)}}, - Vars: &tt.want, - }, + Inputs: wantsInputs, + Vars: &tt.want, + } + + private := privateData{} + diags := integration_policy.HandleReqRespSecrets(ctx, req, &resp, &private) + require.Empty(t, diags) + + // Policy vars + got := *resp.Vars + want := *wants.Vars + require.Equal(t, want, got) + + // Input vars + finalRespMappedInputs, err := resp.Inputs.AsPackagePolicyMappedInputs() + require.NoError(t, err) + got = *finalRespMappedInputs["input1"].Vars + want = *wantsMappedInputs["input1"].Vars + require.Equal(t, want, got) + + // Stream vars + got = *(*finalRespMappedInputs["input1"].Streams)["stream1"].Vars + want = *(*wantsMappedInputs["input1"].Streams)["stream1"].Vars + require.Equal(t, want, got) + + privateWants := privateData{"secrets": `{}`} + if v, ok := tt.reqInput["k"]; ok { + if s, ok := v.(string); ok && s == "secret" { + privateWants = privateData{"secrets": `{"known-secret":"secret"}`} + } else if _, ok := v.([]any); ok { + privateWants = privateData{"secrets": `{"known-secret-1":"secret1","known-secret-2":"secret2"}`} + } + } + require.Equal(t, privateWants, private) + }) + t.Run(fmt.Sprintf("%s - typed inputs", tt.name), func(t *testing.T) { + // Build typed request inputs union + typedReqInputs := []kbapi.PackagePolicyRequestTypedInput{ + { + Type: "input1", + Streams: &[]kbapi.PackagePolicyRequestTypedInputStream{{Vars: utils.Pointer(maps.Clone(tt.reqInput))}}, + Vars: utils.Pointer(maps.Clone(tt.reqInput)), + }, + } + typedReq := kbapi.PackagePolicyRequestTypedInputs{ + Inputs: &typedReqInputs, + Vars: utils.Pointer(maps.Clone(tt.reqInput)), + } + + var req kbapi.PackagePolicyRequest + require.NoError(t, req.FromPackagePolicyRequestTypedInputs(typedReq)) + + // Build typed response inputs union + var respInputs kbapi.PackagePolicy_Inputs + require.NoError(t, respInputs.FromPackagePolicyTypedInputs(kbapi.PackagePolicyTypedInputs{ + { + Type: "input1", + Streams: []kbapi.PackagePolicyTypedInputStream{{Vars: utils.Pointer(maps.Clone(tt.respInput))}}, + Vars: utils.Pointer(maps.Clone(tt.respInput)), }, - Vars: &tt.want, + })) + + wantsTypedInputs := kbapi.PackagePolicyTypedInputs{ + { + Type: "input1", + Streams: []kbapi.PackagePolicyTypedInputStream{{Vars: utils.Pointer(tt.want)}}, + Vars: &tt.want, + }, + } + var wantsInputs kbapi.PackagePolicy_Inputs + require.NoError(t, wantsInputs.FromPackagePolicyTypedInputs(wantsTypedInputs)) + + resp := kbapi.PackagePolicy{ + SecretReferences: secretRefs, + Inputs: respInputs, + Vars: utils.Pointer(maps.Clone(tt.respInput)), + } + wants := kbapi.PackagePolicy{ + Inputs: wantsInputs, + Vars: &tt.want, } private := privateData{} @@ -242,17 +397,19 @@ func TestHandleReqRespSecrets(t *testing.T) { require.Equal(t, want, got) // Input vars - got = *resp.Inputs["input1"].Vars - want = *wants.Inputs["input1"].Vars + finalRespTypedInputs, err := resp.Inputs.AsPackagePolicyTypedInputs() + require.NoError(t, err) + got = *finalRespTypedInputs[0].Vars + want = *wantsTypedInputs[0].Vars require.Equal(t, want, got) // Stream vars - got = *(*resp.Inputs["input1"].Streams)["stream1"].Vars - want = *(*wants.Inputs["input1"].Streams)["stream1"].Vars + got = *(finalRespTypedInputs[0].Streams)[0].Vars + want = *(wantsTypedInputs[0].Streams)[0].Vars require.Equal(t, want, got) privateWants := privateData{"secrets": `{}`} - if v, ok := (*req.Vars)["k"]; ok { + if v, ok := tt.reqInput["k"]; ok { if s, ok := v.(string); ok && s == "secret" { privateWants = privateData{"secrets": `{"known-secret":"secret"}`} } else if _, ok := v.([]any); ok {