github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/profitbricks/resource_profitbricks_ipblock.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 resourceProfitBricksIPBlock() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceProfitBricksIPBlockCreate,
    14  		Read:   resourceProfitBricksIPBlockRead,
    15  		//Update: resourceProfitBricksIPBlockUpdate,
    16  		Delete: resourceProfitBricksIPBlockDelete,
    17  		Schema: map[string]*schema.Schema{
    18  			"location": {
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  			"size": {
    24  				Type:     schema.TypeInt,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  			"ips": {
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func resourceProfitBricksIPBlockCreate(d *schema.ResourceData, meta interface{}) error {
    37  	config := meta.(*Config)
    38  	profitbricks.SetAuth(config.Username, config.Password)
    39  
    40  	ipblock := profitbricks.IpBlock{
    41  		Properties: profitbricks.IpBlockProperties{
    42  			Size:     d.Get("size").(int),
    43  			Location: d.Get("location").(string),
    44  		},
    45  	}
    46  
    47  	ipblock = profitbricks.ReserveIpBlock(ipblock)
    48  
    49  	if ipblock.StatusCode > 299 {
    50  		return fmt.Errorf("An error occured while reserving an ip block: %s", ipblock.Response)
    51  	}
    52  	err := waitTillProvisioned(meta, ipblock.Headers.Get("Location"))
    53  	if err != nil {
    54  		return err
    55  	}
    56  	d.SetId(ipblock.Id)
    57  
    58  	return resourceProfitBricksIPBlockRead(d, meta)
    59  }
    60  
    61  func resourceProfitBricksIPBlockRead(d *schema.ResourceData, meta interface{}) error {
    62  	config := meta.(*Config)
    63  	profitbricks.SetAuth(config.Username, config.Password)
    64  
    65  	ipblock := profitbricks.GetIpBlock(d.Id())
    66  
    67  	if ipblock.StatusCode > 299 {
    68  		return fmt.Errorf("An error occured while fetching an ip block ID %s %s", d.Id(), ipblock.Response)
    69  	}
    70  
    71  	log.Printf("[INFO] IPS: %s", strings.Join(ipblock.Properties.Ips, ","))
    72  
    73  	d.Set("ips", strings.Join(ipblock.Properties.Ips, ","))
    74  	d.Set("location", ipblock.Properties.Location)
    75  	d.Set("size", ipblock.Properties.Size)
    76  
    77  	return nil
    78  }
    79  
    80  func resourceProfitBricksIPBlockDelete(d *schema.ResourceData, meta interface{}) error {
    81  	config := meta.(*Config)
    82  	profitbricks.SetAuth(config.Username, config.Password)
    83  
    84  	resp := profitbricks.ReleaseIpBlock(d.Id())
    85  	if resp.StatusCode > 299 {
    86  		return fmt.Errorf("An error occured while releasing an ipblock ID: %s %s", d.Id(), string(resp.Body))
    87  	}
    88  
    89  	err := waitTillProvisioned(meta, resp.Headers.Get("Location"))
    90  	if err != nil {
    91  		return err
    92  	}
    93  	d.SetId("")
    94  	return nil
    95  }