From 5b2f5a784d8e48f17444361f52a2774af2e0976d Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 17 Oct 2024 19:27:25 +0000 Subject: [PATCH] add auto_accept_projects in ncc group (#11828) Co-authored-by: Zhenhua Li [upstream:1d989ef58dd39f393918ae086ffde8b6bf6383a0] Signed-off-by: Modular Magician --- .changelog/11828.txt | 3 + google/provider/provider_mmv1_resources.go | 5 +- .../resource_network_connectivity_group.go | 598 ++++++++++++++++++ ...twork_connectivity_group_generated_test.go | 122 ++++ ...esource_network_connectivity_group_test.go | 97 +++ .../network_connectivity_group.html.markdown | 174 +++++ 6 files changed, 997 insertions(+), 2 deletions(-) create mode 100644 .changelog/11828.txt create mode 100644 google/services/networkconnectivity/resource_network_connectivity_group.go create mode 100644 google/services/networkconnectivity/resource_network_connectivity_group_generated_test.go create mode 100644 google/services/networkconnectivity/resource_network_connectivity_group_test.go create mode 100644 website/docs/r/network_connectivity_group.html.markdown diff --git a/.changelog/11828.txt b/.changelog/11828.txt new file mode 100644 index 00000000000..0b2b9780e06 --- /dev/null +++ b/.changelog/11828.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_network_connectivity_group` +``` \ No newline at end of file diff --git a/google/provider/provider_mmv1_resources.go b/google/provider/provider_mmv1_resources.go index 627177ec7c9..bc075608951 100644 --- a/google/provider/provider_mmv1_resources.go +++ b/google/provider/provider_mmv1_resources.go @@ -446,9 +446,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{ } // Resources -// Generated resources: 476 +// Generated resources: 477 // Generated IAM resources: 261 -// Total generated resources: 737 +// Total generated resources: 738 var generatedResources = map[string]*schema.Resource{ "google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(), "google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(), @@ -976,6 +976,7 @@ var generatedResources = map[string]*schema.Resource{ "google_netapp_volume_replication": netapp.ResourceNetappVolumeReplication(), "google_netapp_volume_snapshot": netapp.ResourceNetappVolumeSnapshot(), "google_netapp_kmsconfig": netapp.ResourceNetappkmsconfig(), + "google_network_connectivity_group": networkconnectivity.ResourceNetworkConnectivityGroup(), "google_network_connectivity_hub": networkconnectivity.ResourceNetworkConnectivityHub(), "google_network_connectivity_internal_range": networkconnectivity.ResourceNetworkConnectivityInternalRange(), "google_network_connectivity_policy_based_route": networkconnectivity.ResourceNetworkConnectivityPolicyBasedRoute(), diff --git a/google/services/networkconnectivity/resource_network_connectivity_group.go b/google/services/networkconnectivity/resource_network_connectivity_group.go new file mode 100644 index 00000000000..d92aa5869b5 --- /dev/null +++ b/google/services/networkconnectivity/resource_network_connectivity_group.go @@ -0,0 +1,598 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// 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 networkconnectivity + +import ( + "fmt" + "log" + "net/http" + "reflect" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" + "github.com/hashicorp/terraform-provider-google/google/verify" +) + +func ResourceNetworkConnectivityGroup() *schema.Resource { + return &schema.Resource{ + Create: resourceNetworkConnectivityGroupCreate, + Read: resourceNetworkConnectivityGroupRead, + Update: resourceNetworkConnectivityGroupUpdate, + Delete: resourceNetworkConnectivityGroupDelete, + + Importer: &schema.ResourceImporter{ + State: resourceNetworkConnectivityGroupImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(20 * time.Minute), + Update: schema.DefaultTimeout(20 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), + }, + + CustomizeDiff: customdiff.All( + tpgresource.SetLabelsDiff, + tpgresource.DefaultProviderProject, + ), + + Schema: map[string]*schema.Schema{ + "hub": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, + Description: `The name of the hub. Hub names must be unique. They use the following form: projects/{projectNumber}/locations/global/hubs/{hubId}`, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: verify.ValidateEnum([]string{"default", "center", "edge"}), + DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, + Description: `The name of the group. Group names must be unique. Possible values: ["default", "center", "edge"]`, + }, + "auto_accept": { + Type: schema.TypeList, + Optional: true, + Description: `Optional. The auto-accept setting for this group.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "auto_accept_projects": { + Type: schema.TypeList, + Required: true, + Description: `A list of project ids or project numbers for which you want to enable auto-accept. The auto-accept setting is applied to spokes being created or updated in these projects.`, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "description": { + Type: schema.TypeString, + Optional: true, + Description: `An optional description of the group.`, + }, + "labels": { + Type: schema.TypeMap, + Optional: true, + Description: `Optional labels in key:value format. For more information about labels, see [Requirements for labels](https://cloud.google.com/resource-manager/docs/creating-managing-labels#requirements). + +**Note**: This field is non-authoritative, and will only manage the labels present in your configuration. +Please refer to the field 'effective_labels' for all of the labels present on the resource.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The time the hub was created.`, + }, + "effective_labels": { + Type: schema.TypeMap, + Computed: true, + Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "route_table": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The name of the route table that corresponds to this group. They use the following form: 'projects/{projectNumber}/locations/global/hubs/{hubId}/routeTables/{route_table_id}'`, + }, + "state": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The current lifecycle state of this hub.`, + ExactlyOneOf: []string{}, + }, + "terraform_labels": { + Type: schema.TypeMap, + Computed: true, + Description: `The combination of labels configured directly on the resource + and default labels configured on the provider.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "uid": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The Google-generated UUID for the group. This value is unique across all group resources. If a group is deleted and another with the same name is created, the new route table is assigned a different uniqueId.`, + }, + "update_time": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The time the hub was last updated.`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourceNetworkConnectivityGroupCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + nameProp, err := expandNetworkConnectivityGroupName(d.Get("name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + descriptionProp, err := expandNetworkConnectivityGroupDescription(d.Get("description"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + autoAcceptProp, err := expandNetworkConnectivityGroupAutoAccept(d.Get("auto_accept"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("auto_accept"); !tpgresource.IsEmptyValue(reflect.ValueOf(autoAcceptProp)) && (ok || !reflect.DeepEqual(v, autoAcceptProp)) { + obj["autoAccept"] = autoAcceptProp + } + labelsProp, err := expandNetworkConnectivityGroupEffectiveLabels(d.Get("effective_labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + + url, err := tpgresource.ReplaceVarsForId(d, config, "{{NetworkConnectivityBasePath}}projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}?updateMask=autoAccept.autoAcceptProjects,labels,description") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Group: %#v", obj) + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Group: %s", err) + } + billingProject = strings.TrimPrefix(project, "projects/") + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + headers := make(http.Header) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutCreate), + Headers: headers, + }) + if err != nil { + return fmt.Errorf("Error creating Group: %s", err) + } + + // Store the ID now + id, err := tpgresource.ReplaceVarsForId(d, config, "projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + err = NetworkConnectivityOperationWaitTime( + config, res, tpgresource.GetResourceNameFromSelfLink(project), "Creating Group", userAgent, + d.Timeout(schema.TimeoutCreate)) + + if err != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create Group: %s", err) + } + + log.Printf("[DEBUG] Finished creating Group %q: %#v", d.Id(), res) + + return resourceNetworkConnectivityGroupRead(d, meta) +} + +func resourceNetworkConnectivityGroupRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + url, err := tpgresource.ReplaceVarsForId(d, config, "{{NetworkConnectivityBasePath}}projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Group: %s", err) + } + billingProject = strings.TrimPrefix(project, "projects/") + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + headers := make(http.Header) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Headers: headers, + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("NetworkConnectivityGroup %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + + if err := d.Set("name", flattenNetworkConnectivityGroupName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("create_time", flattenNetworkConnectivityGroupCreateTime(res["createTime"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("update_time", flattenNetworkConnectivityGroupUpdateTime(res["updateTime"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("labels", flattenNetworkConnectivityGroupLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("description", flattenNetworkConnectivityGroupDescription(res["description"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("uid", flattenNetworkConnectivityGroupUid(res["uid"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("state", flattenNetworkConnectivityGroupState(res["state"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("auto_accept", flattenNetworkConnectivityGroupAutoAccept(res["autoAccept"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("route_table", flattenNetworkConnectivityGroupRouteTable(res["routeTable"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("terraform_labels", flattenNetworkConnectivityGroupTerraformLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("effective_labels", flattenNetworkConnectivityGroupEffectiveLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + + return nil +} + +func resourceNetworkConnectivityGroupUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Group: %s", err) + } + billingProject = strings.TrimPrefix(project, "projects/") + + obj := make(map[string]interface{}) + descriptionProp, err := expandNetworkConnectivityGroupDescription(d.Get("description"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + autoAcceptProp, err := expandNetworkConnectivityGroupAutoAccept(d.Get("auto_accept"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("auto_accept"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, autoAcceptProp)) { + obj["autoAccept"] = autoAcceptProp + } + labelsProp, err := expandNetworkConnectivityGroupEffectiveLabels(d.Get("effective_labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + + url, err := tpgresource.ReplaceVarsForId(d, config, "{{NetworkConnectivityBasePath}}projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}?updateMask=autoAccept.autoAcceptProjects,labels,description") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Group %q: %#v", d.Id(), obj) + headers := make(http.Header) + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), + Headers: headers, + }) + + if err != nil { + return fmt.Errorf("Error updating Group %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating Group %q: %#v", d.Id(), res) + } + + err = NetworkConnectivityOperationWaitTime( + config, res, tpgresource.GetResourceNameFromSelfLink(project), "Updating Group", userAgent, + d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return err + } + + return resourceNetworkConnectivityGroupRead(d, meta) +} + +func resourceNetworkConnectivityGroupDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Group: %s", err) + } + billingProject = strings.TrimPrefix(project, "projects/") + + url, err := tpgresource.ReplaceVarsForId(d, config, "{{NetworkConnectivityBasePath}}projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + headers := make(http.Header) + + log.Printf("[DEBUG] Deleting Group %q", d.Id()) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + Headers: headers, + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "Group") + } + + err = NetworkConnectivityOperationWaitTime( + config, res, tpgresource.GetResourceNameFromSelfLink(project), "Deleting Group", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting Group %q: %#v", d.Id(), res) + return nil +} + +func resourceNetworkConnectivityGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*transport_tpg.Config) + if err := tpgresource.ParseImportId([]string{ + "^projects/(?P[^/]+)/locations/global/hubs/(?P[^/]+)/groups/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)$", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVarsForId(d, config, "projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenNetworkConnectivityGroupName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + return tpgresource.NameFromSelfLinkStateFunc(v) +} + +func flattenNetworkConnectivityGroupCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupUpdateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + + transformed := make(map[string]interface{}) + if l, ok := d.GetOkExists("labels"); ok { + for k := range l.(map[string]interface{}) { + transformed[k] = v.(map[string]interface{})[k] + } + } + + return transformed +} + +func flattenNetworkConnectivityGroupDescription(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupUid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupAutoAccept(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return nil + } + original := v.(map[string]interface{}) + if len(original) == 0 { + return nil + } + transformed := make(map[string]interface{}) + transformed["auto_accept_projects"] = + flattenNetworkConnectivityGroupAutoAcceptAutoAcceptProjects(original["autoAcceptProjects"], d, config) + return []interface{}{transformed} +} +func flattenNetworkConnectivityGroupAutoAcceptAutoAcceptProjects(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupRouteTable(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkConnectivityGroupTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + + transformed := make(map[string]interface{}) + if l, ok := d.GetOkExists("terraform_labels"); ok { + for k := range l.(map[string]interface{}) { + transformed[k] = v.(map[string]interface{})[k] + } + } + + return transformed +} + +func flattenNetworkConnectivityGroupEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func expandNetworkConnectivityGroupName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandNetworkConnectivityGroupDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandNetworkConnectivityGroupAutoAccept(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + l := v.([]interface{}) + if len(l) == 0 || l[0] == nil { + return nil, nil + } + raw := l[0] + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedAutoAcceptProjects, err := expandNetworkConnectivityGroupAutoAcceptAutoAcceptProjects(original["auto_accept_projects"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedAutoAcceptProjects); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["autoAcceptProjects"] = transformedAutoAcceptProjects + } + + return transformed, nil +} + +func expandNetworkConnectivityGroupAutoAcceptAutoAcceptProjects(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandNetworkConnectivityGroupEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) { + if v == nil { + return map[string]string{}, nil + } + m := make(map[string]string) + for k, val := range v.(map[string]interface{}) { + m[k] = val.(string) + } + return m, nil +} diff --git a/google/services/networkconnectivity/resource_network_connectivity_group_generated_test.go b/google/services/networkconnectivity/resource_network_connectivity_group_generated_test.go new file mode 100644 index 00000000000..c0759ab89aa --- /dev/null +++ b/google/services/networkconnectivity/resource_network_connectivity_group_generated_test.go @@ -0,0 +1,122 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// 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 networkconnectivity_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + + "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" +) + +func TestAccNetworkConnectivityGroup_networkConnectivityGroupBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckNetworkConnectivityGroupDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccNetworkConnectivityGroup_networkConnectivityGroupBasicExample(context), + }, + { + ResourceName: "google_network_connectivity_group.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"hub", "labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccNetworkConnectivityGroup_networkConnectivityGroupBasicExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_network_connectivity_hub" "basic_hub" { + name = "tf-test-network-connectivity-hub1%{random_suffix}" + description = "A sample hub" + labels = { + label-one = "value-one" + } +} + +resource "google_network_connectivity_group" "primary" { + hub = google_network_connectivity_hub.basic_hub.id + name = "default" + labels = { + label-one = "value-one" + } + description = "A sample hub group" + auto_accept { + auto_accept_projects = [ + "foo%{random_suffix}", + "bar%{random_suffix}", + ] + } +} +`, context) +} + +func testAccCheckNetworkConnectivityGroupDestroyProducer(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_network_connectivity_group" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := acctest.GoogleProviderConfig(t) + + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{NetworkConnectivityBasePath}}projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, + }) + if err == nil { + return fmt.Errorf("NetworkConnectivityGroup still exists at %s", url) + } + } + + return nil + } +} diff --git a/google/services/networkconnectivity/resource_network_connectivity_group_test.go b/google/services/networkconnectivity/resource_network_connectivity_group_test.go new file mode 100644 index 00000000000..973542d448c --- /dev/null +++ b/google/services/networkconnectivity/resource_network_connectivity_group_test.go @@ -0,0 +1,97 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package networkconnectivity_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + + "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/envvar" +) + +func TestAccNetworkConnectivityGroup_BasicGroup(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "project_name": envvar.GetTestProjectFromEnv(), + "hub_random_suffix": acctest.RandString(t, 10), + "project_random_suffix_1": acctest.RandString(t, 10), + "project_random_suffix_2": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckNetworkConnectivityGroupDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccNetworkConnectivityGroup_BasicGroup(context), + }, + { + ResourceName: "google_network_connectivity_group.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"hub", "labels", "terraform_labels"}, + }, + { + Config: testAccNetworkConnectivityGroup_BasicGroupUpdate0(context), + }, + { + ResourceName: "google_network_connectivity_group.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"hub", "labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccNetworkConnectivityGroup_BasicGroup(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_network_connectivity_hub" "basic_hub" { + name = "tf-test-hub%{hub_random_suffix}" + description = "A sample hub" + project = "%{project_name}" + labels = { + label-one = "value-one" + } +} +resource "google_network_connectivity_group" "primary" { + hub = google_network_connectivity_hub.basic_hub.id + name = "default" + labels = { + label-one = "value-one" + } + description = "A sample group" + auto_accept { + auto_accept_projects = ["tf-test-name%{project_random_suffix_1}"] + } +} +`, context) +} + +func testAccNetworkConnectivityGroup_BasicGroupUpdate0(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_network_connectivity_hub" "basic_hub" { + name = "tf-test-hub%{hub_random_suffix}" + description = "A sample hub" + project = "%{project_name}" + labels = { + label-one = "value-one" + } +} +resource "google_network_connectivity_group" "primary" { + hub = google_network_connectivity_hub.basic_hub.id + name = "default" + labels = { + label-two = "value-one" + } + description = "An updated sample group" + auto_accept { + auto_accept_projects = ["tf-test-name%{project_random_suffix_1}", "tf-test-name%{project_random_suffix_2}"] + } +} +`, context) +} diff --git a/website/docs/r/network_connectivity_group.html.markdown b/website/docs/r/network_connectivity_group.html.markdown new file mode 100644 index 00000000000..0516490cce6 --- /dev/null +++ b/website/docs/r/network_connectivity_group.html.markdown @@ -0,0 +1,174 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** Type: MMv1 *** +# +# ---------------------------------------------------------------------------- +# +# 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: "Network Connectivity" +description: |- + The NetworkConnectivity Group resource +--- + +# google_network_connectivity_group + +The NetworkConnectivity Group resource + + +To get more information about Group, see: + +* [API documentation](https://cloud.google.com/network-connectivity/docs/reference/networkconnectivity/rest/v1beta/projects.locations.global.hubs.groups) +* How-to Guides + * [Official Documentation](https://cloud.google.com/network-connectivity/docs/network-connectivity-center/concepts/overview) + + +## Example Usage - Network Connectivity Group Basic + + +```hcl +resource "google_network_connectivity_hub" "basic_hub" { + name = "network-connectivity-hub1" + description = "A sample hub" + labels = { + label-one = "value-one" + } +} + +resource "google_network_connectivity_group" "primary" { + hub = google_network_connectivity_hub.basic_hub.id + name = "default" + labels = { + label-one = "value-one" + } + description = "A sample hub group" + auto_accept { + auto_accept_projects = [ + "foo", + "bar", + ] + } +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `name` - + (Required) + The name of the group. Group names must be unique. + Possible values are: `default`, `center`, `edge`. + +* `hub` - + (Required) + The name of the hub. Hub names must be unique. They use the following form: projects/{projectNumber}/locations/global/hubs/{hubId} + + +- - - + + +* `labels` - + (Optional) + Optional labels in key:value format. For more information about labels, see [Requirements for labels](https://cloud.google.com/resource-manager/docs/creating-managing-labels#requirements). + **Note**: This field is non-authoritative, and will only manage the labels present in your configuration. + Please refer to the field `effective_labels` for all of the labels present on the resource. + +* `description` - + (Optional) + An optional description of the group. + +* `auto_accept` - + (Optional) + Optional. The auto-accept setting for this group. + Structure is [documented below](#nested_auto_accept). + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +The `auto_accept` block supports: + +* `auto_accept_projects` - + (Required) + A list of project ids or project numbers for which you want to enable auto-accept. The auto-accept setting is applied to spokes being created or updated in these projects. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}` + +* `create_time` - + Output only. The time the hub was created. + +* `update_time` - + Output only. The time the hub was last updated. + +* `uid` - + Output only. The Google-generated UUID for the group. This value is unique across all group resources. If a group is deleted and another with the same name is created, the new route table is assigned a different uniqueId. + +* `state` - + Output only. The current lifecycle state of this hub. + +* `route_table` - + Output only. The name of the route table that corresponds to this group. They use the following form: `projects/{projectNumber}/locations/global/hubs/{hubId}/routeTables/{route_table_id}` + +* `terraform_labels` - + The combination of labels configured directly on the resource + and default labels configured on the provider. + +* `effective_labels` - + All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services. + + +## Timeouts + +This resource provides the following +[Timeouts](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts) configuration options: + +- `create` - Default is 20 minutes. +- `update` - Default is 20 minutes. +- `delete` - Default is 20 minutes. + +## Import + + +Group can be imported using any of these accepted formats: + +* `projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}` +* `{{project}}/{{hub}}/{{name}}` +* `{{hub}}/{{name}}` + + +In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import Group using one of the formats above. For example: + +```tf +import { + id = "projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}}" + to = google_network_connectivity_group.default +} +``` + +When using the [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import), Group can be imported using one of the formats above. For example: + +``` +$ terraform import google_network_connectivity_group.default projects/{{project}}/locations/global/hubs/{{hub}}/groups/{{name}} +$ terraform import google_network_connectivity_group.default {{project}}/{{hub}}/{{name}} +$ terraform import google_network_connectivity_group.default {{hub}}/{{name}} +``` + +## User Project Overrides + +This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override).