|
1 | 1 | package nsone |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/ns1/ns1-go" |
5 | 4 | "github.com/hashicorp/terraform/helper/schema" |
| 5 | + |
| 6 | + nsone "gopkg.in/ns1/ns1-go.v2/rest" |
| 7 | + "gopkg.in/ns1/ns1-go.v2/rest/model/account" |
6 | 8 | ) |
7 | 9 |
|
8 | 10 | func apikeyResource() *schema.Resource { |
@@ -35,43 +37,43 @@ func apikeyResource() *schema.Resource { |
35 | 37 | } |
36 | 38 | } |
37 | 39 |
|
38 | | -func apikeyToResourceData(d *schema.ResourceData, u *nsone.Apikey) error { |
39 | | - d.SetId(u.Id) |
| 40 | +func apikeyToResourceData(d *schema.ResourceData, u *account.APIKey) error { |
| 41 | + d.SetId(u.ID) |
40 | 42 | d.Set("name", u.Name) |
41 | 43 | d.Set("key", u.Key) |
42 | | - d.Set("teams", u.Teams) |
| 44 | + d.Set("teams", u.TeamIDs) |
43 | 45 | permissionsToResourceData(d, u.Permissions) |
44 | 46 | return nil |
45 | 47 | } |
46 | 48 |
|
47 | | -func resourceDataToPermissions(d *schema.ResourceData) nsone.PermissionsMap { |
48 | | - var p nsone.PermissionsMap |
| 49 | +func resourceDataToPermissions(d *schema.ResourceData) account.PermissionsMap { |
| 50 | + var p account.PermissionsMap |
49 | 51 | if v, ok := d.GetOk("dns_view_zones"); ok { |
50 | | - p.Dns.ViewZones = v.(bool) |
| 52 | + p.DNS.ViewZones = v.(bool) |
51 | 53 | } |
52 | 54 | if v, ok := d.GetOk("dns_manage_zones"); ok { |
53 | | - p.Dns.ManageZones = v.(bool) |
| 55 | + p.DNS.ManageZones = v.(bool) |
54 | 56 | } |
55 | 57 | if v, ok := d.GetOk("dns_zones_allow_by_default"); ok { |
56 | | - p.Dns.ZonesAllowByDefault = v.(bool) |
| 58 | + p.DNS.ZonesAllowByDefault = v.(bool) |
57 | 59 | } |
58 | 60 | if v, ok := d.GetOk("dns_zones_deny"); ok { |
59 | 61 | denyRaw := v.([]interface{}) |
60 | | - p.Dns.ZonesDeny = make([]string, len(denyRaw)) |
| 62 | + p.DNS.ZonesDeny = make([]string, len(denyRaw)) |
61 | 63 | for i, deny := range denyRaw { |
62 | | - p.Dns.ZonesDeny[i] = deny.(string) |
| 64 | + p.DNS.ZonesDeny[i] = deny.(string) |
63 | 65 | } |
64 | 66 | } else { |
65 | | - p.Dns.ZonesDeny = make([]string, 0) |
| 67 | + p.DNS.ZonesDeny = make([]string, 0) |
66 | 68 | } |
67 | 69 | if v, ok := d.GetOk("dns_zones_allow"); ok { |
68 | 70 | allowRaw := v.([]interface{}) |
69 | | - p.Dns.ZonesAllow = make([]string, len(allowRaw)) |
| 71 | + p.DNS.ZonesAllow = make([]string, len(allowRaw)) |
70 | 72 | for i, allow := range allowRaw { |
71 | | - p.Dns.ZonesAllow[i] = allow.(string) |
| 73 | + p.DNS.ZonesAllow[i] = allow.(string) |
72 | 74 | } |
73 | 75 | } else { |
74 | | - p.Dns.ZonesAllow = make([]string, 0) |
| 76 | + p.DNS.ZonesAllow = make([]string, 0) |
75 | 77 | } |
76 | 78 | if v, ok := d.GetOk("data_push_to_datafeeds"); ok { |
77 | 79 | p.Data.PushToDatafeeds = v.(bool) |
@@ -118,64 +120,64 @@ func resourceDataToPermissions(d *schema.ResourceData) nsone.PermissionsMap { |
118 | 120 | return p |
119 | 121 | } |
120 | 122 |
|
121 | | -func resourceDataToApikey(u *nsone.Apikey, d *schema.ResourceData) error { |
122 | | - u.Id = d.Id() |
| 123 | +func resourceDataToApikey(u *account.APIKey, d *schema.ResourceData) error { |
| 124 | + u.ID = d.Id() |
123 | 125 | u.Name = d.Get("name").(string) |
124 | 126 | if v, ok := d.GetOk("teams"); ok { |
125 | 127 | teamsRaw := v.([]interface{}) |
126 | | - u.Teams = make([]string, len(teamsRaw)) |
| 128 | + u.TeamIDs = make([]string, len(teamsRaw)) |
127 | 129 | for i, team := range teamsRaw { |
128 | | - u.Teams[i] = team.(string) |
| 130 | + u.TeamIDs[i] = team.(string) |
129 | 131 | } |
130 | 132 | } else { |
131 | | - u.Teams = make([]string, 0) |
| 133 | + u.TeamIDs = make([]string, 0) |
132 | 134 | } |
133 | 135 | u.Permissions = resourceDataToPermissions(d) |
134 | 136 | return nil |
135 | 137 | } |
136 | 138 |
|
137 | 139 | // ApikeyCreate creates ns1 API key |
138 | 140 | func ApikeyCreate(d *schema.ResourceData, meta interface{}) error { |
139 | | - client := meta.(*nsone.APIClient) |
140 | | - mj := nsone.Apikey{} |
| 141 | + client := meta.(*nsone.Client) |
| 142 | + mj := account.APIKey{} |
141 | 143 | if err := resourceDataToApikey(&mj, d); err != nil { |
142 | 144 | return err |
143 | 145 | } |
144 | | - if err := client.CreateApikey(&mj); err != nil { |
| 146 | + if _, err := client.APIKeys.Create(&mj); err != nil { |
145 | 147 | return err |
146 | 148 | } |
147 | 149 | return apikeyToResourceData(d, &mj) |
148 | 150 | } |
149 | 151 |
|
150 | 152 | // ApikeyRead reads API key from ns1 |
151 | 153 | func ApikeyRead(d *schema.ResourceData, meta interface{}) error { |
152 | | - client := meta.(*nsone.APIClient) |
153 | | - mj, err := client.GetApikey(d.Id()) |
| 154 | + client := meta.(*nsone.Client) |
| 155 | + mj, _, err := client.APIKeys.Get(d.Id()) |
154 | 156 | if err != nil { |
155 | 157 | return err |
156 | 158 | } |
157 | | - apikeyToResourceData(d, &mj) |
| 159 | + apikeyToResourceData(d, mj) |
158 | 160 | return nil |
159 | 161 | } |
160 | 162 |
|
161 | 163 | //ApikeyDelete deletes the given ns1 api key |
162 | 164 | func ApikeyDelete(d *schema.ResourceData, meta interface{}) error { |
163 | | - client := meta.(*nsone.APIClient) |
164 | | - err := client.DeleteApikey(d.Id()) |
| 165 | + client := meta.(*nsone.Client) |
| 166 | + _, err := client.APIKeys.Delete(d.Id()) |
165 | 167 | d.SetId("") |
166 | 168 | return err |
167 | 169 | } |
168 | 170 |
|
169 | 171 | //ApikeyUpdate updates the given api key in ns1 |
170 | 172 | func ApikeyUpdate(d *schema.ResourceData, meta interface{}) error { |
171 | | - client := meta.(*nsone.APIClient) |
172 | | - mj := nsone.Apikey{ |
173 | | - Id: d.Id(), |
| 173 | + client := meta.(*nsone.Client) |
| 174 | + mj := account.APIKey{ |
| 175 | + ID: d.Id(), |
174 | 176 | } |
175 | 177 | if err := resourceDataToApikey(&mj, d); err != nil { |
176 | 178 | return err |
177 | 179 | } |
178 | | - if err := client.UpdateApikey(&mj); err != nil { |
| 180 | + if _, err := client.APIKeys.Update(&mj); err != nil { |
179 | 181 | return err |
180 | 182 | } |
181 | 183 | apikeyToResourceData(d, &mj) |
|
0 commit comments