Skip to content

Commit

Permalink
Release generated GlobalAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo authored and modular-magician committed Apr 26, 2018
1 parent 31a5c9d commit 8493072
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 54 deletions.
192 changes: 156 additions & 36 deletions google/resource_compute_global_address.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,74 @@
// ----------------------------------------------------------------------------
//
// *** 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"
"time"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

"google.golang.org/api/compute/v1"
compute "google.golang.org/api/compute/v1"
)

func resourceComputeGlobalAddress() *schema.Resource {
return &schema.Resource{
Create: resourceComputeGlobalAddressCreate,
Read: resourceComputeGlobalAddressRead,
Delete: resourceComputeGlobalAddressDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceComputeGlobalAddressImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(240 * time.Second),
Delete: schema.DefaultTimeout(240 * time.Second),
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"ip_version": &schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ip_version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6", ""}, false),
ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6"}, false),
},

"project": &schema.Schema{
"address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"address": &schema.Schema{
"creation_timestamp": {
Type: schema.TypeString,
Computed: true,
},

"self_link": &schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
Expand All @@ -61,25 +82,59 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}
return err
}

// Build the address parameter
addr := &compute.Address{
Name: d.Get("name").(string),
IpVersion: d.Get("ip_version").(string),
descriptionProp, err := expandComputeGlobalAddressDescription(d.Get("description"), d, config)
if err != nil {
return err
}
nameProp, err := expandComputeGlobalAddressName(d.Get("name"), d, config)
if err != nil {
return err
}
ipVersionProp, err := expandComputeGlobalAddressIpVersion(d.Get("ip_version"), d, config)
if err != nil {
return err
}

obj := map[string]interface{}{
"description": descriptionProp,
"name": nameProp,
"ipVersion": ipVersionProp,
}

op, err := config.clientCompute.GlobalAddresses.Insert(project, addr).Do()
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses")
if err != nil {
return fmt.Errorf("Error creating address: %s", err)
return err
}

// It probably maybe worked, so store the ID now
d.SetId(addr.Name)
log.Printf("[DEBUG] Creating new GlobalAddress: %#v", obj)
res, err := Post(config, url, obj)
if err != nil {
return fmt.Errorf("Error creating GlobalAddress: %s", err)
}

err = computeSharedOperationWait(config.clientCompute, op, project, "Creating Global Address")
// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return err
}

waitErr := computeOperationWaitTime(
config.clientCompute, op, project, "Creating GlobalAddress",
int(d.Timeout(schema.TimeoutCreate).Minutes()))

if waitErr != nil {
// The resource didn't actually create
d.SetId("")
return waitErr
}

return resourceComputeGlobalAddressRead(d, meta)
}

Expand All @@ -91,16 +146,23 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
return err
}

addr, err := config.clientCompute.GlobalAddresses.Get(project, d.Id()).Do()
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}")
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Global Address %q", d.Get("name").(string)))
return err
}

d.Set("name", addr.Name)
d.Set("ip_version", addr.IpVersion)
d.Set("address", addr.Address)
res, err := Get(config, url)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ComputeGlobalAddress %q", d.Id()))
}

d.Set("address", flattenComputeGlobalAddressAddress(res["address"]))
d.Set("creation_timestamp", flattenComputeGlobalAddressCreationTimestamp(res["creationTimestamp"]))
d.Set("description", flattenComputeGlobalAddressDescription(res["description"]))
d.Set("name", flattenComputeGlobalAddressName(res["name"]))
d.Set("ip_version", flattenComputeGlobalAddressIpVersion(res["ipVersion"]))
d.Set("self_link", res["selfLink"])
d.Set("project", project)
d.Set("self_link", ConvertSelfLinkToV1(addr.SelfLink))

return nil
}
Expand All @@ -113,18 +175,76 @@ func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}
return err
}

// Delete the address
log.Printf("[DEBUG] address delete request")
op, err := config.clientCompute.GlobalAddresses.Delete(project, d.Id()).Do()
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}")
if err != nil {
return err
}

log.Printf("[DEBUG] Deleting GlobalAddress %q", d.Id())
res, err := Delete(config, url)
if err != nil {
return fmt.Errorf("Error deleting GlobalAddress %q: %s", d.Id(), err)
}

op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return fmt.Errorf("Error deleting address: %s", err)
return err
}

err = computeSharedOperationWait(config.clientCompute, op, project, "Deleting Global Address")
err = computeOperationWaitTime(
config.clientCompute, op, project, "Deleting GlobalAddress",
int(d.Timeout(schema.TimeoutDelete).Minutes()))

if err != nil {
return err
}

d.SetId("")
return nil
}

func resourceComputeGlobalAddressImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
parseImportId([]string{"projects/(?P<project>[^/]+)/global/addresses/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?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 flattenComputeGlobalAddressAddress(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressCreationTimestamp(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressDescription(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressName(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressIpVersion(v interface{}) interface{} {
return v
}

func expandComputeGlobalAddressDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeGlobalAddressName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeGlobalAddressIpVersion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
2 changes: 2 additions & 0 deletions google/resource_compute_global_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ func testAccComputeGlobalAddress_basic() string {
return fmt.Sprintf(`
resource "google_compute_global_address" "foobar" {
name = "address-test-%s"
description = "Created for Terraform acceptance testing"
}`, acctest.RandString(10))
}

func testAccComputeGlobalAddress_ipv6() string {
return fmt.Sprintf(`
resource "google_compute_global_address" "foobar" {
name = "address-test-%s"
description = "Created for Terraform acceptance testing"
ip_version = "IPV6"
}`, acctest.RandString(10))
}
8 changes: 5 additions & 3 deletions google/resource_compute_target_ssl_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
compute "google.golang.org/api/compute/v1"
)

Expand Down Expand Up @@ -67,9 +68,10 @@ func resourceComputeTargetSslProxy() *schema.Resource {
ForceNew: true,
},
"proxy_header": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"NONE", "PROXY_V1"}, false),
Default: "NONE",
},
"creation_timestamp": {
Type: schema.TypeString,
Expand Down
Loading

0 comments on commit 8493072

Please sign in to comment.