github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/cloudstack/resource_cloudstack_vpn_connection.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 resourceCloudStackVPNConnection() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackVPNConnectionCreate,
    15  		Read:   resourceCloudStackVPNConnectionRead,
    16  		Delete: resourceCloudStackVPNConnectionDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"customer_gateway_id": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"vpn_gateway_id": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func resourceCloudStackVPNConnectionCreate(d *schema.ResourceData, meta interface{}) error {
    35  	cs := meta.(*cloudstack.CloudStackClient)
    36  
    37  	// Create a new parameter struct
    38  	p := cs.VPN.NewCreateVpnConnectionParams(
    39  		d.Get("customer_gateway_id").(string),
    40  		d.Get("vpn_gateway_id").(string),
    41  	)
    42  
    43  	// Create the new VPN Connection
    44  	v, err := cs.VPN.CreateVpnConnection(p)
    45  	if err != nil {
    46  		return fmt.Errorf("Error creating VPN Connection: %s", err)
    47  	}
    48  
    49  	d.SetId(v.Id)
    50  
    51  	return resourceCloudStackVPNConnectionRead(d, meta)
    52  }
    53  
    54  func resourceCloudStackVPNConnectionRead(d *schema.ResourceData, meta interface{}) error {
    55  	cs := meta.(*cloudstack.CloudStackClient)
    56  
    57  	// Get the VPN Connection details
    58  	v, count, err := cs.VPN.GetVpnConnectionByID(d.Id())
    59  	if err != nil {
    60  		if count == 0 {
    61  			log.Printf("[DEBUG] VPN Connection does no longer exist")
    62  			d.SetId("")
    63  			return nil
    64  		}
    65  
    66  		return err
    67  	}
    68  
    69  	d.Set("customer_gateway_id", v.S2scustomergatewayid)
    70  	d.Set("vpn_gateway_id", v.S2svpngatewayid)
    71  
    72  	return nil
    73  }
    74  
    75  func resourceCloudStackVPNConnectionDelete(d *schema.ResourceData, meta interface{}) error {
    76  	cs := meta.(*cloudstack.CloudStackClient)
    77  
    78  	// Create a new parameter struct
    79  	p := cs.VPN.NewDeleteVpnConnectionParams(d.Id())
    80  
    81  	// Delete the VPN Connection
    82  	_, err := cs.VPN.DeleteVpnConnection(p)
    83  	if err != nil {
    84  		// This is a very poor way to be told the ID does no longer exist :(
    85  		if strings.Contains(err.Error(), fmt.Sprintf(
    86  			"Invalid parameter id value=%s due to incorrect long value format, "+
    87  				"or entity does not exist", d.Id())) {
    88  			return nil
    89  		}
    90  
    91  		return fmt.Errorf("Error deleting VPN Connection: %s", err)
    92  	}
    93  
    94  	return nil
    95  }