Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NewArnFromString to generalise the functionality of ARN parsing #1721

Merged
merged 2 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/notification/notification.go
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"strings"

"github.com/minio/minio-go/v7/pkg/set"
)
Expand Down Expand Up @@ -88,6 +89,27 @@ func NewArn(partition, service, region, accountID, resource string) Arn {
}
}

var (
// ErrInvalidArnPrefix is returned when ARN string format does not start with 'arn'
ErrInvalidArnPrefix = errors.New("invalid ARN format, must start with 'arn:'")
// ErrInvalidArnFormat is returned when ARN string format is not valid
ErrInvalidArnFormat = errors.New("invalid ARN format, must be 'arn:<partition>:<service>:<region>:<accountID>:<resource>'")
)

// NewArnFromString parses string representation of ARN into Arn object.
// Returns an error if the string format is incorrect.
func NewArnFromString(arn string) (Arn, error) {
parts := strings.Split(arn, ":")
if len(parts) != 6 {
return Arn{}, ErrInvalidArnFormat
}
if parts[0] != "arn" {
return Arn{}, ErrInvalidArnPrefix
}

return NewArn(parts[1], parts[2], parts[3], parts[4], parts[5]), nil
harshavardhana marked this conversation as resolved.
Show resolved Hide resolved
}

// String returns the string format of the ARN
func (arn Arn) String() string {
return "arn:" + arn.Partition + ":" + arn.Service + ":" + arn.Region + ":" + arn.AccountID + ":" + arn.Resource
Expand Down
28 changes: 28 additions & 0 deletions pkg/notification/notification_test.go
Expand Up @@ -1405,3 +1405,31 @@ func TestConfigEqual(t *testing.T) {
})
}
}

func TestNewArnFromString(t *testing.T) {
t.Run("valid ARN", func(t *testing.T) {
arn := NewArn("partition", "service", "region", "accountID", "resource")
arnString := arn.String()
arnFromString, err := NewArnFromString(arnString)
if err != nil {
t.Fatalf("did not exect an error, but got %v", err)
}
if arnFromString.String() != arnString {
t.Errorf("expected ARNs to be equal, but they are not: arnFromString = %s, arn = %s", arnFromString.String(), arnString)
}
})

t.Run("invalid ARN format", func(t *testing.T) {
_, err := NewArnFromString("arn:only:four:parts")
if err != ErrInvalidArnFormat {
t.Errorf("expected an error %v, but got %v", ErrInvalidArnFormat, err)
}
})

t.Run("invalid ARN prefix", func(t *testing.T) {
_, err := NewArnFromString("non-arn:partition:service:region:accountID:resource")
if err != ErrInvalidArnPrefix {
t.Errorf("expected an error %v, but got %v", ErrInvalidArnPrefix, err)
}
})
}