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

Commit

Permalink
Merge pull request #274 from displague/check-more-errors
Browse files Browse the repository at this point in the history
adds setMap to check errors when setting IP block data
  • Loading branch information
displague authored Oct 15, 2020
2 parents e702f46 + 27e2f9a commit 1da9983
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 33 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ require (
github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 // indirect
github.com/aws/aws-sdk-go v1.22.0 // indirect
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-retryablehttp v0.6.6
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/packethost/packngo v0.3.0
github.com/vmihailenco/msgpack v4.0.1+incompatible // indirect
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)

Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a h1:y6sBfNd1b9Wy08a6K1Z1DZc4aXABUN5TKjkYhz7UKmo=
golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand All @@ -222,8 +224,8 @@ golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI=
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
Expand All @@ -246,8 +248,8 @@ golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 h1:HmbHVPwrPEKPGLAcHSrMe6+hqSUlvZU0rab6x5EXfGU=
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down
8 changes: 6 additions & 2 deletions packet/datasource_packet_precreated_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func dataSourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{
facility := fval.(string)
for _, ip := range ips {
if ip.Public == public && ip.AddressFamily == ipv && facility == ip.Facility.Code {
loadBlock(d, &ip)
if err := loadBlock(d, &ip); err != nil {
return err
}
break
}
}
Expand All @@ -82,7 +84,9 @@ func dataSourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{
for _, ip := range ips {
blockGlobal := getGlobalBool(&ip)
if ip.Public == public && ip.AddressFamily == ipv && blockGlobal {
loadBlock(d, &ip)
if err := loadBlock(d, &ip); err != nil {
return err
}
break
}
}
Expand Down
29 changes: 29 additions & 0 deletions packet/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package packet

import (
"net/http"
"sort"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/packethost/packngo"
)

Expand Down Expand Up @@ -61,3 +65,28 @@ type ErrorResponse struct {
Errors
IsAPIError bool
}

// setMap sets the map of values to ResourceData, checking and returning the
// errors. Typically d.Set is not error checked. This helper makes checking
// those errors less tedious. Because this works with a map, the order of the
// errors would not be predictable, to avoid this the errors will be sorted.
func setMap(d *schema.ResourceData, m map[string]interface{}) error {
errs := &multierror.Error{}
for key, v := range m {
var err error
if f, ok := v.(setFn); ok {
err = f(d, key)
} else {
err = d.Set(key, v)
}

if err != nil {
errs = multierror.Append(errs, err)
}
}
sort.Sort(errs)

return errs.ErrorOrNil()
}

type setFn = func(d *schema.ResourceData, key string) error
60 changes: 35 additions & 25 deletions packet/resource_packet_reserved_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,40 +170,49 @@ func loadBlock(d *schema.ResourceData, reservedBlock *packngo.IPAddressReservati
ipv4CIDRToQuantity := map[int]int{32: 1, 31: 2, 30: 4, 29: 8, 28: 16, 27: 32, 26: 64, 25: 128, 24: 256}

d.SetId(reservedBlock.ID)
d.Set("address", reservedBlock.Address)
if reservedBlock.Facility != nil {
d.Set("facility", reservedBlock.Facility.Code)
}
d.Set("gateway", reservedBlock.Gateway)
d.Set("network", reservedBlock.Network)
d.Set("netmask", reservedBlock.Netmask)
d.Set("address_family", reservedBlock.AddressFamily)
d.Set("cidr", reservedBlock.CIDR)

typ, err := getType(reservedBlock)
if err != nil {
return err
}
d.Set("type", typ)
d.Set("public", reservedBlock.Public)
d.Set("management", reservedBlock.Management)
d.Set("manageable", reservedBlock.Manageable)
quantity := 0
if reservedBlock.AddressFamily == 4 {
d.Set("quantity", ipv4CIDRToQuantity[reservedBlock.CIDR])
quantity = ipv4CIDRToQuantity[reservedBlock.CIDR]
} else {
// In Equinix Metal, a reserved IPv6 block is allocated when a device is run in a project.
// It's always /56, and it can't be created with Terraform, only imported.
// The longest assignable prefix is /64, making it max 256 subnets per block.
// The following logic will hold as long as /64 is the smallest assignable subnet size.
// In Equinix Metal, a reserved IPv6 block is allocated when a device is
// run in a project. It's always /56, and it can't be created with
// Terraform, only imported. The longest assignable prefix is /64,
// making it max 256 subnets per block. The following logic will hold as
// long as /64 is the smallest assignable subnet size.
bits := 64 - reservedBlock.CIDR
if bits > 30 {
return fmt.Errorf("Strange (too small) CIDR prefix: %d", reservedBlock.CIDR)
}
d.Set("quantity", 1<<uint(bits))
quantity = 1 << uint(bits)
}
d.Set("project_id", path.Base(reservedBlock.Project.Href))
d.Set("cidr_notation", fmt.Sprintf("%s/%d", reservedBlock.Network, reservedBlock.CIDR))
return nil

err = setMap(d, map[string]interface{}{
"address": reservedBlock.Address,
"facility": func(d *schema.ResourceData, k string) error {
if reservedBlock.Facility == nil {
return nil
}
return d.Set(k, reservedBlock.Facility.Code)
},
"gateway": reservedBlock.Gateway,
"network": reservedBlock.Network,
"netmask": reservedBlock.Netmask,
"address_family": reservedBlock.AddressFamily,
"cidr": reservedBlock.CIDR,
"type": typ,
"public": reservedBlock.Public,
"management": reservedBlock.Management,
"manageable": reservedBlock.Manageable,
"quantity": quantity,
"project_id": path.Base(reservedBlock.Project.Href),
"cidr_notation": fmt.Sprintf("%s/%d", reservedBlock.Network, reservedBlock.CIDR),
})
return err
}

func resourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -221,13 +230,14 @@ func resourcePacketReservedIPBlockRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error reading IP address block with ID %s: %s", id, err)
}
err = loadBlock(d, reservedBlock)
if err != nil {
return err
}

if (reservedBlock.Description != nil) && (*(reservedBlock.Description) != "") {
d.Set("description", *(reservedBlock.Description))
}
d.Set("global", getGlobalBool(reservedBlock))
if err != nil {
return err
}

return nil
}
Expand Down

0 comments on commit 1da9983

Please sign in to comment.