| 1 | package queues |
| 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 Queue |
| 12 | // This can be useful when, for example, you're using this as a unique identifier |
| 13 | func (client Client) GetResourceID(accountName, queueName string) string { |
| 14 | domain := endpoints.GetQueueEndpoint(client.BaseURI, accountName) |
| 15 | return fmt.Sprintf("%s/%s", domain, queueName) |
| 16 | } |
| 17 | |
| 18 | type ResourceID struct { |
| 19 | AccountName string |
| 20 | QueueName string |
| 21 | } |
| 22 | |
| 23 | // ParseResourceID parses the Resource ID and returns an Object which |
| 24 | // can be used to interact with a Queue within a Storage Account |
| 25 | func ParseResourceID(id string) (*ResourceID, error) { |
| 26 | // example: https://foo.queue.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 | queueName := strings.TrimPrefix(uri.Path, "/") |
| 42 | return &ResourceID{ |
| 43 | AccountName: *accountName, |
| 44 | QueueName: queueName, |
| 45 | }, nil |
| 46 | } |