From 12625997c1124f5bc410d1d311d6b49909ead03f Mon Sep 17 00:00:00 2001 From: Lars Wander Date: Fri, 25 Sep 2015 17:48:08 -0400 Subject: [PATCH 1/2] Added global address & tests --- builtin/providers/google/provider.go | 1 + .../google/resource_compute_global_address.go | 100 ++++++++++++++++++ .../resource_compute_global_address_test.go | 81 ++++++++++++++ .../r/compute_global_address.html.markdown | 37 +++++++ 4 files changed, 219 insertions(+) create mode 100644 builtin/providers/google/resource_compute_global_address.go create mode 100644 builtin/providers/google/resource_compute_global_address_test.go create mode 100644 website/source/docs/providers/google/r/compute_global_address.html.markdown diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 7c9587219..87a299d81 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -40,6 +40,7 @@ func Provider() terraform.ResourceProvider { "google_compute_disk": resourceComputeDisk(), "google_compute_firewall": resourceComputeFirewall(), "google_compute_forwarding_rule": resourceComputeForwardingRule(), + "google_compute_global_address": resourceComputeGlobalAddress(), "google_compute_http_health_check": resourceComputeHttpHealthCheck(), "google_compute_instance": resourceComputeInstance(), "google_compute_instance_template": resourceComputeInstanceTemplate(), diff --git a/builtin/providers/google/resource_compute_global_address.go b/builtin/providers/google/resource_compute_global_address.go new file mode 100644 index 000000000..0d19bdfcf --- /dev/null +++ b/builtin/providers/google/resource_compute_global_address.go @@ -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 = resourceOperationWaitGlobal(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 = resourceOperationWaitGlobal(config, op, "Deletingg Global Address") + if err != nil { + return err + } + + d.SetId("") + return nil +} diff --git a/builtin/providers/google/resource_compute_global_address_test.go b/builtin/providers/google/resource_compute_global_address_test.go new file mode 100644 index 000000000..2ef7b97ea --- /dev/null +++ b/builtin/providers/google/resource_compute_global_address_test.go @@ -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" +}` diff --git a/website/source/docs/providers/google/r/compute_global_address.html.markdown b/website/source/docs/providers/google/r/compute_global_address.html.markdown new file mode 100644 index 000000000..1fdb24e6d --- /dev/null +++ b/website/source/docs/providers/google/r/compute_global_address.html.markdown @@ -0,0 +1,37 @@ +--- +layout: "google" +page_title: "Google: google_compute_global_address" +sidebar_current: "docs-google-resource-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. From b7f7c7a7315a2e6c355f45c330c9d59ebc9c0e36 Mon Sep 17 00:00:00 2001 From: Lars Wander Date: Wed, 14 Oct 2015 13:17:08 -0400 Subject: [PATCH 2/2] Provider GCE, fixed metadata state update bug --- builtin/providers/google/provider.go | 1 - .../google/resource_compute_global_address.go | 100 ------------------ .../resource_compute_global_address_test.go | 81 -------------- .../google/resource_compute_instance.go | 18 +++- .../google/resource_compute_instance_test.go | 2 +- .../r/compute_global_address.html.markdown | 37 ------- 6 files changed, 18 insertions(+), 221 deletions(-) delete mode 100644 builtin/providers/google/resource_compute_global_address.go delete mode 100644 builtin/providers/google/resource_compute_global_address_test.go delete mode 100644 website/source/docs/providers/google/r/compute_global_address.html.markdown diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 87a299d81..7c9587219 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -40,7 +40,6 @@ func Provider() terraform.ResourceProvider { "google_compute_disk": resourceComputeDisk(), "google_compute_firewall": resourceComputeFirewall(), "google_compute_forwarding_rule": resourceComputeForwardingRule(), - "google_compute_global_address": resourceComputeGlobalAddress(), "google_compute_http_health_check": resourceComputeHttpHealthCheck(), "google_compute_instance": resourceComputeInstance(), "google_compute_instance_template": resourceComputeInstanceTemplate(), diff --git a/builtin/providers/google/resource_compute_global_address.go b/builtin/providers/google/resource_compute_global_address.go deleted file mode 100644 index 0d19bdfcf..000000000 --- a/builtin/providers/google/resource_compute_global_address.go +++ /dev/null @@ -1,100 +0,0 @@ -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 = resourceOperationWaitGlobal(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 = resourceOperationWaitGlobal(config, op, "Deletingg Global Address") - if err != nil { - return err - } - - d.SetId("") - return nil -} diff --git a/builtin/providers/google/resource_compute_global_address_test.go b/builtin/providers/google/resource_compute_global_address_test.go deleted file mode 100644 index 2ef7b97ea..000000000 --- a/builtin/providers/google/resource_compute_global_address_test.go +++ /dev/null @@ -1,81 +0,0 @@ -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" -}` diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 52575767e..229d1b05e 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -515,10 +515,17 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error // Synch metadata md := instance.Metadata - if err = d.Set("metadata", MetadataFormatSchema(md)); err != nil { + _md := MetadataFormatSchema(md) + if script, scriptExists := d.GetOk("metadata_startup_script"); scriptExists { + d.Set("metadata_startup_script", script) + delete(_md, "startup-script") + } + + if err = d.Set("metadata", _md); err != nil { return fmt.Errorf("Error setting metadata: %s", err) } + d.Set("can_ip_forward", instance.CanIpForward) // Set the service accounts @@ -635,6 +642,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error } d.Set("self_link", instance.SelfLink) + d.SetId(instance.Name) return nil } @@ -655,6 +663,14 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err // If the Metadata has changed, then update that. if d.HasChange("metadata") { o, n := d.GetChange("metadata") + if script, scriptExists := d.GetOk("metadata_startup_script"); scriptExists { + if _, ok := n.(map[string]interface{})["startup-script"]; ok { + return fmt.Errorf("Only one of metadata.startup-script and metadata_startup_script may be defined") + } + + n.(map[string]interface{})["startup-script"] = script + } + updateMD := func() error { // Reload the instance in the case of a fingerprint mismatch diff --git a/builtin/providers/google/resource_compute_instance_test.go b/builtin/providers/google/resource_compute_instance_test.go index 61c4906a2..f59da73ef 100644 --- a/builtin/providers/google/resource_compute_instance_test.go +++ b/builtin/providers/google/resource_compute_instance_test.go @@ -32,7 +32,7 @@ func TestAccComputeInstance_basic_deprecated_network(t *testing.T) { }) } -func TestAccComputeInstance_basic(t *testing.T) { +func TestAccComputeInstance_basic1(t *testing.T) { var instance compute.Instance resource.Test(t, resource.TestCase{ diff --git a/website/source/docs/providers/google/r/compute_global_address.html.markdown b/website/source/docs/providers/google/r/compute_global_address.html.markdown deleted file mode 100644 index 1fdb24e6d..000000000 --- a/website/source/docs/providers/google/r/compute_global_address.html.markdown +++ /dev/null @@ -1,37 +0,0 @@ ---- -layout: "google" -page_title: "Google: google_compute_global_address" -sidebar_current: "docs-google-resource-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.