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"
|
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{
|
2017-04-14 20:58:44 +02:00
|
|
|
State: schema.ImportStatePassthrough,
|
2017-04-01 19:01:53 +02:00
|
|
|
},
|
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 {
|
2017-05-10 01:00:47 +02:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Address %q", d.Get("name").(string)))
|
2014-08-25 21:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("address", addr.Address)
|
2014-11-20 18:40:17 +01:00
|
|
|
d.Set("self_link", addr.SelfLink)
|
2017-04-14 20:58:44 +02:00
|
|
|
d.Set("name", addr.Name)
|
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
|
|
|
|
}
|