2016-03-30 00:13:03 +02:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2017-05-10 23:54:57 +02:00
|
|
|
"context"
|
2016-03-30 00:13:03 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-03-31 00:25:27 +02:00
|
|
|
"github.com/joyent/triton-go"
|
2016-03-30 00:13:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceFabric() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceFabricCreate,
|
|
|
|
Exists: resourceFabricExists,
|
|
|
|
Read: resourceFabricRead,
|
|
|
|
Delete: resourceFabricDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Network name",
|
2016-03-30 00:13:03 +02:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"public": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Whether or not this is an RFC1918 network",
|
2016-03-30 00:13:03 +02:00
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
},
|
|
|
|
"fabric": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Whether or not this network is on a fabric",
|
2016-03-30 00:13:03 +02:00
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
},
|
|
|
|
"description": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Description of network",
|
2016-03-30 00:13:03 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"subnet": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "CIDR formatted string describing network address space",
|
2016-03-30 00:13:03 +02:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"provision_start_ip": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "First IP on the network that can be assigned",
|
2016-03-30 00:13:03 +02:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"provision_end_ip": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Last assignable IP on the network",
|
2016-03-30 00:13:03 +02:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"gateway": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Gateway IP",
|
2016-03-30 00:13:03 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"resolvers": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "List of IP addresses for DNS resolvers",
|
2016-03-30 00:13:03 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
"routes": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Map of CIDR block to Gateway IP address",
|
2016-03-30 00:13:03 +02:00
|
|
|
Computed: true,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
},
|
|
|
|
"internet_nat": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Whether or not a NAT zone is provisioned at the Gateway IP address",
|
2016-03-30 00:13:03 +02:00
|
|
|
Computed: true,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
},
|
|
|
|
"vlan_id": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "VLAN on which the network exists",
|
2016-03-30 00:13:03 +02:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFabricCreate(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-03-30 00:13:03 +02:00
|
|
|
|
|
|
|
var resolvers []string
|
|
|
|
for _, resolver := range d.Get("resolvers").([]interface{}) {
|
|
|
|
resolvers = append(resolvers, resolver.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
routes := map[string]string{}
|
|
|
|
for cidr, v := range d.Get("routes").(map[string]interface{}) {
|
|
|
|
ip, ok := v.(string)
|
|
|
|
if !ok {
|
2017-03-31 00:25:27 +02:00
|
|
|
return fmt.Errorf(`Cannot use "%v" as an IP address`, v)
|
2016-03-30 00:13:03 +02:00
|
|
|
}
|
|
|
|
routes[cidr] = ip
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
fabric, err := client.Fabrics().CreateFabricNetwork(context.Background(), &triton.CreateFabricNetworkInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
FabricVLANID: d.Get("vlan_id").(int),
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
Subnet: d.Get("subnet").(string),
|
|
|
|
ProvisionStartIP: d.Get("provision_start_ip").(string),
|
|
|
|
ProvisionEndIP: d.Get("provision_end_ip").(string),
|
|
|
|
Gateway: d.Get("gateway").(string),
|
|
|
|
Resolvers: resolvers,
|
|
|
|
Routes: routes,
|
|
|
|
InternetNAT: d.Get("internet_nat").(bool),
|
|
|
|
},
|
2016-03-30 00:13:03 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(fabric.Id)
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return resourceFabricRead(d, meta)
|
2016-03-30 00:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFabricExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-03-30 00:13:03 +02:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
return resourceExists(client.Fabrics().GetFabricNetwork(context.Background(), &triton.GetFabricNetworkInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
FabricVLANID: d.Get("vlan_id").(int),
|
|
|
|
NetworkID: d.Id(),
|
|
|
|
}))
|
2016-03-30 00:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFabricRead(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-03-30 00:13:03 +02:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
fabric, err := client.Fabrics().GetFabricNetwork(context.Background(), &triton.GetFabricNetworkInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
FabricVLANID: d.Get("vlan_id").(int),
|
|
|
|
NetworkID: d.Id(),
|
|
|
|
})
|
2016-03-30 00:13:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(fabric.Id)
|
|
|
|
d.Set("name", fabric.Name)
|
|
|
|
d.Set("public", fabric.Public)
|
|
|
|
d.Set("fabric", fabric.Fabric)
|
|
|
|
d.Set("description", fabric.Description)
|
|
|
|
d.Set("subnet", fabric.Subnet)
|
2017-03-31 00:25:27 +02:00
|
|
|
d.Set("provision_start_ip", fabric.ProvisioningStartIP)
|
|
|
|
d.Set("provision_end_ip", fabric.ProvisioningEndIP)
|
2016-03-30 00:13:03 +02:00
|
|
|
d.Set("gateway", fabric.Gateway)
|
|
|
|
d.Set("resolvers", fabric.Resolvers)
|
|
|
|
d.Set("routes", fabric.Routes)
|
|
|
|
d.Set("internet_nat", fabric.InternetNAT)
|
2017-03-31 00:25:27 +02:00
|
|
|
d.Set("vlan_id", d.Get("vlan_id").(int))
|
2016-03-30 00:13:03 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFabricDelete(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-03-30 00:13:03 +02:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
return client.Fabrics().DeleteFabricNetwork(context.Background(), &triton.DeleteFabricNetworkInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
FabricVLANID: d.Get("vlan_id").(int),
|
|
|
|
NetworkID: d.Id(),
|
|
|
|
})
|
2016-03-30 00:13:03 +02:00
|
|
|
}
|