From 7aa23030b4ebba5889474870a76911d7ad52709d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= Date: Thu, 11 Apr 2024 11:12:55 +0200 Subject: [PATCH] CDPCP-9785 - put policy JSONs and service names into a map instead of a set of objects --- .../data_source_aws_credential_prerequisites.go | 13 +++++++------ .../model_aws_credential_prerequisites.go | 15 +++++---------- .../schema_aws_credential_prerequisites.go | 16 +++------------- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/resources/environments/data_source_aws_credential_prerequisites.go b/resources/environments/data_source_aws_credential_prerequisites.go index cfd03766..b00a0f74 100644 --- a/resources/environments/data_source_aws_credential_prerequisites.go +++ b/resources/environments/data_source_aws_credential_prerequisites.go @@ -86,14 +86,15 @@ func (d *awsCredentialPrerequisitesDataSource) Read(ctx context.Context, req dat data.ExternalID = types.StringValue(*prerequisites.Aws.ExternalID) data.ID = types.StringValue(prerequisites.AccountID + ":" + *prerequisites.Aws.ExternalID) data.Policy = types.StringPointerValue(prerequisites.Aws.PolicyJSON) - data.Policies = make([]*credentialGranularPolicyDataSourceModel, len(prerequisites.Aws.Policies)) - for i, policy := range prerequisites.Aws.Policies { - data.Policies[i] = &credentialGranularPolicyDataSourceModel{ - Service: types.StringPointerValue(policy.Service), - PolicyJson: types.StringPointerValue(policy.PolicyJSON), - } + + var policyMap = make(map[string]string) + for _, policy := range prerequisites.Aws.Policies { + policyMap[*policy.Service] = *policy.PolicyJSON } + policyTypeMap, _ := types.MapValueFrom(ctx, types.StringType, policyMap) + data.Policies = policyTypeMap + // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } diff --git a/resources/environments/model_aws_credential_prerequisites.go b/resources/environments/model_aws_credential_prerequisites.go index d83cf734..0b82a0b1 100644 --- a/resources/environments/model_aws_credential_prerequisites.go +++ b/resources/environments/model_aws_credential_prerequisites.go @@ -13,14 +13,9 @@ package environments import "github.com/hashicorp/terraform-plugin-framework/types" type awsCredentialPrerequisitesDataSourceModel struct { - ID types.String `tfsdk:"id"` - AccountID types.String `tfsdk:"account_id"` - ExternalID types.String `tfsdk:"external_id"` - Policy types.String `tfsdk:"policy"` - Policies []*credentialGranularPolicyDataSourceModel `tfsdk:"policies"` -} - -type credentialGranularPolicyDataSourceModel struct { - Service types.String `tfsdk:"service"` - PolicyJson types.String `tfsdk:"policy_json"` + ID types.String `tfsdk:"id"` + AccountID types.String `tfsdk:"account_id"` + ExternalID types.String `tfsdk:"external_id"` + Policy types.String `tfsdk:"policy"` + Policies types.Map `tfsdk:"policies"` } diff --git a/resources/environments/schema_aws_credential_prerequisites.go b/resources/environments/schema_aws_credential_prerequisites.go index 3fc48bf7..6f75003f 100644 --- a/resources/environments/schema_aws_credential_prerequisites.go +++ b/resources/environments/schema_aws_credential_prerequisites.go @@ -14,6 +14,7 @@ import ( "context" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" ) func (d *awsCredentialPrerequisitesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { @@ -35,21 +36,10 @@ func (d *awsCredentialPrerequisitesDataSource) Schema(_ context.Context, _ datas MarkdownDescription: "The related policy json encoded in base64", Computed: true, }, - "policies": schema.SetNestedAttribute{ + "policies": schema.MapAttribute{ Computed: true, MarkdownDescription: "The fine-grained policies related to each service.", - NestedObject: schema.NestedAttributeObject{ - Attributes: map[string]schema.Attribute{ - "service": schema.StringAttribute{ - Computed: true, - MarkdownDescription: "The name of the service that requires the given policy for resource management.", - }, - "policy_json": schema.StringAttribute{ - Computed: true, - MarkdownDescription: "The policy JSON encoded in base64.", - }, - }, - }, + ElementType: types.StringType, }, }, }