Skip to content

Commit

Permalink
Added tag product and tagkey resource (GoogleCloudPlatform#4565)
Browse files Browse the repository at this point in the history
* Added tag product and tagkey resource

* Skipped vcr tests and added id_format and import_format

vcr tests don't work due to randomness

* Added self_link definition

* Added tag key update test

* Re-added id_format and import_format

* Marked error code 10 as retryable

See https://github.com/googleapis/googleapis/blob/8f117308d5bb55816953a0d6ad1a7d27a69a7d3f/google/rpc/code.proto#L130

* Added additional logging of potentially-retriable errors

* Switched to custom import

generated imports can't handle slashes in ids

* Removed error code 10 from retry errors

TLDR if an operation returns error code 10, we can't actually retry the operation itself; we would have to retry the original POST, which would take a larger modification to creating an object

* Added mutex for tagkey operations

* Removed logging line for manual testing

* Run tags resource tests sequentially due to concurrency issues

* Switched tagkey mutex to use parent field directly

* Made tagkey tests eligible for VCR and non-parallel

* Switched to self_link/id_format/import_format using name_from_self_link

* Cleaned up docs

* Removed etag field

* Corrected id/import formats to match self link

* Removed custom import
  • Loading branch information
melinath authored Mar 17, 2021
1 parent a452ad3 commit 16ca167
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 0 deletions.
95 changes: 95 additions & 0 deletions mmv1/products/tags/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright 2021 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

--- !ruby/object:Api::Product
name: Tags
versions:
- !ruby/object:Api::Product::Version
name: beta
base_url: https://cloudresourcemanager.googleapis.com/v3/
scopes:
- https://www.googleapis.com/auth/cloud-platform
async: !ruby/object:Api::OpAsync
operation: !ruby/object:Api::OpAsync::Operation
path: 'name'
base_url: '{{op_id}}'
wait_ms: 1000
result: !ruby/object:Api::OpAsync::Result
path: 'response'
resource_inside_response: true
status: !ruby/object:Api::OpAsync::Status
path: 'done'
complete: true
allowed:
- true
- false
error: !ruby/object:Api::OpAsync::Error
path: 'error'
message: 'message'
objects:
- !ruby/object:Api::Resource
name: 'TagKey'
min_version: beta
base_url: tagKeys
self_link: "tagKeys/{{name}}"
update_verb: :PATCH
update_mask: true
description: A TagKey, used to group a set of TagValues.
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Official Documentation':
'https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing'
api: 'https://cloud.google.com/resource-manager/reference/rest/v3/tagKeys'
properties:
- !ruby/object:Api::Type::String
name: name
description: |
The generated numeric id for the TagKey.
output: true
- !ruby/object:Api::Type::String
name: parent
description: |
Input only. The resource name of the new TagKey's parent. Must be of the form organizations/{org_id}.
input: true
required: true
- !ruby/object:Api::Type::String
name: shortName
description: |
Input only. The user friendly name for a TagKey. The short name should be unique for TagKeys within the same tag namespace.
The short name must be 1-63 characters, beginning and ending with an alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between.
input: true
required: true
- !ruby/object:Api::Type::String
name: namespacedName
description: |
Output only. Namespaced name of the TagKey.
output: true
- !ruby/object:Api::Type::String
name: description
description: |
User-assigned description of the TagKey. Must not exceed 256 characters.
- !ruby/object:Api::Type::String
name: createTime
description: |
Output only. Creation time.
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::String
name: updateTime
description: |
Output only. Update time.
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
34 changes: 34 additions & 0 deletions mmv1/products/tags/terraform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2021 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

--- !ruby/object:Provider::Terraform::Config
overrides: !ruby/object:Overrides::ResourceOverrides
TagKey: !ruby/object:Overrides::Terraform::ResourceOverride
autogen_async: true
mutex: tagKey/{{parent}}
id_format: "tagKeys/{{name}}"
import_format: ["tagKeys/{{name}}", "{{name}}"]
properties:
name: !ruby/object:Overrides::Terraform::PropertyOverride
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
examples:
- !ruby/object:Provider::Terraform::Examples
name: "tag_key_basic"
min_version: 'beta'
skip_vcr: true
skip_test: true
primary_resource_id: "key"
vars:
short_name: "foo"
test_env_vars:
org_id: :ORG_ID
7 changes: 7 additions & 0 deletions mmv1/templates/terraform/examples/tag_key_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "google_tags_tag_key" "<%= ctx[:primary_resource_id] %>" {
provider = google-beta

parent = "organizations/<%= ctx[:test_env_vars]['org_id'] %>"
short_name = "<%= ctx[:vars]['short_name'] %>"
description = "For <%= ctx[:vars]['short_name'] %> resources."
}
159 changes: 159 additions & 0 deletions mmv1/third_party/terraform/tests/resource_tags_tag_key_test.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<% autogen_exception -%>
package google

<% unless version == "ga" -%>
import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// Tags tests cannot be run in parallel without running into Error Code 10: ABORTED
// See https://github.com/hashicorp/terraform-provider-google/issues/8637

func TestAccTags(t *testing.T) {
testCases := map[string]func(t *testing.T){
"basic": testAccTagsTagKey_tagKeyBasic,
"update": testAccTagsTagKey_update,
}

for name, tc := range testCases {
// shadow the tc variable into scope so that when
// the loop continues, if t.Run hasn't executed tc(t)
// yet, we don't have a race condition
// see https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
})
}
}

func testAccTagsTagKey_tagKeyBasic(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: testAccProvidersOiCS,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: testAccCheckTagsTagKeyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccTagsTagKey_tagKeyBasicExample(context),
},
},
})
}

func testAccTagsTagKey_tagKeyBasicExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_tags_tag_key" "key" {
provider = google-beta

parent = "organizations/%{org_id}"
short_name = "foo%{random_suffix}"
description = "For foo%{random_suffix} resources."
}
`, context)
}

func testAccTagsTagKey_update(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: testAccProvidersOiCS,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: testAccCheckTagsTagKeyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccTagsTagKey_basic(context),
},
{
ResourceName: "google_tags_tag_key.key",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccTagsTagKey_basicUpdated(context),
},
{
ResourceName: "google_tags_tag_key.key",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccTagsTagKey_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_tags_tag_key" "key" {
provider = google-beta

parent = "organizations/%{org_id}"
short_name = "foo%{random_suffix}"
description = "For foo%{random_suffix} resources."
}
`, context)
}

func testAccTagsTagKey_basicUpdated(context map[string]interface{}) string {
return Nprintf(`
resource "google_tags_tag_key" "key" {
provider = google-beta

parent = "organizations/%{org_id}"
short_name = "foo%{random_suffix}"
description = "Anything related to foo%{random_suffix}"
}
`, context)
}

func testAccCheckTagsTagKeyDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_tags_tag_key" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}

config := googleProviderConfig(t)

url, err := replaceVarsForTest(config, rs, "{{TagsBasePath}}{{name}}")
if err != nil {
return err
}

billingProject := ""

if config.BillingProject != "" {
billingProject = config.BillingProject
}

_, err = sendRequest(config, "GET", billingProject, url, config.userAgent, nil)
if err == nil {
return fmt.Errorf("TagsTagKey still exists at %s", url)
}
}

return nil
}
}

<% end %>
29 changes: 29 additions & 0 deletions mmv1/third_party/terraform/utils/error_retry_predicates_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"strconv"
"testing"

"google.golang.org/api/googleapi"
Expand Down Expand Up @@ -49,3 +50,31 @@ func TestIsAppEngineRetryableError_serverError(t *testing.T) {
t.Errorf("Error incorrectly detected as retryable")
}
}

func TestIsCommonRetryableErrorCode_retryableErrorCode(t *testing.T) {
codes := []int{429, 500, 502, 503}
for _, code := range codes {
code := code
t.Run(strconv.Itoa(code), func(t *testing.T) {
err := googleapi.Error{
Code: code,
Body: "some text describing error",
}
isRetryable, _ := isCommonRetryableErrorCode(&err)
if !isRetryable {
t.Errorf("Error not detected as retryable")
}
})
}
}

func TestIsCommonRetryableErrorCode_otherError(t *testing.T) {
err := googleapi.Error{
Code: 404,
Body: "Some unretryable issue",
}
isRetryable, _ := isCommonRetryableErrorCode(&err)
if isRetryable {
t.Errorf("Error incorrectly detected as retryable")
}
}

0 comments on commit 16ca167

Please sign in to comment.