Skip to content

Commit

Permalink
Don't send update request if updatmask is empty when resource has ter…
Browse files Browse the repository at this point in the history
…raform_labels
  • Loading branch information
zli82016 committed Oct 3, 2023
1 parent 847abda commit ea7e7b5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
8 changes: 8 additions & 0 deletions mmv1/api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@ def get_labels_field_note(title)
Please refer to the field `effective_#{title}` for all of the #{title} present on the resource."
end

def terraform_labels?
terraform_label = root_properties.select do |p|
p.is_a?(Api::Type::KeyValueTerraformLabels)
end

terraform_label.length.positive?
end

# ====================
# Version-related methods
# ====================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
err = NetworkSecurityAddressGroupOperationWaitTime(
config, res, "Updating AddressGroup", userAgent,
d.Timeout(schema.TimeoutUpdate))
if len(updateMask) > 0 {
err = NetworkSecurityAddressGroupOperationWaitTime(
config, res, "Updating AddressGroup", userAgent,
d.Timeout(schema.TimeoutUpdate))

if err != nil {
return err
if err != nil {
return err
}
}
4 changes: 2 additions & 2 deletions mmv1/templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ func resource<%= resource_name -%>Update(d *schema.ResourceData, meta interface{
billingProject = bp
}

<% if object.update_mask && field_specific_update_methods(object.root_properties) -%>
<% if object.update_mask && (field_specific_update_methods(object.root_properties) || object.terraform_labels?) -%>
// if updateMask is empty we are not updating anything so skip the post
if len(updateMask) > 0 {
<% end -%>
Expand Down Expand Up @@ -838,7 +838,7 @@ if len(updateMask) > 0 {
}
<% end -%>
<% end -%>
<% if object.update_mask && field_specific_update_methods(object.root_properties) -%>
<% if object.update_mask && (field_specific_update_methods(object.root_properties) || object.terraform_labels?) -%>
}
<% end -%>
<% end # if !object.immutable -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ func TestAccPubsubTopic_schema(t *testing.T) {
})
}

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

topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(t, 10))

oldVersion := map[string]resource.ExternalProvider{
"google": {
VersionConstraint: "4.84.0", // a version that doesn't separate user defined labels and system labels
Source: "registry.terraform.io/hashicorp/google",
},
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
CheckDestroy: testAccCheckPubsubTopicDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccPubsubTopic_update(topic, "foo", "bar"),
ExternalProviders: oldVersion,
},
{
Config: testAccPubsubTopic_update(topic, "foo", "bar"),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
},
{
ResourceName: "google_pubsub_topic.foo",
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
ImportStateId: topic,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
}

func testAccPubsubTopic_update(topic, key, value string) string {
return fmt.Sprintf(`
resource "google_pubsub_topic" "foo" {
Expand Down

0 comments on commit ea7e7b5

Please sign in to comment.