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