mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
392 lines
13 KiB
Go
392 lines
13 KiB
Go
![]() |
// ----------------------------------------------------------------------------
|
||
|
//
|
||
|
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
|
||
|
//
|
||
|
// ----------------------------------------------------------------------------
|
||
|
//
|
||
|
// 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 google
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"reflect"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
|
"github.com/hashicorp/terraform/helper/schema"
|
||
|
compute "google.golang.org/api/compute/v1"
|
||
|
)
|
||
|
|
||
|
func resourceComputeInterconnectAttachment() *schema.Resource {
|
||
|
return &schema.Resource{
|
||
|
Create: resourceComputeInterconnectAttachmentCreate,
|
||
|
Read: resourceComputeInterconnectAttachmentRead,
|
||
|
Delete: resourceComputeInterconnectAttachmentDelete,
|
||
|
|
||
|
Importer: &schema.ResourceImporter{
|
||
|
State: resourceComputeInterconnectAttachmentImport,
|
||
|
},
|
||
|
|
||
|
Timeouts: &schema.ResourceTimeout{
|
||
|
Create: schema.DefaultTimeout(240 * time.Second),
|
||
|
Delete: schema.DefaultTimeout(240 * time.Second),
|
||
|
},
|
||
|
|
||
|
Schema: map[string]*schema.Schema{
|
||
|
"interconnect": {
|
||
|
Type: schema.TypeString,
|
||
|
Required: true,
|
||
|
ForceNew: true,
|
||
|
DiffSuppressFunc: compareSelfLinkOrResourceName,
|
||
|
},
|
||
|
"name": {
|
||
|
Type: schema.TypeString,
|
||
|
Required: true,
|
||
|
ForceNew: true,
|
||
|
ValidateFunc: validateRegexp(`^[a-z]([-a-z0-9]*[a-z0-9])?$`),
|
||
|
},
|
||
|
"router": {
|
||
|
Type: schema.TypeString,
|
||
|
Required: true,
|
||
|
ForceNew: true,
|
||
|
DiffSuppressFunc: compareSelfLinkOrResourceName,
|
||
|
},
|
||
|
"description": {
|
||
|
Type: schema.TypeString,
|
||
|
Optional: true,
|
||
|
ForceNew: true,
|
||
|
},
|
||
|
"region": {
|
||
|
Type: schema.TypeString,
|
||
|
Computed: true,
|
||
|
Optional: true,
|
||
|
ForceNew: true,
|
||
|
DiffSuppressFunc: compareSelfLinkOrResourceName,
|
||
|
},
|
||
|
"cloud_router_ip_address": {
|
||
|
Type: schema.TypeString,
|
||
|
Computed: true,
|
||
|
},
|
||
|
"creation_timestamp": {
|
||
|
Type: schema.TypeString,
|
||
|
Computed: true,
|
||
|
},
|
||
|
"customer_router_ip_address": {
|
||
|
Type: schema.TypeString,
|
||
|
Computed: true,
|
||
|
},
|
||
|
"google_reference_id": {
|
||
|
Type: schema.TypeString,
|
||
|
Computed: true,
|
||
|
},
|
||
|
"private_interconnect_info": {
|
||
|
Type: schema.TypeList,
|
||
|
Computed: true,
|
||
|
MaxItems: 1,
|
||
|
Elem: &schema.Resource{
|
||
|
Schema: map[string]*schema.Schema{
|
||
|
"tag8021q": {
|
||
|
Type: schema.TypeInt,
|
||
|
Computed: true,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
"project": {
|
||
|
Type: schema.TypeString,
|
||
|
Optional: true,
|
||
|
Computed: true,
|
||
|
ForceNew: true,
|
||
|
},
|
||
|
"self_link": {
|
||
|
Type: schema.TypeString,
|
||
|
Computed: true,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func resourceComputeInterconnectAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
obj := make(map[string]interface{})
|
||
|
interconnectProp, err := expandComputeInterconnectAttachmentInterconnect(d.Get("interconnect"), d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else if v, ok := d.GetOkExists("interconnect"); !isEmptyValue(reflect.ValueOf(interconnectProp)) && (ok || !reflect.DeepEqual(v, interconnectProp)) {
|
||
|
obj["interconnect"] = interconnectProp
|
||
|
}
|
||
|
descriptionProp, err := expandComputeInterconnectAttachmentDescription(d.Get("description"), d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
|
||
|
obj["description"] = descriptionProp
|
||
|
}
|
||
|
routerProp, err := expandComputeInterconnectAttachmentRouter(d.Get("router"), d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else if v, ok := d.GetOkExists("router"); !isEmptyValue(reflect.ValueOf(routerProp)) && (ok || !reflect.DeepEqual(v, routerProp)) {
|
||
|
obj["router"] = routerProp
|
||
|
}
|
||
|
nameProp, err := expandComputeInterconnectAttachmentName(d.Get("name"), d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
|
||
|
obj["name"] = nameProp
|
||
|
}
|
||
|
regionProp, err := expandComputeInterconnectAttachmentRegion(d.Get("region"), d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
} else if v, ok := d.GetOkExists("region"); !isEmptyValue(reflect.ValueOf(regionProp)) && (ok || !reflect.DeepEqual(v, regionProp)) {
|
||
|
obj["region"] = regionProp
|
||
|
}
|
||
|
|
||
|
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/interconnectAttachments")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Printf("[DEBUG] Creating new InterconnectAttachment: %#v", obj)
|
||
|
res, err := sendRequest(config, "POST", url, obj)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Error creating InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
|
||
|
// Store the ID now
|
||
|
id, err := replaceVars(d, config, "{{name}}")
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Error constructing id: %s", err)
|
||
|
}
|
||
|
d.SetId(id)
|
||
|
|
||
|
project, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
op := &compute.Operation{}
|
||
|
err = Convert(res, op)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
waitErr := computeOperationWaitTime(
|
||
|
config.clientCompute, op, project, "Creating InterconnectAttachment",
|
||
|
int(d.Timeout(schema.TimeoutCreate).Minutes()))
|
||
|
|
||
|
if waitErr != nil {
|
||
|
// The resource didn't actually create
|
||
|
d.SetId("")
|
||
|
return fmt.Errorf("Error waiting to create InterconnectAttachment: %s", waitErr)
|
||
|
}
|
||
|
|
||
|
log.Printf("[DEBUG] Finished creating InterconnectAttachment %q: %#v", d.Id(), res)
|
||
|
|
||
|
return resourceComputeInterconnectAttachmentRead(d, meta)
|
||
|
}
|
||
|
|
||
|
func resourceComputeInterconnectAttachmentRead(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/interconnectAttachments/{{name}}")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
res, err := sendRequest(config, "GET", url, nil)
|
||
|
if err != nil {
|
||
|
return handleNotFoundError(err, d, fmt.Sprintf("ComputeInterconnectAttachment %q", d.Id()))
|
||
|
}
|
||
|
|
||
|
if err := d.Set("cloud_router_ip_address", flattenComputeInterconnectAttachmentCloudRouterIpAddress(res["cloudRouterIpAddress"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("customer_router_ip_address", flattenComputeInterconnectAttachmentCustomerRouterIpAddress(res["customerRouterIpAddress"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("interconnect", flattenComputeInterconnectAttachmentInterconnect(res["interconnect"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("description", flattenComputeInterconnectAttachmentDescription(res["description"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("private_interconnect_info", flattenComputeInterconnectAttachmentPrivateInterconnectInfo(res["privateInterconnectInfo"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("google_reference_id", flattenComputeInterconnectAttachmentGoogleReferenceId(res["googleReferenceId"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("router", flattenComputeInterconnectAttachmentRouter(res["router"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("creation_timestamp", flattenComputeInterconnectAttachmentCreationTimestamp(res["creationTimestamp"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("name", flattenComputeInterconnectAttachmentName(res["name"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("region", flattenComputeInterconnectAttachmentRegion(res["region"])); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
project, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := d.Set("project", project); err != nil {
|
||
|
return fmt.Errorf("Error reading InterconnectAttachment: %s", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func resourceComputeInterconnectAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/interconnectAttachments/{{name}}")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
var obj map[string]interface{}
|
||
|
log.Printf("[DEBUG] Deleting InterconnectAttachment %q", d.Id())
|
||
|
res, err := sendRequest(config, "DELETE", url, obj)
|
||
|
if err != nil {
|
||
|
return handleNotFoundError(err, d, "InterconnectAttachment")
|
||
|
}
|
||
|
|
||
|
project, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
op := &compute.Operation{}
|
||
|
err = Convert(res, op)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = computeOperationWaitTime(
|
||
|
config.clientCompute, op, project, "Deleting InterconnectAttachment",
|
||
|
int(d.Timeout(schema.TimeoutDelete).Minutes()))
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Printf("[DEBUG] Finished deleting InterconnectAttachment %q: %#v", d.Id(), res)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func resourceComputeInterconnectAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||
|
config := meta.(*Config)
|
||
|
parseImportId([]string{"projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/interconnectAttachments/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)
|
||
|
|
||
|
// Replace import id for the resource id
|
||
|
id, err := replaceVars(d, config, "{{name}}")
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("Error constructing id: %s", err)
|
||
|
}
|
||
|
d.SetId(id)
|
||
|
|
||
|
return []*schema.ResourceData{d}, nil
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentCloudRouterIpAddress(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentCustomerRouterIpAddress(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentInterconnect(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentDescription(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentPrivateInterconnectInfo(v interface{}) interface{} {
|
||
|
if v == nil {
|
||
|
return nil
|
||
|
}
|
||
|
original := v.(map[string]interface{})
|
||
|
transformed := make(map[string]interface{})
|
||
|
transformed["tag8021q"] =
|
||
|
flattenComputeInterconnectAttachmentPrivateInterconnectInfoTag8021q(original["tag8021q"])
|
||
|
return []interface{}{transformed}
|
||
|
}
|
||
|
func flattenComputeInterconnectAttachmentPrivateInterconnectInfoTag8021q(v interface{}) interface{} {
|
||
|
// Handles the string fixed64 format
|
||
|
if strVal, ok := v.(string); ok {
|
||
|
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
|
||
|
return intVal
|
||
|
} // let terraform core handle it if we can't convert the string to an int.
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentGoogleReferenceId(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentRouter(v interface{}) interface{} {
|
||
|
if v == nil {
|
||
|
return v
|
||
|
}
|
||
|
return ConvertSelfLinkToV1(v.(string))
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentCreationTimestamp(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentName(v interface{}) interface{} {
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func flattenComputeInterconnectAttachmentRegion(v interface{}) interface{} {
|
||
|
if v == nil {
|
||
|
return v
|
||
|
}
|
||
|
return ConvertSelfLinkToV1(v.(string))
|
||
|
}
|
||
|
|
||
|
func expandComputeInterconnectAttachmentInterconnect(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
|
||
|
return v, nil
|
||
|
}
|
||
|
|
||
|
func expandComputeInterconnectAttachmentDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
|
||
|
return v, nil
|
||
|
}
|
||
|
|
||
|
func expandComputeInterconnectAttachmentRouter(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
|
||
|
f, err := parseRegionalFieldValue("routers", v.(string), "project", "region", "zone", d, config, true)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("Invalid value for router: %s", err)
|
||
|
}
|
||
|
return f.RelativeLink(), nil
|
||
|
}
|
||
|
|
||
|
func expandComputeInterconnectAttachmentName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
|
||
|
return v, nil
|
||
|
}
|
||
|
|
||
|
func expandComputeInterconnectAttachmentRegion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
|
||
|
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("Invalid value for region: %s", err)
|
||
|
}
|
||
|
return f.RelativeLink(), nil
|
||
|
}
|