-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/google: global address + tests & documentation
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 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
100 changes: 100 additions & 0 deletions
100
builtin/providers/google/resource_compute_global_address.go
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,100 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"google.golang.org/api/compute/v1" | ||
"google.golang.org/api/googleapi" | ||
) | ||
|
||
func resourceComputeGlobalAddress() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceComputeGlobalAddressCreate, | ||
Read: resourceComputeGlobalAddressRead, | ||
Delete: resourceComputeGlobalAddressDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"address": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"self_link": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
// Build the address parameter | ||
addr := &compute.Address{Name: d.Get("name").(string)} | ||
op, err := config.clientCompute.GlobalAddresses.Insert( | ||
config.Project, addr).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error creating address: %s", err) | ||
} | ||
|
||
// It probably maybe worked, so store the ID now | ||
d.SetId(addr.Name) | ||
|
||
err = computeOperationWaitGlobal(config, op, "Creating Global Address") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceComputeGlobalAddressRead(d, meta) | ||
} | ||
|
||
func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
addr, err := config.clientCompute.GlobalAddresses.Get( | ||
config.Project, d.Id()).Do() | ||
if err != nil { | ||
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { | ||
// The resource doesn't exist anymore | ||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error reading address: %s", err) | ||
} | ||
|
||
d.Set("address", addr.Address) | ||
d.Set("self_link", addr.SelfLink) | ||
|
||
return nil | ||
} | ||
|
||
func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
// Delete the address | ||
log.Printf("[DEBUG] address delete request") | ||
op, err := config.clientCompute.GlobalAddresses.Delete( | ||
config.Project, d.Id()).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting address: %s", err) | ||
} | ||
|
||
err = computeOperationWaitGlobal(config, op, "Deletingg Global Address") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
81 changes: 81 additions & 0 deletions
81
builtin/providers/google/resource_compute_global_address_test.go
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,81 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
func TestAccComputeGlobalAddress_basic(t *testing.T) { | ||
var addr compute.Address | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckComputeGlobalAddressDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccComputeGlobalAddress_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeGlobalAddressExists( | ||
"google_compute_global_address.foobar", &addr), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckComputeGlobalAddressDestroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "google_compute_global_address" { | ||
continue | ||
} | ||
|
||
_, err := config.clientCompute.GlobalAddresses.Get( | ||
config.Project, rs.Primary.ID).Do() | ||
if err == nil { | ||
return fmt.Errorf("Address still exists") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckComputeGlobalAddressExists(n string, addr *compute.Address) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
|
||
found, err := config.clientCompute.GlobalAddresses.Get( | ||
config.Project, rs.Primary.ID).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if found.Name != rs.Primary.ID { | ||
return fmt.Errorf("Addr not found") | ||
} | ||
|
||
*addr = *found | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccComputeGlobalAddress_basic = ` | ||
resource "google_compute_global_address" "foobar" { | ||
name = "terraform-test" | ||
}` |
37 changes: 37 additions & 0 deletions
37
website/source/docs/providers/google/r/compute_global_address.html.markdown
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,37 @@ | ||
--- | ||
layout: "google" | ||
page_title: "Google: google_compute_global_address" | ||
sidebar_current: "docs-google-compute-global-address" | ||
description: |- | ||
Creates a static global IP address resource for a Google Compute Engine project. | ||
--- | ||
|
||
# google\_compute\_global\_address | ||
|
||
Creates a static IP address resource global to a for Google Compute Engine project. For more information see | ||
[the official documentation](https://cloud.google.com/compute/docs/instances-and-network) and | ||
[API](https://cloud.google.com/compute/docs/reference/latest/globalAddresses). | ||
|
||
|
||
## Example Usage | ||
|
||
``` | ||
resource "google_compute_global_address" "default" { | ||
name = "test-address" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) A unique name for the resource, required by GCE. | ||
Changing this forces a new resource to be created. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `name` - The name of the resource. | ||
* `address` - The IP address that was allocated. | ||
* `self_link` - The URI of the created resource. |
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