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

Update TagKeys to support purpose and purposeData #6593

Merged
merged 1 commit into from
Sep 26, 2022
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
17 changes: 17 additions & 0 deletions mmv1/products/tags/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ objects:
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
output: true
- !ruby/object:Api::Type::Enum
name: purpose
description: |
Optional. A purpose cannot be changed once set.
A purpose denotes that this Tag is intended for use in policies of a specific policy engine, and will involve that policy engine in management operations involving this Tag.
values:
- :GCE_FIREWALL
input: true
- !ruby/object:Api::Type::KeyValuePairs
name: purposeData
description: |
Optional. Purpose data cannot be changed once set.
Purpose data corresponds to the policy system that the tag is intended for. For example, the GCE_FIREWALL purpose expects data in the following format: `network = "<project-name>/<vpc-name>"`.
input: true

- !ruby/object:Api::Resource
name: 'TagValue'
base_url: tagValues
Expand Down
6 changes: 6 additions & 0 deletions mmv1/products/tags/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ overrides: !ruby/object:Overrides::ResourceOverrides
description: !ruby/object:Overrides::Terraform::PropertyOverride
validation: !ruby/object:Provider::Terraform::Validation
function: 'validation.StringLenBetween(0, 256)'
purposeData: !ruby/object:Overrides::Terraform::PropertyOverride
# This property expects input like: network = "<project-id>/<vpc-name or vpc-id>" or selfLinkWithId (selfLink is not supported)
# However, the API response stored in Terraform state looks like: network = "https://www.googleapis.com/compute/v1/projects/<project-id>/global/networks/<vpc-id>"
# This results in persistent diffs, so we surpress them by using ignore_read. The API will reject incorrect references to non-existent or inaccessible VPCs.
# Since this property is configured as 'input: true', any modifications to this property will result in a forced replacement of the resource.
ignore_read: true
examples:
- !ruby/object:Provider::Terraform::Examples
name: "tag_key_basic"
Expand Down
61 changes: 50 additions & 11 deletions mmv1/third_party/terraform/tests/resource_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import (

func TestAccTags(t *testing.T) {
testCases := map[string]func(t *testing.T){
"tagKeyBasic": testAccTagsTagKey_tagKeyBasic,
"tagKeyUpdate": testAccTagsTagKey_tagKeyUpdate,
"tagKeyIamBinding": testAccTagsTagKeyIamBinding,
"tagKeyIamMember": testAccTagsTagKeyIamMember,
"tagKeyIamPolicy": testAccTagsTagKeyIamPolicy,
"tagValueBasic": testAccTagsTagValue_tagValueBasic,
"tagValueUpdate": testAccTagsTagValue_tagValueUpdate,
"tagBindingBasic": testAccTagsTagBinding_tagBindingBasic,
"tagValueIamBinding": testAccTagsTagValueIamBinding,
"tagValueIamMember": testAccTagsTagValueIamMember,
"tagValueIamPolicy": testAccTagsTagValueIamPolicy,
"tagKeyBasic": testAccTagsTagKey_tagKeyBasic,
"tagKeyBasicWithPurposeGceFirewall": testAccTagsTagKey_tagKeyBasicWithPurposeGceFirewall,
"tagKeyUpdate": testAccTagsTagKey_tagKeyUpdate,
"tagKeyIamBinding": testAccTagsTagKeyIamBinding,
"tagKeyIamMember": testAccTagsTagKeyIamMember,
"tagKeyIamPolicy": testAccTagsTagKeyIamPolicy,
"tagValueBasic": testAccTagsTagValue_tagValueBasic,
"tagValueUpdate": testAccTagsTagValue_tagValueUpdate,
"tagBindingBasic": testAccTagsTagBinding_tagBindingBasic,
"tagValueIamBinding": testAccTagsTagValueIamBinding,
"tagValueIamMember": testAccTagsTagValueIamMember,
"tagValueIamPolicy": testAccTagsTagValueIamPolicy,
}

for name, tc := range testCases {
Expand Down Expand Up @@ -68,6 +69,44 @@ resource "google_tags_tag_key" "key" {
`, context)
}

func testAccTagsTagKey_tagKeyBasicWithPurposeGceFirewall(t *testing.T) {
context := map[string]interface{}{
"org_id": getTestOrgFromEnv(t),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTagsTagKeyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccTagsTagKey_tagKeyBasicWithPurposeGceFirewallExample(context),
},
},
})
}

func testAccTagsTagKey_tagKeyBasicWithPurposeGceFirewallExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network" "tag_network" {
name = "vpc-%{random_suffix}"
auto_create_subnetworks = false
}

resource "google_tags_tag_key" "key" {
parent = "organizations/%{org_id}"
short_name = "foo%{random_suffix}"
description = "For foo%{random_suffix} resources."
purpose = "GCE_FIREWALL"
# purpose_data expects either a selfLinkWithId (not a property of google_compute_network) or the format <project-name>/<vpc-name>.
# selfLink is not sufficient and will result in an error, so we build a string to match the second option.
purpose_data = {network = "${google_compute_network.tag_network.project}/${google_compute_network.tag_network.name}"}
}

`, context)
}

func testAccTagsTagKey_tagKeyUpdate(t *testing.T) {
context := map[string]interface{}{
"org_id": getTestOrgFromEnv(t),
Expand Down