github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/triton/resource_fabric.go (about) 1 package triton 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "github.com/joyent/triton-go" 9 ) 10 11 func resourceFabric() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceFabricCreate, 14 Exists: resourceFabricExists, 15 Read: resourceFabricRead, 16 Delete: resourceFabricDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": { 20 Description: "Network name", 21 Required: true, 22 ForceNew: true, 23 Type: schema.TypeString, 24 }, 25 "public": { 26 Description: "Whether or not this is an RFC1918 network", 27 Computed: true, 28 Type: schema.TypeBool, 29 }, 30 "fabric": { 31 Description: "Whether or not this network is on a fabric", 32 Computed: true, 33 Type: schema.TypeBool, 34 }, 35 "description": { 36 Description: "Description of network", 37 Optional: true, 38 ForceNew: true, 39 Type: schema.TypeString, 40 }, 41 "subnet": { 42 Description: "CIDR formatted string describing network address space", 43 Required: true, 44 ForceNew: true, 45 Type: schema.TypeString, 46 }, 47 "provision_start_ip": { 48 Description: "First IP on the network that can be assigned", 49 Required: true, 50 ForceNew: true, 51 Type: schema.TypeString, 52 }, 53 "provision_end_ip": { 54 Description: "Last assignable IP on the network", 55 Required: true, 56 ForceNew: true, 57 Type: schema.TypeString, 58 }, 59 "gateway": { 60 Description: "Gateway IP", 61 Optional: true, 62 ForceNew: true, 63 Type: schema.TypeString, 64 }, 65 "resolvers": { 66 Description: "List of IP addresses for DNS resolvers", 67 Optional: true, 68 Computed: true, 69 Type: schema.TypeList, 70 Elem: &schema.Schema{Type: schema.TypeString}, 71 }, 72 "routes": { 73 Description: "Map of CIDR block to Gateway IP address", 74 Computed: true, 75 Optional: true, 76 ForceNew: true, 77 Type: schema.TypeMap, 78 }, 79 "internet_nat": { 80 Description: "Whether or not a NAT zone is provisioned at the Gateway IP address", 81 Computed: true, 82 Optional: true, 83 ForceNew: true, 84 Type: schema.TypeBool, 85 }, 86 "vlan_id": { 87 Description: "VLAN on which the network exists", 88 Required: true, 89 ForceNew: true, 90 Type: schema.TypeInt, 91 }, 92 }, 93 } 94 } 95 96 func resourceFabricCreate(d *schema.ResourceData, meta interface{}) error { 97 client := meta.(*triton.Client) 98 99 var resolvers []string 100 for _, resolver := range d.Get("resolvers").([]interface{}) { 101 resolvers = append(resolvers, resolver.(string)) 102 } 103 104 routes := map[string]string{} 105 for cidr, v := range d.Get("routes").(map[string]interface{}) { 106 ip, ok := v.(string) 107 if !ok { 108 return fmt.Errorf(`Cannot use "%v" as an IP address`, v) 109 } 110 routes[cidr] = ip 111 } 112 113 fabric, err := client.Fabrics().CreateFabricNetwork(context.Background(), &triton.CreateFabricNetworkInput{ 114 FabricVLANID: d.Get("vlan_id").(int), 115 Name: d.Get("name").(string), 116 Description: d.Get("description").(string), 117 Subnet: d.Get("subnet").(string), 118 ProvisionStartIP: d.Get("provision_start_ip").(string), 119 ProvisionEndIP: d.Get("provision_end_ip").(string), 120 Gateway: d.Get("gateway").(string), 121 Resolvers: resolvers, 122 Routes: routes, 123 InternetNAT: d.Get("internet_nat").(bool), 124 }, 125 ) 126 if err != nil { 127 return err 128 } 129 130 d.SetId(fabric.Id) 131 132 return resourceFabricRead(d, meta) 133 } 134 135 func resourceFabricExists(d *schema.ResourceData, meta interface{}) (bool, error) { 136 client := meta.(*triton.Client) 137 138 return resourceExists(client.Fabrics().GetFabricNetwork(context.Background(), &triton.GetFabricNetworkInput{ 139 FabricVLANID: d.Get("vlan_id").(int), 140 NetworkID: d.Id(), 141 })) 142 } 143 144 func resourceFabricRead(d *schema.ResourceData, meta interface{}) error { 145 client := meta.(*triton.Client) 146 147 fabric, err := client.Fabrics().GetFabricNetwork(context.Background(), &triton.GetFabricNetworkInput{ 148 FabricVLANID: d.Get("vlan_id").(int), 149 NetworkID: d.Id(), 150 }) 151 if err != nil { 152 return err 153 } 154 155 d.SetId(fabric.Id) 156 d.Set("name", fabric.Name) 157 d.Set("public", fabric.Public) 158 d.Set("fabric", fabric.Fabric) 159 d.Set("description", fabric.Description) 160 d.Set("subnet", fabric.Subnet) 161 d.Set("provision_start_ip", fabric.ProvisioningStartIP) 162 d.Set("provision_end_ip", fabric.ProvisioningEndIP) 163 d.Set("gateway", fabric.Gateway) 164 d.Set("resolvers", fabric.Resolvers) 165 d.Set("routes", fabric.Routes) 166 d.Set("internet_nat", fabric.InternetNAT) 167 d.Set("vlan_id", d.Get("vlan_id").(int)) 168 169 return nil 170 } 171 172 func resourceFabricDelete(d *schema.ResourceData, meta interface{}) error { 173 client := meta.(*triton.Client) 174 175 return client.Fabrics().DeleteFabricNetwork(context.Background(), &triton.DeleteFabricNetworkInput{ 176 FabricVLANID: d.Get("vlan_id").(int), 177 NetworkID: d.Id(), 178 }) 179 }