github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resource_oneandone_public_ip.go (about) 1 package oneandone 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/terraform/helper/schema" 6 "strings" 7 ) 8 9 func resourceOneandOnePublicIp() *schema.Resource { 10 return &schema.Resource{ 11 12 Create: resourceOneandOnePublicIpCreate, 13 Read: resourceOneandOnePublicIpRead, 14 Update: resourceOneandOnePublicIpUpdate, 15 Delete: resourceOneandOnePublicIpDelete, 16 Schema: map[string]*schema.Schema{ 17 "ip_type": { //IPV4 or IPV6 18 Type: schema.TypeString, 19 Required: true, 20 }, 21 "reverse_dns": { 22 Type: schema.TypeString, 23 Optional: true, 24 }, 25 "datacenter": { 26 Type: schema.TypeString, 27 Optional: true, 28 }, 29 "ip_address": { 30 Type: schema.TypeString, 31 Computed: true, 32 }, 33 }, 34 } 35 } 36 37 func resourceOneandOnePublicIpCreate(d *schema.ResourceData, meta interface{}) error { 38 config := meta.(*Config) 39 40 var reverse_dns string 41 var datacenter_id string 42 43 if raw, ok := d.GetOk("reverse_dns"); ok { 44 reverse_dns = raw.(string) 45 } 46 47 if raw, ok := d.GetOk("datacenter"); ok { 48 dcs, err := config.API.ListDatacenters() 49 50 if err != nil { 51 return fmt.Errorf("An error occured while fetching list of datacenters %s", err) 52 53 } 54 55 decenter := raw.(string) 56 for _, dc := range dcs { 57 if strings.ToLower(dc.CountryCode) == strings.ToLower(decenter) { 58 datacenter_id = dc.Id 59 break 60 } 61 } 62 63 } 64 65 ip_id, ip, err := config.API.CreatePublicIp(d.Get("ip_type").(string), reverse_dns, datacenter_id) 66 if err != nil { 67 return err 68 } 69 70 err = config.API.WaitForState(ip, "ACTIVE", 10, config.Retries) 71 if err != nil { 72 return err 73 } 74 d.SetId(ip_id) 75 76 return resourceOneandOnePublicIpRead(d, meta) 77 } 78 79 func resourceOneandOnePublicIpRead(d *schema.ResourceData, meta interface{}) error { 80 config := meta.(*Config) 81 82 ip, err := config.API.GetPublicIp(d.Id()) 83 if err != nil { 84 if strings.Contains(err.Error(), "404") { 85 d.SetId("") 86 return nil 87 } 88 return err 89 } 90 91 d.Set("ip_address", ip.IpAddress) 92 d.Set("revers_dns", ip.ReverseDns) 93 d.Set("datacenter", ip.Datacenter.CountryCode) 94 d.Set("ip_type", ip.Type) 95 96 return nil 97 } 98 99 func resourceOneandOnePublicIpUpdate(d *schema.ResourceData, meta interface{}) error { 100 config := meta.(*Config) 101 102 if d.HasChange("reverse_dns") { 103 _, n := d.GetChange("reverse_dns") 104 ip, err := config.API.UpdatePublicIp(d.Id(), n.(string)) 105 if err != nil { 106 return err 107 } 108 109 err = config.API.WaitForState(ip, "ACTIVE", 10, config.Retries) 110 if err != nil { 111 return err 112 } 113 } 114 115 return resourceOneandOnePublicIpRead(d, meta) 116 } 117 118 func resourceOneandOnePublicIpDelete(d *schema.ResourceData, meta interface{}) error { 119 config := meta.(*Config) 120 121 ip, err := config.API.DeletePublicIp(d.Id()) 122 if err != nil { 123 return err 124 } 125 126 err = config.API.WaitUntilDeleted(ip) 127 if err != nil { 128 129 return err 130 } 131 132 return nil 133 }