Debian Codegraph @

storage/2020-08-04/file/shares/resource_id.go

raw
1 package shares
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 File Share
12 // This can be useful when, for example, you're using this as a unique identifier
13 func (client Client) GetResourceID(accountName, shareName string) string {
14 domain := endpoints.GetFileEndpoint(client.BaseURI, accountName)
15 return fmt.Sprintf("%s/%s", domain, shareName)
16 }
17
18 // GetResourceManagerResourceID returns the Resource Manager specific
19 // ResourceID for a specific Storage Share
20 func (client Client) GetResourceManagerResourceID(subscriptionID, resourceGroup, accountName, shareName string) string {
21 fmtStr := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/fileServices/default/shares/%s"
22 return fmt.Sprintf(fmtStr, subscriptionID, resourceGroup, accountName, shareName)
23 }
24
25 type ResourceID struct {
26 AccountName string
27 ShareName string
28 }
29
30 // ParseResourceID parses the specified Resource ID and returns an object
31 // which can be used to interact with the Storage Shares SDK
32 func ParseResourceID(id string) (*ResourceID, error) {
33 // example: https://foo.file.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 shareName := strings.TrimPrefix(uri.Path, "/")
49 return &ResourceID{
50 AccountName: *accountName,
51 ShareName: shareName,
52 }, nil
53 }