github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/triton/resource_fabric.go (about)

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