github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/triton/resource_vlan.go (about)

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