Skip to content

Commit

Permalink
CDPCP-9785 - put policy JSONs and service names into a map instead of…
Browse files Browse the repository at this point in the history
… a set of objects
  • Loading branch information
gregito committed Apr 15, 2024
1 parent 0dd87d4 commit 7aa2303
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
15 changes: 5 additions & 10 deletions resources/environments/model_aws_credential_prerequisites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
16 changes: 3 additions & 13 deletions resources/environments/schema_aws_credential_prerequisites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
},
},
}
Expand Down

0 comments on commit 7aa2303

Please sign in to comment.