github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/cloudstack/resource_cloudstack_static_nat.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 resourceCloudStackStaticNAT() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceCloudStackStaticNATCreate, 15 Exists: resourceCloudStackStaticNATExists, 16 Read: resourceCloudStackStaticNATRead, 17 Delete: resourceCloudStackStaticNATDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "ip_address_id": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "network_id": &schema.Schema{ 27 Type: schema.TypeString, 28 Optional: true, 29 Computed: true, 30 ForceNew: true, 31 }, 32 33 "virtual_machine_id": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 ForceNew: true, 37 }, 38 39 "vm_guest_ip": &schema.Schema{ 40 Type: schema.TypeString, 41 Optional: true, 42 Computed: true, 43 ForceNew: true, 44 }, 45 }, 46 } 47 } 48 49 func resourceCloudStackStaticNATCreate(d *schema.ResourceData, meta interface{}) error { 50 cs := meta.(*cloudstack.CloudStackClient) 51 52 ipaddressid := d.Get("ip_address_id").(string) 53 virtualmachineid := d.Get("virtual_machine_id").(string) 54 55 // Create a new parameter struct 56 p := cs.NAT.NewEnableStaticNatParams(ipaddressid, virtualmachineid) 57 58 if networkid, ok := d.GetOk("network_id"); ok { 59 p.SetNetworkid(networkid.(string)) 60 } 61 62 if vmGuestIP, ok := d.GetOk("vm_guest_ip"); ok { 63 p.SetVmguestip(vmGuestIP.(string)) 64 } 65 66 _, err := cs.NAT.EnableStaticNat(p) 67 if err != nil { 68 return fmt.Errorf("Error enabling static NAT: %s", err) 69 } 70 71 d.SetId(ipaddressid) 72 73 return resourceCloudStackStaticNATRead(d, meta) 74 } 75 76 func resourceCloudStackStaticNATExists(d *schema.ResourceData, meta interface{}) (bool, error) { 77 cs := meta.(*cloudstack.CloudStackClient) 78 79 // Get the IP address details 80 ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id()) 81 if err != nil { 82 if count == 0 { 83 log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id()) 84 return false, nil 85 } 86 87 return false, err 88 } 89 90 return ip.Isstaticnat, nil 91 } 92 93 func resourceCloudStackStaticNATRead(d *schema.ResourceData, meta interface{}) error { 94 cs := meta.(*cloudstack.CloudStackClient) 95 96 // Get the IP address details 97 ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id()) 98 if err != nil { 99 if count == 0 { 100 log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id()) 101 d.SetId("") 102 return nil 103 } 104 105 return err 106 } 107 108 if !ip.Isstaticnat { 109 log.Printf("[DEBUG] Static NAT is no longer enabled for IP address with ID %s", d.Id()) 110 d.SetId("") 111 return nil 112 } 113 114 d.Set("network_id", ip.Associatednetworkid) 115 d.Set("virtual_machine_id", ip.Virtualmachineid) 116 d.Set("vm_guest_ip", ip.Vmipaddress) 117 118 return nil 119 } 120 121 func resourceCloudStackStaticNATDelete(d *schema.ResourceData, meta interface{}) error { 122 cs := meta.(*cloudstack.CloudStackClient) 123 124 // Create a new parameter struct 125 p := cs.NAT.NewDisableStaticNatParams(d.Id()) 126 127 // Disable static NAT 128 _, err := cs.NAT.DisableStaticNat(p) 129 if err != nil { 130 // This is a very poor way to be told the ID does no longer exist :( 131 if strings.Contains(err.Error(), fmt.Sprintf( 132 "Invalid parameter id value=%s due to incorrect long value format, "+ 133 "or entity does not exist", d.Id())) { 134 return nil 135 } 136 137 return fmt.Errorf("Error disabling static NAT: %s", err) 138 } 139 140 return nil 141 }