| // 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 vmwareengine |
| |
| import ( |
| "fmt" |
| "log" |
| "net/http" |
| "reflect" |
| "strings" |
| "time" |
| |
| "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 ResourceVmwareengineExternalAddress() *schema.Resource { |
| return &schema.Resource{ |
| Create: resourceVmwareengineExternalAddressCreate, |
| Read: resourceVmwareengineExternalAddressRead, |
| Update: resourceVmwareengineExternalAddressUpdate, |
| Delete: resourceVmwareengineExternalAddressDelete, |
| |
| Importer: &schema.ResourceImporter{ |
| State: resourceVmwareengineExternalAddressImport, |
| }, |
| |
| Timeouts: &schema.ResourceTimeout{ |
| Create: schema.DefaultTimeout(40 * time.Minute), |
| Update: schema.DefaultTimeout(20 * time.Minute), |
| Delete: schema.DefaultTimeout(20 * time.Minute), |
| }, |
| |
| Schema: map[string]*schema.Schema{ |
| "internal_ip": { |
| Type: schema.TypeString, |
| Required: true, |
| Description: `The internal IP address of a workload VM.`, |
| }, |
| "name": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `The ID of the external IP Address.`, |
| }, |
| "parent": { |
| Type: schema.TypeString, |
| Required: true, |
| ForceNew: true, |
| Description: `The resource name of the private cloud to create a new external address in. |
| Resource names are schemeless URIs that follow the conventions in https://cloud.google.com/apis/design/resource_names. |
| For example: projects/my-project/locations/us-west1-a/privateClouds/my-cloud`, |
| }, |
| "description": { |
| Type: schema.TypeString, |
| Optional: true, |
| Description: `User-provided description for this resource.`, |
| }, |
| "create_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `Creation time of this resource. |
| A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and |
| up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`, |
| }, |
| "external_ip": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `The external IP address of a workload VM.`, |
| }, |
| "state": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `State of the resource.`, |
| }, |
| "uid": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `System-generated unique identifier for the resource.`, |
| }, |
| "update_time": { |
| Type: schema.TypeString, |
| Computed: true, |
| Description: `Last updated time of this resource. |
| A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine |
| fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`, |
| }, |
| }, |
| UseJSONNumber: true, |
| } |
| } |
| |
| func resourceVmwareengineExternalAddressCreate(d *schema.ResourceData, meta interface{}) error { |
| var project string |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| obj := make(map[string]interface{}) |
| internalIpProp, err := expandVmwareengineExternalAddressInternalIp(d.Get("internal_ip"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("internal_ip"); !tpgresource.IsEmptyValue(reflect.ValueOf(internalIpProp)) && (ok || !reflect.DeepEqual(v, internalIpProp)) { |
| obj["internalIp"] = internalIpProp |
| } |
| descriptionProp, err := expandVmwareengineExternalAddressDescription(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 |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{VmwareengineBasePath}}{{parent}}/externalAddresses?externalAddressId={{name}}") |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Creating new ExternalAddress: %#v", obj) |
| billingProject := "" |
| |
| // 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, |
| ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.ExternalIpServiceNotActive}, |
| }) |
| if err != nil { |
| return fmt.Errorf("Error creating ExternalAddress: %s", err) |
| } |
| |
| // Store the ID now |
| id, err := tpgresource.ReplaceVars(d, config, "{{parent}}/externalAddresses/{{name}}") |
| if err != nil { |
| return fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| err = VmwareengineOperationWaitTime( |
| config, res, project, "Creating ExternalAddress", userAgent, |
| d.Timeout(schema.TimeoutCreate)) |
| |
| if err != nil { |
| // The resource didn't actually create |
| d.SetId("") |
| return fmt.Errorf("Error waiting to create ExternalAddress: %s", err) |
| } |
| |
| log.Printf("[DEBUG] Finished creating ExternalAddress %q: %#v", d.Id(), res) |
| |
| return resourceVmwareengineExternalAddressRead(d, meta) |
| } |
| |
| func resourceVmwareengineExternalAddressRead(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, "{{VmwareengineBasePath}}{{parent}}/externalAddresses/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| |
| // 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, |
| ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.ExternalIpServiceNotActive}, |
| }) |
| if err != nil { |
| return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("VmwareengineExternalAddress %q", d.Id())) |
| } |
| |
| if err := d.Set("create_time", flattenVmwareengineExternalAddressCreateTime(res["createTime"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| if err := d.Set("update_time", flattenVmwareengineExternalAddressUpdateTime(res["updateTime"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| if err := d.Set("internal_ip", flattenVmwareengineExternalAddressInternalIp(res["internalIp"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| if err := d.Set("external_ip", flattenVmwareengineExternalAddressExternalIp(res["externalIp"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| if err := d.Set("state", flattenVmwareengineExternalAddressState(res["state"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| if err := d.Set("uid", flattenVmwareengineExternalAddressUid(res["uid"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| if err := d.Set("description", flattenVmwareengineExternalAddressDescription(res["description"], d, config)); err != nil { |
| return fmt.Errorf("Error reading ExternalAddress: %s", err) |
| } |
| |
| return nil |
| } |
| |
| func resourceVmwareengineExternalAddressUpdate(d *schema.ResourceData, meta interface{}) error { |
| var project string |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| |
| obj := make(map[string]interface{}) |
| internalIpProp, err := expandVmwareengineExternalAddressInternalIp(d.Get("internal_ip"), d, config) |
| if err != nil { |
| return err |
| } else if v, ok := d.GetOkExists("internal_ip"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, internalIpProp)) { |
| obj["internalIp"] = internalIpProp |
| } |
| descriptionProp, err := expandVmwareengineExternalAddressDescription(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 |
| } |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{VmwareengineBasePath}}{{parent}}/externalAddresses/{{name}}") |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Updating ExternalAddress %q: %#v", d.Id(), obj) |
| headers := make(http.Header) |
| updateMask := []string{} |
| |
| if d.HasChange("internal_ip") { |
| updateMask = append(updateMask, "internalIp") |
| } |
| |
| if d.HasChange("description") { |
| updateMask = append(updateMask, "description") |
| } |
| // 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, |
| ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.ExternalIpServiceNotActive}, |
| }) |
| |
| if err != nil { |
| return fmt.Errorf("Error updating ExternalAddress %q: %s", d.Id(), err) |
| } else { |
| log.Printf("[DEBUG] Finished updating ExternalAddress %q: %#v", d.Id(), res) |
| } |
| |
| err = VmwareengineOperationWaitTime( |
| config, res, project, "Updating ExternalAddress", userAgent, |
| d.Timeout(schema.TimeoutUpdate)) |
| |
| if err != nil { |
| return err |
| } |
| } |
| |
| return resourceVmwareengineExternalAddressRead(d, meta) |
| } |
| |
| func resourceVmwareengineExternalAddressDelete(d *schema.ResourceData, meta interface{}) error { |
| var project string |
| config := meta.(*transport_tpg.Config) |
| userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| if err != nil { |
| return err |
| } |
| |
| billingProject := "" |
| |
| url, err := tpgresource.ReplaceVars(d, config, "{{VmwareengineBasePath}}{{parent}}/externalAddresses/{{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 ExternalAddress %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, |
| ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.ExternalIpServiceNotActive}, |
| }) |
| if err != nil { |
| return transport_tpg.HandleNotFoundError(err, d, "ExternalAddress") |
| } |
| |
| err = VmwareengineOperationWaitTime( |
| config, res, project, "Deleting ExternalAddress", userAgent, |
| d.Timeout(schema.TimeoutDelete)) |
| |
| if err != nil { |
| return err |
| } |
| |
| log.Printf("[DEBUG] Finished deleting ExternalAddress %q: %#v", d.Id(), res) |
| return nil |
| } |
| |
| func resourceVmwareengineExternalAddressImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| config := meta.(*transport_tpg.Config) |
| if err := tpgresource.ParseImportId([]string{ |
| "^(?P<parent>.+)/externalAddresses/(?P<name>[^/]+)$", |
| }, d, config); err != nil { |
| return nil, err |
| } |
| |
| // Replace import id for the resource id |
| id, err := tpgresource.ReplaceVars(d, config, "{{parent}}/externalAddresses/{{name}}") |
| if err != nil { |
| return nil, fmt.Errorf("Error constructing id: %s", err) |
| } |
| d.SetId(id) |
| |
| return []*schema.ResourceData{d}, nil |
| } |
| |
| func flattenVmwareengineExternalAddressCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenVmwareengineExternalAddressUpdateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenVmwareengineExternalAddressInternalIp(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenVmwareengineExternalAddressExternalIp(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenVmwareengineExternalAddressState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenVmwareengineExternalAddressUid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func flattenVmwareengineExternalAddressDescription(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| return v |
| } |
| |
| func expandVmwareengineExternalAddressInternalIp(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |
| |
| func expandVmwareengineExternalAddressDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| return v, nil |
| } |