From 1781c9b6f76956ad8b2657ae87e289633e4d54b2 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 27 Feb 2024 06:55:10 -0800 Subject: [PATCH] Promote Cloud Deploy Automation to GA (#10043) (#17427) * Add Cloud Deploy Automation * Attempt to solve the test errors based on https://yaqs.corp.google.com/eng/q/7753655943518224384 * Add update test for the new automation resource * fix lint errors * fix lint errors * fix errors in GA provider test * add the missing test * add a full test * Attempt to fix the acceptance test errors * fix a lint error * mark labels as default_from_api:true * fix advance rollout rule definition * Mark service_account as 'ignore_read: true' * Fix test errors * Resolve review comments * Format test file * promote google_clouddeploy_automation to GA * promote google_clouddeploy_automation to GA * fix lint errors [upstream:aee8d1d332c09af8dda321a9a466cd8ae7232602] Signed-off-by: Modular Magician --- .changelog/10043.txt | 3 + google/provider/provider_mmv1_resources.go | 5 +- .../resource_clouddeploy_automation.go | 1086 +++++++++++++++++ ...e_clouddeploy_automation_generated_test.go | 212 ++++ ...resource_clouddeploy_automation_sweeper.go | 139 +++ .../resource_clouddeploy_automation_test.go | 135 ++ .../r/clouddeploy_automation.html.markdown | 6 - 7 files changed, 1578 insertions(+), 8 deletions(-) create mode 100644 .changelog/10043.txt create mode 100644 google/services/clouddeploy/resource_clouddeploy_automation.go create mode 100644 google/services/clouddeploy/resource_clouddeploy_automation_generated_test.go create mode 100644 google/services/clouddeploy/resource_clouddeploy_automation_sweeper.go diff --git a/.changelog/10043.txt b/.changelog/10043.txt new file mode 100644 index 00000000000..d637212dca8 --- /dev/null +++ b/.changelog/10043.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +clouddeploy: promoted `google_clouddeploy_automation` resource to GA +``` \ No newline at end of file diff --git a/google/provider/provider_mmv1_resources.go b/google/provider/provider_mmv1_resources.go index e5df4cbc979..2951e1203fb 100644 --- a/google/provider/provider_mmv1_resources.go +++ b/google/provider/provider_mmv1_resources.go @@ -387,9 +387,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{ } // Resources -// Generated resources: 386 +// Generated resources: 387 // Generated IAM resources: 228 -// Total generated resources: 614 +// Total generated resources: 615 var generatedResources = map[string]*schema.Resource{ "google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(), "google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(), @@ -501,6 +501,7 @@ var generatedResources = map[string]*schema.Resource{ "google_cloudbuildv2_connection_iam_member": tpgiamresource.ResourceIamMember(cloudbuildv2.Cloudbuildv2ConnectionIamSchema, cloudbuildv2.Cloudbuildv2ConnectionIamUpdaterProducer, cloudbuildv2.Cloudbuildv2ConnectionIdParseFunc), "google_cloudbuildv2_connection_iam_policy": tpgiamresource.ResourceIamPolicy(cloudbuildv2.Cloudbuildv2ConnectionIamSchema, cloudbuildv2.Cloudbuildv2ConnectionIamUpdaterProducer, cloudbuildv2.Cloudbuildv2ConnectionIdParseFunc), "google_cloudbuildv2_repository": cloudbuildv2.ResourceCloudbuildv2Repository(), + "google_clouddeploy_automation": clouddeploy.ResourceClouddeployAutomation(), "google_clouddeploy_custom_target_type": clouddeploy.ResourceClouddeployCustomTargetType(), "google_clouddeploy_delivery_pipeline_iam_binding": tpgiamresource.ResourceIamBinding(clouddeploy.ClouddeployDeliveryPipelineIamSchema, clouddeploy.ClouddeployDeliveryPipelineIamUpdaterProducer, clouddeploy.ClouddeployDeliveryPipelineIdParseFunc), "google_clouddeploy_delivery_pipeline_iam_member": tpgiamresource.ResourceIamMember(clouddeploy.ClouddeployDeliveryPipelineIamSchema, clouddeploy.ClouddeployDeliveryPipelineIamUpdaterProducer, clouddeploy.ClouddeployDeliveryPipelineIdParseFunc), diff --git a/google/services/clouddeploy/resource_clouddeploy_automation.go b/google/services/clouddeploy/resource_clouddeploy_automation.go new file mode 100644 index 00000000000..6a990ed78fc --- /dev/null +++ b/google/services/clouddeploy/resource_clouddeploy_automation.go @@ -0,0 +1,1086 @@ +// 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 clouddeploy + +import ( + "fmt" + "log" + "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" +) + +func ResourceClouddeployAutomation() *schema.Resource { + return &schema.Resource{ + Create: resourceClouddeployAutomationCreate, + Read: resourceClouddeployAutomationRead, + Update: resourceClouddeployAutomationUpdate, + Delete: resourceClouddeployAutomationDelete, + + Importer: &schema.ResourceImporter{ + State: resourceClouddeployAutomationImport, + }, + + 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.SetAnnotationsDiff, + tpgresource.SetLabelsDiff, + tpgresource.DefaultProviderProject, + ), + + Schema: map[string]*schema.Schema{ + "delivery_pipeline": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The delivery_pipeline for the resource`, + }, + "location": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `The location for the resource`, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: `Name of the 'Automation'.`, + }, + "rules": { + Type: schema.TypeList, + Required: true, + Description: `Required. List of Automation rules associated with the Automation resource. Must have at least one rule and limited to 250 rules per Delivery Pipeline. Note: the order of the rules here is not the same as the order of execution.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "advance_rollout_rule": { + Type: schema.TypeList, + Optional: true, + Description: `Optional. The 'AdvanceRolloutRule' will automatically advance a successful Rollout.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Required: true, + Description: `Required. ID of the rule. This id must be unique in the 'Automation' resource to which this rule belongs. The format is 'a-z{0,62}'.`, + }, + "source_phases": { + Type: schema.TypeList, + Optional: true, + Description: `Optional. Proceeds only after phase name matched any one in the list. This value must consist of lower-case letters, numbers, and hyphens, start with a letter and end with a letter or a number, and have a max length of 63 characters. In other words, it must match the following regex: '^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$'.`, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "wait": { + Type: schema.TypeString, + Optional: true, + Description: `Optional. How long to wait after a rollout is finished.`, + }, + }, + }, + }, + "promote_release_rule": { + Type: schema.TypeList, + Optional: true, + Description: `Optional. 'PromoteReleaseRule' will automatically promote a release from the current target to a specified target.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Required: true, + Description: `Required. ID of the rule. This id must be unique in the 'Automation' resource to which this rule belongs. The format is 'a-z{0,62}'.`, + }, + "destination_phase": { + Type: schema.TypeString, + Optional: true, + Description: `Optional. The starting phase of the rollout created by this operation. Default to the first phase.`, + }, + "destination_target_id": { + Type: schema.TypeString, + Optional: true, + Description: `Optional. The ID of the stage in the pipeline to which this 'Release' is deploying. If unspecified, default it to the next stage in the promotion flow. The value of this field could be one of the following: * The last segment of a target name. It only needs the ID to determine if the target is one of the stages in the promotion sequence defined in the pipeline. * "@next", the next target in the promotion sequence.`, + }, + "wait": { + Type: schema.TypeString, + Optional: true, + Description: `Optional. How long the release need to be paused until being promoted to the next target.`, + }, + }, + }, + }, + }, + }, + }, + "selector": { + Type: schema.TypeList, + Required: true, + Description: `Required. Selected resources to which the automation will be applied.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "targets": { + Type: schema.TypeList, + Required: true, + Description: `Contains attributes about a target.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Optional: true, + Description: `ID of the 'Target'. The value of this field could be one of the following: * The last segment of a target name. It only needs the ID to determine which target is being referred to * "*", all targets in a location.`, + }, + "labels": { + Type: schema.TypeMap, + Computed: true, + Optional: true, + Description: `Target labels.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }, + }, + }, + "service_account": { + Type: schema.TypeString, + Required: true, + Description: `Required. Email address of the user-managed IAM service account that creates Cloud Deploy release and rollout resources.`, + }, + "annotations": { + Type: schema.TypeMap, + Optional: true, + Description: `Optional. User annotations. These attributes can only be set and used by the user, and not by Cloud Deploy. Annotations must meet the following constraints: * Annotations are key/value pairs. * Valid annotation keys have two segments: an optional prefix and name, separated by a slash ('/'). * The name segment is required and must be 63 characters or less, beginning and ending with an alphanumeric character ('[a-z0-9A-Z]') with dashes ('-'), underscores ('_'), dots ('.'), and alphanumerics between. * The prefix is optional. If specified, the prefix must be a DNS subdomain: a series of DNS labels separated by dots('.'), not longer than 253 characters in total, followed by a slash ('/'). See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set for more details. + +**Note**: This field is non-authoritative, and will only manage the annotations present in your configuration. +Please refer to the field 'effective_annotations' for all of the annotations present on the resource.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "description": { + Type: schema.TypeString, + Optional: true, + Description: `Optional. Description of the 'Automation'. Max length is 255 characters.`, + }, + "labels": { + Type: schema.TypeMap, + Optional: true, + Description: `Optional. Labels are attributes that can be set and used by both the user and by Cloud Deploy. Labels must meet the following constraints: * Keys and values can contain only lowercase letters, numeric characters, underscores, and dashes. * All characters must use UTF-8 encoding, and international characters are allowed. * Keys must start with a lowercase letter or international character. * Each resource is limited to a maximum of 64 labels. Both keys and values are additionally constrained to be <= 63 characters. + +**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}, + }, + "suspended": { + Type: schema.TypeBool, + Optional: true, + Description: `Optional. When Suspended, automation is deactivated from execution.`, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. Time at which the automation was created.`, + }, + "effective_annotations": { + Type: schema.TypeMap, + Computed: true, + Description: `All of annotations (key/value pairs) present on the resource in GCP, including the annotations configured through Terraform, other clients and services.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "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}, + }, + "etag": { + Type: schema.TypeString, + Computed: true, + Description: `Optional. The weak etag of the 'Automation' resource. This checksum is computed by the server based on the value of other fields, and may be sent on update and delete requests to ensure the client has an up-to-date value before proceeding.`, + }, + "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. Unique identifier of the 'Automation'.`, + }, + "update_time": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. Time at which the automation was updated.`, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + UseJSONNumber: true, + } +} + +func resourceClouddeployAutomationCreate(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{}) + descriptionProp, err := expandClouddeployAutomationDescription(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 + } + suspendedProp, err := expandClouddeployAutomationSuspended(d.Get("suspended"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("suspended"); ok || !reflect.DeepEqual(v, suspendedProp) { + obj["suspended"] = suspendedProp + } + serviceAccountProp, err := expandClouddeployAutomationServiceAccount(d.Get("service_account"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("service_account"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceAccountProp)) && (ok || !reflect.DeepEqual(v, serviceAccountProp)) { + obj["serviceAccount"] = serviceAccountProp + } + selectorProp, err := expandClouddeployAutomationSelector(d.Get("selector"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("selector"); !tpgresource.IsEmptyValue(reflect.ValueOf(selectorProp)) && (ok || !reflect.DeepEqual(v, selectorProp)) { + obj["selector"] = selectorProp + } + rulesProp, err := expandClouddeployAutomationRules(d.Get("rules"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("rules"); !tpgresource.IsEmptyValue(reflect.ValueOf(rulesProp)) && (ok || !reflect.DeepEqual(v, rulesProp)) { + obj["rules"] = rulesProp + } + annotationsProp, err := expandClouddeployAutomationEffectiveAnnotations(d.Get("effective_annotations"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("effective_annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) { + obj["annotations"] = annotationsProp + } + labelsProp, err := expandClouddeployAutomationEffectiveLabels(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, "{{ClouddeployBasePath}}projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations?automationId={{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Automation: %#v", obj) + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Automation: %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 + } + + 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), + }) + if err != nil { + return fmt.Errorf("Error creating Automation: %s", err) + } + + // Store the ID now + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + err = ClouddeployOperationWaitTime( + config, res, project, "Creating Automation", userAgent, + d.Timeout(schema.TimeoutCreate)) + + if err != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create Automation: %s", err) + } + + log.Printf("[DEBUG] Finished creating Automation %q: %#v", d.Id(), res) + + return resourceClouddeployAutomationRead(d, meta) +} + +func resourceClouddeployAutomationRead(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, "{{ClouddeployBasePath}}projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for Automation: %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 + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ClouddeployAutomation %q", d.Id())) + } + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + + if err := d.Set("uid", flattenClouddeployAutomationUid(res["uid"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("description", flattenClouddeployAutomationDescription(res["description"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("create_time", flattenClouddeployAutomationCreateTime(res["createTime"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("update_time", flattenClouddeployAutomationUpdateTime(res["updateTime"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("annotations", flattenClouddeployAutomationAnnotations(res["annotations"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("labels", flattenClouddeployAutomationLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("etag", flattenClouddeployAutomationEtag(res["etag"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("suspended", flattenClouddeployAutomationSuspended(res["suspended"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("service_account", flattenClouddeployAutomationServiceAccount(res["serviceAccount"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("selector", flattenClouddeployAutomationSelector(res["selector"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("rules", flattenClouddeployAutomationRules(res["rules"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("effective_annotations", flattenClouddeployAutomationEffectiveAnnotations(res["annotations"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("terraform_labels", flattenClouddeployAutomationTerraformLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + if err := d.Set("effective_labels", flattenClouddeployAutomationEffectiveLabels(res["labels"], d, config)); err != nil { + return fmt.Errorf("Error reading Automation: %s", err) + } + + return nil +} + +func resourceClouddeployAutomationUpdate(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 Automation: %s", err) + } + billingProject = project + + obj := make(map[string]interface{}) + descriptionProp, err := expandClouddeployAutomationDescription(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 + } + suspendedProp, err := expandClouddeployAutomationSuspended(d.Get("suspended"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("suspended"); ok || !reflect.DeepEqual(v, suspendedProp) { + obj["suspended"] = suspendedProp + } + serviceAccountProp, err := expandClouddeployAutomationServiceAccount(d.Get("service_account"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("service_account"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, serviceAccountProp)) { + obj["serviceAccount"] = serviceAccountProp + } + selectorProp, err := expandClouddeployAutomationSelector(d.Get("selector"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("selector"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, selectorProp)) { + obj["selector"] = selectorProp + } + rulesProp, err := expandClouddeployAutomationRules(d.Get("rules"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("rules"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, rulesProp)) { + obj["rules"] = rulesProp + } + annotationsProp, err := expandClouddeployAutomationEffectiveAnnotations(d.Get("effective_annotations"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("effective_annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, annotationsProp)) { + obj["annotations"] = annotationsProp + } + labelsProp, err := expandClouddeployAutomationEffectiveLabels(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, "{{ClouddeployBasePath}}projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Automation %q: %#v", d.Id(), obj) + updateMask := []string{} + + if d.HasChange("description") { + updateMask = append(updateMask, "description") + } + + if d.HasChange("suspended") { + updateMask = append(updateMask, "suspended") + } + + if d.HasChange("service_account") { + updateMask = append(updateMask, "serviceAccount") + } + + if d.HasChange("selector") { + updateMask = append(updateMask, "selector") + } + + if d.HasChange("rules") { + updateMask = append(updateMask, "rules") + } + + if d.HasChange("effective_annotations") { + updateMask = append(updateMask, "annotations") + } + + 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), + }) + + if err != nil { + return fmt.Errorf("Error updating Automation %q: %s", d.Id(), err) + } else { + log.Printf("[DEBUG] Finished updating Automation %q: %#v", d.Id(), res) + } + + err = ClouddeployOperationWaitTime( + config, res, project, "Updating Automation", userAgent, + d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return err + } + } + + return resourceClouddeployAutomationRead(d, meta) +} + +func resourceClouddeployAutomationDelete(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 Automation: %s", err) + } + billingProject = project + + url, err := tpgresource.ReplaceVars(d, config, "{{ClouddeployBasePath}}projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Automation %q", d.Id()) + + // 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: "DELETE", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "Automation") + } + + err = ClouddeployOperationWaitTime( + config, res, project, "Deleting Automation", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting Automation %q: %#v", d.Id(), res) + return nil +} + +func resourceClouddeployAutomationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*transport_tpg.Config) + if err := tpgresource.ParseImportId([]string{ + "^projects/(?P[^/]+)/locations/(?P[^/]+)/deliveryPipelines/(?P[^/]+)/automations/(?P[^/]+)$", + "^(?P[^/]+)/(?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}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenClouddeployAutomationUid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationDescription(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationUpdateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationAnnotations(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("annotations"); ok { + for k := range l.(map[string]interface{}) { + transformed[k] = v.(map[string]interface{})[k] + } + } + + return transformed +} + +func flattenClouddeployAutomationLabels(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 flattenClouddeployAutomationEtag(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationSuspended(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationServiceAccount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationSelector(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["targets"] = + flattenClouddeployAutomationSelectorTargets(original["targets"], d, config) + return []interface{}{transformed} +} +func flattenClouddeployAutomationSelectorTargets(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + if len(original) < 1 { + // Do not include empty json objects coming back from the api + continue + } + transformed = append(transformed, map[string]interface{}{ + "id": flattenClouddeployAutomationSelectorTargetsId(original["id"], d, config), + "labels": flattenClouddeployAutomationSelectorTargetsLabels(original["labels"], d, config), + }) + } + return transformed +} +func flattenClouddeployAutomationSelectorTargetsId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationSelectorTargetsLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRules(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + if v == nil { + return v + } + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + if len(original) < 1 { + // Do not include empty json objects coming back from the api + continue + } + transformed = append(transformed, map[string]interface{}{ + "promote_release_rule": flattenClouddeployAutomationRulesPromoteReleaseRule(original["promoteReleaseRule"], d, config), + "advance_rollout_rule": flattenClouddeployAutomationRulesAdvanceRolloutRule(original["advanceRolloutRule"], d, config), + }) + } + return transformed +} +func flattenClouddeployAutomationRulesPromoteReleaseRule(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["id"] = + flattenClouddeployAutomationRulesPromoteReleaseRuleId(original["id"], d, config) + transformed["wait"] = + flattenClouddeployAutomationRulesPromoteReleaseRuleWait(original["wait"], d, config) + transformed["destination_target_id"] = + flattenClouddeployAutomationRulesPromoteReleaseRuleDestinationTargetId(original["destinationTargetId"], d, config) + transformed["destination_phase"] = + flattenClouddeployAutomationRulesPromoteReleaseRuleDestinationPhase(original["destinationPhase"], d, config) + return []interface{}{transformed} +} +func flattenClouddeployAutomationRulesPromoteReleaseRuleId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRulesPromoteReleaseRuleWait(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRulesPromoteReleaseRuleDestinationTargetId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRulesPromoteReleaseRuleDestinationPhase(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRulesAdvanceRolloutRule(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["id"] = + flattenClouddeployAutomationRulesAdvanceRolloutRuleId(original["id"], d, config) + transformed["wait"] = + flattenClouddeployAutomationRulesAdvanceRolloutRuleWait(original["wait"], d, config) + transformed["source_phases"] = + flattenClouddeployAutomationRulesAdvanceRolloutRuleSourcePhases(original["sourcePhases"], d, config) + return []interface{}{transformed} +} +func flattenClouddeployAutomationRulesAdvanceRolloutRuleId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRulesAdvanceRolloutRuleWait(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationRulesAdvanceRolloutRuleSourcePhases(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationEffectiveAnnotations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenClouddeployAutomationTerraformLabels(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 flattenClouddeployAutomationEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func expandClouddeployAutomationDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationSuspended(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationServiceAccount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationSelector(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{}) + + transformedTargets, err := expandClouddeployAutomationSelectorTargets(original["targets"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedTargets); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["targets"] = transformedTargets + } + + return transformed, nil +} + +func expandClouddeployAutomationSelectorTargets(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + l := v.([]interface{}) + req := make([]interface{}, 0, len(l)) + for _, raw := range l { + if raw == nil { + continue + } + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedId, err := expandClouddeployAutomationSelectorTargetsId(original["id"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedId); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["id"] = transformedId + } + + transformedLabels, err := expandClouddeployAutomationSelectorTargetsLabels(original["labels"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedLabels); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["labels"] = transformedLabels + } + + req = append(req, transformed) + } + return req, nil +} + +func expandClouddeployAutomationSelectorTargetsId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationSelectorTargetsLabels(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 +} + +func expandClouddeployAutomationRules(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + l := v.([]interface{}) + req := make([]interface{}, 0, len(l)) + for _, raw := range l { + if raw == nil { + continue + } + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedPromoteReleaseRule, err := expandClouddeployAutomationRulesPromoteReleaseRule(original["promote_release_rule"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedPromoteReleaseRule); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["promoteReleaseRule"] = transformedPromoteReleaseRule + } + + transformedAdvanceRolloutRule, err := expandClouddeployAutomationRulesAdvanceRolloutRule(original["advance_rollout_rule"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedAdvanceRolloutRule); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["advanceRolloutRule"] = transformedAdvanceRolloutRule + } + + req = append(req, transformed) + } + return req, nil +} + +func expandClouddeployAutomationRulesPromoteReleaseRule(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{}) + + transformedId, err := expandClouddeployAutomationRulesPromoteReleaseRuleId(original["id"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedId); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["id"] = transformedId + } + + transformedWait, err := expandClouddeployAutomationRulesPromoteReleaseRuleWait(original["wait"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedWait); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["wait"] = transformedWait + } + + transformedDestinationTargetId, err := expandClouddeployAutomationRulesPromoteReleaseRuleDestinationTargetId(original["destination_target_id"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDestinationTargetId); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["destinationTargetId"] = transformedDestinationTargetId + } + + transformedDestinationPhase, err := expandClouddeployAutomationRulesPromoteReleaseRuleDestinationPhase(original["destination_phase"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedDestinationPhase); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["destinationPhase"] = transformedDestinationPhase + } + + return transformed, nil +} + +func expandClouddeployAutomationRulesPromoteReleaseRuleId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationRulesPromoteReleaseRuleWait(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationRulesPromoteReleaseRuleDestinationTargetId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationRulesPromoteReleaseRuleDestinationPhase(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationRulesAdvanceRolloutRule(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{}) + + transformedId, err := expandClouddeployAutomationRulesAdvanceRolloutRuleId(original["id"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedId); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["id"] = transformedId + } + + transformedWait, err := expandClouddeployAutomationRulesAdvanceRolloutRuleWait(original["wait"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedWait); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["wait"] = transformedWait + } + + transformedSourcePhases, err := expandClouddeployAutomationRulesAdvanceRolloutRuleSourcePhases(original["source_phases"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedSourcePhases); val.IsValid() && !tpgresource.IsEmptyValue(val) { + transformed["sourcePhases"] = transformedSourcePhases + } + + return transformed, nil +} + +func expandClouddeployAutomationRulesAdvanceRolloutRuleId(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationRulesAdvanceRolloutRuleWait(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationRulesAdvanceRolloutRuleSourcePhases(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + +func expandClouddeployAutomationEffectiveAnnotations(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 +} + +func expandClouddeployAutomationEffectiveLabels(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/clouddeploy/resource_clouddeploy_automation_generated_test.go b/google/services/clouddeploy/resource_clouddeploy_automation_generated_test.go new file mode 100644 index 00000000000..b88175d4cb0 --- /dev/null +++ b/google/services/clouddeploy/resource_clouddeploy_automation_generated_test.go @@ -0,0 +1,212 @@ +// 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 clouddeploy_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + + "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/envvar" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" +) + +func TestAccClouddeployAutomation_clouddeployAutomationBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "service_account": envvar.GetTestServiceAccountFromEnv(t), + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckClouddeployAutomationDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccClouddeployAutomation_clouddeployAutomationBasicExample(context), + }, + { + ResourceName: "google_clouddeploy_automation.b-automation", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name", "location", "delivery_pipeline", "annotations", "labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccClouddeployAutomation_clouddeployAutomationBasicExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_clouddeploy_automation" "b-automation" { + name = "tf-test-cd-automation%{random_suffix}" + project = google_clouddeploy_delivery_pipeline.pipeline.project + location = google_clouddeploy_delivery_pipeline.pipeline.location + delivery_pipeline = google_clouddeploy_delivery_pipeline.pipeline.name + service_account = "%{service_account}" + selector { + targets { + id = "*" + } + } + suspended = false + rules { + promote_release_rule { + id = "promote-release" + } + } +} + +resource "google_clouddeploy_delivery_pipeline" "pipeline" { + name = "tf-test-cd-pipeline%{random_suffix}" + location = "us-central1" + serial_pipeline { + stages { + target_id = "test" + profiles = [] + } + } + } +`, context) +} + +func TestAccClouddeployAutomation_clouddeployAutomationFullExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "service_account": envvar.GetTestServiceAccountFromEnv(t), + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckClouddeployAutomationDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccClouddeployAutomation_clouddeployAutomationFullExample(context), + }, + { + ResourceName: "google_clouddeploy_automation.f-automation", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name", "location", "delivery_pipeline", "annotations", "labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccClouddeployAutomation_clouddeployAutomationFullExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_clouddeploy_automation" "f-automation" { + name = "tf-test-cd-automation%{random_suffix}" + location = "us-central1" + delivery_pipeline = google_clouddeploy_delivery_pipeline.pipeline.name + service_account = "%{service_account}" + annotations = { + my_first_annotation = "example-annotation-1" + my_second_annotation = "example-annotation-2" + } + labels = { + my_first_label = "example-label-1" + my_second_label = "example-label-2" + } + description = "automation resource" + selector { + targets { + id = "test" + labels = { + foo = "bar" + } + } + } + suspended = true + rules { + promote_release_rule{ + id = "promote-release" + wait = "200s" + destination_target_id = "@next" + destination_phase = "stable" + } + } + rules { + advance_rollout_rule { + id = "advance-rollout" + source_phases = ["deploy"] + wait = "200s" + } + } +} + +resource "google_clouddeploy_delivery_pipeline" "pipeline" { + name = "tf-test-cd-pipeline%{random_suffix}" + location = "us-central1" + serial_pipeline { + stages { + target_id = "test" + profiles = ["test-profile"] + } + } +} +`, context) +} + +func testAccCheckClouddeployAutomationDestroyProducer(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_clouddeploy_automation" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := acctest.GoogleProviderConfig(t) + + url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{ClouddeployBasePath}}projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{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("ClouddeployAutomation still exists at %s", url) + } + } + + return nil + } +} diff --git a/google/services/clouddeploy/resource_clouddeploy_automation_sweeper.go b/google/services/clouddeploy/resource_clouddeploy_automation_sweeper.go new file mode 100644 index 00000000000..4fd400d26f7 --- /dev/null +++ b/google/services/clouddeploy/resource_clouddeploy_automation_sweeper.go @@ -0,0 +1,139 @@ +// 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 clouddeploy + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-provider-google/google/envvar" + "github.com/hashicorp/terraform-provider-google/google/sweeper" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" +) + +func init() { + sweeper.AddTestSweepers("ClouddeployAutomation", testSweepClouddeployAutomation) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepClouddeployAutomation(region string) error { + resourceName := "ClouddeployAutomation" + 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://clouddeploy.googleapis.com/v1/projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations", "?")[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["automations"] + 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 := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !sweeper.IsSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://clouddeploy.googleapis.com/v1/projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}" + 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/google/services/clouddeploy/resource_clouddeploy_automation_test.go b/google/services/clouddeploy/resource_clouddeploy_automation_test.go index 4ee3d902428..8402bbb227a 100644 --- a/google/services/clouddeploy/resource_clouddeploy_automation_test.go +++ b/google/services/clouddeploy/resource_clouddeploy_automation_test.go @@ -1,3 +1,138 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 package clouddeploy_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/envvar" +) + +func TestAccClouddeployAutomation_update(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "service_account": envvar.GetTestServiceAccountFromEnv(t), + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckClouddeployAutomationDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccClouddeployAutomation_basic(context), + }, + { + ResourceName: "google_clouddeploy_automation.automation", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"location", "delivery_pipeline", "annotations", "labels", "terraform_labels"}, + }, + { + Config: testAccClouddeployAutomation_update(context), + }, + { + ResourceName: "google_clouddeploy_automation.automation", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"location", "delivery_pipeline", "annotations", "labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccClouddeployAutomation_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_clouddeploy_automation" "automation" { + name = "tf-test-cd-automation%{random_suffix}" + location = "us-central1" + delivery_pipeline = google_clouddeploy_delivery_pipeline.pipeline.name + service_account = "%{service_account}" + selector { + targets { + id = "*" + labels = {} + } + } + rules { + advance_rollout_rule { + id = "advance-rollout" + source_phases = ["deploy"] + wait = "200s" + } + } +} + +resource "google_clouddeploy_delivery_pipeline" "pipeline" { + name = "tf-test-cd-pipeline%{random_suffix}" + location = "us-central1" + serial_pipeline { + stages { + target_id = "test" + profiles = ["test-profile"] + } + } + } +`, context) +} + +func testAccClouddeployAutomation_update(context map[string]interface{}) string { + return acctest.Nprintf(` + +resource "google_clouddeploy_automation" "automation" { + name = "tf-test-cd-automation%{random_suffix}" + location = "us-central1" + delivery_pipeline = google_clouddeploy_delivery_pipeline.pipeline.name + service_account = "%{service_account}" + annotations = { + first_annotation = "example-annotation-1" + second_annotation = "example-annotation-2" + } + labels = { + first_label = "example-label-1" + second_label = "example-label-2" + } + description = "automation resource" + selector { + targets { + id = "dev" + labels = { + foo = "bar2" + } + } + } + suspended = true + rules { + advance_rollout_rule { + id = "advance-rollout" + source_phases = ["verify"] + wait = "100s" + } + } + rules { + promote_release_rule{ + id = "promote-release" + wait = "200s" + destination_target_id = "@next" + destination_phase = "stable" + } + } +} + +resource "google_clouddeploy_delivery_pipeline" "pipeline" { + name = "tf-test-cd-pipeline%{random_suffix}" + location = "us-central1" + serial_pipeline { + stages { + target_id = "test" + profiles = ["test-profile"] + } + } + } +`, context) +} diff --git a/website/docs/r/clouddeploy_automation.html.markdown b/website/docs/r/clouddeploy_automation.html.markdown index ee58a8a6998..4a05277b1be 100644 --- a/website/docs/r/clouddeploy_automation.html.markdown +++ b/website/docs/r/clouddeploy_automation.html.markdown @@ -21,8 +21,6 @@ description: |- An `Automation` enables the automation of manually driven actions for a Delivery Pipeline, which includes Release promotion amongst Targets, Rollout repair and Rollout deployment strategy advancement. -~> **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 Automation, see: @@ -35,7 +33,6 @@ To get more information about Automation, see: ```hcl resource "google_clouddeploy_automation" "b-automation" { - provider = google-beta name = "cd-automation" project = google_clouddeploy_delivery_pipeline.pipeline.project location = google_clouddeploy_delivery_pipeline.pipeline.location @@ -55,7 +52,6 @@ resource "google_clouddeploy_automation" "b-automation" { } resource "google_clouddeploy_delivery_pipeline" "pipeline" { - provider = google-beta name = "cd-pipeline" location = "us-central1" serial_pipeline { @@ -71,7 +67,6 @@ resource "google_clouddeploy_delivery_pipeline" "pipeline" { ```hcl resource "google_clouddeploy_automation" "f-automation" { - provider = google-beta name = "cd-automation" location = "us-central1" delivery_pipeline = google_clouddeploy_delivery_pipeline.pipeline.name @@ -112,7 +107,6 @@ resource "google_clouddeploy_automation" "f-automation" { } resource "google_clouddeploy_delivery_pipeline" "pipeline" { - provider = google-beta name = "cd-pipeline" location = "us-central1" serial_pipeline {