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