github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/triton/resource_vlan.go (about)

     1  package triton
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/joyent/triton-go"
     9  )
    10  
    11  func resourceVLAN() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create:   resourceVLANCreate,
    14  		Exists:   resourceVLANExists,
    15  		Read:     resourceVLANRead,
    16  		Update:   resourceVLANUpdate,
    17  		Delete:   resourceVLANDelete,
    18  		Timeouts: fastResourceTimeout,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"vlan_id": {
    25  				Description: "Number between 0-4095 indicating VLAN ID",
    26  				Required:    true,
    27  				ForceNew:    true,
    28  				Type:        schema.TypeInt,
    29  				ValidateFunc: func(val interface{}, field string) (warn []string, err []error) {
    30  					value := val.(int)
    31  					if value < 0 || value > 4095 {
    32  						err = append(err, errors.New("vlan_id must be between 0 and 4095"))
    33  					}
    34  					return
    35  				},
    36  			},
    37  			"name": {
    38  				Description: "Unique name to identify VLAN",
    39  				Required:    true,
    40  				Type:        schema.TypeString,
    41  			},
    42  			"description": {
    43  				Description: "Description of the VLAN",
    44  				Optional:    true,
    45  				Type:        schema.TypeString,
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func resourceVLANCreate(d *schema.ResourceData, meta interface{}) error {
    52  	client := meta.(*triton.Client)
    53  
    54  	vlan, err := client.Fabrics().CreateFabricVLAN(&triton.CreateFabricVLANInput{
    55  		ID:          d.Get("vlan_id").(int),
    56  		Name:        d.Get("name").(string),
    57  		Description: d.Get("description").(string),
    58  	})
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	d.SetId(strconv.Itoa(vlan.ID))
    64  	return resourceVLANRead(d, meta)
    65  }
    66  
    67  func resourceVLANExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    68  	client := meta.(*triton.Client)
    69  
    70  	id, err := resourceVLANIDInt(d.Id())
    71  	if err != nil {
    72  		return false, err
    73  	}
    74  
    75  	return resourceExists(client.Fabrics().GetFabricVLAN(&triton.GetFabricVLANInput{
    76  		ID: id,
    77  	}))
    78  }
    79  
    80  func resourceVLANRead(d *schema.ResourceData, meta interface{}) error {
    81  	client := meta.(*triton.Client)
    82  
    83  	id, err := resourceVLANIDInt(d.Id())
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	vlan, err := client.Fabrics().GetFabricVLAN(&triton.GetFabricVLANInput{
    89  		ID: id,
    90  	})
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	d.Set("vlan_id", vlan.ID)
    96  	d.Set("name", vlan.Name)
    97  	d.Set("description", vlan.Description)
    98  
    99  	return nil
   100  }
   101  
   102  func resourceVLANUpdate(d *schema.ResourceData, meta interface{}) error {
   103  	client := meta.(*triton.Client)
   104  
   105  	vlan, err := client.Fabrics().UpdateFabricVLAN(&triton.UpdateFabricVLANInput{
   106  		ID:          d.Get("vlan_id").(int),
   107  		Name:        d.Get("name").(string),
   108  		Description: d.Get("description").(string),
   109  	})
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	d.SetId(strconv.Itoa(vlan.ID))
   115  	return resourceVLANRead(d, meta)
   116  }
   117  
   118  func resourceVLANDelete(d *schema.ResourceData, meta interface{}) error {
   119  	client := meta.(*triton.Client)
   120  
   121  	id, err := resourceVLANIDInt(d.Id())
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	return client.Fabrics().DeleteFabricVLAN(&triton.DeleteFabricVLANInput{
   127  		ID: id,
   128  	})
   129  }
   130  
   131  func resourceVLANIDInt(id string) (int, error) {
   132  	result, err := strconv.ParseInt(id, 10, 32)
   133  	if err != nil {
   134  		return -1, err
   135  	}
   136  
   137  	return int(result), nil
   138  }