ceph-csi/vendor/github.com/aws/aws-sdk-go-v2/internal/endpoints/awsrulesfn/arn.go
dependabot[bot] f9310c84f4 rebase: Bump github.com/aws/aws-sdk-go-v2/service/sts
Bumps [github.com/aws/aws-sdk-go-v2/service/sts](https://github.com/aws/aws-sdk-go-v2) from 1.20.0 to 1.21.0.
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Changelog](https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/v1.20.0...service/s3/v1.21.0)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go-v2/service/sts
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-08-01 13:50:10 +00:00

95 lines
2.1 KiB
Go

package awsrulesfn
import (
"strings"
)
// ARN provides AWS ARN components broken out into a data structure.
type ARN struct {
Partition string
Service string
Region string
AccountId string
ResourceId OptionalStringSlice
}
const (
arnDelimiters = ":"
resourceDelimiters = "/:"
arnSections = 6
arnPrefix = "arn:"
// zero-indexed
sectionPartition = 1
sectionService = 2
sectionRegion = 3
sectionAccountID = 4
sectionResource = 5
)
// ParseARN returns an [ARN] value parsed from the input string provided. If
// the ARN cannot be parsed nil will be returned, and error added to
// [ErrorCollector].
func ParseARN(input string) *ARN {
if !strings.HasPrefix(input, arnPrefix) {
return nil
}
sections := strings.SplitN(input, arnDelimiters, arnSections)
if numSections := len(sections); numSections != arnSections {
return nil
}
if sections[sectionPartition] == "" {
return nil
}
if sections[sectionService] == "" {
return nil
}
if sections[sectionResource] == "" {
return nil
}
return &ARN{
Partition: sections[sectionPartition],
Service: sections[sectionService],
Region: sections[sectionRegion],
AccountId: sections[sectionAccountID],
ResourceId: splitResource(sections[sectionResource]),
}
}
// splitResource splits the resource components by the ARN resource delimiters.
func splitResource(v string) []string {
var parts []string
var offset int
for offset <= len(v) {
idx := strings.IndexAny(v[offset:], "/:")
if idx < 0 {
parts = append(parts, v[offset:])
break
}
parts = append(parts, v[offset:idx+offset])
offset += idx + 1
}
return parts
}
// OptionalStringSlice provides a helper to safely get the index of a string
// slice that may be out of bounds. Returns pointer to string if index is
// valid. Otherwise returns nil.
type OptionalStringSlice []string
// Get returns a string pointer of the string at index i if the index is valid.
// Otherwise returns nil.
func (s OptionalStringSlice) Get(i int) *string {
if i < 0 || i >= len(s) {
return nil
}
v := s[i]
return &v
}