github.com/aavshr/aws-sdk-go@v1.41.3/internal/s3shared/arn/outpost_arn.go (about) 1 package arn 2 3 import ( 4 "strings" 5 6 "github.com/aavshr/aws-sdk-go/aws/arn" 7 ) 8 9 // OutpostARN interface that should be satisfied by outpost ARNs 10 type OutpostARN interface { 11 Resource 12 GetOutpostID() string 13 } 14 15 // ParseOutpostARNResource will parse a provided ARNs resource using the appropriate ARN format 16 // and return a specific OutpostARN type 17 // 18 // Currently supported outpost ARN formats: 19 // * Outpost AccessPoint ARN format: 20 // - ARN format: arn:{partition}:s3-outposts:{region}:{accountId}:outpost/{outpostId}/accesspoint/{accesspointName} 21 // - example: arn:aws:s3-outposts:us-west-2:012345678901:outpost/op-1234567890123456/accesspoint/myaccesspoint 22 // 23 // * Outpost Bucket ARN format: 24 // - ARN format: arn:{partition}:s3-outposts:{region}:{accountId}:outpost/{outpostId}/bucket/{bucketName} 25 // - example: arn:aws:s3-outposts:us-west-2:012345678901:outpost/op-1234567890123456/bucket/mybucket 26 // 27 // Other outpost ARN formats may be supported and added in the future. 28 // 29 func ParseOutpostARNResource(a arn.ARN, resParts []string) (OutpostARN, error) { 30 if len(a.Region) == 0 { 31 return nil, InvalidARNError{ARN: a, Reason: "region not set"} 32 } 33 34 if len(a.AccountID) == 0 { 35 return nil, InvalidARNError{ARN: a, Reason: "account-id not set"} 36 } 37 38 // verify if outpost id is present and valid 39 if len(resParts) == 0 || len(strings.TrimSpace(resParts[0])) == 0 { 40 return nil, InvalidARNError{ARN: a, Reason: "outpost resource-id not set"} 41 } 42 43 // verify possible resource type exists 44 if len(resParts) < 3 { 45 return nil, InvalidARNError{ 46 ARN: a, Reason: "incomplete outpost resource type. Expected bucket or access-point resource to be present", 47 } 48 } 49 50 // Since we know this is a OutpostARN fetch outpostID 51 outpostID := strings.TrimSpace(resParts[0]) 52 53 switch resParts[1] { 54 case "accesspoint": 55 accesspointARN, err := ParseAccessPointResource(a, resParts[2:]) 56 if err != nil { 57 return OutpostAccessPointARN{}, err 58 } 59 return OutpostAccessPointARN{ 60 AccessPointARN: accesspointARN, 61 OutpostID: outpostID, 62 }, nil 63 64 case "bucket": 65 bucketName, err := parseBucketResource(a, resParts[2:]) 66 if err != nil { 67 return nil, err 68 } 69 return OutpostBucketARN{ 70 ARN: a, 71 BucketName: bucketName, 72 OutpostID: outpostID, 73 }, nil 74 75 default: 76 return nil, InvalidARNError{ARN: a, Reason: "unknown resource set for outpost ARN"} 77 } 78 } 79 80 // OutpostAccessPointARN represents outpost access point ARN. 81 type OutpostAccessPointARN struct { 82 AccessPointARN 83 OutpostID string 84 } 85 86 // GetOutpostID returns the outpost id of outpost access point arn 87 func (o OutpostAccessPointARN) GetOutpostID() string { 88 return o.OutpostID 89 } 90 91 // OutpostBucketARN represents the outpost bucket ARN. 92 type OutpostBucketARN struct { 93 arn.ARN 94 BucketName string 95 OutpostID string 96 } 97 98 // GetOutpostID returns the outpost id of outpost bucket arn 99 func (o OutpostBucketARN) GetOutpostID() string { 100 return o.OutpostID 101 } 102 103 // GetARN retrives the base ARN from outpost bucket ARN resource 104 func (o OutpostBucketARN) GetARN() arn.ARN { 105 return o.ARN 106 } 107 108 // parseBucketResource attempts to parse the ARN's bucket resource and retrieve the 109 // bucket resource id. 110 // 111 // parseBucketResource only parses the bucket resource id. 112 // 113 func parseBucketResource(a arn.ARN, resParts []string) (bucketName string, err error) { 114 if len(resParts) == 0 { 115 return bucketName, InvalidARNError{ARN: a, Reason: "bucket resource-id not set"} 116 } 117 if len(resParts) > 1 { 118 return bucketName, InvalidARNError{ARN: a, Reason: "sub resource not supported"} 119 } 120 121 bucketName = strings.TrimSpace(resParts[0]) 122 if len(bucketName) == 0 { 123 return bucketName, InvalidARNError{ARN: a, Reason: "bucket resource-id not set"} 124 } 125 return bucketName, err 126 }