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

Labels compute instance template #9051

Merged
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func ResourceComputeInstanceTemplate() *schema.Resource {
resourceComputeInstanceTemplateSourceImageCustomizeDiff,
resourceComputeInstanceTemplateScratchDiskCustomizeDiff,
resourceComputeInstanceTemplateBootDiskCustomizeDiff,
tpgresource.SetLabelsDiff,
),
MigrateState: resourceComputeInstanceTemplateMigrateState,

Expand Down Expand Up @@ -927,9 +928,19 @@ be from 0 to 999,999,999 inclusive.`,
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
},

"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Set: schema.HashString,
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},

"effective_labels": {
Type: schema.TypeMap,
Computed: true,
ForceNew: true,
Set: schema.HashString,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -1320,8 +1331,8 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
ReservationAffinity: reservationAffinity,
}

if _, ok := d.GetOk("labels"); ok {
instanceProperties.Labels = tpgresource.ExpandLabels(d)
if _, ok := d.GetOk("effective_labels"); ok {
instanceProperties.Labels = tpgresource.ExpandEffectiveLabels(d)
}

var itName string
Expand Down Expand Up @@ -1649,6 +1660,9 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting labels: %s", err)
}
}
if err := tpgresource.SetLabels(instanceTemplate.Properties.Labels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", instanceTemplate.Properties.Labels); err != nil {
return fmt.Errorf("Error setting effective_labels: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAccComputeInstanceTemplate_basic(t *testing.T) {
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand All @@ -78,7 +78,7 @@ func TestAccComputeInstanceTemplate_imageShorthand(t *testing.T) {
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestAccComputeInstanceTemplate_maintenance_interval(t *testing.T) {
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccComputeInstanceTemplate_basic(acctest.RandString(t, 10)),
Expand Down Expand Up @@ -612,7 +612,7 @@ func TestAccComputeInstanceTemplate_EncryptKMS(t *testing.T) {
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -944,7 +944,7 @@ func TestAccComputeInstanceTemplate_diskResourcePolicies(t *testing.T) {
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func TestAccComputeInstanceTemplate_managedEnvoy(t *testing.T) {
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func ResourceComputeRegionInstanceTemplate() *schema.Resource {
resourceComputeInstanceTemplateSourceImageCustomizeDiff,
resourceComputeInstanceTemplateScratchDiskCustomizeDiff,
resourceComputeInstanceTemplateBootDiskCustomizeDiff,
tpgresource.SetLabelsDiff,
),

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -877,7 +878,27 @@ be from 0 to 999,999,999 inclusive.`,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: `A set of key/value label pairs to assign to instances created from this template,`,
Description: `A set of key/value label pairs to assign to instances created from this template,

**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.`,
},

"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Set: schema.HashString,
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},

"effective_labels": {
Type: schema.TypeMap,
Computed: true,
ForceNew: true,
Set: schema.HashString,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},

"resource_policies": {
Expand Down Expand Up @@ -1010,8 +1031,8 @@ func resourceComputeRegionInstanceTemplateCreate(d *schema.ResourceData, meta in
ReservationAffinity: reservationAffinity,
}

if _, ok := d.GetOk("labels"); ok {
instanceProperties.Labels = tpgresource.ExpandLabels(d)
if _, ok := d.GetOk("effective_labels"); ok {
instanceProperties.Labels = tpgresource.ExpandEffectiveLabels(d)
}

var itName string
Expand Down Expand Up @@ -1137,10 +1158,16 @@ func resourceComputeRegionInstanceTemplateRead(d *schema.ResourceData, meta inte
}
}
if instanceProperties.Labels != nil {
if err := d.Set("labels", instanceProperties.Labels); err != nil {
if err := tpgresource.SetLabels(instanceProperties.Labels, d, "labels"); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
}
if err := tpgresource.SetLabels(instanceProperties.Labels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", instanceProperties.Labels); err != nil {
return fmt.Errorf("Error setting effective_labels: %s", err)
}
if err = d.Set("self_link", instanceTemplate["selfLink"]); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
Expand Down
Loading