github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_vpn_customer_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 resourceCloudStackVPNCustomerGateway() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackVPNCustomerGatewayCreate,
    15  		Read:   resourceCloudStackVPNCustomerGatewayRead,
    16  		Update: resourceCloudStackVPNCustomerGatewayUpdate,
    17  		Delete: resourceCloudStackVPNCustomerGatewayDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  
    25  			"cidr": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  
    30  			"esp_policy": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  			},
    34  
    35  			"gateway": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  			},
    39  
    40  			"ike_policy": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  			},
    44  
    45  			"ipsec_psk": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Required: true,
    48  			},
    49  
    50  			"dpd": &schema.Schema{
    51  				Type:     schema.TypeBool,
    52  				Optional: true,
    53  				Computed: true,
    54  			},
    55  
    56  			"esp_lifetime": &schema.Schema{
    57  				Type:     schema.TypeInt,
    58  				Optional: true,
    59  				Computed: true,
    60  			},
    61  
    62  			"ike_lifetime": &schema.Schema{
    63  				Type:     schema.TypeInt,
    64  				Optional: true,
    65  				Computed: true,
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  func resourceCloudStackVPNCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    72  	cs := meta.(*cloudstack.CloudStackClient)
    73  
    74  	// Create a new parameter struct
    75  	p := cs.VPN.NewCreateVpnCustomerGatewayParams(
    76  		d.Get("cidr").(string),
    77  		d.Get("esp_policy").(string),
    78  		d.Get("gateway").(string),
    79  		d.Get("ike_policy").(string),
    80  		d.Get("ipsec_psk").(string),
    81  	)
    82  
    83  	p.SetName(d.Get("name").(string))
    84  
    85  	if dpd, ok := d.GetOk("dpd"); ok {
    86  		p.SetDpd(dpd.(bool))
    87  	}
    88  
    89  	if esplifetime, ok := d.GetOk("esp_lifetime"); ok {
    90  		p.SetEsplifetime(int64(esplifetime.(int)))
    91  	}
    92  
    93  	if ikelifetime, ok := d.GetOk("ike_lifetime"); ok {
    94  		p.SetIkelifetime(int64(ikelifetime.(int)))
    95  	}
    96  
    97  	// Create the new VPN Customer Gateway
    98  	v, err := cs.VPN.CreateVpnCustomerGateway(p)
    99  	if err != nil {
   100  		return fmt.Errorf("Error creating VPN Customer Gateway %s: %s", d.Get("name").(string), err)
   101  	}
   102  
   103  	d.SetId(v.Id)
   104  
   105  	return resourceCloudStackVPNCustomerGatewayRead(d, meta)
   106  }
   107  
   108  func resourceCloudStackVPNCustomerGatewayRead(d *schema.ResourceData, meta interface{}) error {
   109  	cs := meta.(*cloudstack.CloudStackClient)
   110  
   111  	// Get the VPN Customer Gateway details
   112  	v, count, err := cs.VPN.GetVpnCustomerGatewayByID(d.Id())
   113  	if err != nil {
   114  		if count == 0 {
   115  			log.Printf(
   116  				"[DEBUG] VPN Customer Gateway %s does no longer exist", d.Get("name").(string))
   117  			d.SetId("")
   118  			return nil
   119  		}
   120  
   121  		return err
   122  	}
   123  
   124  	d.Set("name", v.Name)
   125  	d.Set("cidr", v.Cidrlist)
   126  	d.Set("esp_policy", v.Esppolicy)
   127  	d.Set("gateway", v.Gateway)
   128  	d.Set("ike_policy", v.Ikepolicy)
   129  	d.Set("ipsec_psk", v.Ipsecpsk)
   130  	d.Set("dpd", v.Dpd)
   131  	d.Set("esp_lifetime", int(v.Esplifetime))
   132  	d.Set("ike_lifetime", int(v.Ikelifetime))
   133  
   134  	return nil
   135  }
   136  
   137  func resourceCloudStackVPNCustomerGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
   138  	cs := meta.(*cloudstack.CloudStackClient)
   139  
   140  	// Create a new parameter struct
   141  	p := cs.VPN.NewUpdateVpnCustomerGatewayParams(
   142  		d.Get("cidr").(string),
   143  		d.Get("esp_policy").(string),
   144  		d.Get("gateway").(string),
   145  		d.Id(),
   146  		d.Get("ike_policy").(string),
   147  		d.Get("ipsec_psk").(string),
   148  	)
   149  
   150  	p.SetName(d.Get("name").(string))
   151  
   152  	if dpd, ok := d.GetOk("dpd"); ok {
   153  		p.SetDpd(dpd.(bool))
   154  	}
   155  
   156  	if esplifetime, ok := d.GetOk("esp_lifetime"); ok {
   157  		p.SetEsplifetime(int64(esplifetime.(int)))
   158  	}
   159  
   160  	if ikelifetime, ok := d.GetOk("ike_lifetime"); ok {
   161  		p.SetIkelifetime(int64(ikelifetime.(int)))
   162  	}
   163  
   164  	// Update the VPN Customer Gateway
   165  	_, err := cs.VPN.UpdateVpnCustomerGateway(p)
   166  	if err != nil {
   167  		return fmt.Errorf("Error updating VPN Customer Gateway %s: %s", d.Get("name").(string), err)
   168  	}
   169  
   170  	return resourceCloudStackVPNCustomerGatewayRead(d, meta)
   171  }
   172  
   173  func resourceCloudStackVPNCustomerGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   174  	cs := meta.(*cloudstack.CloudStackClient)
   175  
   176  	// Create a new parameter struct
   177  	p := cs.VPN.NewDeleteVpnCustomerGatewayParams(d.Id())
   178  
   179  	// Delete the VPN Customer Gateway
   180  	_, err := cs.VPN.DeleteVpnCustomerGateway(p)
   181  	if err != nil {
   182  		// This is a very poor way to be told the ID does no longer exist :(
   183  		if strings.Contains(err.Error(), fmt.Sprintf(
   184  			"Invalid parameter id value=%s due to incorrect long value format, "+
   185  				"or entity does not exist", d.Id())) {
   186  			return nil
   187  		}
   188  
   189  		return fmt.Errorf("Error deleting VPN Customer Gateway %s: %s", d.Get("name").(string), err)
   190  	}
   191  
   192  	return nil
   193  }