2017-01-18 15:43:09 +01:00
|
|
|
package profitbricks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/profitbricks/profitbricks-sdk-go"
|
|
|
|
"log"
|
2017-02-02 14:26:14 +01:00
|
|
|
"time"
|
2017-01-18 15:43:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceProfitBricksLan() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceProfitBricksLanCreate,
|
|
|
|
Read: resourceProfitBricksLanRead,
|
|
|
|
Update: resourceProfitBricksLanUpdate,
|
|
|
|
Delete: resourceProfitBricksLanDelete,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
|
|
|
"public": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"datacenter_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceProfitBricksLanCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
request := profitbricks.Lan{
|
|
|
|
Properties: profitbricks.LanProperties{
|
|
|
|
Public: d.Get("public").(bool),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] NAME %s", d.Get("name"))
|
|
|
|
if d.Get("name") != nil {
|
|
|
|
request.Properties.Name = d.Get("name").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
lan := profitbricks.CreateLan(d.Get("datacenter_id").(string), request)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] LAN ID: %s", lan.Id)
|
|
|
|
log.Printf("[DEBUG] LAN RESPONSE: %s", lan.Response)
|
|
|
|
|
|
|
|
if lan.StatusCode > 299 {
|
|
|
|
return fmt.Errorf("An error occured while creating a lan: %s", lan.Response)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := waitTillProvisioned(meta, lan.Headers.Get("Location"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.SetId(lan.Id)
|
|
|
|
return resourceProfitBricksLanRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceProfitBricksLanRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
lan := profitbricks.GetLan(d.Get("datacenter_id").(string), d.Id())
|
|
|
|
|
|
|
|
if lan.StatusCode > 299 {
|
2017-03-25 21:43:41 +01:00
|
|
|
if lan.StatusCode == 404 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-18 15:43:09 +01:00
|
|
|
return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), lan.Response)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("public", lan.Properties.Public)
|
|
|
|
d.Set("name", lan.Properties.Name)
|
|
|
|
d.Set("datacenter_id", d.Get("datacenter_id").(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceProfitBricksLanUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
properties := &profitbricks.LanProperties{}
|
|
|
|
if d.HasChange("public") {
|
|
|
|
_, newValue := d.GetChange("public")
|
|
|
|
properties.Public = newValue.(bool)
|
|
|
|
}
|
|
|
|
if d.HasChange("name") {
|
|
|
|
_, newValue := d.GetChange("name")
|
|
|
|
properties.Name = newValue.(string)
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] LAN UPDATE: %s : %s", properties, d.Get("name"))
|
|
|
|
if properties != nil {
|
|
|
|
lan := profitbricks.PatchLan(d.Get("datacenter_id").(string), d.Id(), *properties)
|
|
|
|
if lan.StatusCode > 299 {
|
|
|
|
return fmt.Errorf("An error occured while patching a lan ID %s %s", d.Id(), lan.Response)
|
|
|
|
}
|
|
|
|
err := waitTillProvisioned(meta, lan.Headers.Get("Location"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resourceProfitBricksLanRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceProfitBricksLanDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
resp := profitbricks.DeleteLan(d.Get("datacenter_id").(string), d.Id())
|
2017-02-02 14:26:14 +01:00
|
|
|
if resp.StatusCode > 299 {
|
|
|
|
//try again in 20 seconds
|
|
|
|
time.Sleep(60 * time.Second)
|
|
|
|
resp = profitbricks.DeleteLan(d.Get("datacenter_id").(string), d.Id())
|
|
|
|
if resp.StatusCode > 299 && resp.StatusCode != 404 {
|
|
|
|
return fmt.Errorf("An error occured while deleting a lan dcId %s ID %s %s", d.Get("datacenter_id").(string), d.Id(), string(resp.Body))
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 15:43:09 +01:00
|
|
|
|
|
|
|
if resp.Headers.Get("Location") != "" {
|
|
|
|
err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|