Skip to content

Commit

Permalink
Make labels field have all labels in data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Sep 21, 2023
1 parent 63fe377 commit 13d8e87
Show file tree
Hide file tree
Showing 56 changed files with 218 additions and 25 deletions.
6 changes: 0 additions & 6 deletions mmv1/third_party/terraform/acctest/test_utils.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ 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.") {
continue
}
if k == "%" {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func dataSourceArtifactRegistryRepositoryRead(d *schema.ResourceData, meta inter
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestAccDataSourceGoogleArtifactRegistryRepositoryConfig(t *testing.T) {
{
Config: testAccDataSourceGoogleArtifactRegistryRepositoryConfig(context),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(funcDataName,
"google_artifact_registry_repository.my-repo", map[string]struct{}{"labels.%": {}, "terraform_labels.%": {}}),
acctest.CheckDataSourceStateMatchesResourceState(funcDataName,
"google_artifact_registry_repository.my-repo"),
),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func dataSourceGoogleBeyondcorpAppConnectionRead(d *schema.ResourceData, meta in
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ resource "google_beyondcorp_app_connection" "foo" {
port = 8080
}
connectors = [google_beyondcorp_app_connector.app_connector.id]
labels = {
my-label = "my-label-value"
}
}
data "google_beyondcorp_app_connection" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func dataSourceGoogleBeyondcorpAppConnectorRead(d *schema.ResourceData, meta int
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ resource "google_beyondcorp_app_connector" "foo" {
email = google_service_account.service_account.email
}
}
labels = {
my-label = "my-label-value"
}
}
data "google_beyondcorp_app_connector" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func dataSourceGoogleBeyondcorpAppGatewayRead(d *schema.ResourceData, meta inter
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ resource "google_beyondcorp_app_gateway" "foo" {
name = "tf-test-appgateway-%{random_suffix}"
type = "TCP_PROXY"
host_type = "GCP_REGIONAL_MIG"
labels = {
my-label = "my-label-value"
}
}
data "google_beyondcorp_app_gateway" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func dataSourceGoogleCloudFunctionsFunctionRead(d *schema.ResourceData, meta int
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", cloudFuncId.CloudFunctionId())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ resource "google_cloudfunctions_function" "function_http" {
trigger_http = true
timeout = 61
entry_point = "helloGET"
labels = {
my-label = "my-label-value"
}
}
data "google_cloudfunctions_function" "function_http" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func dataSourceGoogleCloudFunctions2FunctionRead(d *schema.ResourceData, meta in
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ func TestAccDataSourceGoogleCloudFunctions2Function_basic(t *testing.T) {
{
Config: testAccDataSourceGoogleCloudFunctions2FunctionConfig(functionName,
bucketName, zipFilePath),
// As the value of "labels" and "terraform_labels" in the state is dependent on the configuration,
// and these fields are not set in the configuration of the data source, so these fields are empty in the state of the data source.
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(funcDataNameHttp,
"google_cloudfunctions2_function.function_http_v2", map[string]struct{}{"build_config.0.source.0.storage_source.0.bucket": {}, "build_config.0.source.0.storage_source.0.object": {}, "labels.%": {}, "terraform_labels.%": {}}),
"google_cloudfunctions2_function.function_http_v2", map[string]struct{}{"build_config.0.source.0.storage_source.0.bucket": {}, "build_config.0.source.0.storage_source.0.object": {}}),
),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func dataSourceGoogleComposerEnvironmentRead(d *schema.ResourceData, meta interf
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestAccDataSourceComposerEnvironment_basic(t *testing.T) {
{
Config: testAccDataSourceComposerEnvironment_basic(context),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceState("data.google_composer_environment.test",
"google_composer_environment.test"),
testAccCheckGoogleComposerEnvironmentMeta("data.google_composer_environment.test"),
),
},
Expand Down Expand Up @@ -91,6 +93,9 @@ resource "google_composer_environment" "test" {
image_version = "composer-1-airflow-2"
}
}
labels = {
my-label = "my-label-value"
}
}
// use a separate network to avoid conflicts with other tests running in parallel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func dataSourceGoogleComputeDiskRead(d *schema.ResourceData, meta interface{}) e
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func TestAccDataSourceGoogleComputeDisk_basic(t *testing.T) {
func testAccDataSourceGoogleComputeDisk_basic(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_disk" "foo" {
name = "tf-test-compute-disk-%{random_suffix}"
name = "tf-test-compute-disk-%{random_suffix}"
labels = {
my-label = "my-label-value"
}
}
data "google_compute_disk" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func dataSourceGoogleComputeForwardingRuleRead(d *schema.ResourceData, meta inte
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ resource "google_compute_forwarding_rule" "foobar-fr" {
name = "%s"
port_range = "80-81"
target = google_compute_target_pool.foobar-tp.self_link
labels = {
my-label = "my-label-value"
}
}
data "google_compute_forwarding_rule" "my_forwarding_rule" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func dataSourceGoogleComputeInstanceRead(d *schema.ResourceData, meta interface{
return err
}

if err := d.Set("terraform_labels", instance.Labels); err != nil {
return err
}

if instance.LabelFingerprint != "" {
if err := d.Set("label_fingerprint", instance.LabelFingerprint); err != nil {
return fmt.Errorf("Error setting label_fingerprint: %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func dataSourceGoogleComputeSnapshotRead(d *schema.ResourceData, meta interface{
func retrieveSnapshot(d *schema.ResourceData, meta interface{}, project, name string) error {
d.SetId("projects/" + project + "/global/snapshots/" + name)
d.Set("name", name)
return resourceComputeSnapshotRead(d, meta)
if err := resourceComputeSnapshotRead(d, meta); err != nil {
return err
}
return tpgresource.SetDataSourceLabels(d)
}

// ByCreationTimestamp implements sort.Interface for []*Snapshot based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccSnapshotDatasource_name(t *testing.T) {
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
"data.google_compute_snapshot.default",
"google_compute_snapshot.default",
map[string]struct{}{"zone": {}, "labels.%": {}, "terraform_labels.%": {}},
map[string]struct{}{"zone": {}},
),
),
},
Expand All @@ -42,7 +42,7 @@ func TestAccSnapshotDatasource_filter(t *testing.T) {
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
"data.google_compute_snapshot.default",
"google_compute_snapshot.c",
map[string]struct{}{"zone": {}, "labels.%": {}, "terraform_labels.%": {}},
map[string]struct{}{"zone": {}},
),
),
},
Expand All @@ -63,7 +63,7 @@ func TestAccSnapshotDatasource_filterMostRecent(t *testing.T) {
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
"data.google_compute_snapshot.default",
"google_compute_snapshot.c",
map[string]struct{}{"zone": {}, "labels.%": {}, "terraform_labels.%": {}},
map[string]struct{}{"zone": {}},
),
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func dataSourceGoogleComputeGlobalForwardingRuleRead(d *schema.ResourceData, met
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ resource "google_compute_global_forwarding_rule" "foobar-fr" {
name = "%s"
target = google_compute_target_http_proxy.default.id
port_range = "80"
labels = {
my-label = "my-label-value"
}
}
resource "google_compute_target_http_proxy" "default" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func dataSourceDataprocMetastoreServiceRead(d *schema.ResourceData, meta interfa
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ resource "google_dataproc_metastore_service" "my_metastore" {
hive_metastore_config {
version = "2.3.6"
}

labels = {
env = "test"
}
}

data "google_dataproc_metastore_service" "my_metastore" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func dataSourceGoogleFirebaseHostingChannelRead(d *schema.ResourceData, meta int
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ resource "google_firebase_hosting_site" "default" {
resource "google_firebase_hosting_channel" "channel" {
site_id = google_firebase_hosting_site.default.site_id
channel_id = "tf-test-channel%{random_suffix}"

labels = {
foo = "bar"
}
}

data "google_firebase_hosting_channel" "channel" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func dataSourceGoogleKmsCryptoKeyRead(d *schema.ResourceData, meta interface{})
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func dataSourcePrivatecaCertificateAuthorityRead(d *schema.ResourceData, meta in
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

// pem_csr is only applicable for SUBORDINATE CertificateAuthorities when their state is AWAITING_USER_ACTIVATION
if d.Get("type") == "SUBORDINATE" && d.Get("state") == "AWAITING_USER_ACTIVATION" {
url, err := tpgresource.ReplaceVars(d, config, "{{PrivatecaBasePath}}projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}:fetch")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccDataSourcePrivatecaCertificateAuthority_privatecaCertificateAuthorit
{
Config: testAccDataSourcePrivatecaCertificateAuthority_privatecaCertificateAuthorityBasicExample(context),
Check: resource.ComposeTestCheckFunc(
acctest.CheckDataSourceStateMatchesResourceState("data.google_privateca_certificate_authority.default", "google_privateca_certificate_authority.default"),
resource.TestCheckResourceAttrSet("data.google_privateca_certificate_authority.default", "pem_csr"),
),
},
Expand Down Expand Up @@ -81,6 +82,9 @@ resource "google_privateca_certificate_authority" "default" {
key_spec {
algorithm = "RSA_PKCS1_4096_SHA256"
}
labels = {
my-label = "my-label-value"
}
}
data "google_privateca_certificate_authority" "default" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func dataSourceGooglePubsubSubscriptionRead(d *schema.ResourceData, meta interfa
return err
}

if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ resource "google_pubsub_topic" "foo" {
resource "google_pubsub_subscription" "foo" {
name = "tf-test-pubsub-subscription-%{random_suffix}"
topic = google_pubsub_topic.foo.name
labels = {
my-label = "my-label-value"
}
}
data "google_pubsub_subscription" "foo" {
Expand Down
Loading

0 comments on commit 13d8e87

Please sign in to comment.