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

Enable goog-terraform-provisioned provider label #11439

Merged
merged 7 commits into from
Aug 20, 2024
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
2 changes: 1 addition & 1 deletion .ci/gcb-generate-diffs-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ steps:
- COMMIT_SHA=$COMMIT_SHA

# Long timeout to enable waiting on VCR test
timeout: 20000s
timeout: 64800s
options:
machineType: 'N1_HIGHCPU_32'

Expand Down
10 changes: 9 additions & 1 deletion mmv1/api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ module Properties

# Add a deprecation message for a resource that's been deprecated in the API.
attr_reader :deprecation_message

# Do not apply the default attribution label
attr_reader :skip_attribution_label
end

include Properties
Expand Down Expand Up @@ -342,6 +345,7 @@ def validate
check :taint_resource_on_failed_create, type: :boolean, default: false
check :skip_sweeper, type: :boolean, default: false
check :deprecation_message, type: ::String
check :skip_attribution_label, type: :boolean, default: false

validate_identity unless @identity.nil?
end
Expand Down Expand Up @@ -475,7 +479,11 @@ def add_labels_related_fields(props, parent)
def add_labels_fields(props, parent, labels)
@custom_diff ||= []
if parent.nil? || parent.flatten_object
@custom_diff.append('tpgresource.SetLabelsDiff')
if @skip_attribution_label
@custom_diff.append('tpgresource.SetLabelsDiffWithoutAttributionLabel')
else
@custom_diff.append('tpgresource.SetLabelsDiff')
end
elsif parent.name == 'metadata'
@custom_diff.append('tpgresource.SetMetadataLabelsDiff')
end
Expand Down
4 changes: 4 additions & 0 deletions mmv1/products/cloudrunv2/Service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ examples:
- 'deletion_protection'
- !ruby/object:Provider::Terraform::Examples
name: 'cloudrunv2_service_mount_gcs'
# Currently failing
skip_vcr: true
primary_resource_id: 'default'
primary_resource_name: "fmt.Sprintf(\"tf-test-cloudrun-service-%s\", context[\"random_suffix\"])"
vars:
Expand All @@ -149,6 +151,8 @@ examples:
- 'deletion_protection'
- !ruby/object:Provider::Terraform::Examples
name: 'cloudrunv2_service_mount_nfs'
# Currently failing
skip_vcr: true
primary_resource_id: 'default'
primary_resource_name: "fmt.Sprintf(\"tf-test-cloudrun-service-%s\", context[\"random_suffix\"])"
vars:
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/compute/ForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ kind: 'compute#forwardingRule'
base_url: projects/{{project}}/regions/{{region}}/forwardingRules
collection_url_key: 'items'
has_self_link: true
# Has a separate endpoint for labels
skip_attribution_label: true
description: |
A ForwardingRule resource. A ForwardingRule resource specifies which pool
of target virtual machines to forward a packet to if it matches the given
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/compute/GlobalForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ kind: 'compute#forwardingRule'
base_url: projects/{{project}}/global/forwardingRules
immutable: true
has_self_link: true
# Has a separate endpoint for labels
skip_attribution_label: true
legacy_long_form_project: true
collection_url_key: 'items'
description: |
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/compute/NetworkPeeringRoutesConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ examples:
network_secondary_name: 'secondary-network'
- !ruby/object:Provider::Terraform::Examples
name: 'network_peering_routes_config_gke'
# currently failing
skip_vcr: true
primary_resource_id: 'peering_gke_routes'
vars:
network_name: 'container-network'
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/dataplex/Datascan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ self_link: 'projects/{{project}}/locations/{{location}}/dataScans/{{data_scan_id
create_url: 'projects/{{project}}/locations/{{location}}/dataScans?dataScanId={{data_scan_id}}'
update_verb: :PATCH
update_mask: true
# User-provided label cannot start with goog-
skip_attribution_label: true
import_format:
[
'projects/{{project}}/locations/{{location}}/dataScans/{{data_scan_id}}',
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/dataplex/Task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ update_url: 'projects/{{project}}/locations/{{location}}/lakes/{{lake}}/tasks/{{
delete_url: 'projects/{{project}}/locations/{{location}}/lakes/{{lake}}/tasks/{{task_id}}'
update_verb: :PATCH
update_mask: true
# User-provided label cannot start with goog-
skip_attribution_label: true
description: |
A Dataplex task represents the work that you want Dataplex to do on a schedule. It encapsulates code, parameters, and the schedule.
async: !ruby/object:Api::OpAsync
Expand Down
4 changes: 4 additions & 0 deletions mmv1/products/edgecontainer/Cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ import_format: ["projects/{{project}}/locations/{{location}}/clusters/{{name}}"]
examples:
- !ruby/object:Provider::Terraform::Examples
name: "edgecontainer_cluster"
# Currently failing
skip_vcr: true
primary_resource_id: "default"
vars:
edgecontainer_cluster_name: "basic-cluster"
- !ruby/object:Provider::Terraform::Examples
name: "edgecontainer_cluster_with_maintenance_window"
# Currently failing
skip_vcr: true
primary_resource_id: "default"
vars:
edgecontainer_cluster_name: "cluster-with-maintenance"
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/kms/AutokeyConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ examples:
- !ruby/object:Provider::Terraform::Examples
name: 'kms_autokey_config_all'
# Need the time_sleep resource
# Currently failing
skip_vcr: true
external_providers: ["random", "time"]
primary_resource_id:
'example-autokeyconfig'
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/kms/KeyHandle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ examples:
- !ruby/object:Provider::Terraform::Examples
name: 'kms_key_handle_basic'
# Need the time_sleep resource
# Currently failing
skip_vcr: true
external_providers: ["random", "time"]
primary_resource_id:
'example-keyhandle'
Expand Down
4 changes: 4 additions & 0 deletions mmv1/products/networksecurity/ClientTlsPolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ examples:
- !ruby/object:Provider::Terraform::Examples
min_version: beta
name: 'network_security_client_tls_policy_basic'
# Currently failing
skip_vcr: true
primary_resource_id: 'default'
vars:
resource_name: 'my-client-tls-policy'
- !ruby/object:Provider::Terraform::Examples
min_version: beta
name: 'network_security_client_tls_policy_advanced'
# Currently failing
skip_vcr: true
primary_resource_id: 'default'
vars:
resource_name: 'my-client-tls-policy'
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/vertexai/FeatureOnlineStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ examples:
- force_destroy
- !ruby/object:Provider::Terraform::Examples
name: vertex_ai_featureonlinestore_with_beta_fields_bigtable
# currently failing
skip_vcr: true
primary_resource_id: featureonlinestore
vars:
name: example_feature_online_store_beta_bigtable
Expand Down
2 changes: 2 additions & 0 deletions mmv1/products/vertexai/FeatureOnlineStoreFeatureview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ examples:
name: 'example_cross_project_featureview'
- !ruby/object:Provider::Terraform::Examples
name: 'vertex_ai_featureonlinestore_featureview_with_vector_search'
# currently failing
skip_vcr: true
primary_resource_id: 'featureview_vector_search'
vars:
name: 'example_feature_view_vector_search'
Expand Down
5 changes: 1 addition & 4 deletions mmv1/third_party/terraform/acctest/test_utils.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc
if _, ok := ignoreFields[k]; ok {
continue
}
if _, ok := ignoreFields["labels.%"]; ok && strings.HasPrefix(k, "labels.") {
continue
}
if _, ok := ignoreFields["terraform_labels.%"]; ok && strings.HasPrefix(k, "terraform_labels.") {
if strings.HasPrefix(k, "labels.") || strings.HasPrefix(k, "terraform_labels.") || strings.HasPrefix(k, "effective_labels.") {
continue
}
if k == "%" {
Expand Down
15 changes: 15 additions & 0 deletions mmv1/third_party/terraform/acctest/vcr_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"slices"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review: the slices package was added in Go 1.21 - as of this week (1.23 was just released) that's a couple of major versions ago but I though I'd call it out in case it's a problem.

"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -147,6 +148,20 @@ func VcrTest(t *testing.T, c resource.TestCase) {
} else if isReleaseDiffEnabled() {
c = initializeReleaseDiffTest(c, t.Name())
}

// terraform_labels is a computed field to which "goog-terraform-provisioned": "true" is always
// added by the provider. ImportStateVerify "checks for strict equality and does not respect
// DiffSuppressFunc or CustomizeDiff" so any test using ImportStateVerify must ignore
// terraform_labels.
var steps []resource.TestStep
for _, s := range c.Steps {
if s.ImportStateVerify && !slices.Contains(s.ImportStateVerifyIgnore, "terraform_labels") {
s.ImportStateVerifyIgnore = append(s.ImportStateVerifyIgnore, "terraform_labels")
}
steps = append(steps, s)
}
c.Steps = steps

resource.Test(t, c)
}

Expand Down
2 changes: 1 addition & 1 deletion mmv1/third_party/terraform/provider/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func Provider() *schema.Provider {
"add_terraform_attribution_label": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"terraform_attribution_label_addition_strategy": {
Expand Down Expand Up @@ -328,7 +329,6 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
config.DefaultLabels[k] = v.(string)
}

// Attribution label is opt-in; if unset, the default for AddTerraformAttributionLabel is false.
config.AddTerraformAttributionLabel = d.Get("add_terraform_attribution_label").(bool)
if config.AddTerraformAttributionLabel {
config.TerraformAttributionLabelAdditionStrategy = transport_tpg.CreateOnlyAttributionStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestAccDataSourceArtifactRegistryDockerImage(t *testing.T) {
acctest.SkipIfVcr(t)
t.Parallel()

resourceName := "data.google_artifact_registry_docker_image.test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestAccBigQueryDataset_withComputedLabels(t *testing.T) {
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "labels.env", "foo"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "labels.default_table_expiration_ms", "3600000"),

resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.%", "2"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.%", "3"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.env", "foo"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.default_table_expiration_ms", "3600000"),
),
Expand Down Expand Up @@ -494,6 +494,10 @@ func addOutOfBandLabels(t *testing.T, datasetID string) resource.TestCheckFunc {

func testAccBigQueryDataset_withoutLabels(datasetID string) string {
return fmt.Sprintf(`
provider "google" {
add_terraform_attribution_label = false
}

resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
friendly_name = "foo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ func TestAccBigqueryDataTransferConfig(t *testing.T) {
"booleanParam": testAccBigqueryDataTransferConfig_copy_booleanParam,
"update_params": testAccBigqueryDataTransferConfig_force_new_update_params,
"update_service_account": testAccBigqueryDataTransferConfig_scheduledQuery_update_service_account,
"salesforce": testAccBigqueryDataTransferConfig_salesforce_basic,
// Multiple connector.authentication.* fields have been deprecated and return 400 errors
// "salesforce": testAccBigqueryDataTransferConfig_salesforce_basic,
}

for name, tc := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ provider "google" {
default_labels = {
default_key1 = "default_value1"
}
add_terraform_attribution_label = false
}

resource "google_clouddeploy_target" "primary" {
Expand Down Expand Up @@ -175,6 +176,7 @@ provider "google" {
default_labels = {
default_key1 = "default_value1"
}
add_terraform_attribution_label = false
}

resource "google_clouddeploy_target" "primary" {
Expand Down Expand Up @@ -216,6 +218,7 @@ provider "google" {
default_key1 = "default_value1"
my_second_label = "example-label-2"
}
add_terraform_attribution_label = false
}

resource "google_clouddeploy_target" "primary" {
Expand Down Expand Up @@ -251,6 +254,10 @@ resource "google_clouddeploy_target" "primary" {

func testAccClouddeployTarget_withoutLabels(context map[string]interface{}) string {
return acctest.Nprintf(`
provider "google" {
add_terraform_attribution_label = false
}

resource "google_clouddeploy_target" "primary" {
location = "%{region}"
name = "tf-test-target%{random_suffix}"
Expand All @@ -277,6 +284,79 @@ resource "google_clouddeploy_target" "primary" {
`, context)
}

func TestAccClouddeployTarget_withAttributionDisabled(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"project_name": envvar.GetTestProjectFromEnv(),
"region": envvar.GetTestRegionFromEnv(),
"random_suffix": acctest.RandString(t, 10),
"add_attribution": "false",
"attribution_strategy": "CREATION_ONLY",
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckClouddeployTargetDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccClouddeployTarget_createWithAttribution(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "labels.%", "2"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "labels.my_first_label", "example-label-1"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "labels.my_second_label", "example-label-2"),

resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.%", "3"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.my_first_label", "example-label-1"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.my_second_label", "example-label-2"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.default_key1", "default_value1"),

resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "effective_labels.%", "3"),
),
},
{
ResourceName: "google_clouddeploy_target.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "annotations"},
},
{
Config: testAccClouddeployTarget_updateWithAttribution(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "labels.%", "2"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "labels.my_first_label", "example-label-updated-1"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "labels.my_second_label", "example-label-updated-2"),

resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.%", "3"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.my_first_label", "example-label-updated-1"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.my_second_label", "example-label-updated-2"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.default_key1", "default_value1"),

resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "effective_labels.%", "3"),
),
},
{
ResourceName: "google_clouddeploy_target.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "annotations"},
},
{
Config: testAccClouddeployTarget_clearWithAttribution(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("google_clouddeploy_target.primary", "labels.%"),

resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.%", "1"),
resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "terraform_labels.default_key1", "default_value1"),

resource.TestCheckResourceAttr("google_clouddeploy_target.primary", "effective_labels.%", "1"),
),
},
},
})
}

func TestAccClouddeployTarget_withCreationOnlyAttribution(t *testing.T) {
t.Parallel()

Expand Down
Loading
Loading