Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 1da9983

Browse files
authored
Merge pull request #274 from displague/check-more-errors
adds setMap to check errors when setting IP block data
2 parents e702f46 + 27e2f9a commit 1da9983

File tree

5 files changed

+78
-33
lines changed

5 files changed

+78
-33
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ require (
44
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 // indirect
55
github.com/aws/aws-sdk-go v1.22.0 // indirect
66
github.com/hashicorp/errwrap v1.0.0
7+
github.com/hashicorp/go-multierror v1.0.0
78
github.com/hashicorp/go-retryablehttp v0.6.6
89
github.com/hashicorp/terraform-plugin-sdk v1.0.0
910
github.com/mattn/go-colorable v0.1.1 // indirect
1011
github.com/packethost/packngo v0.3.0
1112
github.com/vmihailenco/msgpack v4.0.1+incompatible // indirect
12-
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
13-
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 // indirect
13+
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
1414
gopkg.in/yaml.v2 v2.2.8 // indirect
1515
)
1616

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
199199
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
200200
golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a h1:y6sBfNd1b9Wy08a6K1Z1DZc4aXABUN5TKjkYhz7UKmo=
201201
golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
202+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
203+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
202204
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
203205
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
204206
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
@@ -222,8 +224,8 @@ golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn
222224
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
223225
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
224226
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
225-
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
226-
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
227+
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
228+
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
227229
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
228230
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
229231
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
@@ -246,8 +248,8 @@ golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7w
246248
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
247249
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
248250
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
249-
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 h1:HmbHVPwrPEKPGLAcHSrMe6+hqSUlvZU0rab6x5EXfGU=
250-
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
251+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
252+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
251253
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
252254
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
253255
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=

packet/datasource_packet_precreated_ip_block.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func dataSourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{
7373
facility := fval.(string)
7474
for _, ip := range ips {
7575
if ip.Public == public && ip.AddressFamily == ipv && facility == ip.Facility.Code {
76-
loadBlock(d, &ip)
76+
if err := loadBlock(d, &ip); err != nil {
77+
return err
78+
}
7779
break
7880
}
7981
}
@@ -82,7 +84,9 @@ func dataSourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{
8284
for _, ip := range ips {
8385
blockGlobal := getGlobalBool(&ip)
8486
if ip.Public == public && ip.AddressFamily == ipv && blockGlobal {
85-
loadBlock(d, &ip)
87+
if err := loadBlock(d, &ip); err != nil {
88+
return err
89+
}
8690
break
8791
}
8892
}

packet/errors.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package packet
22

33
import (
44
"net/http"
5+
"sort"
56
"strings"
67

8+
"github.com/hashicorp/go-multierror"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
711
"github.com/packethost/packngo"
812
)
913

@@ -61,3 +65,28 @@ type ErrorResponse struct {
6165
Errors
6266
IsAPIError bool
6367
}
68+
69+
// setMap sets the map of values to ResourceData, checking and returning the
70+
// errors. Typically d.Set is not error checked. This helper makes checking
71+
// those errors less tedious. Because this works with a map, the order of the
72+
// errors would not be predictable, to avoid this the errors will be sorted.
73+
func setMap(d *schema.ResourceData, m map[string]interface{}) error {
74+
errs := &multierror.Error{}
75+
for key, v := range m {
76+
var err error
77+
if f, ok := v.(setFn); ok {
78+
err = f(d, key)
79+
} else {
80+
err = d.Set(key, v)
81+
}
82+
83+
if err != nil {
84+
errs = multierror.Append(errs, err)
85+
}
86+
}
87+
sort.Sort(errs)
88+
89+
return errs.ErrorOrNil()
90+
}
91+
92+
type setFn = func(d *schema.ResourceData, key string) error

packet/resource_packet_reserved_ip_block.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -170,40 +170,49 @@ func loadBlock(d *schema.ResourceData, reservedBlock *packngo.IPAddressReservati
170170
ipv4CIDRToQuantity := map[int]int{32: 1, 31: 2, 30: 4, 29: 8, 28: 16, 27: 32, 26: 64, 25: 128, 24: 256}
171171

172172
d.SetId(reservedBlock.ID)
173-
d.Set("address", reservedBlock.Address)
174-
if reservedBlock.Facility != nil {
175-
d.Set("facility", reservedBlock.Facility.Code)
176-
}
177-
d.Set("gateway", reservedBlock.Gateway)
178-
d.Set("network", reservedBlock.Network)
179-
d.Set("netmask", reservedBlock.Netmask)
180-
d.Set("address_family", reservedBlock.AddressFamily)
181-
d.Set("cidr", reservedBlock.CIDR)
173+
182174
typ, err := getType(reservedBlock)
183175
if err != nil {
184176
return err
185177
}
186-
d.Set("type", typ)
187-
d.Set("public", reservedBlock.Public)
188-
d.Set("management", reservedBlock.Management)
189-
d.Set("manageable", reservedBlock.Manageable)
178+
quantity := 0
190179
if reservedBlock.AddressFamily == 4 {
191-
d.Set("quantity", ipv4CIDRToQuantity[reservedBlock.CIDR])
180+
quantity = ipv4CIDRToQuantity[reservedBlock.CIDR]
192181
} else {
193-
// In Equinix Metal, a reserved IPv6 block is allocated when a device is run in a project.
194-
// It's always /56, and it can't be created with Terraform, only imported.
195-
// The longest assignable prefix is /64, making it max 256 subnets per block.
196-
// The following logic will hold as long as /64 is the smallest assignable subnet size.
182+
// In Equinix Metal, a reserved IPv6 block is allocated when a device is
183+
// run in a project. It's always /56, and it can't be created with
184+
// Terraform, only imported. The longest assignable prefix is /64,
185+
// making it max 256 subnets per block. The following logic will hold as
186+
// long as /64 is the smallest assignable subnet size.
197187
bits := 64 - reservedBlock.CIDR
198188
if bits > 30 {
199189
return fmt.Errorf("Strange (too small) CIDR prefix: %d", reservedBlock.CIDR)
200190
}
201-
d.Set("quantity", 1<<uint(bits))
191+
quantity = 1 << uint(bits)
202192
}
203-
d.Set("project_id", path.Base(reservedBlock.Project.Href))
204-
d.Set("cidr_notation", fmt.Sprintf("%s/%d", reservedBlock.Network, reservedBlock.CIDR))
205-
return nil
206193

194+
err = setMap(d, map[string]interface{}{
195+
"address": reservedBlock.Address,
196+
"facility": func(d *schema.ResourceData, k string) error {
197+
if reservedBlock.Facility == nil {
198+
return nil
199+
}
200+
return d.Set(k, reservedBlock.Facility.Code)
201+
},
202+
"gateway": reservedBlock.Gateway,
203+
"network": reservedBlock.Network,
204+
"netmask": reservedBlock.Netmask,
205+
"address_family": reservedBlock.AddressFamily,
206+
"cidr": reservedBlock.CIDR,
207+
"type": typ,
208+
"public": reservedBlock.Public,
209+
"management": reservedBlock.Management,
210+
"manageable": reservedBlock.Manageable,
211+
"quantity": quantity,
212+
"project_id": path.Base(reservedBlock.Project.Href),
213+
"cidr_notation": fmt.Sprintf("%s/%d", reservedBlock.Network, reservedBlock.CIDR),
214+
})
215+
return err
207216
}
208217

209218
func resourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{}) error {
@@ -221,13 +230,14 @@ func resourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{})
221230
return fmt.Errorf("Error reading IP address block with ID %s: %s", id, err)
222231
}
223232
err = loadBlock(d, reservedBlock)
233+
if err != nil {
234+
return err
235+
}
236+
224237
if (reservedBlock.Description != nil) && (*(reservedBlock.Description) != "") {
225238
d.Set("description", *(reservedBlock.Description))
226239
}
227240
d.Set("global", getGlobalBool(reservedBlock))
228-
if err != nil {
229-
return err
230-
}
231241

232242
return nil
233243
}

0 commit comments

Comments
 (0)