2014-08-25 21:55:08 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
2014-08-25 21:55:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeAddress() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeAddressCreate,
|
|
|
|
Read: resourceComputeAddressRead,
|
|
|
|
Delete: resourceComputeAddressDelete,
|
2017-04-01 19:01:53 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
d.Set("name", d.Id())
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
},
|
|
|
|
},
|
2014-08-25 21:55:08 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-11-20 18:40:17 +01:00
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
2015-09-04 22:54:18 +02:00
|
|
|
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2014-08-25 21:55:08 +02:00
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"self_link": &schema.Schema{
|
2016-04-10 18:59:57 +02:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
|
|
|
},
|
2015-09-04 22:54:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-25 21:55:08 +02:00
|
|
|
|
|
|
|
// Build the address parameter
|
|
|
|
addr := &compute.Address{Name: d.Get("name").(string)}
|
|
|
|
op, err := config.clientCompute.Addresses.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, addr).Do()
|
2014-08-25 21:55:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(addr.Name)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Creating Address")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-25 21:55:08 +02:00
|
|
|
|
|
|
|
return resourceComputeAddressRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-04 22:54:18 +02:00
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
addr, err := config.clientCompute.Addresses.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, d.Id()).Do()
|
2014-08-25 21:55:08 +02:00
|
|
|
if err != nil {
|
2014-08-26 07:49:14 +02:00
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't exist anymore
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing Address %q because it's gone", d.Get("name").(string))
|
2014-08-26 07:49:14 +02:00
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
return fmt.Errorf("Error reading address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("address", addr.Address)
|
2014-11-20 18:40:17 +01:00
|
|
|
d.Set("self_link", addr.SelfLink)
|
2014-08-25 21:55:08 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
// Delete the address
|
2014-11-20 18:40:17 +01:00
|
|
|
log.Printf("[DEBUG] address delete request")
|
2014-08-25 21:55:08 +02:00
|
|
|
op, err := config.clientCompute.Addresses.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, d.Id()).Do()
|
2014-08-25 21:55:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting address: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Deleting Address")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-25 21:55:08 +02:00
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|