| 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 9 | ) |
| 10 | |
| 11 | // GetResourceID returns the Resource ID for the given Container |
| 12 | // This can be useful when, for example, you're using this as a unique identifier |
| 13 | func (client Client) GetResourceID(accountName, containerName string) string { |
| 14 | domain := endpoints.GetBlobEndpoint(client.BaseURI, accountName) |
| 15 | return fmt.Sprintf("%s/%s", domain, containerName) |
| 16 | } |
| 17 | |
| 18 | type ResourceID struct { |
| 19 | AccountName string |
| 20 | ContainerName string |
| 21 | } |
| 22 | |
| 23 | // ParseResourceID parses the Resource ID and returns an object which can be used |
| 24 | // to interact with the Container Resource |
| 25 | func ParseResourceID(id string) (*ResourceID, error) { |
| 26 | // example: https://foo.blob.core.windows.net/Bar |
| 27 | if id == "" { |
| 28 | return nil, fmt.Errorf("`id` was empty") |
| 29 | } |
| 30 | |
| 31 | uri, err := url.Parse(id) |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("Error parsing ID as a URL: %s", err) |
| 34 | } |
| 35 | |
| 36 | accountName, err := endpoints.GetAccountNameFromEndpoint(uri.Host) |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("Error parsing Account Name: %s", err) |
| 39 | } |
| 40 | |
| 41 | containerName := strings.TrimPrefix(uri.Path, "/") |
| 42 | return &ResourceID{ |
| 43 | AccountName: *accountName, |
| 44 | ContainerName: containerName, |
| 45 | }, nil |
| 46 | } |