github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 "project": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 Computed: true, 50 ForceNew: true, 51 }, 52 }, 53 } 54 } 55 56 func resourceCloudStackStaticNATCreate(d *schema.ResourceData, meta interface{}) error { 57 cs := meta.(*cloudstack.CloudStackClient) 58 59 ipaddressid := d.Get("ip_address_id").(string) 60 virtualmachineid := d.Get("virtual_machine_id").(string) 61 62 // Create a new parameter struct 63 p := cs.NAT.NewEnableStaticNatParams(ipaddressid, virtualmachineid) 64 65 if networkid, ok := d.GetOk("network_id"); ok { 66 p.SetNetworkid(networkid.(string)) 67 } 68 69 if vmGuestIP, ok := d.GetOk("vm_guest_ip"); ok { 70 p.SetVmguestip(vmGuestIP.(string)) 71 } 72 73 _, err := cs.NAT.EnableStaticNat(p) 74 if err != nil { 75 return fmt.Errorf("Error enabling static NAT: %s", err) 76 } 77 78 d.SetId(ipaddressid) 79 80 return resourceCloudStackStaticNATRead(d, meta) 81 } 82 83 func resourceCloudStackStaticNATExists(d *schema.ResourceData, meta interface{}) (bool, error) { 84 cs := meta.(*cloudstack.CloudStackClient) 85 86 // Get the IP address details 87 ip, count, err := cs.Address.GetPublicIpAddressByID( 88 d.Id(), 89 cloudstack.WithProject(d.Get("project").(string)), 90 ) 91 if err != nil { 92 if count == 0 { 93 log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id()) 94 return false, nil 95 } 96 97 return false, err 98 } 99 100 return ip.Isstaticnat, nil 101 } 102 103 func resourceCloudStackStaticNATRead(d *schema.ResourceData, meta interface{}) error { 104 cs := meta.(*cloudstack.CloudStackClient) 105 106 // Get the IP address details 107 ip, count, err := cs.Address.GetPublicIpAddressByID( 108 d.Id(), 109 cloudstack.WithProject(d.Get("project").(string)), 110 ) 111 if err != nil { 112 if count == 0 { 113 log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id()) 114 d.SetId("") 115 return nil 116 } 117 118 return err 119 } 120 121 if !ip.Isstaticnat { 122 log.Printf("[DEBUG] Static NAT is no longer enabled for IP address with ID %s", d.Id()) 123 d.SetId("") 124 return nil 125 } 126 127 d.Set("network_id", ip.Associatednetworkid) 128 d.Set("virtual_machine_id", ip.Virtualmachineid) 129 d.Set("vm_guest_ip", ip.Vmipaddress) 130 131 setValueOrID(d, "project", ip.Project, ip.Projectid) 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 }