Skip to content

Commit

Permalink
handle errors where smithy errorcode are compared after switch to aws…
Browse files Browse the repository at this point in the history
…-sdk-v2

Signed-off-by: Lars Haugan <[email protected]>
  • Loading branch information
larhauga committed Oct 13, 2021
1 parent 4dbf043 commit f4c8535
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 31 deletions.
4 changes: 3 additions & 1 deletion pkg/clients/ec2/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2

import (
"context"
"errors"
"sort"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -30,7 +31,8 @@ type AddressClient interface {

// IsAddressNotFoundErr returns true if the error is because the address doesn't exist
func IsAddressNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == AddressAddressNotFound || awsErr.ErrorCode() == AddressAllocationNotFound {
return true
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/clients/ec2/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ package ec2

import (
"context"
"errors"
"fmt"
"sort"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
ec2 "github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/smithy-go"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/crossplane/provider-aws/apis/ec2/manualv1alpha1"
Expand Down Expand Up @@ -51,8 +52,9 @@ func NewInstanceClient(cfg aws.Config) InstanceClient {

// IsInstanceNotFoundErr returns true if the error is because the item doesn't exist
func IsInstanceNotFoundErr(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == InstanceNotFound {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == InstanceNotFound {
return true
}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/clients/ec2/internetgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand Down Expand Up @@ -37,7 +38,8 @@ func NewInternetGatewayClient(cfg aws.Config) InternetGatewayClient {

// IsInternetGatewayNotFoundErr returns true if the error is because the item doesn't exist
func IsInternetGatewayNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == InternetGatewayIDNotFound {
return true
}
Expand All @@ -47,7 +49,8 @@ func IsInternetGatewayNotFoundErr(err error) bool {

// IsInternetGatewayAlreadyAttached returns true if the error is because the item doesn't exist
func IsInternetGatewayAlreadyAttached(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == InternetGatewayAlreadyAttached {
return true
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/clients/ec2/natgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand Down Expand Up @@ -35,7 +36,8 @@ func NewNatGatewayClient(cfg aws.Config) NatGatewayClient {

// IsNatGatewayNotFoundErr returns true if the error is because the item doesn't exist
func IsNatGatewayNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == NatGatewayNotFound {
return true
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/clients/ec2/routetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ec2
import (
"context"
"encoding/json"
"errors"
"sort"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -52,7 +53,8 @@ func NewRouteTableClient(cfg aws.Config) RouteTableClient {

// IsRouteTableNotFoundErr returns true if the error is because the route table doesn't exist
func IsRouteTableNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == RouteTableIDNotFound {
return true
}
Expand All @@ -62,7 +64,8 @@ func IsRouteTableNotFoundErr(err error) bool {

// IsRouteNotFoundErr returns true if the error is because the route doesn't exist
func IsRouteNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == RouteNotFound {
return true
}
Expand All @@ -72,7 +75,8 @@ func IsRouteNotFoundErr(err error) bool {

// IsAssociationIDNotFoundErr returns true if the error is because the association doesn't exist
func IsAssociationIDNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == AssociationIDNotFound {
return true
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/clients/ec2/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ec2
import (
"context"
"encoding/json"
"errors"
"sort"
"strings"

Expand Down Expand Up @@ -47,7 +48,8 @@ func NewSecurityGroupClient(cfg awsgo.Config) SecurityGroupClient {

// IsSecurityGroupNotFoundErr returns true if the error is because the item doesn't exist
func IsSecurityGroupNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == InvalidGroupNotFound {
return true
}
Expand All @@ -57,7 +59,8 @@ func IsSecurityGroupNotFoundErr(err error) bool {

// IsRuleAlreadyExistsErr returns true if the error is because the rule already exists.
func IsRuleAlreadyExistsErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == InvalidPermissionDuplicate {
return true
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/clients/ec2/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand Down Expand Up @@ -33,7 +34,8 @@ func NewSubnetClient(cfg aws.Config) SubnetClient {

// IsSubnetNotFoundErr returns true if the error is because the item doesn't exist
func IsSubnetNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == SubnetIDNotFound {
return true
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/clients/ec2/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand Down Expand Up @@ -35,7 +36,8 @@ func NewVPCClient(cfg aws.Config) VPCClient {

// IsVPCNotFoundErr returns true if the error is because the item doesn't exist
func IsVPCNotFoundErr(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == VPCIDNotFound {
return true
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/clients/ec2/vpccidrblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/smithy-go"

"github.com/crossplane/provider-aws/apis/ec2/manualv1alpha1"
awsclient "github.com/crossplane/provider-aws/pkg/clients"
Expand Down Expand Up @@ -40,7 +41,16 @@ func (r *CIDRNotFoundError) Error() string {
// IsCIDRNotFound returns true if the error code indicates that the CIDR Block Association was not found
func IsCIDRNotFound(err error) bool {
var notFoundError *CIDRNotFoundError
return errors.As(err, &notFoundError)
if errors.As(err, &notFoundError) {
return true
}
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == errCIDRAssociationNotFound {
return true
}
}
return false
}

// IsVpcCidrBlockUpToDate returns true if there is no update-able difference between desired
Expand Down
5 changes: 3 additions & 2 deletions pkg/clients/ecr/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ecr
import (
"context"
"encoding/json"
"errors"
"sort"
"strings"

Expand Down Expand Up @@ -105,8 +106,8 @@ func IsRepositoryUpToDate(e *v1alpha1.RepositoryParameters, tags []ecrtypes.Tag,

// IsRepoNotFoundErr returns true if the error is because the item doesn't exist
func IsRepoNotFoundErr(err error) bool {
_, ok := err.(*ecrtypes.RepositoryNotFoundException)
return ok
var notFoundError *ecrtypes.RepositoryNotFoundException
return errors.As(err, &notFoundError)
}

// GenerateCreateRepositoryInput Generates the CreateRepositoryInput from the RepositoryParameters
Expand Down
31 changes: 20 additions & 11 deletions pkg/clients/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func IsNotFound(err error) bool {

// IsAlreadyExists helper function to test for ErrCodeBucketAlreadyOwnedByYou error
func IsAlreadyExists(err error) bool {
var notFoundError *s3types.BucketAlreadyOwnedByYou
return errors.As(err, &notFoundError)
var alreadyOwnedByYou *s3types.BucketAlreadyOwnedByYou
return errors.As(err, &alreadyOwnedByYou)
}

// GenerateCreateBucketInput creates the input for CreateBucket S3 Client request
Expand Down Expand Up @@ -156,7 +156,8 @@ func GenerateBucketObservation(name string) v1beta1.BucketExternalStatus {

// CORSConfigurationNotFound is parses the aws Error and validates if the cors configuration does not exist
func CORSConfigurationNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == CORSNotFoundErrCode {
return true
}
Expand All @@ -166,7 +167,8 @@ func CORSConfigurationNotFound(err error) bool {

// ReplicationConfigurationNotFound is parses the aws Error and validates if the replication configuration does not exist
func ReplicationConfigurationNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == ReplicationNotFoundErrCode {
return true
}
Expand All @@ -176,7 +178,8 @@ func ReplicationConfigurationNotFound(err error) bool {

// PublicAccessBlockConfigurationNotFound is parses the aws Error and validates if the public access block does not exist
func PublicAccessBlockConfigurationNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == PublicAccessBlockNotFoundErrCode {
return true
}
Expand All @@ -186,7 +189,8 @@ func PublicAccessBlockConfigurationNotFound(err error) bool {

// LifecycleConfigurationNotFound is parses the aws Error and validates if the lifecycle configuration does not exist
func LifecycleConfigurationNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == LifecycleNotFoundErrCode {
return true
}
Expand All @@ -196,7 +200,8 @@ func LifecycleConfigurationNotFound(err error) bool {

// SSEConfigurationNotFound is parses the aws Error and validates if the SSE configuration does not exist
func SSEConfigurationNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == SSENotFoundErrCode {
return true
}
Expand All @@ -206,7 +211,8 @@ func SSEConfigurationNotFound(err error) bool {

// TaggingNotFound is parses the aws Error and validates if the tagging configuration does not exist
func TaggingNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == TaggingNotFoundErrCode {
return true
}
Expand All @@ -216,7 +222,8 @@ func TaggingNotFound(err error) bool {

// WebsiteConfigurationNotFound is parses the aws Error and validates if the website configuration does not exist
func WebsiteConfigurationNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == WebsiteNotFoundErrCode {
return true
}
Expand All @@ -226,7 +233,8 @@ func WebsiteConfigurationNotFound(err error) bool {

// MethodNotSupported is parses the aws Error and validates if the method is allowed for a request
func MethodNotSupported(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == MethodNotAllowed {
return true
}
Expand All @@ -236,7 +244,8 @@ func MethodNotSupported(err error) bool {

// ArgumentNotSupported is parses the aws Error and validates if parameters are now allowed for a request
func ArgumentNotSupported(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == UnsupportedArgument {
return true
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/clients/s3/bucketpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func NewBucketPolicyClient(cfg aws.Config) BucketPolicyClient {

// IsErrorPolicyNotFound returns true if the error code indicates that the item was not found
func IsErrorPolicyNotFound(err error) bool {
if s3Err, ok := err.(smithy.APIError); ok {
if s3Err.ErrorCode() == "NoSuchBucketPolicy" {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == "NoSuchBucketPolicy" {
return true
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/clients/sqs/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sqs
import (
"context"
"encoding/json"
"errors"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -145,7 +146,8 @@ func GenerateQueueObservation(url string, attr map[string]string) v1beta1.QueueO

// IsNotFound checks if the error returned by AWS API says that the queue being probed doesn't exist
func IsNotFound(err error) bool {
if awsErr, ok := err.(smithy.APIError); ok {
var awsErr smithy.APIError
if errors.As(err, &awsErr) {
if awsErr.ErrorCode() == QueueNotFound {
return true
}
Expand Down

0 comments on commit f4c8535

Please sign in to comment.