Skip to content

Commit

Permalink
Test out of band labels
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Sep 25, 2023
1 parent b967be6 commit 7d1ee30
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 11 deletions.
7 changes: 7 additions & 0 deletions mmv1/api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ def add_labels_related_fields(props, parent)
def add_labels_fields(props, parent, labels)
# The effective_labels field is used to write to API, instead of the labels field.
labels.ignore_write = true
labels.description = "#{labels.description}\n\n#{get_labels_field_note(labels.name)}"

@custom_diff ||= []
if parent.nil? || parent.flatten_object
Expand Down Expand Up @@ -542,6 +543,12 @@ def ignore_read_labels_fields(props)
fields
end

def get_labels_field_note(title)
"**Note**: This field is non-authoritative, and will only manage the #{title} present " \
"in your configuration.
Please refer to the field `effective_#{title}` for all of the #{title} present on the resource."
end

# ====================
# Version-related methods
# ====================
Expand Down
2 changes: 1 addition & 1 deletion mmv1/api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Fields
include Api::Object::Named::Properties

attr_reader :default_value
attr_reader :description
attr_accessor :description
attr_reader :exclude

# Add a deprecation message for a field that's been deprecated in the API
Expand Down
5 changes: 1 addition & 4 deletions mmv1/products/bigquery/Dataset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,7 @@ properties:
name: 'labels'
description: |
The labels associated with this dataset. You can use these to
organize and group your datasets
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field `effective_labels` for all of the labels present on the resource.
organize and group your datasets.
- !ruby/object:Api::Type::Integer
name: 'lastModifiedTime'
description: |
Expand Down
3 changes: 0 additions & 3 deletions mmv1/products/compute/Address.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ properties:
name: 'labels'
description: |
Labels to apply to this address. A list of key->value pairs.
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field `effective_labels` for all of the labels present on the resource.
update_verb: :POST
update_url: 'projects/{{project}}/regions/{{region}}/addresses/{{name}}/setLabels'
min_version: beta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,49 @@ func TestAccBigQueryDataset_withProvider5(t *testing.T) {
})
}

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

datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryDataset(datasetID),
Check: addOutOfBandLabels(t, datasetID),
},
{
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_contents_on_destroy", "labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetUpdated(datasetID),
},
{
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_contents_on_destroy", "labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetUpdated_withOutOfBandLabels(datasetID),
},
{
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_contents_on_destroy", "labels", "terraform_labels"},
},
},
})
}

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

Expand Down Expand Up @@ -302,6 +345,25 @@ func testAccAddTable(t *testing.T, datasetID string, tableID string) resource.Te
}
}

func addOutOfBandLabels(t *testing.T, datasetID string) resource.TestCheckFunc {
// Not actually a check, but adds labels independently of terraform
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)

dataset, err := config.NewBigQueryClient(config.UserAgent).Datasets.Get(config.Project, datasetID).Do()
if err != nil {
return fmt.Errorf("Could not get dataset with ID %s", datasetID)
}

dataset.Labels["outband_key"] = "test"
_, err = config.NewBigQueryClient(config.UserAgent).Datasets.Patch(config.Project, datasetID, dataset).Do()
if err != nil {
return fmt.Errorf("Could not update labele for the dataset")
}
return nil
}
}

func testAccBigQueryDataset_withoutLabels(datasetID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
Expand Down Expand Up @@ -351,6 +413,25 @@ resource "google_bigquery_dataset" "test" {
`, datasetID)
}

func testAccBigQueryDatasetUpdated_withOutOfBandLabels(datasetID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
friendly_name = "bar"
description = "This is a bar description"
location = "EU"
default_partition_expiration_ms = 7200000
default_table_expiration_ms = 7200000
labels = {
env = "bar"
default_table_expiration_ms = 7200000
outband_key = "test-update"
}
}
`, datasetID)
}

func testAccBigQueryDatasetUpdated2(datasetID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,21 @@ 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,9 @@ 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 @@ -124,5 +124,8 @@ func datasourceComputeRegionInstanceTemplateRead(d *schema.ResourceData, meta in
func retrieveInstances(d *schema.ResourceData, meta interface{}, project, region, name string) error {
d.SetId("projects/" + project + "/regions/" + region + "/instanceTemplates/" + name)

return resourceComputeRegionInstanceTemplateRead(d, meta)
if err := resourceComputeRegionInstanceTemplateRead(d, meta); err != nil {
return err
}
return tpgresource.SetDataSourceLabels(d)
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ resource "google_compute_region_instance_template" "default" {
network_interface {
network = "default"
}
labels = {
my-label = "my-label-value"
}
}
data "google_compute_region_instance_template" "default" {
project = "%{project}"
Expand Down

0 comments on commit 7d1ee30

Please sign in to comment.