github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ns1/resource_zone.go (about) 1 package ns1 2 3 import ( 4 "strings" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 8 ns1 "gopkg.in/ns1/ns1-go.v2/rest" 9 "gopkg.in/ns1/ns1-go.v2/rest/model/dns" 10 ) 11 12 func zoneResource() *schema.Resource { 13 return &schema.Resource{ 14 Schema: map[string]*schema.Schema{ 15 // Required 16 "zone": &schema.Schema{ 17 Type: schema.TypeString, 18 Required: true, 19 ForceNew: true, 20 }, 21 // Optional 22 "ttl": &schema.Schema{ 23 Type: schema.TypeInt, 24 Optional: true, 25 Computed: true, 26 }, 27 // SOA attributes per https://tools.ietf.org/html/rfc1035). 28 "refresh": &schema.Schema{ 29 Type: schema.TypeInt, 30 Optional: true, 31 Computed: true, 32 }, 33 "retry": &schema.Schema{ 34 Type: schema.TypeInt, 35 Optional: true, 36 Computed: true, 37 }, 38 "expiry": &schema.Schema{ 39 Type: schema.TypeInt, 40 Optional: true, 41 Computed: true, 42 }, 43 // SOA MINUMUM overloaded as NX TTL per https://tools.ietf.org/html/rfc2308 44 "nx_ttl": &schema.Schema{ 45 Type: schema.TypeInt, 46 Optional: true, 47 Computed: true, 48 }, 49 // TODO: test 50 "link": &schema.Schema{ 51 Type: schema.TypeString, 52 Optional: true, 53 ForceNew: true, 54 }, 55 // TODO: test 56 "primary": &schema.Schema{ 57 Type: schema.TypeString, 58 Optional: true, 59 ForceNew: true, 60 }, 61 // Computed 62 "id": &schema.Schema{ 63 Type: schema.TypeString, 64 Computed: true, 65 }, 66 "dns_servers": &schema.Schema{ 67 Type: schema.TypeString, 68 Computed: true, 69 }, 70 "hostmaster": &schema.Schema{ 71 Type: schema.TypeString, 72 Computed: true, 73 }, 74 }, 75 Create: ZoneCreate, 76 Read: ZoneRead, 77 Update: ZoneUpdate, 78 Delete: ZoneDelete, 79 Importer: &schema.ResourceImporter{State: ZoneStateFunc}, 80 } 81 } 82 83 func zoneToResourceData(d *schema.ResourceData, z *dns.Zone) { 84 d.SetId(z.ID) 85 d.Set("hostmaster", z.Hostmaster) 86 d.Set("ttl", z.TTL) 87 d.Set("nx_ttl", z.NxTTL) 88 d.Set("refresh", z.Refresh) 89 d.Set("retry", z.Retry) 90 d.Set("expiry", z.Expiry) 91 d.Set("dns_servers", strings.Join(z.DNSServers[:], ",")) 92 if z.Secondary != nil && z.Secondary.Enabled { 93 d.Set("primary", z.Secondary.PrimaryIP) 94 } 95 if z.Link != nil && *z.Link != "" { 96 d.Set("link", *z.Link) 97 } 98 } 99 100 func resourceToZoneData(z *dns.Zone, d *schema.ResourceData) { 101 z.ID = d.Id() 102 if v, ok := d.GetOk("hostmaster"); ok { 103 z.Hostmaster = v.(string) 104 } 105 if v, ok := d.GetOk("ttl"); ok { 106 z.TTL = v.(int) 107 } 108 if v, ok := d.GetOk("nx_ttl"); ok { 109 z.NxTTL = v.(int) 110 } 111 if v, ok := d.GetOk("refresh"); ok { 112 z.Refresh = v.(int) 113 } 114 if v, ok := d.GetOk("retry"); ok { 115 z.Retry = v.(int) 116 } 117 if v, ok := d.GetOk("expiry"); ok { 118 z.Expiry = v.(int) 119 } 120 if v, ok := d.GetOk("primary"); ok { 121 z.MakeSecondary(v.(string)) 122 } 123 if v, ok := d.GetOk("link"); ok { 124 z.LinkTo(v.(string)) 125 } 126 } 127 128 // ZoneCreate creates the given zone in ns1 129 func ZoneCreate(d *schema.ResourceData, meta interface{}) error { 130 client := meta.(*ns1.Client) 131 z := dns.NewZone(d.Get("zone").(string)) 132 resourceToZoneData(z, d) 133 if _, err := client.Zones.Create(z); err != nil { 134 return err 135 } 136 zoneToResourceData(d, z) 137 return nil 138 } 139 140 // ZoneRead reads the given zone data from ns1 141 func ZoneRead(d *schema.ResourceData, meta interface{}) error { 142 client := meta.(*ns1.Client) 143 z, _, err := client.Zones.Get(d.Get("zone").(string)) 144 if err != nil { 145 return err 146 } 147 zoneToResourceData(d, z) 148 return nil 149 } 150 151 // ZoneDelete deteles the given zone from ns1 152 func ZoneDelete(d *schema.ResourceData, meta interface{}) error { 153 client := meta.(*ns1.Client) 154 _, err := client.Zones.Delete(d.Get("zone").(string)) 155 d.SetId("") 156 return err 157 } 158 159 // ZoneUpdate updates the zone with given params in ns1 160 func ZoneUpdate(d *schema.ResourceData, meta interface{}) error { 161 client := meta.(*ns1.Client) 162 z := dns.NewZone(d.Get("zone").(string)) 163 resourceToZoneData(z, d) 164 if _, err := client.Zones.Update(z); err != nil { 165 return err 166 } 167 zoneToResourceData(d, z) 168 return nil 169 } 170 171 func ZoneStateFunc(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 172 d.Set("zone", d.Id()) 173 return []*schema.ResourceData{d}, nil 174 }