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 1 commit
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
15 changes: 15 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,20 @@ func NewArn(partition, service, region, accountID, resource string) Arn {
}
}

// ErrInvalidArnFormat is returned when ARN string format is not valid
var ErrInvalidArnFormat = errors.New("invalid ARN format, should 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
}

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
21 changes: 21 additions & 0 deletions pkg/notification/notification_test.go
Expand Up @@ -1405,3 +1405,24 @@ 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 %s, but got none", ErrInvalidArnFormat.Error())
}
})
}