From 4cd3afe3e6e850acb6acccf79f681db4bb64246c Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 5 Apr 2021 15:24:05 +0000 Subject: [PATCH] Added tag binding resource (#4638) * Added tag binding resource * Corrected double-quotes at EOL in tag value definitions * Marked project as using google-beta provider * Split tag mutexes again due to not actually being able to share a parent * Set up custom flatten for tag binding name * Use randString suffixes for tag-related shortnames since there was a conflict * Fixed reading of tagbinding instances * Added explicit delete url * Switched to nested_query with idempotent name flattening Signed-off-by: Modular Magician --- .changelog/4638.txt | 3 + google-beta/provider.go | 5 +- ...esource_dataflow_flex_template_job_test.go | 2 +- google-beta/resource_tags_tag_binding.go | 325 ++++++++++++++++++ .../resource_tags_tag_binding_sweeper_test.go | 124 +++++++ google-beta/resource_tags_tag_key.go | 6 +- google-beta/resource_tags_tag_value.go | 6 +- google-beta/resource_tags_test.go | 101 +++++- website/docs/r/tags_tag_binding.html.markdown | 115 +++++++ website/docs/r/tags_tag_key.html.markdown | 4 +- website/docs/r/tags_tag_value.html.markdown | 6 +- website/google.erb | 4 + 12 files changed, 683 insertions(+), 18 deletions(-) create mode 100644 .changelog/4638.txt create mode 100644 google-beta/resource_tags_tag_binding.go create mode 100644 google-beta/resource_tags_tag_binding_sweeper_test.go create mode 100644 website/docs/r/tags_tag_binding.html.markdown diff --git a/.changelog/4638.txt b/.changelog/4638.txt new file mode 100644 index 0000000000..d21c67e48c --- /dev/null +++ b/.changelog/4638.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_tags_tag_binding` (beta) +``` diff --git a/google-beta/provider.go b/google-beta/provider.go index 60c36eb404..84f8230bc3 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -843,9 +843,9 @@ func Provider() *schema.Provider { return provider } -// Generated resources: 219 +// Generated resources: 220 // Generated IAM resources: 111 -// Total generated resources: 330 +// Total generated resources: 331 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -1181,6 +1181,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_storage_hmac_key": resourceStorageHmacKey(), "google_tags_tag_key": resourceTagsTagKey(), "google_tags_tag_value": resourceTagsTagValue(), + "google_tags_tag_binding": resourceTagsTagBinding(), "google_tpu_node": resourceTPUNode(), "google_vpc_access_connector": resourceVPCAccessConnector(), "google_workflows_workflow": resourceWorkflowsWorkflow(), diff --git a/google-beta/resource_dataflow_flex_template_job_test.go b/google-beta/resource_dataflow_flex_template_job_test.go index df04d28bb0..0d037482e0 100644 --- a/google-beta/resource_dataflow_flex_template_job_test.go +++ b/google-beta/resource_dataflow_flex_template_job_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - "google.golang.org/api/compute/v1" + compute "google.golang.org/api/compute/v1" ) func TestAccDataflowFlexTemplateJob_basic(t *testing.T) { diff --git a/google-beta/resource_tags_tag_binding.go b/google-beta/resource_tags_tag_binding.go new file mode 100644 index 0000000000..0802d291dd --- /dev/null +++ b/google-beta/resource_tags_tag_binding.go @@ -0,0 +1,325 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "log" + "reflect" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceTagsTagBinding() *schema.Resource { + return &schema.Resource{ + Create: resourceTagsTagBindingCreate, + Read: resourceTagsTagBindingRead, + Delete: resourceTagsTagBindingDelete, + + Importer: &schema.ResourceImporter{ + State: resourceTagsTagBindingImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(4 * time.Minute), + Delete: schema.DefaultTimeout(4 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "parent": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The full resource name of the resource the TagValue is bound to. E.g. //cloudresourcemanager.googleapis.com/projects/123`, + }, + "tag_value": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The TagValue of the TagBinding. Must be of the form tagValues/456.`, + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: `The generated id for the TagBinding. This is a string of the form: 'tagBindings/{full-resource-name}/{tag-value-name}'`, + }, + }, + UseJSONNumber: true, + } +} + +func resourceTagsTagBindingCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + parentProp, err := expandNestedTagsTagBindingParent(d.Get("parent"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("parent"); !isEmptyValue(reflect.ValueOf(parentProp)) && (ok || !reflect.DeepEqual(v, parentProp)) { + obj["parent"] = parentProp + } + tagValueProp, err := expandNestedTagsTagBindingTagValue(d.Get("tag_value"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("tag_value"); !isEmptyValue(reflect.ValueOf(tagValueProp)) && (ok || !reflect.DeepEqual(v, tagValueProp)) { + obj["tagValue"] = tagValueProp + } + + url, err := replaceVars(d, config, "{{TagsBasePath}}tagBindings") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new TagBinding: %#v", obj) + billingProject := "" + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) + if err != nil { + return fmt.Errorf("Error creating TagBinding: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "tagBindings/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + // Use the resource in the operation response to populate + // identity fields and d.Id() before read + var opRes map[string]interface{} + err = tagsOperationWaitTimeWithResponse( + config, res, &opRes, "Creating TagBinding", userAgent, + d.Timeout(schema.TimeoutCreate)) + if err != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create TagBinding: %s", err) + } + + if _, ok := opRes["tagBindings"]; ok { + opRes, err = flattenNestedTagsTagBinding(d, meta, opRes) + if err != nil { + return fmt.Errorf("Error getting nested object from operation response: %s", err) + } + if opRes == nil { + // Object isn't there any more - remove it from the state. + return fmt.Errorf("Error decoding response from operation, could not find nested object") + } + } + if err := d.Set("name", flattenNestedTagsTagBindingName(opRes["name"], d, config)); err != nil { + return err + } + + // This may have caused the ID to update - update it if so. + id, err = replaceVars(d, config, "tagBindings/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating TagBinding %q: %#v", d.Id(), res) + + return resourceTagsTagBindingRead(d, meta) +} + +func resourceTagsTagBindingRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "{{TagsBasePath}}tagBindings/?parent={{parent}}&pageSize=300") + if err != nil { + return err + } + + billingProject := "" + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("TagsTagBinding %q", d.Id())) + } + + res, err = flattenNestedTagsTagBinding(d, meta, res) + if err != nil { + return err + } + + if res == nil { + // Object isn't there any more - remove it from the state. + log.Printf("[DEBUG] Removing TagsTagBinding because it couldn't be matched.") + d.SetId("") + return nil + } + + if err := d.Set("name", flattenNestedTagsTagBindingName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading TagBinding: %s", err) + } + if err := d.Set("parent", flattenNestedTagsTagBindingParent(res["parent"], d, config)); err != nil { + return fmt.Errorf("Error reading TagBinding: %s", err) + } + if err := d.Set("tag_value", flattenNestedTagsTagBindingTagValue(res["tagValue"], d, config)); err != nil { + return fmt.Errorf("Error reading TagBinding: %s", err) + } + + return nil +} + +func resourceTagsTagBindingDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + billingProject := "" + + url, err := replaceVars(d, config, "{{TagsBasePath}}tagBindings/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting TagBinding %q", d.Id()) + + // err == nil indicates that the billing_project value was found + if bp, err := getBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete)) + if err != nil { + return handleNotFoundError(err, d, "TagBinding") + } + + err = tagsOperationWaitTime( + config, res, "Deleting TagBinding", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting TagBinding %q: %#v", d.Id(), res) + return nil +} + +func resourceTagsTagBindingImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + + // current import_formats can't import fields with forward slashes in their value + if err := parseImportId([]string{ + "tagBindings/(?P.+)", + "(?P.+)", + }, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + d.SetId(name) + + return []*schema.ResourceData{d}, nil +} + +func flattenNestedTagsTagBindingName(v interface{}, d *schema.ResourceData, config *Config) interface{} { + if v == nil { + return v + } + parts := strings.Split(v.(string), "/") + return strings.Join(parts[len(parts)-3:], "/") +} + +func flattenNestedTagsTagBindingParent(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func flattenNestedTagsTagBindingTagValue(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + +func expandNestedTagsTagBindingParent(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandNestedTagsTagBindingTagValue(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func flattenNestedTagsTagBinding(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) { + var v interface{} + var ok bool + + v, ok = res["tagBindings"] + if !ok || v == nil { + return nil, nil + } + + switch v.(type) { + case []interface{}: + break + case map[string]interface{}: + // Construct list out of single nested resource + v = []interface{}{v} + default: + return nil, fmt.Errorf("expected list or map for value tagBindings. Actual value: %v", v) + } + + _, item, err := resourceTagsTagBindingFindNestedObjectInList(d, meta, v.([]interface{})) + if err != nil { + return nil, err + } + return item, nil +} + +func resourceTagsTagBindingFindNestedObjectInList(d *schema.ResourceData, meta interface{}, items []interface{}) (index int, item map[string]interface{}, err error) { + expectedName := d.Get("name") + expectedFlattenedName := flattenNestedTagsTagBindingName(expectedName, d, meta.(*Config)) + + // Search list for this resource. + for idx, itemRaw := range items { + if itemRaw == nil { + continue + } + item := itemRaw.(map[string]interface{}) + + itemName := flattenNestedTagsTagBindingName(item["name"], d, meta.(*Config)) + // isEmptyValue check so that if one is nil and the other is "", that's considered a match + if !(isEmptyValue(reflect.ValueOf(itemName)) && isEmptyValue(reflect.ValueOf(expectedFlattenedName))) && !reflect.DeepEqual(itemName, expectedFlattenedName) { + log.Printf("[DEBUG] Skipping item with name= %#v, looking for %#v)", itemName, expectedFlattenedName) + continue + } + log.Printf("[DEBUG] Found item for resource %q: %#v)", d.Id(), item) + return idx, item, nil + } + return -1, nil, nil +} diff --git a/google-beta/resource_tags_tag_binding_sweeper_test.go b/google-beta/resource_tags_tag_binding_sweeper_test.go new file mode 100644 index 0000000000..9f67cabd76 --- /dev/null +++ b/google-beta/resource_tags_tag_binding_sweeper_test.go @@ -0,0 +1,124 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func init() { + resource.AddTestSweepers("TagsTagBinding", &resource.Sweeper{ + Name: "TagsTagBinding", + F: testSweepTagsTagBinding, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepTagsTagBinding(region string) error { + resourceName := "TagsTagBinding" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + t := &testing.T{} + billingId := getTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://cloudresourcemanager.googleapis.com/v3/tagBindings", "?")[0] + listUrl, err := replaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := sendRequest(config, "GET", config.Project, listUrl, config.userAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["tagBindings"] + if !ok { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + rl := resourceList.([]interface{}) + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) + // Keep count of items that aren't sweepable for logging. + nonPrefixCount := 0 + for _, ri := range rl { + obj := ri.(map[string]interface{}) + if obj["name"] == nil { + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + return nil + } + + name := GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !isSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://cloudresourcemanager.googleapis.com/v3/tagBindings/{{name}}" + deleteUrl, err := replaceVars(d, config, deleteTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) + return nil + } + deleteUrl = deleteUrl + name + + // Don't wait on operations as we may have a lot to delete + _, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) + } + + return nil +} diff --git a/google-beta/resource_tags_tag_key.go b/google-beta/resource_tags_tag_key.go index 3c2f964001..aa012aa20e 100644 --- a/google-beta/resource_tags_tag_key.go +++ b/google-beta/resource_tags_tag_key.go @@ -120,7 +120,7 @@ func resourceTagsTagKeyCreate(d *schema.ResourceData, meta interface{}) error { obj["description"] = descriptionProp } - lockName, err := replaceVars(d, config, "tagKey/{{parent}}") + lockName, err := replaceVars(d, config, "tagKeys/{{parent}}") if err != nil { return err } @@ -246,7 +246,7 @@ func resourceTagsTagKeyUpdate(d *schema.ResourceData, meta interface{}) error { obj["description"] = descriptionProp } - lockName, err := replaceVars(d, config, "tagKey/{{parent}}") + lockName, err := replaceVars(d, config, "tagKeys/{{parent}}") if err != nil { return err } @@ -304,7 +304,7 @@ func resourceTagsTagKeyDelete(d *schema.ResourceData, meta interface{}) error { billingProject := "" - lockName, err := replaceVars(d, config, "tagKey/{{parent}}") + lockName, err := replaceVars(d, config, "tagKeys/{{parent}}") if err != nil { return err } diff --git a/google-beta/resource_tags_tag_value.go b/google-beta/resource_tags_tag_value.go index 89599f5eca..1477b28f81 100644 --- a/google-beta/resource_tags_tag_value.go +++ b/google-beta/resource_tags_tag_value.go @@ -120,7 +120,7 @@ func resourceTagsTagValueCreate(d *schema.ResourceData, meta interface{}) error obj["description"] = descriptionProp } - lockName, err := replaceVars(d, config, "tagValue/{{parent}}") + lockName, err := replaceVars(d, config, "tagValues/{{parent}}") if err != nil { return err } @@ -246,7 +246,7 @@ func resourceTagsTagValueUpdate(d *schema.ResourceData, meta interface{}) error obj["description"] = descriptionProp } - lockName, err := replaceVars(d, config, "tagValue/{{parent}}") + lockName, err := replaceVars(d, config, "tagValues/{{parent}}") if err != nil { return err } @@ -304,7 +304,7 @@ func resourceTagsTagValueDelete(d *schema.ResourceData, meta interface{}) error billingProject := "" - lockName, err := replaceVars(d, config, "tagValue/{{parent}}") + lockName, err := replaceVars(d, config, "tagValues/{{parent}}") if err != nil { return err } diff --git a/google-beta/resource_tags_test.go b/google-beta/resource_tags_test.go index 69f7c4ce60..8132c79f25 100644 --- a/google-beta/resource_tags_test.go +++ b/google-beta/resource_tags_test.go @@ -14,10 +14,11 @@ import ( func TestAccTags(t *testing.T) { testCases := map[string]func(t *testing.T){ - "tagKeyBasic": testAccTagsTagKey_tagKeyBasic, - "tagKeyUpdate": testAccTagsTagKey_tagKeyUpdate, - "tagValueBasic": testAccTagsTagValue_tagValueBasic, - "tagValueUpdate": testAccTagsTagValue_tagValueUpdate, + "tagKeyBasic": testAccTagsTagKey_tagKeyBasic, + "tagKeyUpdate": testAccTagsTagKey_tagKeyUpdate, + "tagValueBasic": testAccTagsTagValue_tagValueBasic, + "tagValueUpdate": testAccTagsTagValue_tagValueUpdate, + "tagBindingBasic": testAccTagsTagBinding_tagBindingBasic, } for name, tc := range testCases { @@ -303,3 +304,95 @@ func testAccCheckTagsTagValueDestroyProducer(t *testing.T) func(s *terraform.Sta return nil } } + +func testAccTagsTagBinding_tagBindingBasic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "org_id": getTestOrgFromEnv(t), + "project_id": "tf-test-" + randString(t, 10), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProvidersOiCS, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckTagsTagBindingDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccTagsTagBinding_tagBindingBasicExample(context), + }, + }, + }) +} + +func testAccTagsTagBinding_tagBindingBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_project" "project" { + provider = google-beta + + project_id = "%{project_id}" + name = "%{project_id}" + org_id = "%{org_id}" +} + +resource "google_tags_tag_key" "key" { + provider = google-beta + + parent = "organizations/%{org_id}" + short_name = "keyname%{random_suffix}" + description = "For a certain set of resources." +} + +resource "google_tags_tag_value" "value" { + provider = google-beta + + parent = "tagKeys/${google_tags_tag_key.key.name}" + short_name = "foo%{random_suffix}" + description = "For foo%{random_suffix} resources." +} + +resource "google_tags_tag_binding" "binding" { + provider = google-beta + + parent = "//cloudresourcemanager.googleapis.com/projects/${google_project.project.number}" + tag_value = "tagValues/${google_tags_tag_value.value.name}" +} +`, context) +} + +func testAccCheckTagsTagBindingDestroyProducer(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_binding" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{TagsBasePath}}tagBindings/{{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("TagsTagBinding still exists at %s", url) + } + } + + return nil + } +} diff --git a/website/docs/r/tags_tag_binding.html.markdown b/website/docs/r/tags_tag_binding.html.markdown new file mode 100644 index 0000000000..b408da5a60 --- /dev/null +++ b/website/docs/r/tags_tag_binding.html.markdown @@ -0,0 +1,115 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +subcategory: "Tags" +layout: "google" +page_title: "Google: google_tags_tag_binding" +sidebar_current: "docs-google-tags-tag-binding" +description: |- + A TagBinding represents a connection between a TagValue and a cloud resource (currently project, folder, or organization). +--- + +# google\_tags\_tag\_binding + +A TagBinding represents a connection between a TagValue and a cloud resource (currently project, folder, or organization). Once a TagBinding is created, the TagValue is applied to all the descendants of the cloud resource. + +~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources. + +To get more information about TagBinding, see: + +* [API documentation](https://cloud.google.com/resource-manager/reference/rest/v3/tagBindings) +* How-to Guides + * [Official Documentation](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing) + +## Example Usage - Tag Binding Basic + + +```hcl +resource "google_project" "project" { + provider = google-beta + project_id = "project_id" + name = "project_id" + org_id = "123456789" +} + +resource "google_tags_tag_key" "key" { + provider = google-beta + + parent = "organizations/123456789" + short_name = "keyname" + description = "For keyname resources." +} + +resource "google_tags_tag_value" "value" { + provider = google-beta + + parent = "tagKeys/${google_tags_tag_key.key.name}" + short_name = "valuename" + description = "For valuename resources." +} + +resource "google_tags_tag_binding" "binding" { + provider = google-beta + + parent = "//cloudresourcemanager.googleapis.com/projects/${google_project.project.number}" + tag_value = "tagValues/${google_tags_tag_value.value.name}" +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `parent` - + (Required) + The full resource name of the resource the TagValue is bound to. E.g. //cloudresourcemanager.googleapis.com/projects/123 + +* `tag_value` - + (Required) + The TagValue of the TagBinding. Must be of the form tagValues/456. + + +- - - + + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `tagBindings/{{name}}` + +* `name` - + The generated id for the TagBinding. This is a string of the form: `tagBindings/{full-resource-name}/{tag-value-name}` + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 4 minutes. +- `delete` - Default is 4 minutes. + +## Import + + +TagBinding can be imported using any of these accepted formats: + +``` +$ terraform import google_tags_tag_binding.default tagBindings/{{name}} +$ terraform import google_tags_tag_binding.default {{name}} +``` diff --git a/website/docs/r/tags_tag_key.html.markdown b/website/docs/r/tags_tag_key.html.markdown index 607feec95b..e37e03d02d 100644 --- a/website/docs/r/tags_tag_key.html.markdown +++ b/website/docs/r/tags_tag_key.html.markdown @@ -41,8 +41,8 @@ resource "google_tags_tag_key" "key" { provider = google-beta parent = "organizations/123456789" - short_name = "foo" - description = "For foo resources." + short_name = "keyname" + description = "For keyname resources." } ``` diff --git a/website/docs/r/tags_tag_value.html.markdown b/website/docs/r/tags_tag_value.html.markdown index b526950988..46f744b20c 100644 --- a/website/docs/r/tags_tag_value.html.markdown +++ b/website/docs/r/tags_tag_value.html.markdown @@ -42,15 +42,15 @@ resource "google_tags_tag_key" "key" { parent = "organizations/123456789" short_name = "keyname" - description = "For a certain set of resources." + description = "For keyname resources." } resource "google_tags_tag_value" "value" { provider = google-beta parent = "tagKeys/${google_tags_tag_key.key.name}" - short_name = "foo" - description = "For foo resources."" + short_name = "valuename" + description = "For valuename resources." } ``` diff --git a/website/google.erb b/website/google.erb index e41c909308..341279d1d0 100644 --- a/website/google.erb +++ b/website/google.erb @@ -2872,6 +2872,10 @@ Resources