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

[New Data Source]: aws_servicequotas_templates #33871

Merged
merged 2 commits into from
Oct 11, 2023
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
3 changes: 3 additions & 0 deletions .changelog/33871.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_servicequotas_templates
```
7 changes: 6 additions & 1 deletion internal/service/servicequotas/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/service/servicequotas/servicequotas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func TestAccServiceQuotas_serial(t *testing.T) {
"disappears": testAccTemplateAssociation_disappears,
"skipDestroy": testAccTemplateAssociation_skipDestroy,
},
"TemplatesDataSource": {
"basic": testAccTemplatesDataSource_basic,
},
}

acctest.RunSerialTests2Levels(t, testCases, 0)
Expand Down
155 changes: 155 additions & 0 deletions internal/service/servicequotas/templates_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package servicequotas

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/servicequotas"
awstypes "github.com/aws/aws-sdk-go-v2/service/servicequotas/types"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name="Templates")
func newDataSourceTemplates(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceTemplates{}, nil
}

const (
DSNameTemplates = "Templates Data Source"
)

type dataSourceTemplates struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceTemplates) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
resp.TypeName = "aws_servicequotas_templates"
}

func (d *dataSourceTemplates) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": framework.IDAttribute(),
"region": schema.StringAttribute{
Required: true,
},
},
Blocks: map[string]schema.Block{
"templates": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"global_quota": schema.BoolAttribute{
Computed: true,
},
"quota_code": schema.StringAttribute{
Computed: true,
},
"quota_name": schema.StringAttribute{
Computed: true,
},
"region": schema.StringAttribute{
Computed: true,
},
"service_code": schema.StringAttribute{
Computed: true,
},
"service_name": schema.StringAttribute{
Computed: true,
},
"unit": schema.StringAttribute{
Computed: true,
},
"value": schema.Float64Attribute{
Computed: true,
},
},
},
},
},
}
}

func (d *dataSourceTemplates) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().ServiceQuotasClient(ctx)

var data dataSourceTemplatesData
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

input := servicequotas.ListServiceQuotaIncreaseRequestsInTemplateInput{
AwsRegion: aws.String(data.Region.ValueString()),
}
out, err := conn.ListServiceQuotaIncreaseRequestsInTemplate(ctx, &input)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.ServiceQuotas, create.ErrActionReading, DSNameTemplates, data.Region.String(), err),
err.Error(),
)
return
}

data.ID = types.StringValue(data.Region.ValueString())

templates, diags := flattenTemplates(ctx, out.ServiceQuotaIncreaseRequestInTemplateList)
resp.Diagnostics.Append(diags...)
data.Templates = templates

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

var templatesSourceAttrTypes = map[string]attr.Type{
"global_quota": types.BoolType,
"quota_code": types.StringType,
"quota_name": types.StringType,
"region": types.StringType,
"service_code": types.StringType,
"service_name": types.StringType,
"unit": types.StringType,
"value": types.Float64Type,
}

type dataSourceTemplatesData struct {
Region types.String `tfsdk:"region"`
ID types.String `tfsdk:"id"`
Templates types.List `tfsdk:"templates"`
}

func flattenTemplates(ctx context.Context, apiObject []awstypes.ServiceQuotaIncreaseRequestInTemplate) (types.List, diag.Diagnostics) {
var diags diag.Diagnostics
elemType := types.ObjectType{AttrTypes: templatesSourceAttrTypes}

elems := []attr.Value{}
for _, t := range apiObject {
obj := map[string]attr.Value{
"global_quota": types.BoolValue(t.GlobalQuota),
"quota_code": flex.StringToFramework(ctx, t.QuotaCode),
"quota_name": flex.StringToFramework(ctx, t.QuotaName),
"region": flex.StringToFramework(ctx, t.AwsRegion),
"service_code": flex.StringToFramework(ctx, t.ServiceCode),
"service_name": flex.StringToFramework(ctx, t.ServiceName),
"unit": flex.StringToFramework(ctx, t.Unit),
"value": flex.Float64ToFramework(ctx, t.DesiredValue),
}
objVal, d := types.ObjectValue(templatesSourceAttrTypes, obj)
diags.Append(d...)

elems = append(elems, objVal)
}
listVal, d := types.ListValue(elemType, elems)
diags.Append(d...)

return listVal, diags
}
49 changes: 49 additions & 0 deletions internal/service/servicequotas/templates_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package servicequotas_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func testAccTemplatesDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_servicequotas_templates.test"
regionDataSourceName := "data.aws_region.current"

resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, names.USEast1RegionID)
acctest.PreCheckPartitionHasService(t, names.ServiceQuotasEndpointID)
testAccPreCheckTemplate(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.ServiceQuotasEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckTemplateDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccTemplatesDataSourceConfig_basic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "region", regionDataSourceName, "name"),
resource.TestCheckResourceAttr(dataSourceName, "templates.#", "1"),
),
},
},
})
}

func testAccTemplatesDataSourceConfig_basic() string {
return acctest.ConfigCompose(
testAccTemplateConfig_basic(lambdaStorageQuotaCode, lambdaServiceCode, lambdaStorageValue),
`
data "aws_servicequotas_templates" "test" {
region = aws_servicequotas_template.test.region
}
`)
}
44 changes: 44 additions & 0 deletions website/docs/d/servicequotas_templates.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Service Quotas"
layout: "aws"
page_title: "AWS: aws_servicequotas_templates"
description: |-
Terraform data source for managing an AWS Service Quotas Templates.
---

# Data Source: aws_servicequotas_templates

Terraform data source for managing an AWS Service Quotas Templates.

## Example Usage

### Basic Usage

```terraform
data "aws_servicequotas_templates" "example" {
region = "us-east-1"
}
```

## Argument Reference

The following arguments are required:

* `region` - (Required) AWS Region to which the quota increases apply.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `templates` - A list of quota increase templates for specified region. See [`templates`](#templates).

### `templates`

* `global_quota` - Indicates whether the quota is global.
* `quota_name` - Quota name.
* `quota_code` - Quota identifier.
* `region` - AWS Region to which the template applies.
* `service_code` - (Required) Service identifier.
* `service_name` - Service name.
* `unit` - Unit of measurement.
* `value` - (Required) The new, increased value for the quota.