-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating resource for modern/HA vpn gateway in terraform
Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
f5e6612
commit bbb9a30
Showing
6 changed files
with
1,186 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,343 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** 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" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
func resourceComputeHaVpnGateway() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceComputeHaVpnGatewayCreate, | ||
Read: resourceComputeHaVpnGatewayRead, | ||
Delete: resourceComputeHaVpnGatewayDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: resourceComputeHaVpnGatewayImport, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(240 * time.Second), | ||
Delete: schema.DefaultTimeout(240 * time.Second), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateGCPName, | ||
}, | ||
"network": { | ||
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, | ||
}, | ||
"vpn_interfaces": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"ip_address": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"self_link": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceComputeHaVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
obj := make(map[string]interface{}) | ||
descriptionProp, err := expandComputeHaVpnGatewayDescription(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 | ||
} | ||
nameProp, err := expandComputeHaVpnGatewayName(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 | ||
} | ||
networkProp, err := expandComputeHaVpnGatewayNetwork(d.Get("network"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) { | ||
obj["network"] = networkProp | ||
} | ||
regionProp, err := expandComputeHaVpnGatewayRegion(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/beta/projects/{{project}}/regions/{{region}}/vpnGateways") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating new HaVpnGateway: %#v", obj) | ||
res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutCreate)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating HaVpnGateway: %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 HaVpnGateway", | ||
int(d.Timeout(schema.TimeoutCreate).Minutes())) | ||
|
||
if waitErr != nil { | ||
// The resource didn't actually create | ||
d.SetId("") | ||
return fmt.Errorf("Error waiting to create HaVpnGateway: %s", waitErr) | ||
} | ||
|
||
log.Printf("[DEBUG] Finished creating HaVpnGateway %q: %#v", d.Id(), res) | ||
|
||
return resourceComputeHaVpnGatewayRead(d, meta) | ||
} | ||
|
||
func resourceComputeHaVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/regions/{{region}}/vpnGateways/{{name}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := sendRequest(config, "GET", url, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("ComputeHaVpnGateway %q", d.Id())) | ||
} | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
if err := d.Set("project", project); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
|
||
if err := d.Set("description", flattenComputeHaVpnGatewayDescription(res["description"], d)); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
if err := d.Set("name", flattenComputeHaVpnGatewayName(res["name"], d)); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
if err := d.Set("network", flattenComputeHaVpnGatewayNetwork(res["network"], d)); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
if err := d.Set("vpn_interfaces", flattenComputeHaVpnGatewayVpnInterfaces(res["vpnInterfaces"], d)); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
if err := d.Set("region", flattenComputeHaVpnGatewayRegion(res["region"], d)); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil { | ||
return fmt.Errorf("Error reading HaVpnGateway: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceComputeHaVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/regions/{{region}}/vpnGateways/{{name}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var obj map[string]interface{} | ||
log.Printf("[DEBUG] Deleting HaVpnGateway %q", d.Id()) | ||
res, err := sendRequestWithTimeout(config, "DELETE", url, obj, d.Timeout(schema.TimeoutDelete)) | ||
if err != nil { | ||
return handleNotFoundError(err, d, "HaVpnGateway") | ||
} | ||
|
||
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 HaVpnGateway", | ||
int(d.Timeout(schema.TimeoutDelete).Minutes())) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Finished deleting HaVpnGateway %q: %#v", d.Id(), res) | ||
return nil | ||
} | ||
|
||
func resourceComputeHaVpnGatewayImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
config := meta.(*Config) | ||
if err := parseImportId([]string{"projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/vpnGateways/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil { | ||
return nil, err | ||
} | ||
|
||
// 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 flattenComputeHaVpnGatewayDescription(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenComputeHaVpnGatewayName(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenComputeHaVpnGatewayNetwork(v interface{}, d *schema.ResourceData) interface{} { | ||
if v == nil { | ||
return v | ||
} | ||
return ConvertSelfLinkToV1(v.(string)) | ||
} | ||
|
||
func flattenComputeHaVpnGatewayVpnInterfaces(v interface{}, d *schema.ResourceData) 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": flattenComputeHaVpnGatewayVpnInterfacesId(original["id"], d), | ||
"ip_address": flattenComputeHaVpnGatewayVpnInterfacesIpAddress(original["ipAddress"], d), | ||
}) | ||
} | ||
return transformed | ||
} | ||
func flattenComputeHaVpnGatewayVpnInterfacesId(v interface{}, d *schema.ResourceData) 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 flattenComputeHaVpnGatewayVpnInterfacesIpAddress(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenComputeHaVpnGatewayRegion(v interface{}, d *schema.ResourceData) interface{} { | ||
if v == nil { | ||
return v | ||
} | ||
return NameFromSelfLinkStateFunc(v) | ||
} | ||
|
||
func expandComputeHaVpnGatewayDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandComputeHaVpnGatewayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { | ||
return v, nil | ||
} | ||
|
||
func expandComputeHaVpnGatewayNetwork(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { | ||
f, err := parseGlobalFieldValue("networks", v.(string), "project", d, config, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("Invalid value for network: %s", err) | ||
} | ||
return f.RelativeLink(), nil | ||
} | ||
|
||
func expandComputeHaVpnGatewayRegion(v interface{}, d TerraformResourceData, 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 | ||
} |
Oops, something went wrong.