2015-03-04 18:51:07 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackVPNGateway() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackVPNGatewayCreate,
|
|
|
|
Read: resourceCloudStackVPNGatewayRead,
|
|
|
|
Delete: resourceCloudStackVPNGatewayDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-11 17:14:19 +02:00
|
|
|
"vpc_id": &schema.Schema{
|
2015-03-04 18:51:07 +01:00
|
|
|
Type: schema.TypeString,
|
2016-06-25 19:42:01 +02:00
|
|
|
Required: true,
|
2015-03-04 18:51:07 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-03-09 14:00:29 +01:00
|
|
|
"public_ip": &schema.Schema{
|
2015-03-04 18:51:07 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPNGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2016-06-25 19:42:01 +02:00
|
|
|
vpcid := d.Get("vpc_id").(string)
|
2015-03-04 18:51:07 +01:00
|
|
|
p := cs.VPN.NewCreateVpnGatewayParams(vpcid)
|
|
|
|
|
|
|
|
// Create the new VPN Gateway
|
|
|
|
v, err := cs.VPN.CreateVpnGateway(p)
|
|
|
|
if err != nil {
|
2016-04-11 17:14:19 +02:00
|
|
|
return fmt.Errorf("Error creating VPN Gateway for VPC ID %s: %s", vpcid, err)
|
2015-03-04 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(v.Id)
|
|
|
|
|
|
|
|
return resourceCloudStackVPNGatewayRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPNGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Get the VPN Gateway details
|
|
|
|
v, count, err := cs.VPN.GetVpnGatewayByID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf(
|
2016-04-11 17:14:19 +02:00
|
|
|
"[DEBUG] VPN Gateway for VPC ID %s does no longer exist", d.Get("vpc_id").(string))
|
2015-03-04 18:51:07 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
d.Set("vpc_id", v.Vpcid)
|
2015-03-09 14:00:29 +01:00
|
|
|
d.Set("public_ip", v.Publicip)
|
2015-03-04 18:51:07 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPNGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.VPN.NewDeleteVpnGatewayParams(d.Id())
|
|
|
|
|
|
|
|
// Delete the VPN Gateway
|
|
|
|
_, err := cs.VPN.DeleteVpnGateway(p)
|
|
|
|
if err != nil {
|
2015-10-05 14:05:21 +02:00
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
2015-03-04 18:51:07 +01:00
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf(
|
|
|
|
"Invalid parameter id value=%s due to incorrect long value format, "+
|
|
|
|
"or entity does not exist", d.Id())) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
return fmt.Errorf("Error deleting VPN Gateway for VPC %s: %s", d.Get("vpc_id").(string), err)
|
2015-03-04 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|