|
| 1 | +package auditmanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/auditmanager" |
| 8 | + awstypes "github.com/aws/aws-sdk-go-v2/service/auditmanager/types" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + sdkv2resource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 15 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 16 | + "github.com/hashicorp/terraform-provider-aws/internal/enum" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/flex" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 19 | + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + _sp.registerFrameworkDataSourceFactory(newDataSourceFramework) |
| 24 | +} |
| 25 | + |
| 26 | +func newDataSourceFramework(context.Context) (datasource.DataSourceWithConfigure, error) { |
| 27 | + return &dataSourceFramework{}, nil |
| 28 | +} |
| 29 | + |
| 30 | +type dataSourceFramework struct { |
| 31 | + framework.DataSourceWithConfigure |
| 32 | +} |
| 33 | + |
| 34 | +func (d *dataSourceFramework) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name |
| 35 | + response.TypeName = "aws_auditmanager_framework" |
| 36 | +} |
| 37 | + |
| 38 | +func (d *dataSourceFramework) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 39 | + resp.Schema = schema.Schema{ |
| 40 | + Attributes: map[string]schema.Attribute{ |
| 41 | + "arn": framework.ARNAttributeComputedOnly(), |
| 42 | + "compliance_type": schema.StringAttribute{ |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "description": schema.StringAttribute{ |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "framework_type": schema.StringAttribute{ |
| 49 | + Required: true, |
| 50 | + Validators: []validator.String{ |
| 51 | + enum.FrameworkValidate[awstypes.FrameworkType](), |
| 52 | + }, |
| 53 | + }, |
| 54 | + "id": framework.IDAttribute(), |
| 55 | + "name": schema.StringAttribute{ |
| 56 | + Required: true, |
| 57 | + }, |
| 58 | + "tags": tftags.TagsAttributeComputedOnly(), |
| 59 | + }, |
| 60 | + Blocks: map[string]schema.Block{ |
| 61 | + "control_sets": schema.SetNestedBlock{ |
| 62 | + NestedObject: schema.NestedBlockObject{ |
| 63 | + Attributes: map[string]schema.Attribute{ |
| 64 | + "id": framework.IDAttribute(), |
| 65 | + "name": schema.StringAttribute{ |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + Blocks: map[string]schema.Block{ |
| 70 | + "controls": schema.SetNestedBlock{ |
| 71 | + NestedObject: schema.NestedBlockObject{ |
| 72 | + Attributes: map[string]schema.Attribute{ |
| 73 | + "id": schema.StringAttribute{ |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (d *dataSourceFramework) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 87 | + conn := d.Meta().AuditManagerClient() |
| 88 | + |
| 89 | + var data dataSourceFrameworkData |
| 90 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 91 | + if resp.Diagnostics.HasError() { |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + frameworkMetadata, err := FindFrameworkByName(ctx, conn, data.Name.ValueString(), data.FrameworkType.ValueString()) |
| 96 | + if err != nil { |
| 97 | + resp.Diagnostics.AddError("finding framework by name", err.Error()) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // Framework metadata from the ListFrameworks API does not contain all information available |
| 102 | + // about a framework. Use framework ID to get complete information. |
| 103 | + framework, err := FindFrameworkByID(ctx, conn, aws.ToString(frameworkMetadata.Id)) |
| 104 | + if err != nil { |
| 105 | + resp.Diagnostics.AddError("finding framework by ID", err.Error()) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + resp.Diagnostics.Append(data.refreshFromOutput(ctx, d.Meta(), framework)...) |
| 110 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 111 | +} |
| 112 | + |
| 113 | +func FindFrameworkByName(ctx context.Context, conn *auditmanager.Client, name, frameworkType string) (*awstypes.AssessmentFrameworkMetadata, error) { |
| 114 | + in := &auditmanager.ListAssessmentFrameworksInput{ |
| 115 | + FrameworkType: awstypes.FrameworkType(frameworkType), |
| 116 | + } |
| 117 | + pages := auditmanager.NewListAssessmentFrameworksPaginator(conn, in) |
| 118 | + |
| 119 | + for pages.HasMorePages() { |
| 120 | + page, err := pages.NextPage(ctx) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + for _, framework := range page.FrameworkMetadataList { |
| 126 | + if name == aws.ToString(framework.Name) { |
| 127 | + return &framework, nil |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return nil, &sdkv2resource.NotFoundError{ |
| 133 | + LastRequest: in, |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +type dataSourceFrameworkData struct { |
| 138 | + ARN types.String `tfsdk:"arn"` |
| 139 | + ComplianceType types.String `tfsdk:"compliance_type"` |
| 140 | + ControlSets types.Set `tfsdk:"control_sets"` |
| 141 | + Description types.String `tfsdk:"description"` |
| 142 | + FrameworkType types.String `tfsdk:"framework_type"` |
| 143 | + ID types.String `tfsdk:"id"` |
| 144 | + Name types.String `tfsdk:"name"` |
| 145 | + Tags types.Map `tfsdk:"tags"` |
| 146 | +} |
| 147 | + |
| 148 | +// refreshFromOutput writes state data from an AWS response object |
| 149 | +func (rd *dataSourceFrameworkData) refreshFromOutput(ctx context.Context, meta *conns.AWSClient, out *awstypes.Framework) diag.Diagnostics { |
| 150 | + var diags diag.Diagnostics |
| 151 | + |
| 152 | + if out == nil { |
| 153 | + return diags |
| 154 | + } |
| 155 | + |
| 156 | + rd.ID = types.StringValue(aws.ToString(out.Id)) |
| 157 | + rd.Name = types.StringValue(aws.ToString(out.Name)) |
| 158 | + cs, d := flattenFrameworkControlSets(ctx, out.ControlSets) |
| 159 | + diags.Append(d...) |
| 160 | + rd.ControlSets = cs |
| 161 | + |
| 162 | + rd.ComplianceType = flex.StringToFramework(ctx, out.ComplianceType) |
| 163 | + rd.Description = flex.StringToFramework(ctx, out.Description) |
| 164 | + rd.FrameworkType = flex.StringValueToFramework(ctx, out.Type) |
| 165 | + rd.ARN = flex.StringToFramework(ctx, out.Arn) |
| 166 | + |
| 167 | + ignoreTagsConfig := meta.IgnoreTagsConfig |
| 168 | + tags := KeyValueTags(out.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig) |
| 169 | + rd.Tags = flex.FlattenFrameworkStringValueMapLegacy(ctx, tags.Map()) |
| 170 | + |
| 171 | + return diags |
| 172 | +} |
0 commit comments