github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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": &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 // Retrieve the VPC ID 37 vpcid, e := retrieveID(cs, "vpc", d.Get("vpc").(string)) 38 if e != nil { 39 return e.Error() 40 } 41 42 // Create a new parameter struct 43 p := cs.VPN.NewCreateVpnGatewayParams(vpcid) 44 45 // Create the new VPN Gateway 46 v, err := cs.VPN.CreateVpnGateway(p) 47 if err != nil { 48 return fmt.Errorf("Error creating VPN Gateway for VPC %s: %s", d.Get("vpc").(string), err) 49 } 50 51 d.SetId(v.Id) 52 53 return resourceCloudStackVPNGatewayRead(d, meta) 54 } 55 56 func resourceCloudStackVPNGatewayRead(d *schema.ResourceData, meta interface{}) error { 57 cs := meta.(*cloudstack.CloudStackClient) 58 59 // Get the VPN Gateway details 60 v, count, err := cs.VPN.GetVpnGatewayByID(d.Id()) 61 if err != nil { 62 if count == 0 { 63 log.Printf( 64 "[DEBUG] VPN Gateway for VPC %s does no longer exist", d.Get("vpc").(string)) 65 d.SetId("") 66 return nil 67 } 68 69 return err 70 } 71 72 setValueOrID(d, "vpc", d.Get("vpc").(string), v.Vpcid) 73 74 d.Set("public_ip", v.Publicip) 75 76 return nil 77 } 78 79 func resourceCloudStackVPNGatewayDelete(d *schema.ResourceData, meta interface{}) error { 80 cs := meta.(*cloudstack.CloudStackClient) 81 82 // Create a new parameter struct 83 p := cs.VPN.NewDeleteVpnGatewayParams(d.Id()) 84 85 // Delete the VPN Gateway 86 _, err := cs.VPN.DeleteVpnGateway(p) 87 if err != nil { 88 // This is a very poor way to be told the ID does no longer exist :( 89 if strings.Contains(err.Error(), fmt.Sprintf( 90 "Invalid parameter id value=%s due to incorrect long value format, "+ 91 "or entity does not exist", d.Id())) { 92 return nil 93 } 94 95 return fmt.Errorf("Error deleting VPN Gateway for VPC %s: %s", d.Get("vpc").(string), err) 96 } 97 98 return nil 99 }