Skip to content

Commit

Permalink
Merge pull request hashicorp#37451 from hashicorp/t-literal-content-s…
Browse files Browse the repository at this point in the history
…ubnets-s3_bucket-string-constant

ci: Prefer constants for string literals
  • Loading branch information
YakDriver authored May 12, 2024
2 parents 6442c60 + 1794696 commit 42ad982
Show file tree
Hide file tree
Showing 270 changed files with 1,064 additions and 929 deletions.
100 changes: 100 additions & 0 deletions .ci/.semgrep-constants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ rules:
- pattern: '"configuration"'
severity: ERROR
fix: "names.AttrConfiguration"
- id: literal-content-string-constant
languages: [go]
message: Use the constant `names.AttrContent` for the string literal "content"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"content"'
severity: ERROR
fix: "names.AttrContent"
- id: literal-content_type-string-constant
languages: [go]
message: Use the constant `names.AttrContentType` for the string literal "content_type"
Expand Down Expand Up @@ -240,6 +250,26 @@ rules:
- pattern: '"creation_time"'
severity: ERROR
fix: "names.AttrCreationTime"
- id: literal-dns_name-string-constant
languages: [go]
message: Use the constant `names.AttrDNSName` for the string literal "dns_name"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"dns_name"'
severity: ERROR
fix: "names.AttrDNSName"
- id: literal-database-string-constant
languages: [go]
message: Use the constant `names.AttrDatabase` for the string literal "database"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"database"'
severity: ERROR
fix: "names.AttrDatabase"
- id: literal-database_name-string-constant
languages: [go]
message: Use the constant `names.AttrDatabaseName` for the string literal "database_name"
Expand Down Expand Up @@ -310,6 +340,16 @@ rules:
- pattern: '"display_name"'
severity: ERROR
fix: "names.AttrDisplayName"
- id: literal-domain-string-constant
languages: [go]
message: Use the constant `names.AttrDomain` for the string literal "domain"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"domain"'
severity: ERROR
fix: "names.AttrDomain"
- id: literal-domain_name-string-constant
languages: [go]
message: Use the constant `names.AttrDomainName` for the string literal "domain_name"
Expand All @@ -320,6 +360,16 @@ rules:
- pattern: '"domain_name"'
severity: ERROR
fix: "names.AttrDomainName"
- id: literal-duration-string-constant
languages: [go]
message: Use the constant `names.AttrDuration` for the string literal "duration"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"duration"'
severity: ERROR
fix: "names.AttrDuration"
- id: literal-enabled-string-constant
languages: [go]
message: Use the constant `names.AttrEnabled` for the string literal "enabled"
Expand Down Expand Up @@ -570,6 +620,16 @@ rules:
- pattern: '"log_group_name"'
severity: ERROR
fix: "names.AttrLogGroupName"
- id: literal-logging_configuration-string-constant
languages: [go]
message: Use the constant `names.AttrLoggingConfiguration` for the string literal "logging_configuration"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"logging_configuration"'
severity: ERROR
fix: "names.AttrLoggingConfiguration"
- id: literal-max-string-constant
languages: [go]
message: Use the constant `names.AttrMax` for the string literal "max"
Expand Down Expand Up @@ -860,6 +920,16 @@ rules:
- pattern: '"role_arn"'
severity: ERROR
fix: "names.AttrRoleARN"
- id: literal-s3_bucket-string-constant
languages: [go]
message: Use the constant `names.AttrS3Bucket` for the string literal "s3_bucket"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"s3_bucket"'
severity: ERROR
fix: "names.AttrS3Bucket"
- id: literal-s3_bucket_name-string-constant
languages: [go]
message: Use the constant `names.AttrS3BucketName` for the string literal "s3_bucket_name"
Expand Down Expand Up @@ -900,6 +970,16 @@ rules:
- pattern: '"schedule_expression"'
severity: ERROR
fix: "names.AttrScheduleExpression"
- id: literal-schema-string-constant
languages: [go]
message: Use the constant `names.AttrSchema` for the string literal "schema"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"schema"'
severity: ERROR
fix: "names.AttrSchema"
- id: literal-secret_key-string-constant
languages: [go]
message: Use the constant `names.AttrSecretKey` for the string literal "secret_key"
Expand Down Expand Up @@ -1030,6 +1110,16 @@ rules:
- pattern: '"subnet_ids"'
severity: ERROR
fix: "names.AttrSubnetIDs"
- id: literal-subnets-string-constant
languages: [go]
message: Use the constant `names.AttrSubnets` for the string literal "subnets"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"subnets"'
severity: ERROR
fix: "names.AttrSubnets"
- id: literal-table_name-string-constant
languages: [go]
message: Use the constant `names.AttrTableName` for the string literal "table_name"
Expand Down Expand Up @@ -1210,3 +1300,13 @@ rules:
- pattern: '"version"'
severity: ERROR
fix: "names.AttrVersion"
- id: literal-weight-string-constant
languages: [go]
message: Use the constant `names.AttrWeight` for the string literal "weight"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"weight"'
severity: ERROR
fix: "names.AttrWeight"
4 changes: 2 additions & 2 deletions internal/service/acm/certificate_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func dataSourceCertificate() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"domain": {
names.AttrDomain: {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -80,7 +80,7 @@ func dataSourceCertificateRead(ctx context.Context, d *schema.ResourceData, meta
conn := meta.(*conns.AWSClient).ACMClient(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

domain := d.Get("domain")
domain := d.Get(names.AttrDomain)
input := &acm.ListCertificatesInput{}

if v, ok := d.GetOk("key_types"); ok && v.(*schema.Set).Len() > 0 {
Expand Down
14 changes: 7 additions & 7 deletions internal/service/amp/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func resourceWorkspace() *schema.Resource {
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"logging_configuration": {
names.AttrLoggingConfiguration: {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Expand Down Expand Up @@ -114,7 +114,7 @@ func resourceWorkspaceCreate(ctx context.Context, d *schema.ResourceData, meta i
return sdkdiag.AppendErrorf(diags, "waiting for Prometheus Workspace (%s) create: %s", d.Id(), err)
}

if v, ok := d.GetOk("logging_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
if v, ok := d.GetOk(names.AttrLoggingConfiguration); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
input := &amp.CreateLoggingConfigurationInput{
LogGroupArn: aws.String(tfMap["log_group_arn"].(string)),
Expand Down Expand Up @@ -160,11 +160,11 @@ func resourceWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta int
loggingConfiguration, err := findLoggingConfigurationByWorkspaceID(ctx, conn, d.Id())

if tfresource.NotFound(err) {
d.Set("logging_configuration", nil)
d.Set(names.AttrLoggingConfiguration, nil)
} else if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Prometheus Workspace (%s) logging configuration: %s", d.Id(), err)
} else {
if err := d.Set("logging_configuration", []interface{}{flattenLoggingConfigurationMetadata(loggingConfiguration)}); err != nil {
if err := d.Set(names.AttrLoggingConfiguration, []interface{}{flattenLoggingConfigurationMetadata(loggingConfiguration)}); err != nil {
return sdkdiag.AppendErrorf(diags, "setting logging_configuration: %s", err)
}
}
Expand Down Expand Up @@ -193,11 +193,11 @@ func resourceWorkspaceUpdate(ctx context.Context, d *schema.ResourceData, meta i
}
}

if d.HasChange("logging_configuration") {
if v, ok := d.GetOk("logging_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
if d.HasChange(names.AttrLoggingConfiguration) {
if v, ok := d.GetOk(names.AttrLoggingConfiguration); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})

if o, _ := d.GetChange("logging_configuration"); o == nil || len(o.([]interface{})) == 0 || o.([]interface{})[0] == nil {
if o, _ := d.GetChange(names.AttrLoggingConfiguration); o == nil || len(o.([]interface{})) == 0 || o.([]interface{})[0] == nil {
input := &amp.CreateLoggingConfigurationInput{
LogGroupArn: aws.String(tfMap["log_group_arn"].(string)),
WorkspaceId: aws.String(d.Id()),
Expand Down
10 changes: 5 additions & 5 deletions internal/service/apigateway/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func resourceModel() *schema.Resource {
Required: true,
ForceNew: true,
},
"schema": {
names.AttrSchema: {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
Expand Down Expand Up @@ -107,7 +107,7 @@ func resourceModelCreate(ctx context.Context, d *schema.ResourceData, meta inter
input.Description = aws.String(v.(string))
}

if v, ok := d.GetOk("schema"); ok {
if v, ok := d.GetOk(names.AttrSchema); ok {
input.Schema = aws.String(v.(string))
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func resourceModelRead(ctx context.Context, d *schema.ResourceData, meta interfa

d.Set(names.AttrContentType, model.ContentType)
d.Set(names.AttrDescription, model.Description)
d.Set("schema", model.Schema)
d.Set(names.AttrSchema, model.Schema)

return diags
}
Expand All @@ -159,11 +159,11 @@ func resourceModelUpdate(ctx context.Context, d *schema.ResourceData, meta inter
})
}

if d.HasChange("schema") {
if d.HasChange(names.AttrSchema) {
operations = append(operations, types.PatchOperation{
Op: types.OpReplace,
Path: aws.String("/schema"),
Value: aws.String(d.Get("schema").(string)),
Value: aws.String(d.Get(names.AttrSchema).(string)),
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/apigateway/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestAccAPIGatewayModel_basic(t *testing.T) {
ImportState: true,
ImportStateIdFunc: testAccModelImportStateIdFunc(resourceName),
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"schema"},
ImportStateVerifyIgnore: []string{names.AttrSchema},
},
},
})
Expand Down
10 changes: 5 additions & 5 deletions internal/service/apigatewayv2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func resourceModel() *schema.Resource {
validation.StringMatch(regexache.MustCompile(`^[0-9A-Za-z]+$`), "must be alphanumeric"),
),
},
"schema": {
names.AttrSchema: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
Expand All @@ -89,7 +89,7 @@ func resourceModelCreate(ctx context.Context, d *schema.ResourceData, meta inter
ApiId: aws.String(d.Get("api_id").(string)),
ContentType: aws.String(d.Get(names.AttrContentType).(string)),
Name: aws.String(name),
Schema: aws.String(d.Get("schema").(string)),
Schema: aws.String(d.Get(names.AttrSchema).(string)),
}

if v, ok := d.GetOk(names.AttrDescription); ok {
Expand Down Expand Up @@ -126,7 +126,7 @@ func resourceModelRead(ctx context.Context, d *schema.ResourceData, meta interfa
d.Set(names.AttrContentType, output.ContentType)
d.Set(names.AttrDescription, output.Description)
d.Set(names.AttrName, output.Name)
d.Set("schema", output.Schema)
d.Set(names.AttrSchema, output.Schema)

return diags
}
Expand All @@ -152,8 +152,8 @@ func resourceModelUpdate(ctx context.Context, d *schema.ResourceData, meta inter
input.Name = aws.String(d.Get(names.AttrName).(string))
}

if d.HasChange("schema") {
input.Schema = aws.String(d.Get("schema").(string))
if d.HasChange(names.AttrSchema) {
input.Schema = aws.String(d.Get(names.AttrSchema).(string))
}

_, err := conn.UpdateModel(ctx, input)
Expand Down
8 changes: 4 additions & 4 deletions internal/service/apigatewayv2/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAccAPIGatewayV2Model_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrContentType, "application/json"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, ""),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
acctest.CheckResourceAttrEquivalentJSON(resourceName, "schema", schema),
acctest.CheckResourceAttrEquivalentJSON(resourceName, names.AttrSchema, schema),
),
},
{
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestAccAPIGatewayV2Model_allAttributes(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrContentType, "text/x-json"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "test"),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
acctest.CheckResourceAttrEquivalentJSON(resourceName, "schema", schema1),
acctest.CheckResourceAttrEquivalentJSON(resourceName, names.AttrSchema, schema1),
),
},
{
Expand All @@ -162,7 +162,7 @@ func TestAccAPIGatewayV2Model_allAttributes(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrContentType, "application/json"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, ""),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
acctest.CheckResourceAttrEquivalentJSON(resourceName, "schema", schema2),
acctest.CheckResourceAttrEquivalentJSON(resourceName, names.AttrSchema, schema2),
),
},
{
Expand All @@ -172,7 +172,7 @@ func TestAccAPIGatewayV2Model_allAttributes(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrContentType, "text/x-json"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "test"),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
acctest.CheckResourceAttrEquivalentJSON(resourceName, "schema", schema1),
acctest.CheckResourceAttrEquivalentJSON(resourceName, names.AttrSchema, schema1),
),
},
{
Expand Down
6 changes: 3 additions & 3 deletions internal/service/appconfig/configuration_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ResourceConfigurationProfile() *schema.Resource {
MaxItems: 2,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
names.AttrContent: {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Expand Down Expand Up @@ -309,7 +309,7 @@ func expandValidator(tfMap map[string]interface{}) awstypes.Validator {
validator := awstypes.Validator{}

// AppConfig API supports empty content
if v, ok := tfMap["content"].(string); ok {
if v, ok := tfMap[names.AttrContent].(string); ok {
validator.Content = aws.String(v)
}

Expand Down Expand Up @@ -343,7 +343,7 @@ func flattenValidator(validator awstypes.Validator) map[string]interface{} {
tfMap := map[string]interface{}{}

if v := validator.Content; v != nil {
tfMap["content"] = aws.ToString(v)
tfMap[names.AttrContent] = aws.ToString(v)
}

tfMap[names.AttrType] = string(validator.Type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func DataSourceConfigurationProfile() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
names.AttrContent: {
Type: schema.TypeString,
Computed: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func TestAccAppConfigConfigurationProfileDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceName, "tags.key1", "value1"),
resource.TestCheckResourceAttr(dataSourceName, names.AttrType, "AWS.Freeform"),
resource.TestCheckTypeSetElemNestedAttrs(dataSourceName, "validator.*", map[string]string{
"content": "{\"$schema\":\"http://json-schema.org/draft-05/schema#\",\"description\":\"BasicFeatureToggle-1\",\"title\":\"$id$\"}",
names.AttrType: string(awstypes.ValidatorTypeJsonSchema),
names.AttrContent: "{\"$schema\":\"http://json-schema.org/draft-05/schema#\",\"description\":\"BasicFeatureToggle-1\",\"title\":\"$id$\"}",
names.AttrType: string(awstypes.ValidatorTypeJsonSchema),
}),
),
},
Expand Down
Loading

0 comments on commit 42ad982

Please sign in to comment.