| // 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 cloudbuildv2 |
| |
| 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 ResourceCloudbuildv2Repository() *schema.Resource { |
| return &schema.Resource{ |
| Create: resourceCloudbuildv2RepositoryCreate, |
| Read: resourceCloudbuildv2RepositoryRead, |
| Delete: resourceCloudbuildv2RepositoryDelete, |
| |
| Importer: &schema.ResourceImporter{ |
| State: resourceCloudbuildv2RepositoryImport, |
| }, |
| |
| Timeouts: &schema.ResourceTimeout{ |
| Create: schema.DefaultTimeout(20 * time.Minute), |
| Delete: schema.DefaultTimeout(20 * time.Minute), |
| }, |
| |
| CustomizeDiff: customdiff.All( |
| tpgresource.SetAnnotationsDiff, |
| tpgresource.DefaultProviderProject, |
| ), |
| |
| Schema: map[string]*schema.Schema{ |
| "name": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `Name of the repository.`, |
| }, |
| "parent_connection": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, |
| Description: `The connection for the resource`, |
| }, |
| "remote_uri": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `Required. Git Clone HTTPS URI.`, |
| }, |
| "annotations": { |
| Type: schema.TypeMap, |
| Optional: true, |
| ForceNew: true, |
| Description: `Allows clients to store small amounts of arbitrary data. |
| |
| **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}, |
| }, |
| "location": { |
| Type: schema.TypeString, |
| Computed: true, |
| Optional: true, |
| ForceNew: true, |
| Description: `The location for the resource`, |
| }, |
| "create_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `Output only. Server assigned timestamp for when the connection was created.`, |
| }, |
| "effective_annotations": { |
| Type: schema.TypeMap, |
| Computed: true, |
| ForceNew: 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}, |
| }, |
| "etag": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `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.`, |
| }, |
| "update_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `Output only. Server assigned timestamp for when the connection was updated.`, |
| }, |
| "project": { |
| Type: schema.TypeString, |
| Optional: true, |
| Computed: true, |
| ForceNew: true, |
| }, |
| }, |
| UseJSONNumber: true, |
| } |
| } |
| |
| func resourceCloudbuildv2RepositoryCreate(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| obj := make(map[string]interface{}) |
| nameProp, err := expandCloudbuildv2RepositoryName(d.Get("name"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { |
| obj["name"] = nameProp |
| } |
| remoteUriProp, err := expandCloudbuildv2RepositoryRemoteUri(d.Get("remote_uri"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("remote_uri"); !tpgresource.IsEmptyValue(reflect.ValueOf(remoteUriProp)) && (ok || !reflect.DeepEqual(v, remoteUriProp)) { |
| obj["remoteUri"] = remoteUriProp |
| } |
| annotationsProp, err := expandCloudbuildv2RepositoryEffectiveAnnotations(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 |
| } |
| |
| obj, err = resourceCloudbuildv2RepositoryEncoder(d, meta, obj) |
| if err != nil { |
| return err |
| } |
| |
| url, err := tpgresource.ReplaceVarsForId(d, config, "{{Cloudbuildv2BasePath}}projects/{{project}}/locations/{{location}}/connections/{{parent_connection}}/repositories?repositoryId={{name}}") |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Creating new Repository: %#v", obj) |
| billingProject := "" |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return fmt.Errorf("Error fetching project for Repository: %s", err) |
| } |
| billingProject = strings.TrimPrefix(project, "projects/") |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| headers := make(http.Header) |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "POST", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| Body: obj, |
| Timeout: d.Timeout(schema.TimeoutCreate), |
| Headers: headers, |
| }) |
| if err != nil { |
| return fmt.Errorf("Error creating Repository: %s", err) |
| } |
| |
| // Store the ID now |
| id, err := tpgresource.ReplaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/connections/{{parent_connection}}/repositories/{{name}}") |
| if err != nil { |
| return fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| err = Cloudbuildv2OperationWaitTime( |
| config, res, tpgresource.GetResourceNameFromSelfLink(project), "Creating Repository", userAgent, |
| d.Timeout(schema.TimeoutCreate)) |
| |
| if err != nil { |
| // The resource didn't actually create |
| d.SetId("") |
| return fmt.Errorf("Error waiting to create Repository: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished creating Repository %q: %#v", d.Id(), res) |
| |
| return resourceCloudbuildv2RepositoryRead(d, meta) |
| } |
| |
| func resourceCloudbuildv2RepositoryRead(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| url, err := tpgresource.ReplaceVarsForId(d, config, "{{Cloudbuildv2BasePath}}projects/{{project}}/locations/{{location}}/connections/{{parent_connection}}/repositories/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return fmt.Errorf("Error fetching project for Repository: %s", err) |
| } |
| billingProject = strings.TrimPrefix(project, "projects/") |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| headers := make(http.Header) |
| res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| Config: config, |
| Method: "GET", |
| Project: billingProject, |
| RawURL: url, |
| UserAgent: userAgent, |
| Headers: headers, |
| }) |
| if err != nil { |
| return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Cloudbuildv2Repository %q", d.Id())) |
| } |
| |
| if err := d.Set("project", project); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| |
| if err := d.Set("remote_uri", flattenCloudbuildv2RepositoryRemoteUri(res["remoteUri"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| if err := d.Set("create_time", flattenCloudbuildv2RepositoryCreateTime(res["createTime"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| if err := d.Set("update_time", flattenCloudbuildv2RepositoryUpdateTime(res["updateTime"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| if err := d.Set("annotations", flattenCloudbuildv2RepositoryAnnotations(res["annotations"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| if err := d.Set("etag", flattenCloudbuildv2RepositoryEtag(res["etag"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| if err := d.Set("effective_annotations", flattenCloudbuildv2RepositoryEffectiveAnnotations(res["annotations"], d, config)); err != nil { |
| return fmt.Errorf("Error reading Repository: %s", err) |
| } |
| |
| return nil |
| } |
| |
| func resourceCloudbuildv2RepositoryDelete(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 Repository: %s", err) |
| } |
| billingProject = strings.TrimPrefix(project, "projects/") |
| |
| url, err := tpgresource.ReplaceVarsForId(d, config, "{{Cloudbuildv2BasePath}}projects/{{project}}/locations/{{location}}/connections/{{parent_connection}}/repositories/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| var obj map[string]interface{} |
| |
| // err == nil indicates that the billing_project value was found |
| if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| billingProject = bp |
| } |
| |
| headers := make(http.Header) |
| |
| log.Printf("[DEBUG] Deleting Repository %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, "Repository") |
| } |
| |
| err = Cloudbuildv2OperationWaitTime( |
| config, res, tpgresource.GetResourceNameFromSelfLink(project), "Deleting Repository", userAgent, |
| d.Timeout(schema.TimeoutDelete)) |
| |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Finished deleting Repository %q: %#v", d.Id(), res) |
| return nil |
| } |
| |
| func resourceCloudbuildv2RepositoryImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| config := meta.(*transport_tpg.Config) |
| if err := tpgresource.ParseImportId([]string{ |
| "^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/connections/(?P<parent_connection>[^/]+)/repositories/(?P<name>[^/]+)$", |
| "^(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<parent_connection>[^/]+)/(?P<name>[^/]+)$", |
| "^(?P<location>[^/]+)/(?P<parent_connection>[^/]+)/(?P<name>[^/]+)$", |
| }, d, config); err != nil { |
| return nil, err |
| } |
| |
| // Replace import id for the resource id |
| id, err := tpgresource.ReplaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/connections/{{parent_connection}}/repositories/{{name}}") |
| if err != nil { |
| return nil, fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| return []*schema.ResourceData{d}, nil |
| } |
| |
| func flattenCloudbuildv2RepositoryRemoteUri(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenCloudbuildv2RepositoryCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenCloudbuildv2RepositoryUpdateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenCloudbuildv2RepositoryAnnotations(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 flattenCloudbuildv2RepositoryEtag(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenCloudbuildv2RepositoryEffectiveAnnotations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func expandCloudbuildv2RepositoryName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandCloudbuildv2RepositoryRemoteUri(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandCloudbuildv2RepositoryEffectiveAnnotations(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 resourceCloudbuildv2RepositoryEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { |
| config := meta.(*transport_tpg.Config) |
| // Extract any empty fields from the parent_connection field. |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return nil, fmt.Errorf("error getting project: %s", err) |
| } |
| parent_connection := d.Get("parent_connection").(string) |
| project, err = tpgresource.ExtractFieldByPattern("project", project, parent_connection, "projects/([a-z0-9A-Z-]*)/locations/.*") |
| if err != nil { |
| return nil, fmt.Errorf("error extracting project field: %s", err) |
| } |
| location := d.Get("location").(string) |
| location, err = tpgresource.ExtractFieldByPattern("location", location, parent_connection, "projects/[a-z0-9A-Z-]*/locations/([a-z0-9-]*)/connections/.*") |
| if err != nil { |
| return nil, fmt.Errorf("error extracting location field: %s", err) |
| } |
| // Set project to the extracted value. |
| d.Set("project", project) |
| // Set all the other fields to their short forms before forming url and setting ID. |
| name := d.Get("name").(string) |
| d.Set("location", location) |
| d.Set("name", name) |
| return obj, nil |
| } |