Skip to content

Commit

Permalink
fix: check dstatus code for subnet not found
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyRadford committed May 17, 2021
1 parent 68607ca commit 217833e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
12 changes: 6 additions & 6 deletions pkg/asset/installconfig/ibmcloud/client.go
Expand Up @@ -3,6 +3,7 @@ package ibmcloud
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand All @@ -27,7 +28,7 @@ type API interface {
GetEncryptionKey(ctx context.Context, keyCRN string) (*ibmcloudtypes.EncryptionKeyResponse, error)
GetResourceGroups(ctx context.Context) ([]resourcemanagerv2.ResourceGroup, error)
GetResourceGroup(ctx context.Context, nameOrID string) (*resourcemanagerv2.ResourceGroup, error)
GetSubnet(ctx context.Context, subnetID string) (*vpcv1.Subnet, error)
GetSubnet(ctx context.Context, subnetID string) (*vpcv1.Subnet, *core.DetailedResponse, error)
GetVSIProfiles(ctx context.Context) ([]vpcv1.InstanceProfile, error)
GetVPC(ctx context.Context, vpcID string) (*vpcv1.VPC, error)
GetVPCZonesForRegion(ctx context.Context, region string) ([]string, error)
Expand Down Expand Up @@ -213,12 +214,11 @@ func (c *Client) GetResourceGroups(ctx context.Context) ([]resourcemanagerv2.Res
}

// GetSubnet gets a subnet by its ID.
func (c *Client) GetSubnet(ctx context.Context, subnetID string) (*vpcv1.Subnet, error) {
func (c *Client) GetSubnet(ctx context.Context, subnetID string) (*vpcv1.Subnet, *core.DetailedResponse, error) {
_, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()

subnet, _, err := c.vpcAPI.GetSubnet(&vpcv1.GetSubnetOptions{ID: &subnetID})
return subnet, err
return c.vpcAPI.GetSubnet(&vpcv1.GetSubnetOptions{ID: &subnetID})
}

// GetCustomImages gets a list of custom images within a region. If the image
Expand Down Expand Up @@ -286,8 +286,8 @@ func (c *Client) GetVPC(ctx context.Context, vpcID string) (*vpcv1.VPC, error) {
return nil, errors.Wrap(err, "failed to set vpc api service url")
}

if vpc, _, err := c.vpcAPI.GetVPC(c.vpcAPI.NewGetVPCOptions(vpcID)); err != nil {
if err.Error() != "VPC not found" {
if vpc, detailedResponse, err := c.vpcAPI.GetVPC(c.vpcAPI.NewGetVPCOptions(vpcID)); err != nil {
if detailedResponse.GetStatusCode() != http.StatusNotFound {
return nil, err
}
} else if vpc != nil {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/asset/installconfig/ibmcloud/validation.go
Expand Up @@ -3,6 +3,7 @@ package ibmcloud
import (
"context"
"fmt"
"net/http"
"sort"

"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -210,14 +211,13 @@ func validateSubnets(client API, ic *types.InstallConfig, subnets []string, path

func validateSubnetZone(client API, subnetID string, validZones []string, subnetPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if subnet, err := client.GetSubnet(context.TODO(), subnetID); err == nil {
if subnet, detailedResponse, err := client.GetSubnet(context.TODO(), subnetID); err == nil {
zoneName := *subnet.Zone.Name
if !contains(validZones, zoneName) {
allErrs = append(allErrs, field.Invalid(subnetPath, subnetID, fmt.Sprintf("subnet is not in expected zones: %s", validZones)))
}
} else {
msg := err.Error()
if msg == "not found" {
if detailedResponse.GetStatusCode() == http.StatusNotFound {
allErrs = append(allErrs, field.NotFound(subnetPath, subnetID))
} else {
allErrs = append(allErrs, field.InternalError(subnetPath, err))
Expand Down

0 comments on commit 217833e

Please sign in to comment.