github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_private_gateway.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func resourceCloudStackPrivateGateway() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackPrivateGatewayCreate,
    15  		Read:   resourceCloudStackPrivateGatewayRead,
    16  		Update: resourceCloudStackPrivateGatewayUpdate,
    17  		Delete: resourceCloudStackPrivateGatewayDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"gateway": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"ip_address": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"netmask": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"vlan": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Required: true,
    41  				ForceNew: true,
    42  			},
    43  
    44  			"physical_network_id": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Optional: true,
    47  				ForceNew: true,
    48  			},
    49  
    50  			"network_offering": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				ForceNew: true,
    54  			},
    55  
    56  			"acl_id": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Required: true,
    59  			},
    60  
    61  			"vpc_id": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Required: true,
    64  				ForceNew: true,
    65  			},
    66  		},
    67  	}
    68  }
    69  
    70  func resourceCloudStackPrivateGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    71  	cs := meta.(*cloudstack.CloudStackClient)
    72  
    73  	ipaddress := d.Get("ip_address").(string)
    74  	networkofferingid := d.Get("network_offering").(string)
    75  
    76  	// Create a new parameter struct
    77  	p := cs.VPC.NewCreatePrivateGatewayParams(
    78  		d.Get("gateway").(string),
    79  		ipaddress,
    80  		d.Get("netmask").(string),
    81  		d.Get("vlan").(string),
    82  		d.Get("vpc_id").(string),
    83  	)
    84  
    85  	// Retrieve the network_offering ID
    86  	if networkofferingid != "" {
    87  		networkofferingid, e := retrieveID(cs, "network_offering", networkofferingid)
    88  		if e != nil {
    89  			return e.Error()
    90  		}
    91  		p.SetNetworkofferingid(networkofferingid)
    92  	}
    93  
    94  	// Check if we want to associate an ACL
    95  	if aclid, ok := d.GetOk("acl_id"); ok {
    96  		// Set the acl ID
    97  		p.SetAclid(aclid.(string))
    98  	}
    99  
   100  	// Create the new private gateway
   101  	r, err := cs.VPC.CreatePrivateGateway(p)
   102  	if err != nil {
   103  		return fmt.Errorf("Error creating private gateway for %s: %s", ipaddress, err)
   104  	}
   105  
   106  	d.SetId(r.Id)
   107  
   108  	return resourceCloudStackPrivateGatewayRead(d, meta)
   109  }
   110  
   111  func resourceCloudStackPrivateGatewayRead(d *schema.ResourceData, meta interface{}) error {
   112  	cs := meta.(*cloudstack.CloudStackClient)
   113  
   114  	// Get the private gateway details
   115  	gw, count, err := cs.VPC.GetPrivateGatewayByID(d.Id())
   116  	if err != nil {
   117  		if count == 0 {
   118  			log.Printf("[DEBUG] Private gateway %s does no longer exist", d.Id())
   119  			d.SetId("")
   120  			return nil
   121  		}
   122  
   123  		return err
   124  	}
   125  
   126  	d.Set("gateway", gw.Gateway)
   127  	d.Set("ip_address", gw.Ipaddress)
   128  	d.Set("netmask", gw.Netmask)
   129  	d.Set("vlan", gw.Vlan)
   130  	d.Set("acl_id", gw.Aclid)
   131  	d.Set("vpc_id", gw.Vpcid)
   132  
   133  	return nil
   134  }
   135  
   136  func resourceCloudStackPrivateGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
   137  	cs := meta.(*cloudstack.CloudStackClient)
   138  
   139  	// Replace the ACL if the ID has changed
   140  	if d.HasChange("acl_id") {
   141  		p := cs.NetworkACL.NewReplaceNetworkACLListParams(d.Get("acl_id").(string))
   142  		p.SetNetworkid(d.Id())
   143  
   144  		_, err := cs.NetworkACL.ReplaceNetworkACLList(p)
   145  		if err != nil {
   146  			return fmt.Errorf("Error replacing ACL: %s", err)
   147  		}
   148  	}
   149  
   150  	return resourceCloudStackNetworkRead(d, meta)
   151  }
   152  
   153  func resourceCloudStackPrivateGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   154  	cs := meta.(*cloudstack.CloudStackClient)
   155  
   156  	// Create a new parameter struct
   157  	p := cs.VPC.NewDeletePrivateGatewayParams(d.Id())
   158  
   159  	// Delete the private gateway
   160  	_, err := cs.VPC.DeletePrivateGateway(p)
   161  	if err != nil {
   162  		// This is a very poor way to be told the ID does no longer exist :(
   163  		if strings.Contains(err.Error(), fmt.Sprintf(
   164  			"Invalid parameter id value=%s due to incorrect long value format, "+
   165  				"or entity does not exist", d.Id())) {
   166  			return nil
   167  		}
   168  
   169  		return fmt.Errorf("Error deleting private gateway %s: %s", d.Id(), err)
   170  	}
   171  
   172  	return nil
   173  }