Debian Codegraph @

storage/2018-11-09/blob/containers/resource_id.go

raw
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 // GetResourceManagerResourceID returns the Resource Manager specific
19 // ResourceID for a specific Storage Container
20 func (client Client) GetResourceManagerResourceID(subscriptionID, resourceGroup, accountName, containerName string) string {
21 fmtStr := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/blobServices/default/containers/%s"
22 return fmt.Sprintf(fmtStr, subscriptionID, resourceGroup, accountName, containerName)
23 }
24
25 type ResourceID struct {
26 AccountName string
27 ContainerName string
28 }
29
30 // ParseResourceID parses the Resource ID and returns an object which can be used
31 // to interact with the Container Resource
32 func ParseResourceID(id string) (*ResourceID, error) {
33 // example: https://foo.blob.core.windows.net/Bar
34 if id == "" {
35 return nil, fmt.Errorf("`id` was empty")
36 }
37
38 uri, err := url.Parse(id)
39 if err != nil {
40 return nil, fmt.Errorf("Error parsing ID as a URL: %s", err)
41 }
42
43 accountName, err := endpoints.GetAccountNameFromEndpoint(uri.Host)
44 if err != nil {
45 return nil, fmt.Errorf("Error parsing Account Name: %s", err)
46 }
47
48 containerName := strings.TrimPrefix(uri.Path, "/")
49 return &ResourceID{
50 AccountName: *accountName,
51 ContainerName: containerName,
52 }, nil
53 }