Skip to content

Commit

Permalink
Merge pull request hashicorp#37702 from hashicorp/t-literal-applicati…
Browse files Browse the repository at this point in the history
…on_id-engine-string-constant

ci: Prefer constants over string literals
  • Loading branch information
YakDriver authored May 24, 2024
2 parents 76dc1f6 + 9e97711 commit 250de7d
Show file tree
Hide file tree
Showing 267 changed files with 1,050 additions and 878 deletions.
140 changes: 140 additions & 0 deletions .ci/.semgrep-constants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ rules:
severity: ERROR
fix: "names.AttrARNs"

- id: literal-aws_account_id-string-constant
languages: [go]
message: Use the constant `names.AttrAWSAccountID` for the string literal "aws_account_id"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"aws_account_id"'
- pattern-not-regex: '"aws_account_id":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrAWSAccountID"

- id: literal-access_key-string-constant
languages: [go]
message: Use the constant `names.AttrAccessKey` for the string literal "access_key"
Expand Down Expand Up @@ -99,6 +113,20 @@ rules:
severity: ERROR
fix: "names.AttrAlias"

- id: literal-application_id-string-constant
languages: [go]
message: Use the constant `names.AttrApplicationID` for the string literal "application_id"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"application_id"'
- pattern-not-regex: '"application_id":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrApplicationID"

- id: literal-apply_immediately-string-constant
languages: [go]
message: Use the constant `names.AttrApplyImmediately` for the string literal "apply_immediately"
Expand Down Expand Up @@ -771,6 +799,20 @@ rules:
severity: ERROR
fix: "names.AttrEndpoints"

- id: literal-engine-string-constant
languages: [go]
message: Use the constant `names.AttrEngine` for the string literal "engine"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"engine"'
- pattern-not-regex: '"engine":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrEngine"

- id: literal-engine_version-string-constant
languages: [go]
message: Use the constant `names.AttrEngineVersion` for the string literal "engine_version"
Expand Down Expand Up @@ -813,6 +855,20 @@ rules:
severity: ERROR
fix: "names.AttrExecutionRoleARN"

- id: literal-expected_bucket_owner-string-constant
languages: [go]
message: Use the constant `names.AttrExpectedBucketOwner` for the string literal "expected_bucket_owner"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"expected_bucket_owner"'
- pattern-not-regex: '"expected_bucket_owner":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrExpectedBucketOwner"

- id: literal-expression-string-constant
languages: [go]
message: Use the constant `names.AttrExpression` for the string literal "expression"
Expand Down Expand Up @@ -1093,6 +1149,20 @@ rules:
severity: ERROR
fix: "names.AttrIPAddressType"

- id: literal-ip_addresses-string-constant
languages: [go]
message: Use the constant `names.AttrIPAddresses` for the string literal "ip_addresses"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"ip_addresses"'
- pattern-not-regex: '"ip_addresses":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrIPAddresses"

- id: literal-identifier-string-constant
languages: [go]
message: Use the constant `names.AttrIdentifier` for the string literal "identifier"
Expand Down Expand Up @@ -1527,6 +1597,20 @@ rules:
severity: ERROR
fix: "names.AttrOwner"

- id: literal-owner_account_id-string-constant
languages: [go]
message: Use the constant `names.AttrOwnerAccountID` for the string literal "owner_account_id"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"owner_account_id"'
- pattern-not-regex: '"owner_account_id":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrOwnerAccountID"

- id: literal-owner_id-string-constant
languages: [go]
message: Use the constant `names.AttrOwnerID` for the string literal "owner_id"
Expand Down Expand Up @@ -1555,6 +1639,20 @@ rules:
severity: ERROR
fix: "names.AttrParameter"

- id: literal-parameter_group_name-string-constant
languages: [go]
message: Use the constant `names.AttrParameterGroupName` for the string literal "parameter_group_name"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"parameter_group_name"'
- pattern-not-regex: '"parameter_group_name":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrParameterGroupName"

- id: literal-parameters-string-constant
languages: [go]
message: Use the constant `names.AttrParameters` for the string literal "parameters"
Expand Down Expand Up @@ -1863,6 +1961,20 @@ rules:
severity: ERROR
fix: "names.AttrResourceID"

- id: literal-resource_owner-string-constant
languages: [go]
message: Use the constant `names.AttrResourceOwner` for the string literal "resource_owner"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"resource_owner"'
- pattern-not-regex: '"resource_owner":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrResourceOwner"

- id: literal-resource_tags-string-constant
languages: [go]
message: Use the constant `names.AttrResourceTags` for the string literal "resource_tags"
Expand Down Expand Up @@ -2381,6 +2493,20 @@ rules:
severity: ERROR
fix: "names.AttrStatusReason"

- id: literal-storage_encrypted-string-constant
languages: [go]
message: Use the constant `names.AttrStorageEncrypted` for the string literal "storage_encrypted"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"storage_encrypted"'
- pattern-not-regex: '"storage_encrypted":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrStorageEncrypted"

- id: literal-storage_type-string-constant
languages: [go]
message: Use the constant `names.AttrStorageType` for the string literal "storage_type"
Expand Down Expand Up @@ -2521,6 +2647,20 @@ rules:
severity: ERROR
fix: "names.AttrTargetARN"

- id: literal-throughput-string-constant
languages: [go]
message: Use the constant `names.AttrThroughput` for the string literal "throughput"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"throughput"'
- pattern-not-regex: '"throughput":\s+test\w+,'
- pattern-not-inside: |
config.Variables{ ... }
severity: ERROR
fix: "names.AttrThroughput"

- id: literal-timeout-string-constant
languages: [go]
message: Use the constant `names.AttrTimeout` for the string literal "timeout"
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 @@ -39,7 +39,7 @@ func ResourceConfigurationProfile() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"application_id": {
names.AttrApplicationID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Expand Down Expand Up @@ -123,7 +123,7 @@ func resourceConfigurationProfileCreate(ctx context.Context, d *schema.ResourceD
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AppConfigClient(ctx)

appId := d.Get("application_id").(string)
appId := d.Get(names.AttrApplicationID).(string)
name := d.Get(names.AttrName).(string)
input := &appconfig.CreateConfigurationProfileInput{
ApplicationId: aws.String(appId),
Expand Down Expand Up @@ -198,7 +198,7 @@ func resourceConfigurationProfileRead(ctx context.Context, d *schema.ResourceDat
return sdkdiag.AppendErrorf(diags, "reading AppConfig Configuration Profile (%s) for Application (%s): empty response", confProfID, appID)
}

d.Set("application_id", output.ApplicationId)
d.Set(names.AttrApplicationID, output.ApplicationId)
d.Set("configuration_profile_id", output.Id)
d.Set(names.AttrDescription, output.Description)
d.Set("kms_key_identifier", output.KmsKeyIdentifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func DataSourceConfigurationProfile() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceConfigurationProfileRead,
Schema: map[string]*schema.Schema{
"application_id": {
names.AttrApplicationID: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexache.MustCompile(`[a-z\d]{4,7}`), ""),
Expand Down Expand Up @@ -93,7 +93,7 @@ func dataSourceConfigurationProfileRead(ctx context.Context, d *schema.ResourceD

conn := meta.(*conns.AWSClient).AppConfigClient(ctx)

appId := d.Get("application_id").(string)
appId := d.Get(names.AttrApplicationID).(string)
profileId := d.Get("configuration_profile_id").(string)
ID := fmt.Sprintf("%s:%s", profileId, appId)

Expand All @@ -104,7 +104,7 @@ func dataSourceConfigurationProfileRead(ctx context.Context, d *schema.ResourceD

d.SetId(ID)

d.Set("application_id", appId)
d.Set(names.AttrApplicationID, appId)

arn := arn.ARN{
AccountID: meta.(*conns.AWSClient).AccountID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAccAppConfigConfigurationProfileDataSource_basic(t *testing.T) {
{
Config: testAccConfigurationProfileDataSourceConfig_basic(appName, rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "application_id", appResourceName, names.AttrID),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrApplicationID, appResourceName, names.AttrID),
acctest.MatchResourceAttrRegionalARN(dataSourceName, names.AttrARN, "appconfig", regexache.MustCompile(`application/([a-z\d]{4,7})/configurationprofile/+.`)),
resource.TestMatchResourceAttr(dataSourceName, "configuration_profile_id", regexache.MustCompile(`[a-z\d]{4,7}`)),
resource.TestCheckResourceAttr(dataSourceName, "kms_key_identifier", "alias/"+rName),
Expand Down
2 changes: 1 addition & 1 deletion internal/service/appconfig/configuration_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAccAppConfigConfigurationProfile_basic(t *testing.T) {
Config: testAccConfigurationProfileConfig_name(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckConfigurationProfileExists(ctx, resourceName),
resource.TestCheckResourceAttrPair(resourceName, "application_id", appResourceName, names.AttrID),
resource.TestCheckResourceAttrPair(resourceName, names.AttrApplicationID, appResourceName, names.AttrID),
acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "appconfig", regexache.MustCompile(`application/[0-9a-z]{4,7}/configurationprofile/[0-9a-z]{4,7}`)),
resource.TestMatchResourceAttr(resourceName, "configuration_profile_id", regexache.MustCompile(`[0-9a-z]{4,7}`)),
resource.TestCheckResourceAttr(resourceName, "location_uri", "hosted"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func DataSourceConfigurationProfiles() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceConfigurationProfilesRead,
Schema: map[string]*schema.Schema{
"application_id": {
names.AttrApplicationID: {
Type: schema.TypeString,
Required: true,
},
Expand All @@ -42,7 +42,7 @@ func dataSourceConfigurationProfilesRead(ctx context.Context, d *schema.Resource
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).AppConfigClient(ctx)
appId := d.Get("application_id").(string)
appId := d.Get(names.AttrApplicationID).(string)

out, err := findConfigurationProfileSummariesByApplication(ctx, conn, appId)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/service/appconfig/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ResourceDeployment() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"application_id": {
names.AttrApplicationID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Expand Down Expand Up @@ -113,7 +113,7 @@ func resourceDeploymentCreate(ctx context.Context, d *schema.ResourceData, meta
conn := meta.(*conns.AWSClient).AppConfigClient(ctx)

input := &appconfig.StartDeploymentInput{
ApplicationId: aws.String(d.Get("application_id").(string)),
ApplicationId: aws.String(d.Get(names.AttrApplicationID).(string)),
EnvironmentId: aws.String(d.Get("environment_id").(string)),
ConfigurationProfileId: aws.String(d.Get("configuration_profile_id").(string)),
ConfigurationVersion: aws.String(d.Get("configuration_version").(string)),
Expand Down Expand Up @@ -186,7 +186,7 @@ func resourceDeploymentRead(ctx context.Context, d *schema.ResourceData, meta in
Service: "appconfig",
}.String()

d.Set("application_id", output.ApplicationId)
d.Set(names.AttrApplicationID, output.ApplicationId)
d.Set(names.AttrARN, arn)
d.Set("configuration_profile_id", output.ConfigurationProfileId)
d.Set("configuration_version", output.ConfigurationVersion)
Expand Down
4 changes: 2 additions & 2 deletions internal/service/appconfig/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestAccAppConfigDeployment_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckDeploymentExists(ctx, resourceName),
acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "appconfig", regexache.MustCompile(`application/[0-9a-z]{4,7}/environment/[0-9a-z]{4,7}/deployment/1`)),
resource.TestCheckResourceAttrPair(resourceName, "application_id", appResourceName, names.AttrID),
resource.TestCheckResourceAttrPair(resourceName, names.AttrApplicationID, appResourceName, names.AttrID),
resource.TestCheckResourceAttrPair(resourceName, "configuration_profile_id", confProfResourceName, "configuration_profile_id"),
resource.TestCheckResourceAttrPair(resourceName, "configuration_version", confVersionResourceName, "version_number"),
resource.TestCheckResourceAttr(resourceName, "deployment_number", acctest.Ct1),
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestAccAppConfigDeployment_kms(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckDeploymentExists(ctx, resourceName),
acctest.MatchResourceAttrRegionalARN(resourceName, names.AttrARN, "appconfig", regexache.MustCompile(`application/[0-9a-z]{4,7}/environment/[0-9a-z]{4,7}/deployment/1`)),
resource.TestCheckResourceAttrPair(resourceName, "application_id", appResourceName, names.AttrID),
resource.TestCheckResourceAttrPair(resourceName, names.AttrApplicationID, appResourceName, names.AttrID),
resource.TestCheckResourceAttrPair(resourceName, "configuration_profile_id", confProfResourceName, "configuration_profile_id"),
resource.TestCheckResourceAttrPair(resourceName, "configuration_version", confVersionResourceName, "version_number"),
resource.TestCheckResourceAttr(resourceName, "deployment_number", acctest.Ct1),
Expand Down
8 changes: 4 additions & 4 deletions internal/service/appconfig/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (r *resourceEnvironment) Metadata(_ context.Context, request resource.Metad
func (r *resourceEnvironment) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
s := schema.Schema{
Attributes: map[string]schema.Attribute{
"application_id": schema.StringAttribute{
names.AttrApplicationID: schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
Expand Down Expand Up @@ -282,8 +282,8 @@ func (r *resourceEnvironment) Delete(ctx context.Context, request resource.Delet
}

tflog.Debug(ctx, "Deleting AppConfig Environment", map[string]any{
"application_id": state.ApplicationID.ValueString(),
"environment_id": state.EnvironmentID.ValueString(),
names.AttrApplicationID: state.ApplicationID.ValueString(),
"environment_id": state.EnvironmentID.ValueString(),
})

_, err := conn.DeleteEnvironment(ctx, state.deleteEnvironmentInput())
Expand All @@ -306,7 +306,7 @@ func (r *resourceEnvironment) ImportState(ctx context.Context, request resource.
}

response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("environment_id"), parts[0])...)
response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("application_id"), parts[1])...)
response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root(names.AttrApplicationID), parts[1])...)
}

func (r *resourceEnvironment) ModifyPlan(ctx context.Context, request resource.ModifyPlanRequest, response *resource.ModifyPlanResponse) {
Expand Down
Loading

0 comments on commit 250de7d

Please sign in to comment.