github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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/gosdc/cloudapi" 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 19 Schema: map[string]*schema.Schema{ 20 "vlan_id": { 21 Description: "number between 0-4095 indicating VLAN ID", 22 Required: true, 23 ForceNew: true, 24 Type: schema.TypeInt, 25 ValidateFunc: func(val interface{}, field string) (warn []string, err []error) { 26 value := val.(int) 27 if value < 0 || value > 4095 { 28 err = append(err, errors.New("vlan_id must be between 0 and 4095")) 29 } 30 return 31 }, 32 }, 33 "name": { 34 Description: "Unique name to identify VLAN", 35 Required: true, 36 Type: schema.TypeString, 37 }, 38 "description": { 39 Description: "Optional description of the VLAN", 40 Optional: true, 41 Type: schema.TypeString, 42 }, 43 }, 44 } 45 } 46 47 func resourceVLANCreate(d *schema.ResourceData, meta interface{}) error { 48 client := meta.(*cloudapi.Client) 49 50 vlan, err := client.CreateFabricVLAN(cloudapi.FabricVLAN{ 51 Id: int16(d.Get("vlan_id").(int)), 52 Name: d.Get("name").(string), 53 Description: d.Get("description").(string), 54 }) 55 if err != nil { 56 return err 57 } 58 59 d.SetId(resourceVLANIDString(vlan.Id)) 60 return resourceVLANRead(d, meta) 61 } 62 63 func resourceVLANExists(d *schema.ResourceData, meta interface{}) (bool, error) { 64 client := meta.(*cloudapi.Client) 65 66 id, err := resourceVLANIDInt16(d.Id()) 67 if err != nil { 68 return false, err 69 } 70 71 vlan, err := client.GetFabricVLAN(id) 72 73 return vlan != nil && err == nil, err 74 } 75 76 func resourceVLANRead(d *schema.ResourceData, meta interface{}) error { 77 client := meta.(*cloudapi.Client) 78 79 vlan, err := client.GetFabricVLAN(int16(d.Get("vlan_id").(int))) 80 if err != nil { 81 return err 82 } 83 84 d.SetId(resourceVLANIDString(vlan.Id)) 85 d.Set("vlan_id", vlan.Id) 86 d.Set("name", vlan.Name) 87 d.Set("description", vlan.Description) 88 89 return nil 90 } 91 92 func resourceVLANUpdate(d *schema.ResourceData, meta interface{}) error { 93 client := meta.(*cloudapi.Client) 94 95 vlan, err := client.UpdateFabricVLAN(cloudapi.FabricVLAN{ 96 Id: int16(d.Get("vlan_id").(int)), 97 Name: d.Get("name").(string), 98 Description: d.Get("description").(string), 99 }) 100 if err != nil { 101 return err 102 } 103 104 d.SetId(resourceVLANIDString(vlan.Id)) 105 return resourceVLANRead(d, meta) 106 } 107 108 func resourceVLANDelete(d *schema.ResourceData, meta interface{}) error { 109 client := meta.(*cloudapi.Client) 110 111 id, err := resourceVLANIDInt16(d.Id()) 112 if err != nil { 113 return err 114 } 115 116 return client.DeleteFabricVLAN(id) 117 } 118 119 // convenience conversion functions 120 121 func resourceVLANIDString(id int16) string { 122 return strconv.Itoa(int(id)) 123 } 124 125 func resourceVLANIDInt16(id string) (int16, error) { 126 result, err := strconv.ParseInt(id, 10, 16) 127 if err != nil { 128 return 0, err 129 } 130 131 return int16(result), nil 132 }