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 4a03c32 commit c8a53e5
Show file tree
Hide file tree
Showing 58 changed files with 227 additions and 27 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 @@ -92,15 +92,23 @@ func datasourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interfac
func retrieveInstance(d *schema.ResourceData, meta interface{}, project, name string) error {
d.SetId("projects/" + project + "/global/instanceTemplates/" + name)

return resourceComputeInstanceTemplateRead(d, meta)
if err := resourceComputeInstanceTemplateRead(d, meta); err != nil {
return err
}

return tpgresource.SetDataSourceLabels(d)
}

func retrieveInstanceFromUniqueId(d *schema.ResourceData, meta interface{}, project, self_link_unique string) error {
normalId, _ := parseUniqueId(self_link_unique)
d.SetId(normalId)
d.Set("self_link_unique", self_link_unique)

return resourceComputeInstanceTemplateRead(d, meta)
if err := resourceComputeInstanceTemplateRead(d, meta); err != nil {
return err
}

return tpgresource.SetDataSourceLabels(d)
}

// ByCreationTimestamp implements sort.Interface for []*InstanceTemplate based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ resource "google_compute_instance_template" "default" {
network_interface {
network = "default"
}
labels = {
my-label = "my-label-value"
}
}
data "google_compute_instance_template" "default" {
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
Loading

0 comments on commit c8a53e5

Please sign in to comment.