github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/profitbricks/resource_profitbricks_nic.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  	"strings"
     9  )
    10  
    11  func resourceProfitBricksNic() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceProfitBricksNicCreate,
    14  		Read:   resourceProfitBricksNicRead,
    15  		Update: resourceProfitBricksNicUpdate,
    16  		Delete: resourceProfitBricksNicDelete,
    17  		Schema: map[string]*schema.Schema{
    18  
    19  			"lan": &schema.Schema{
    20  				Type:     schema.TypeInt,
    21  				Required: true,
    22  			},
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  			},
    27  			"dhcp": &schema.Schema{
    28  				Type:     schema.TypeBool,
    29  				Optional: true,
    30  			},
    31  			"ip": {
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  			},
    35  			"firewall_active": {
    36  				Type:     schema.TypeBool,
    37  				Optional: true,
    38  			},
    39  			"server_id": {
    40  				Type:     schema.TypeString,
    41  				Required: true,
    42  			},
    43  			"datacenter_id": {
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func resourceProfitBricksNicCreate(d *schema.ResourceData, meta interface{}) error {
    52  	config := meta.(*Config)
    53  	profitbricks.SetAuth(config.Username, config.Password)
    54  
    55  	nic := profitbricks.Nic{
    56  		Properties: profitbricks.NicProperties{
    57  			Lan: d.Get("lan").(int),
    58  		},
    59  	}
    60  	if _, ok := d.GetOk("name"); ok {
    61  		nic.Properties.Name = d.Get("name").(string)
    62  	}
    63  	if _, ok := d.GetOk("dhcp"); ok {
    64  		nic.Properties.Dhcp = d.Get("dhcp").(bool)
    65  	}
    66  
    67  	if _, ok := d.GetOk("ip"); ok {
    68  		raw := d.Get("ip").(string)
    69  		ips := strings.Split(raw, ",")
    70  		nic.Properties.Ips = ips
    71  	}
    72  
    73  	nic = profitbricks.CreateNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), nic)
    74  	if nic.StatusCode > 299 {
    75  		return fmt.Errorf("Error occured while creating a nic: %s", nic.Response)
    76  	}
    77  
    78  	err := waitTillProvisioned(meta, nic.Headers.Get("Location"))
    79  	if err != nil {
    80  		return err
    81  	}
    82  	resp := profitbricks.RebootServer(d.Get("datacenter_id").(string), d.Get("server_id").(string))
    83  	if resp.StatusCode > 299 {
    84  		return fmt.Errorf("Error occured while creating a nic: %s", string(resp.Body))
    85  
    86  	}
    87  	err = waitTillProvisioned(meta, resp.Headers.Get("Location"))
    88  	if err != nil {
    89  		return err
    90  	}
    91  	d.SetId(nic.Id)
    92  	return resourceProfitBricksNicRead(d, meta)
    93  }
    94  
    95  func resourceProfitBricksNicRead(d *schema.ResourceData, meta interface{}) error {
    96  	config := meta.(*Config)
    97  	profitbricks.SetAuth(config.Username, config.Password)
    98  	profitbricks.SetDepth("5")
    99  
   100  	nic := profitbricks.GetNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id())
   101  	if nic.StatusCode > 299 {
   102  		return fmt.Errorf("Error occured while fetching a nic ID %s %s", d.Id(), nic.Response)
   103  	}
   104  	log.Printf("[INFO] LAN ON NIC: %s", nic.Properties.Lan)
   105  	d.Set("dhcp", nic.Properties.Dhcp)
   106  	d.Set("lan", nic.Properties.Lan)
   107  	d.Set("name", nic.Properties.Name)
   108  	d.Set("ip", nic.Properties.Ips)
   109  
   110  	return nil
   111  }
   112  
   113  func resourceProfitBricksNicUpdate(d *schema.ResourceData, meta interface{}) error {
   114  	config := meta.(*Config)
   115  	profitbricks.SetAuth(config.Username, config.Password)
   116  
   117  	properties := profitbricks.NicProperties{}
   118  
   119  	if d.HasChange("name") {
   120  		_, n := d.GetChange("name")
   121  
   122  		properties.Name = n.(string)
   123  	}
   124  	if d.HasChange("lan") {
   125  		_, n := d.GetChange("lan")
   126  		properties.Lan = n.(int)
   127  	}
   128  	if d.HasChange("dhcp") {
   129  		_, n := d.GetChange("dhcp")
   130  		properties.Dhcp = n.(bool)
   131  	}
   132  	if d.HasChange("ip") {
   133  		_, raw := d.GetChange("ip")
   134  		ips := strings.Split(raw.(string), ",")
   135  		properties.Ips = ips
   136  	}
   137  
   138  	nic := profitbricks.PatchNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id(), properties)
   139  
   140  	if nic.StatusCode > 299 {
   141  		return fmt.Errorf("Error occured while updating a nic: %s", nic.Response)
   142  	}
   143  	err := waitTillProvisioned(meta, nic.Headers.Get("Location"))
   144  	if err != nil {
   145  		return err
   146  	}
   147  	return resourceProfitBricksNicRead(d, meta)
   148  }
   149  
   150  func resourceProfitBricksNicDelete(d *schema.ResourceData, meta interface{}) error {
   151  	config := meta.(*Config)
   152  	profitbricks.SetAuth(config.Username, config.Password)
   153  
   154  	resp := profitbricks.DeleteNic(d.Get("datacenter_id").(string), d.Get("server_id").(string), d.Id())
   155  	err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
   156  	if err != nil {
   157  		return err
   158  	}
   159  	d.SetId("")
   160  	return nil
   161  }