|
| 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 | + "ipaddress": &schema.Schema{ |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + ForceNew: true, |
| 24 | + }, |
| 25 | + |
| 26 | + "network": &schema.Schema{ |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Computed: true, |
| 30 | + ForceNew: true, |
| 31 | + }, |
| 32 | + |
| 33 | + "virtual_machine": &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 | + // Retrieve the ipaddress ID |
| 53 | + ipaddressid, e := retrieveID(cs, "ipaddress", d.Get("ipaddress").(string)) |
| 54 | + if e != nil { |
| 55 | + return e.Error() |
| 56 | + } |
| 57 | + |
| 58 | + // Retrieve the virtual_machine ID |
| 59 | + virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string)) |
| 60 | + if e != nil { |
| 61 | + return e.Error() |
| 62 | + } |
| 63 | + |
| 64 | + // Create a new parameter struct |
| 65 | + p := cs.NAT.NewEnableStaticNatParams(ipaddressid, virtualmachineid) |
| 66 | + |
| 67 | + if network, ok := d.GetOk("network"); ok { |
| 68 | + // Retrieve the network ID |
| 69 | + networkid, e := retrieveID(cs, "network", network.(string)) |
| 70 | + if e != nil { |
| 71 | + return e.Error() |
| 72 | + } |
| 73 | + |
| 74 | + p.SetNetworkid(networkid) |
| 75 | + } |
| 76 | + |
| 77 | + if vmGuestIP, ok := d.GetOk("vm_guest_ip"); ok { |
| 78 | + p.SetVmguestip(vmGuestIP.(string)) |
| 79 | + } |
| 80 | + |
| 81 | + _, err := cs.NAT.EnableStaticNat(p) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("Error enabling static NAT: %s", err) |
| 84 | + } |
| 85 | + |
| 86 | + d.SetId(ipaddressid) |
| 87 | + |
| 88 | + return resourceCloudStackStaticNATRead(d, meta) |
| 89 | +} |
| 90 | + |
| 91 | +func resourceCloudStackStaticNATExists(d *schema.ResourceData, meta interface{}) (bool, error) { |
| 92 | + cs := meta.(*cloudstack.CloudStackClient) |
| 93 | + |
| 94 | + // Get the IP address details |
| 95 | + ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id()) |
| 96 | + if err != nil { |
| 97 | + if count == 0 { |
| 98 | + log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id()) |
| 99 | + return false, nil |
| 100 | + } |
| 101 | + |
| 102 | + return false, err |
| 103 | + } |
| 104 | + |
| 105 | + return ip.Isstaticnat, nil |
| 106 | +} |
| 107 | + |
| 108 | +func resourceCloudStackStaticNATRead(d *schema.ResourceData, meta interface{}) error { |
| 109 | + cs := meta.(*cloudstack.CloudStackClient) |
| 110 | + |
| 111 | + // Get the IP address details |
| 112 | + ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id()) |
| 113 | + if err != nil { |
| 114 | + if count == 0 { |
| 115 | + log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id()) |
| 116 | + d.SetId("") |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + if !ip.Isstaticnat { |
| 124 | + log.Printf("[DEBUG] Static NAT is no longer enabled for IP address with ID %s", d.Id()) |
| 125 | + d.SetId("") |
| 126 | + return nil |
| 127 | + } |
| 128 | + |
| 129 | + setValueOrID(d, "network", ip.Associatednetworkname, ip.Associatednetworkid) |
| 130 | + setValueOrID(d, "virtual_machine", ip.Virtualmachinename, ip.Virtualmachineid) |
| 131 | + d.Set("vm_guest_ip", ip.Vmipaddress) |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func resourceCloudStackStaticNATDelete(d *schema.ResourceData, meta interface{}) error { |
| 137 | + cs := meta.(*cloudstack.CloudStackClient) |
| 138 | + |
| 139 | + // Create a new parameter struct |
| 140 | + p := cs.NAT.NewDisableStaticNatParams(d.Id()) |
| 141 | + |
| 142 | + // Disable static NAT |
| 143 | + _, err := cs.NAT.DisableStaticNat(p) |
| 144 | + if err != nil { |
| 145 | + // This is a very poor way to be told the ID does no longer exist :( |
| 146 | + if strings.Contains(err.Error(), fmt.Sprintf( |
| 147 | + "Invalid parameter id value=%s due to incorrect long value format, "+ |
| 148 | + "or entity does not exist", d.Id())) { |
| 149 | + return nil |
| 150 | + } |
| 151 | + |
| 152 | + return fmt.Errorf("Error disabling static NAT: %s", err) |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
0 commit comments