From b11c43f5cd6c005355300d18af39939144e8edbc Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 27 Nov 2019 12:34:03 -0800 Subject: [PATCH] =?UTF-8?q?resolve=20issues=20that=20would=20cause=20lint?= =?UTF-8?q?=20warnings=20if=20we=20enabled=20the=E2=80=A6=20(#5004)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Modular Magician --- google/data_source_google_compute_node_types.go | 2 +- google/data_source_google_compute_regions.go | 2 +- google/data_source_google_compute_zones.go | 2 +- google/import.go | 6 ++---- google/kms_utils.go | 2 +- google/provider.go | 2 +- google/resource_bigquery_table.go | 8 ++++---- google/resource_bigtable_app_profile.go | 6 +----- google/resource_cloud_scheduler_job.go | 8 ++++---- google/resource_cloudfunctions_function.go | 2 +- google/resource_compute_disk.go | 6 +----- google/resource_compute_firewall.go | 5 +---- google/resource_compute_instance_group_migrate_test.go | 2 +- google/resource_compute_instance_iam_test.go | 2 +- google/resource_compute_instance_migrate.go | 10 +++++----- google/resource_compute_instance_template.go | 2 +- google/resource_compute_subnetwork_iam_test.go | 2 +- google/resource_compute_target_pool.go | 2 +- google/resource_container_cluster.go | 6 +++--- google/resource_firestore_index.go | 2 +- google/resource_pubsub_subscription.go | 2 +- google/resource_security_center_source.go | 2 +- google/resource_spanner_database_iam_test.go | 4 ++-- google/resource_spanner_instance_iam_test.go | 4 ++-- google/resource_storage_bucket.go | 2 +- google/resource_tpu_node.go | 4 ++-- google/utils.go | 5 +---- google/validation_test.go | 3 ++- 28 files changed, 45 insertions(+), 60 deletions(-) diff --git a/google/data_source_google_compute_node_types.go b/google/data_source_google_compute_node_types.go index e10575e4019..f3b96d983f2 100644 --- a/google/data_source_google_compute_node_types.go +++ b/google/data_source_google_compute_node_types.go @@ -62,7 +62,7 @@ func dataSourceGoogleComputeNodeTypesRead(d *schema.ResourceData, meta interface } func flattenComputeNodeTypes(nodeTypes []*compute.NodeType) []string { - result := make([]string, len(nodeTypes), len(nodeTypes)) + result := make([]string, len(nodeTypes)) for i, nodeType := range nodeTypes { result[i] = nodeType.Name } diff --git a/google/data_source_google_compute_regions.go b/google/data_source_google_compute_regions.go index 2b6a359f632..80448d9a659 100644 --- a/google/data_source_google_compute_regions.go +++ b/google/data_source_google_compute_regions.go @@ -64,7 +64,7 @@ func dataSourceGoogleComputeRegionsRead(d *schema.ResourceData, meta interface{} } func flattenRegions(regions []*compute.Region) []string { - result := make([]string, len(regions), len(regions)) + result := make([]string, len(regions)) for i, region := range regions { result[i] = region.Name } diff --git a/google/data_source_google_compute_zones.go b/google/data_source_google_compute_zones.go index 981026e9ab2..8e40941cf7a 100644 --- a/google/data_source_google_compute_zones.go +++ b/google/data_source_google_compute_zones.go @@ -86,7 +86,7 @@ func dataSourceGoogleComputeZonesRead(d *schema.ResourceData, meta interface{}) } func flattenZones(zones []*compute.Zone) []string { - result := make([]string, len(zones), len(zones)) + result := make([]string, len(zones)) for i, zone := range zones { result[i] = zone.Name } diff --git a/google/import.go b/google/import.go index 98c9d7b1a56..3975d79f6f0 100644 --- a/google/import.go +++ b/google/import.go @@ -117,8 +117,7 @@ func getImportIdQualifiers(idRegexes []string, d TerraformResourceData, config * } if fieldValues := re.FindStringSubmatch(id); fieldValues != nil { - var result map[string]string - result = make(map[string]string) + result := make(map[string]string) log.Printf("[DEBUG] matching ID %s to regex %s.", id, idFormat) // Starting at index 1, the first match is the full string. for i := 1; i < len(fieldValues); i++ { @@ -148,8 +147,7 @@ func getImportIdQualifiers(idRegexes []string, d TerraformResourceData, config * // Returns a set of default values that are contained in a regular expression // This does not mutate any parameters, instead returning a map of defaults func getDefaultValues(idRegex string, d TerraformResourceData, config *Config) (map[string]string, error) { - var result map[string]string - result = make(map[string]string) + result := make(map[string]string) if _, ok := d.GetOk("project"); !ok && strings.Contains(idRegex, "?P") { project, err := getProject(d, config) if err != nil { diff --git a/google/kms_utils.go b/google/kms_utils.go index 2cea50c453b..08756354551 100644 --- a/google/kms_utils.go +++ b/google/kms_utils.go @@ -87,7 +87,7 @@ func (s *kmsCryptoKeyId) terraformId() string { func validateKmsCryptoKeyRotationPeriod(value interface{}, _ string) (ws []string, errors []error) { period := value.(string) - pattern := regexp.MustCompile("^([0-9.]*\\d)s$") + pattern := regexp.MustCompile(`^([0-9.]*\\d)s$`) match := pattern.FindStringSubmatch(period) if len(match) == 0 { diff --git a/google/provider.go b/google/provider.go index 3147252e49f..9b03266c96b 100644 --- a/google/provider.go +++ b/google/provider.go @@ -722,7 +722,7 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa scopes := d.Get("scopes").([]interface{}) if len(scopes) > 0 { - config.Scopes = make([]string, len(scopes), len(scopes)) + config.Scopes = make([]string, len(scopes)) } for i, scope := range scopes { config.Scopes[i] = scope.(string) diff --git a/google/resource_bigquery_table.go b/google/resource_bigquery_table.go index d92b1870225..a0483ee4da3 100644 --- a/google/resource_bigquery_table.go +++ b/google/resource_bigquery_table.go @@ -647,7 +647,7 @@ func flattenExternalDataConfiguration(edc *bigquery.ExternalDataConfiguration) ( result["google_sheets_options"] = flattenGoogleSheetsOptions(edc.GoogleSheetsOptions) } - if edc.IgnoreUnknownValues == true { + if edc.IgnoreUnknownValues { result["ignore_unknown_values"] = edc.IgnoreUnknownValues } if edc.MaxBadRecords != 0 { @@ -702,11 +702,11 @@ func expandCsvOptions(configured interface{}) *bigquery.CsvOptions { func flattenCsvOptions(opts *bigquery.CsvOptions) []map[string]interface{} { result := map[string]interface{}{} - if opts.AllowJaggedRows == true { + if opts.AllowJaggedRows { result["allow_jagged_rows"] = opts.AllowJaggedRows } - if opts.AllowQuotedNewlines == true { + if opts.AllowQuotedNewlines { result["allow_quoted_newlines"] = opts.AllowQuotedNewlines } @@ -814,7 +814,7 @@ func flattenTimePartitioning(tp *bigquery.TimePartitioning) []map[string]interfa result["expiration_ms"] = tp.ExpirationMs } - if tp.RequirePartitionFilter == true { + if tp.RequirePartitionFilter { result["require_partition_filter"] = tp.RequirePartitionFilter } diff --git a/google/resource_bigtable_app_profile.go b/google/resource_bigtable_app_profile.go index 781e2f7d625..bb8bae19fd9 100644 --- a/google/resource_bigtable_app_profile.go +++ b/google/resource_bigtable_app_profile.go @@ -296,11 +296,7 @@ func flattenBigtableAppProfileDescription(v interface{}, d *schema.ResourceData) } func flattenBigtableAppProfileMultiClusterRoutingUseAny(v interface{}, d *schema.ResourceData) interface{} { - if v == nil { - return false - } - - return true + return v != nil } func flattenBigtableAppProfileSingleClusterRouting(v interface{}, d *schema.ResourceData) interface{} { diff --git a/google/resource_cloud_scheduler_job.go b/google/resource_cloud_scheduler_job.go index 0a3ee1171e2..b6866e34c6b 100644 --- a/google/resource_cloud_scheduler_job.go +++ b/google/resource_cloud_scheduler_job.go @@ -763,9 +763,9 @@ func flattenCloudSchedulerJobAppEngineHttpTargetHeaders(v interface{}, d *schema delete(headers, "Content-Type") } } + r := regexp.MustCompile(`(X-Google-|X-AppEngine-|Content-Length).*`) for key := range headers { - match, _ := regexp.MatchString("(X-Google-|X-AppEngine-|Content-Length).*", key) - if match { + if r.MatchString(key) { delete(headers, key) } } @@ -823,9 +823,9 @@ func flattenCloudSchedulerJobHttpTargetHeaders(v interface{}, d *schema.Resource delete(headers, "Content-Type") } } + r := regexp.MustCompile(`(X-Google-|X-AppEngine-|Content-Length).*`) for key := range headers { - match, _ := regexp.MatchString("(X-Google-|X-AppEngine-|Content-Length).*", key) - if match { + if r.MatchString(key) { delete(headers, key) } } diff --git a/google/resource_cloudfunctions_function.go b/google/resource_cloudfunctions_function.go index fe693c051e1..2c69f633afd 100644 --- a/google/resource_cloudfunctions_function.go +++ b/google/resource_cloudfunctions_function.go @@ -160,7 +160,7 @@ func resourceCloudFunctionsFunction() *schema.Resource { ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { availableMemoryMB := v.(int) - if functionAllowedMemory[availableMemoryMB] != true { + if !functionAllowedMemory[availableMemoryMB] { errors = append(errors, fmt.Errorf("Allowed values for memory (in MB) are: %s . Got %d", joinMapKeys(&functionAllowedMemory), availableMemoryMB)) } diff --git a/google/resource_compute_disk.go b/google/resource_compute_disk.go index b3d2674e807..93992ff5590 100644 --- a/google/resource_compute_disk.go +++ b/google/resource_compute_disk.go @@ -215,11 +215,7 @@ func suppressWindowsFamilyDiff(imageName, familyName string) bool { updatedFamilyString := strings.Replace(familyName, "windows-", "windows-server-", 1) updatedImageName := strings.Replace(imageName, "-dc-", "-", 1) - if strings.Contains(updatedImageName, updatedFamilyString) { - return true - } - - return false + return strings.Contains(updatedImageName, updatedFamilyString) } func resourceComputeDisk() *schema.Resource { diff --git a/google/resource_compute_firewall.go b/google/resource_compute_firewall.go index 9796083743d..9360402c43a 100644 --- a/google/resource_compute_firewall.go +++ b/google/resource_compute_firewall.go @@ -49,10 +49,7 @@ func resourceComputeFirewallRuleHash(v interface{}) int { } func compareCaseInsensitive(k, old, new string, d *schema.ResourceData) bool { - if strings.ToLower(old) == strings.ToLower(new) { - return true - } - return false + return strings.ToLower(old) == strings.ToLower(new) } func resourceComputeFirewall() *schema.Resource { diff --git a/google/resource_compute_instance_group_migrate_test.go b/google/resource_compute_instance_group_migrate_test.go index 84649d7a1fe..11c286c5e14 100644 --- a/google/resource_compute_instance_group_migrate_test.go +++ b/google/resource_compute_instance_group_migrate_test.go @@ -102,7 +102,7 @@ func TestComputeInstanceGroupMigrateState_empty(t *testing.T) { // should handle non-nil but empty is = &terraform.InstanceState{} - is, err = resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta) + _, err = resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta) if err != nil { t.Fatalf("bad %s, err: %#v", tn, err) diff --git a/google/resource_compute_instance_iam_test.go b/google/resource_compute_instance_iam_test.go index 98f4002a095..0922cd8ca54 100644 --- a/google/resource_compute_instance_iam_test.go +++ b/google/resource_compute_instance_iam_test.go @@ -46,7 +46,7 @@ func TestAccComputeInstanceIamPolicy(t *testing.T) { }, { ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s", instanceName), + ImportStateId: instanceName, ImportState: true, ImportStateVerify: true, }, diff --git a/google/resource_compute_instance_migrate.go b/google/resource_compute_instance_migrate.go index 95d73d5e2d2..fb18fa18fe5 100644 --- a/google/resource_compute_instance_migrate.go +++ b/google/resource_compute_instance_migrate.go @@ -405,7 +405,7 @@ func getDiskFromAttributes(config *Config, instance *compute.Instance, allDisks func getDiskFromSource(instance *compute.Instance, source string) (*compute.AttachedDisk, error) { for _, disk := range instance.Disks { - if disk.Boot == true || disk.Type == "SCRATCH" { + if disk.Boot || disk.Type == "SCRATCH" { // Ignore boot/scratch disks since this is just for finding attached disks continue } @@ -420,7 +420,7 @@ func getDiskFromSource(instance *compute.Instance, source string) (*compute.Atta func getDiskFromDeviceName(instance *compute.Instance, deviceName string) (*compute.AttachedDisk, error) { for _, disk := range instance.Disks { - if disk.Boot == true || disk.Type == "SCRATCH" { + if disk.Boot || disk.Type == "SCRATCH" { // Ignore boot/scratch disks since this is just for finding attached disks continue } @@ -437,7 +437,7 @@ func getDiskFromEncryptionKey(instance *compute.Instance, encryptionKey string) return nil, err } for _, disk := range instance.Disks { - if disk.Boot == true || disk.Type == "SCRATCH" { + if disk.Boot || disk.Type == "SCRATCH" { // Ignore boot/scratch disks since this is just for finding attached disks continue } @@ -457,7 +457,7 @@ func getDiskFromAutoDeleteAndImage(config *Config, instance *compute.Instance, a canonicalImage := imgParts[len(imgParts)-1] for i, disk := range instance.Disks { - if disk.Boot == true || disk.Type == "SCRATCH" { + if disk.Boot || disk.Type == "SCRATCH" { // Ignore boot/scratch disks since this is just for finding attached disks continue } @@ -482,7 +482,7 @@ func getDiskFromAutoDeleteAndImage(config *Config, instance *compute.Instance, a // the image family. canonicalImage = strings.Replace(canonicalImage, "/family/", "/", -1) for i, disk := range instance.Disks { - if disk.Boot == true || disk.Type == "SCRATCH" { + if disk.Boot || disk.Type == "SCRATCH" { // Ignore boot/scratch disks since this is just for finding attached disks continue } diff --git a/google/resource_compute_instance_template.go b/google/resource_compute_instance_template.go index 2eb1634ca72..99c42e24ef0 100644 --- a/google/resource_compute_instance_template.go +++ b/google/resource_compute_instance_template.go @@ -857,7 +857,7 @@ func reorderDisks(configDisks []interface{}, apiDisks []map[string]interface{}) return apiDisks } - result := make([]map[string]interface{}, len(apiDisks), len(apiDisks)) + result := make([]map[string]interface{}, len(apiDisks)) /* Disks aren't necessarily returned from the API in the same order they were sent, so gather diff --git a/google/resource_compute_subnetwork_iam_test.go b/google/resource_compute_subnetwork_iam_test.go index 1f3f49ff0ce..f90caba8d0a 100644 --- a/google/resource_compute_subnetwork_iam_test.go +++ b/google/resource_compute_subnetwork_iam_test.go @@ -47,7 +47,7 @@ func TestAccComputeSubnetworkIamPolicy(t *testing.T) { }, { ResourceName: "google_compute_subnetwork_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s", subnetwork), + ImportStateId: subnetwork, ImportState: true, ImportStateVerify: true, }, diff --git a/google/resource_compute_target_pool.go b/google/resource_compute_target_pool.go index 08c4378a9d1..aa8a6891690 100644 --- a/google/resource_compute_target_pool.go +++ b/google/resource_compute_target_pool.go @@ -122,7 +122,7 @@ func canonicalizeInstanceRef(instanceRef string) string { // Healthchecks need to exist before being referred to from the target pool. func convertHealthChecks(healthChecks []interface{}, d *schema.ResourceData, config *Config) ([]string, error) { - if healthChecks == nil || len(healthChecks) == 0 { + if len(healthChecks) == 0 { return []string{}, nil } diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index 4272f6e96b5..6e8da842e3c 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -1706,7 +1706,7 @@ func expandMaintenancePolicy(d *schema.ResourceData, meta interface{}) *containe if cluster != nil && cluster.MaintenancePolicy != nil { resourceVersion = cluster.MaintenancePolicy.ResourceVersion } - exclusions := make(map[string]containerBeta.TimeWindow, 0) + exclusions := make(map[string]containerBeta.TimeWindow) if cluster != nil && cluster.MaintenancePolicy != nil && cluster.MaintenancePolicy.Window != nil { exclusions = cluster.MaintenancePolicy.Window.MaintenanceExclusions } @@ -1993,7 +1993,7 @@ func flattenPrivateClusterConfig(c *containerBeta.PrivateClusterConfig) []map[st func flattenIPAllocationPolicy(c *containerBeta.Cluster, d *schema.ResourceData, config *Config) []map[string]interface{} { // If IP aliasing isn't enabled, none of the values in this block can be set. - if c == nil || c.IpAllocationPolicy == nil || c.IpAllocationPolicy.UseIpAliases == false { + if c == nil || c.IpAllocationPolicy == nil || !c.IpAllocationPolicy.UseIpAliases { return nil } @@ -2224,7 +2224,7 @@ func containerClusterPrivateClusterConfigCustomDiff(d *schema.ResourceDiff, meta return nil } config := pccList[0].(map[string]interface{}) - if config["enable_private_nodes"].(bool) == true { + if config["enable_private_nodes"].(bool) { block := config["master_ipv4_cidr_block"] // We can only apply this validation if we know the final value of the field, and we may diff --git a/google/resource_firestore_index.go b/google/resource_firestore_index.go index f651ba70d40..3c936673c82 100644 --- a/google/resource_firestore_index.go +++ b/google/resource_firestore_index.go @@ -281,7 +281,7 @@ func resourceFirestoreIndexImport(d *schema.ResourceData, meta interface{}) ([]* ) } - d.Set("project", fmt.Sprintf("%s", stringParts[1])) + d.Set("project", stringParts[1]) return []*schema.ResourceData{d}, nil } diff --git a/google/resource_pubsub_subscription.go b/google/resource_pubsub_subscription.go index 2af183146ee..d93fbe720ba 100644 --- a/google/resource_pubsub_subscription.go +++ b/google/resource_pubsub_subscription.go @@ -635,7 +635,7 @@ func expandPubsubSubscriptionTopic(v interface{}, d TerraformResourceData, confi topic := d.Get("topic").(string) - re := regexp.MustCompile("projects\\/(.*)\\/topics\\/(.*)") + re := regexp.MustCompile(`projects\/(.*)\/topics\/(.*)`) match := re.FindStringSubmatch(topic) if len(match) == 3 { return topic, nil diff --git a/google/resource_security_center_source.go b/google/resource_security_center_source.go index d3aab82e9c1..c01d9acc86a 100644 --- a/google/resource_security_center_source.go +++ b/google/resource_security_center_source.go @@ -223,7 +223,7 @@ func resourceSecurityCenterSourceImport(d *schema.ResourceData, meta interface{} ) } - d.Set("organization", fmt.Sprintf("%s", stringParts[1])) + d.Set("organization", stringParts[1]) return []*schema.ResourceData{d}, nil } diff --git a/google/resource_spanner_database_iam_test.go b/google/resource_spanner_database_iam_test.go index 294fb9f835e..a509ea69b43 100644 --- a/google/resource_spanner_database_iam_test.go +++ b/google/resource_spanner_database_iam_test.go @@ -102,11 +102,11 @@ func TestAccSpannerDatabaseIamPolicy(t *testing.T) { // Test a few import formats { ResourceName: "google_spanner_database_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s", spannerDatabaseId{ + ImportStateId: spannerDatabaseId{ Instance: instance, Database: database, Project: project, - }.terraformId()), + }.terraformId(), ImportState: true, ImportStateVerify: true, }, diff --git a/google/resource_spanner_instance_iam_test.go b/google/resource_spanner_instance_iam_test.go index 854558b9f6d..9b46c19a300 100644 --- a/google/resource_spanner_instance_iam_test.go +++ b/google/resource_spanner_instance_iam_test.go @@ -96,10 +96,10 @@ func TestAccSpannerInstanceIamPolicy(t *testing.T) { // Test a few import formats { ResourceName: "google_spanner_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s", spannerInstanceId{ + ImportStateId: spannerInstanceId{ Instance: instance, Project: project, - }.terraformId()), + }.terraformId(), ImportState: true, ImportStateVerify: true, }, diff --git a/google/resource_storage_bucket.go b/google/resource_storage_bucket.go index df53bf5921f..6e67baa00f7 100644 --- a/google/resource_storage_bucket.go +++ b/google/resource_storage_bucket.go @@ -734,7 +734,7 @@ func flattenCors(corsRules []*storage.BucketCors) []map[string]interface{} { func expandBucketEncryption(configured interface{}) *storage.BucketEncryption { encs := configured.([]interface{}) - if encs == nil || len(encs) == 0 || encs[0] == nil { + if len(encs) == 0 || encs[0] == nil { return nil } enc := encs[0].(map[string]interface{}) diff --git a/google/resource_tpu_node.go b/google/resource_tpu_node.go index 3fa6853ab58..73da687b969 100644 --- a/google/resource_tpu_node.go +++ b/google/resource_tpu_node.go @@ -46,9 +46,9 @@ func validateHttpHeaders() schema.SchemaValidateFunc { es = append(es, fmt.Errorf("Cannot set the Content-Length header on %s", k)) return } + r := regexp.MustCompile(`(X-Google-|X-AppEngine-).*`) for key := range headers { - match, _ := regexp.MatchString("(X-Google-|X-AppEngine-).*", key) - if match { + if r.MatchString(key) { es = append(es, fmt.Errorf("Cannot set the %s header on %s", key, k)) return } diff --git a/google/utils.go b/google/utils.go index 8aa782f5e4b..9c9ce2deec6 100644 --- a/google/utils.go +++ b/google/utils.go @@ -211,10 +211,7 @@ func caseDiffSuppress(_, old, new string, _ *schema.ResourceData) bool { // `old` is read from the server and always has the full range format (e.g. '80-80', '1024-2048'). // `new` can be either a single port or a port range. func portRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool { - if old == new+"-"+new { - return true - } - return false + return old == new+"-"+new } // Single-digit hour is equivalent to hour with leading zero e.g. suppress diff 1:00 => 01:00. diff --git a/google/validation_test.go b/google/validation_test.go index 7bbca94cc2d..8d93451ef54 100644 --- a/google/validation_test.go +++ b/google/validation_test.go @@ -210,8 +210,9 @@ func TestProjectRegex(t *testing.T) { {"", false}, {"example_", false}, } + r := regexp.MustCompile("^" + ProjectRegex + "$") for _, test := range tests { - if got, err := regexp.MatchString("^"+ProjectRegex+"$", test.project); err != nil || got != test.want { + if got := r.MatchString(test.project); got != test.want { t.Errorf("got %t, want %t for project %v", got, test.want, test.project) } }