From 943cd1fb3ba13306a39696d769705069b8f44a12 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 13 Dec 2024 20:29:06 +0000 Subject: [PATCH] Add Intercept Endpoint Group resource to Network Security. (#12522) [upstream:9ffbaefa36297db91b4e79927bd62e5afe260207] Signed-off-by: Modular Magician --- .changelog/12522.txt | 3 + .../provider/provider_mmv1_resources.go | 5 +- ...twork_security_intercept_endpoint_group.go | 533 ++++++++++++++++++ ...tercept_endpoint_group_generated_meta.yaml | 5 + ...intercept_endpoint_group_generated_test.go | 104 ++++ ...curity_intercept_endpoint_group_sweeper.go | 143 +++++ ...ity_intercept_endpoint_group.html.markdown | 169 ++++++ 7 files changed, 960 insertions(+), 2 deletions(-) create mode 100644 .changelog/12522.txt create mode 100644 google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group.go create mode 100644 google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_meta.yaml create mode 100644 google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_test.go create mode 100644 google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_sweeper.go create mode 100644 website/docs/r/network_security_intercept_endpoint_group.html.markdown diff --git a/.changelog/12522.txt b/.changelog/12522.txt new file mode 100644 index 0000000000..7dfacbb9c6 --- /dev/null +++ b/.changelog/12522.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +`google_network_security_intercept_endpoint_group` (beta) +``` \ No newline at end of file diff --git a/google-beta/provider/provider_mmv1_resources.go b/google-beta/provider/provider_mmv1_resources.go index ba74339214..dee5bd020b 100644 --- a/google-beta/provider/provider_mmv1_resources.go +++ b/google-beta/provider/provider_mmv1_resources.go @@ -509,9 +509,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{ } // Resources -// Generated resources: 568 +// Generated resources: 569 // Generated IAM resources: 294 -// Total generated resources: 862 +// Total generated resources: 863 var generatedResources = map[string]*schema.Resource{ "google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(), "google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(), @@ -1145,6 +1145,7 @@ var generatedResources = map[string]*schema.Resource{ "google_network_security_gateway_security_policy_rule": networksecurity.ResourceNetworkSecurityGatewaySecurityPolicyRule(), "google_network_security_intercept_deployment": networksecurity.ResourceNetworkSecurityInterceptDeployment(), "google_network_security_intercept_deployment_group": networksecurity.ResourceNetworkSecurityInterceptDeploymentGroup(), + "google_network_security_intercept_endpoint_group": networksecurity.ResourceNetworkSecurityInterceptEndpointGroup(), "google_network_security_mirroring_deployment": networksecurity.ResourceNetworkSecurityMirroringDeployment(), "google_network_security_mirroring_deployment_group": networksecurity.ResourceNetworkSecurityMirroringDeploymentGroup(), "google_network_security_mirroring_endpoint_group": networksecurity.ResourceNetworkSecurityMirroringEndpointGroup(), diff --git a/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group.go b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group.go new file mode 100644 index 0000000000..1db0041915 --- /dev/null +++ b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group.go @@ -0,0 +1,533 @@ +// 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 networksecurity + +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-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func ResourceNetworkSecurityInterceptEndpointGroup() *schema.Resource { + return &schema.Resource{ + Create: resourceNetworkSecurityInterceptEndpointGroupCreate, + Read: resourceNetworkSecurityInterceptEndpointGroupRead, + Update: resourceNetworkSecurityInterceptEndpointGroupUpdate, + Delete: resourceNetworkSecurityInterceptEndpointGroupDelete, + + Importer: &schema.ResourceImporter{ + State: resourceNetworkSecurityInterceptEndpointGroupImport, + }, + + 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{ + "intercept_deployment_group": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `Immutable. The Intercept Deployment Group that this resource is connected to. Format +is: +'projects/{project}/locations/global/interceptDeploymentGroups/{interceptDeploymentGroup}'`, + }, + "intercept_endpoint_group_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `ID of the Intercept Endpoint Group.`, + }, + "location": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The location of the Intercept Endpoint Group, currently restricted to 'global'.`, + }, + "labels": { + Type: schema.TypeMap, + Optional: true, + Description: `Optional. Labels as key value pairs + +**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: `Create time stamp.`, + }, + "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}, + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: `Identifier. The name of the Intercept Endpoint Group.`, + }, + "reconciling": { + Type: schema.TypeBool, + Computed: true, + Description: `Whether reconciling is in progress, recommended per +https://google.aip.dev/128.`, + }, + "state": { + Type: schema.TypeString, + Computed: true, + Description: `Current state of the endpoint group. + Possible values: + STATE_UNSPECIFIED +ACTIVE +CLOSED +CREATING +DELETING +OUT_OF_SYNC`, + }, + "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}, + }, + "update_time": { + Type: schema.TypeString, + Computed: true, + Description: `Update time stamp.`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourceNetworkSecurityInterceptEndpointGroupCreate(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{}) + interceptDeploymentGroupProp, err := expandNetworkSecurityInterceptEndpointGroupInterceptDeploymentGroup(d.Get("intercept_deployment_group"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("intercept_deployment_group"); !tpgresource.IsEmptyValue(reflect.ValueOf(interceptDeploymentGroupProp)) && (ok || !reflect.DeepEqual(v, interceptDeploymentGroupProp)) { + obj["interceptDeploymentGroup"] = interceptDeploymentGroupProp + } + labelsProp, err := expandNetworkSecurityInterceptEndpointGroupEffectiveLabels(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.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/interceptEndpointGroups?interceptEndpointGroupId={{intercept_endpoint_group_id}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new InterceptEndpointGroup: %#v", obj) + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for InterceptEndpointGroup: %s", err) + } + billingProject = project + + // 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: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutCreate), + Headers: headers, + }) + if err != nil { + return fmt.Errorf("Error creating InterceptEndpointGroup: %s", err) + } + + // Store the ID now + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}") + 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 = NetworkSecurityOperationWaitTimeWithResponse( + config, res, &opRes, project, "Creating InterceptEndpointGroup", userAgent, + d.Timeout(schema.TimeoutCreate)) + if err != nil { + // The resource didn't actually create + d.SetId("") + + return fmt.Errorf("Error waiting to create InterceptEndpointGroup: %s", err) + } + + if err := d.Set("name", flattenNetworkSecurityInterceptEndpointGroupName(opRes["name"], d, config)); err != nil { + return err + } + + // This may have caused the ID to update - update it if so. + id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating InterceptEndpointGroup %q: %#v", d.Id(), res) + + return resourceNetworkSecurityInterceptEndpointGroupRead(d, meta) +} + +func resourceNetworkSecurityInterceptEndpointGroupRead(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.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for InterceptEndpointGroup: %s", err) + } + billingProject = project + + // 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("NetworkSecurityInterceptEndpointGroup %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + + if err := d.Set("name", flattenNetworkSecurityInterceptEndpointGroupName(res["name"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("create_time", flattenNetworkSecurityInterceptEndpointGroupCreateTime(res["createTime"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("update_time", flattenNetworkSecurityInterceptEndpointGroupUpdateTime(res["updateTime"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("labels", flattenNetworkSecurityInterceptEndpointGroupLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("intercept_deployment_group", flattenNetworkSecurityInterceptEndpointGroupInterceptDeploymentGroup(res["interceptDeploymentGroup"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("state", flattenNetworkSecurityInterceptEndpointGroupState(res["state"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("reconciling", flattenNetworkSecurityInterceptEndpointGroupReconciling(res["reconciling"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("terraform_labels", flattenNetworkSecurityInterceptEndpointGroupTerraformLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + if err := d.Set("effective_labels", flattenNetworkSecurityInterceptEndpointGroupEffectiveLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading InterceptEndpointGroup: %s", err) + } + + return nil +} + +func resourceNetworkSecurityInterceptEndpointGroupUpdate(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 InterceptEndpointGroup: %s", err) + } + billingProject = project + + obj := make(map[string]interface{}) + labelsProp, err := expandNetworkSecurityInterceptEndpointGroupEffectiveLabels(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.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating InterceptEndpointGroup %q: %#v", d.Id(), obj) + headers := make(http.Header) + updateMask := []string{} + + if d.HasChange("effective_labels") { + updateMask = append(updateMask, "labels") + } + // updateMask is a URL parameter but not present in the schema, so ReplaceVars + // won't set it + url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) + if err != nil { + return err + } + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + // if updateMask is empty we are not updating anything so skip the post + if len(updateMask) > 0 { + 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 InterceptEndpointGroup %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating InterceptEndpointGroup %q: %#v", d.Id(), res) + } + + err = NetworkSecurityOperationWaitTime( + config, res, project, "Updating InterceptEndpointGroup", userAgent, + d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return err + } + } + + return resourceNetworkSecurityInterceptEndpointGroupRead(d, meta) +} + +func resourceNetworkSecurityInterceptEndpointGroupDelete(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 InterceptEndpointGroup: %s", err) + } + billingProject = project + + url, err := tpgresource.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}") + 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 InterceptEndpointGroup %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, "InterceptEndpointGroup") + } + + err = NetworkSecurityOperationWaitTime( + config, res, project, "Deleting InterceptEndpointGroup", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting InterceptEndpointGroup %q: %#v", d.Id(), res) + return nil +} + +func resourceNetworkSecurityInterceptEndpointGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*transport_tpg.Config) + if err := tpgresource.ParseImportId([]string{ + "^projects/(?P[^/]+)/locations/(?P[^/]+)/interceptEndpointGroups/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)$", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenNetworkSecurityInterceptEndpointGroupName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkSecurityInterceptEndpointGroupCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkSecurityInterceptEndpointGroupUpdateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkSecurityInterceptEndpointGroupLabels(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 flattenNetworkSecurityInterceptEndpointGroupInterceptDeploymentGroup(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkSecurityInterceptEndpointGroupState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkSecurityInterceptEndpointGroupReconciling(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenNetworkSecurityInterceptEndpointGroupTerraformLabels(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 flattenNetworkSecurityInterceptEndpointGroupEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func expandNetworkSecurityInterceptEndpointGroupInterceptDeploymentGroup(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandNetworkSecurityInterceptEndpointGroupEffectiveLabels(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-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_meta.yaml b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_meta.yaml new file mode 100644 index 0000000000..28a3815c33 --- /dev/null +++ b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_meta.yaml @@ -0,0 +1,5 @@ +resource: 'google_network_security_intercept_endpoint_group' +generation_type: 'mmv1' +api_service_name: 'networksecurity.googleapis.com' +api_version: 'v1beta1' +api_resource_type_kind: 'InterceptEndpointGroup' diff --git a/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_test.go b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_test.go new file mode 100644 index 0000000000..d536ec2225 --- /dev/null +++ b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_generated_test.go @@ -0,0 +1,104 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package networksecurity_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/plancheck" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" +) + +func TestAccNetworkSecurityInterceptEndpointGroup_update(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.ProtoV5ProviderBetaFactories(t), + Steps: []resource.TestStep{ + { + Config: testAccNetworkSecurityInterceptEndpointGroup_basic(context), + }, + { + ResourceName: "google_network_security_intercept_endpoint_group.default", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"labels", "terraform_labels"}, + }, + { + Config: testAccNetworkSecurityInterceptEndpointGroup_update(context), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PreApply: []plancheck.PlanCheck{ + plancheck.ExpectResourceAction("google_network_security_intercept_endpoint_group.default", plancheck.ResourceActionUpdate), + }, + }, + }, + { + ResourceName: "google_network_security_intercept_endpoint_group.default", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"update_time", "labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccNetworkSecurityInterceptEndpointGroup_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_compute_network" "network" { + provider = google-beta + name = "tf-test-example-network%{random_suffix}" + auto_create_subnetworks = false +} + +resource "google_network_security_intercept_deployment_group" "deployment_group" { + provider = google-beta + intercept_deployment_group_id = "tf-test-example-dg%{random_suffix}" + location = "global" + network = google_compute_network.network.id +} + +resource "google_network_security_intercept_endpoint_group" "default" { + provider = google-beta + intercept_endpoint_group_id = "tf-test-example-eg%{random_suffix}" + location = "global" + intercept_deployment_group = google_network_security_intercept_deployment_group.deployment_group.id + labels = { + foo = "bar" + } +} +`, context) +} + +func testAccNetworkSecurityInterceptEndpointGroup_update(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_compute_network" "network" { + provider = google-beta + name = "tf-test-example-network%{random_suffix}" + auto_create_subnetworks = false +} + +resource "google_network_security_intercept_deployment_group" "deployment_group" { + provider = google-beta + intercept_deployment_group_id = "tf-test-example-dg%{random_suffix}" + location = "global" + network = google_compute_network.network.id +} + +resource "google_network_security_intercept_endpoint_group" "default" { + provider = google-beta + intercept_endpoint_group_id = "tf-test-example-eg%{random_suffix}" + location = "global" + intercept_deployment_group = google_network_security_intercept_deployment_group.deployment_group.id + labels = { + foo = "goo" + } +} +`, context) +} diff --git a/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_sweeper.go b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_sweeper.go new file mode 100644 index 0000000000..fda03786fd --- /dev/null +++ b/google-beta/services/networksecurity/resource_network_security_intercept_endpoint_group_sweeper.go @@ -0,0 +1,143 @@ +// 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 networksecurity + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/sweeper" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +func init() { + sweeper.AddTestSweepers("NetworkSecurityInterceptEndpointGroup", testSweepNetworkSecurityInterceptEndpointGroup) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepNetworkSecurityInterceptEndpointGroup(region string) error { + resourceName := "NetworkSecurityInterceptEndpointGroup" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sweeper.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 := envvar.GetTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &tpgresource.ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://networksecurity.googleapis.com/v1beta1/projects/{{project}}/locations/{{location}}/interceptEndpointGroups", "?")[0] + listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: config.Project, + RawURL: listUrl, + UserAgent: config.UserAgent, + }) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["interceptEndpointGroups"] + 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{}) + var name string + // Id detected in the delete URL, attempt to use id. + if obj["id"] != nil { + name = tpgresource.GetResourceNameFromSelfLink(obj["id"].(string)) + } else if obj["name"] != nil { + name = tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) + } else { + log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName) + return nil + } + // Skip resources that shouldn't be sweeped + if !sweeper.IsSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://networksecurity.googleapis.com/v1beta1/projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}" + deleteUrl, err := tpgresource.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 = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: config.Project, + RawURL: deleteUrl, + UserAgent: config.UserAgent, + }) + 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/website/docs/r/network_security_intercept_endpoint_group.html.markdown b/website/docs/r/network_security_intercept_endpoint_group.html.markdown new file mode 100644 index 0000000000..f4af2401c1 --- /dev/null +++ b/website/docs/r/network_security_intercept_endpoint_group.html.markdown @@ -0,0 +1,169 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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 security" +description: |- + An intercept endpoint group is a global resource in the consumer account representing the producer’s deployment group. +--- + +# google_network_security_intercept_endpoint_group + +An intercept endpoint group is a global resource in the consumer account representing the producer’s deployment group. + +~> **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. + + + +## Example Usage - Network Security Intercept Endpoint Group Basic + + +```hcl +resource "google_compute_network" "network" { + provider = google-beta + name = "example-network" + auto_create_subnetworks = false +} + +resource "google_network_security_intercept_deployment_group" "deployment_group" { + provider = google-beta + intercept_deployment_group_id = "example-dg" + location = "global" + network = google_compute_network.network.id +} + +resource "google_network_security_intercept_endpoint_group" "default" { + provider = google-beta + intercept_endpoint_group_id = "example-eg" + location = "global" + intercept_deployment_group = google_network_security_intercept_deployment_group.deployment_group.id + labels = { + foo = "bar" + } +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `intercept_deployment_group` - + (Required) + Immutable. The Intercept Deployment Group that this resource is connected to. Format + is: + `projects/{project}/locations/global/interceptDeploymentGroups/{interceptDeploymentGroup}` + +* `location` - + (Required) + The location of the Intercept Endpoint Group, currently restricted to `global`. + +* `intercept_endpoint_group_id` - + (Required) + ID of the Intercept Endpoint Group. + + +- - - + + +* `labels` - + (Optional) + Optional. Labels as key value pairs + **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. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +## 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/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}` + +* `name` - + Identifier. The name of the Intercept Endpoint Group. + +* `create_time` - + Create time stamp. + +* `update_time` - + Update time stamp. + +* `state` - + Current state of the endpoint group. + Possible values: + STATE_UNSPECIFIED + ACTIVE + CLOSED + CREATING + DELETING + OUT_OF_SYNC + +* `reconciling` - + Whether reconciling is in progress, recommended per + https://google.aip.dev/128. + +* `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 + + +InterceptEndpointGroup can be imported using any of these accepted formats: + +* `projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}` +* `{{project}}/{{location}}/{{intercept_endpoint_group_id}}` +* `{{location}}/{{intercept_endpoint_group_id}}` + + +In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import InterceptEndpointGroup using one of the formats above. For example: + +```tf +import { + id = "projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}}" + to = google_network_security_intercept_endpoint_group.default +} +``` + +When using the [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import), InterceptEndpointGroup can be imported using one of the formats above. For example: + +``` +$ terraform import google_network_security_intercept_endpoint_group.default projects/{{project}}/locations/{{location}}/interceptEndpointGroups/{{intercept_endpoint_group_id}} +$ terraform import google_network_security_intercept_endpoint_group.default {{project}}/{{location}}/{{intercept_endpoint_group_id}} +$ terraform import google_network_security_intercept_endpoint_group.default {{location}}/{{intercept_endpoint_group_id}} +``` + +## User Project Overrides + +This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override).