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 e1da22c
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 29 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
29 changes: 25 additions & 4 deletions mmv1/products/networksecurity/AddressGroup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,28 @@ references:
"Use AddressGroups": "https://cloud.google.com/vpc/docs/use-address-groups-firewall-policies"
api: "https://cloud.google.com/traffic-director/docs/reference/network-security/rest/v1beta1/organizations.locations.addressGroups"
# Prevent the operation from being generated so we can call the custom one.
autogen_async: false
autogen_async: true
async: !ruby/object:Api::OpAsync
operation: !ruby/object:Api::OpAsync::Operation
path: 'name'
base_url: '{{op_id}}'
wait_ms: 1000
timeouts: !ruby/object:Api::Timeouts
insert_minutes: 30
update_minutes: 30
delete_minutes: 30
result: !ruby/object:Api::OpAsync::Result
path: 'response'
status: !ruby/object:Api::OpAsync::Status
path: 'done'
complete: true
allowed:
- true
- false
error: !ruby/object:Api::OpAsync::Error
path: 'error'
message: 'message'
include_project: true
import_format: ["{{%parent}}/locations/{{location}}/addressGroups/{{name}}"]
examples:
- !ruby/object:Provider::Terraform::Examples
Expand All @@ -53,9 +74,9 @@ examples:
project: :PROJECT_NAME
custom_code:
!ruby/object:Provider::Terraform::CustomCode # Calling custom operation that are different from other network security resources.
post_create: templates/terraform/post_create/network_security_address_group_operation.go.erb
post_update: templates/terraform/post_update/network_security_address_group_operation.go.erb
post_delete: templates/terraform/post_delete/network_security_address_group_operation.go.erb
pre_create: templates/terraform/pre_create/network_security_address_group_operation.go.erb
pre_update: templates/terraform/pre_update/network_security_address_group_operation.go.erb
pre_delete: templates/terraform/pre_delete/network_security_address_group_operation.go.erb
parameters:
- !ruby/object:Api::Type::String
name: parent
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project = ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project = ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project = ""
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 e1da22c

Please sign in to comment.