Skip to content

Commit

Permalink
ignore vlan attachment API responses "422 Virtual network foo already…
Browse files Browse the repository at this point in the history
… ..."

Signed-off-by: Marques Johansson <mjohansson@equinix.com>
  • Loading branch information
displague committed Jun 11, 2021
1 parent a52bd97 commit 6f55a79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions pkg/clients/metal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/packethost/packngo"
Expand All @@ -42,6 +43,11 @@ type Client struct {
Client *packngo.Client
}

const (
errVirtualNetworkAlreadyContents = " already "
errVirtualNetworkAlreadyPrefix = "Virtual network"
)

// NewCredentialsFromJSON parses JSON bytes returning an Equinix Metal Credentials configuration
func NewCredentialsFromJSON(j []byte) (*Credentials, error) {
config := &Credentials{}
Expand Down Expand Up @@ -113,3 +119,15 @@ func IsNotFound(err error) bool {
}
return false
}

// IsAlreadyDone returns true if, during VLAN assignment operations, the API
// returns an error like "422 Virtual network 1182 already assigned" or "422
// Virtual network 1182 already unassigned"
func IsAlreadyDone(err error) bool {
if e, ok := err.(*packngo.ErrorResponse); ok && e.Response != nil {
return e.Response.StatusCode == http.StatusUnprocessableEntity &&
strings.Contains(e.Response.Status, errVirtualNetworkAlreadyContents) &&
strings.HasPrefix(e.Response.Status, errVirtualNetworkAlreadyPrefix)
}
return false
}
4 changes: 2 additions & 2 deletions pkg/controller/ports/assignment/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
}
a.Status.SetConditions(xpv1.Creating())
_, _, err := e.client.Assign(&packngo.PortAssignRequest{PortID: meta.GetExternalName(a), VirtualNetworkID: a.Spec.ForProvider.VirtualNetworkID})
return managed.ExternalCreation{}, errors.Wrap(err, errCreateAssignment)
return managed.ExternalCreation{}, errors.Wrap(resource.Ignore(packetclient.IsAlreadyDone, err), errCreateAssignment)
}

func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
Expand All @@ -159,5 +159,5 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
}
a.SetConditions(xpv1.Deleting())
_, _, err := e.client.Unassign(&packngo.PortAssignRequest{PortID: meta.GetExternalName(a), VirtualNetworkID: a.Spec.ForProvider.VirtualNetworkID})
return errors.Wrap(resource.Ignore(packetclient.IsNotFound, err), errDeleteAssignment)
return errors.Wrap(resource.IgnoreAny(err, packetclient.IsNotFound, packetclient.IsAlreadyDone), errDeleteAssignment)
}

0 comments on commit 6f55a79

Please sign in to comment.