github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/profitbricks/resource_profitbricks_lan.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"github.com/profitbricks/profitbricks-sdk-go"
     7  	"log"
     8  )
     9  
    10  func resourceProfitBricksLan() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceProfitBricksLanCreate,
    13  		Read:   resourceProfitBricksLanRead,
    14  		Update: resourceProfitBricksLanUpdate,
    15  		Delete: resourceProfitBricksLanDelete,
    16  		Schema: map[string]*schema.Schema{
    17  
    18  			"public": {
    19  				Type:     schema.TypeBool,
    20  				Required: true,
    21  			},
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  			},
    26  			"datacenter_id": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func resourceProfitBricksLanCreate(d *schema.ResourceData, meta interface{}) error {
    35  	config := meta.(*Config)
    36  	profitbricks.SetAuth(config.Username, config.Password)
    37  	profitbricks.SetDepth("5")
    38  	request := profitbricks.Lan{
    39  		Properties: profitbricks.LanProperties{
    40  			Public: d.Get("public").(bool),
    41  		},
    42  	}
    43  
    44  	log.Printf("[DEBUG] NAME %s", d.Get("name"))
    45  	if d.Get("name") != nil {
    46  		request.Properties.Name = d.Get("name").(string)
    47  	}
    48  
    49  	lan := profitbricks.CreateLan(d.Get("datacenter_id").(string), request)
    50  
    51  	log.Printf("[DEBUG] LAN ID: %s", lan.Id)
    52  	log.Printf("[DEBUG] LAN RESPONSE: %s", lan.Response)
    53  
    54  	if lan.StatusCode > 299 {
    55  		return fmt.Errorf("An error occured while creating a lan: %s", lan.Response)
    56  	}
    57  
    58  	err := waitTillProvisioned(meta, lan.Headers.Get("Location"))
    59  	if err != nil {
    60  		return err
    61  	}
    62  	d.SetId(lan.Id)
    63  	return resourceProfitBricksLanRead(d, meta)
    64  }
    65  
    66  func resourceProfitBricksLanRead(d *schema.ResourceData, meta interface{}) error {
    67  	config := meta.(*Config)
    68  	profitbricks.SetAuth(config.Username, config.Password)
    69  
    70  	lan := profitbricks.GetLan(d.Get("datacenter_id").(string), d.Id())
    71  
    72  	if lan.StatusCode > 299 {
    73  		return fmt.Errorf("An error occured while fetching a lan ID %s %s", d.Id(), lan.Response)
    74  	}
    75  
    76  	d.Set("public", lan.Properties.Public)
    77  	d.Set("name", lan.Properties.Name)
    78  	d.Set("datacenter_id", d.Get("datacenter_id").(string))
    79  	return nil
    80  }
    81  
    82  func resourceProfitBricksLanUpdate(d *schema.ResourceData, meta interface{}) error {
    83  	config := meta.(*Config)
    84  	profitbricks.SetAuth(config.Username, config.Password)
    85  
    86  	properties := &profitbricks.LanProperties{}
    87  	if d.HasChange("public") {
    88  		_, newValue := d.GetChange("public")
    89  		properties.Public = newValue.(bool)
    90  	}
    91  	if d.HasChange("name") {
    92  		_, newValue := d.GetChange("name")
    93  		properties.Name = newValue.(string)
    94  	}
    95  	log.Printf("[DEBUG] LAN UPDATE: %s : %s", properties, d.Get("name"))
    96  	if properties != nil {
    97  		lan := profitbricks.PatchLan(d.Get("datacenter_id").(string), d.Id(), *properties)
    98  		if lan.StatusCode > 299 {
    99  			return fmt.Errorf("An error occured while patching a lan ID %s %s", d.Id(), lan.Response)
   100  		}
   101  		err := waitTillProvisioned(meta, lan.Headers.Get("Location"))
   102  		if err != nil {
   103  			return err
   104  		}
   105  	}
   106  	return resourceProfitBricksLanRead(d, meta)
   107  }
   108  
   109  func resourceProfitBricksLanDelete(d *schema.ResourceData, meta interface{}) error {
   110  	config := meta.(*Config)
   111  	profitbricks.SetAuth(config.Username, config.Password)
   112  
   113  	resp := profitbricks.DeleteLan(d.Get("datacenter_id").(string), d.Id())
   114  
   115  	if resp.Headers.Get("Location") != "" {
   116  		err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
   117  		if err != nil {
   118  			return err
   119  		}
   120  	}
   121  	d.SetId("")
   122  	return nil
   123  }