Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDPCP-9785 - put policy JSONs and service names into a map instead of a set of objects #107

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

// Save data into Terraform state
policyTypeMap, convDiag := types.MapValueFrom(ctx, types.StringType, policyMap)
data.Policies = policyTypeMap
resp.Diagnostics.Append(convDiag...)

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
Loading