-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource_id.go
80 lines (66 loc) · 2.4 KB
/
resource_id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package directories
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/accounts"
)
// TODO: update this to implement `resourceids.ResourceId` once
// https://github.com/hashicorp/go-azure-helpers/issues/187 is fixed
var _ resourceids.Id = DirectoryId{}
type DirectoryId struct {
// AccountId specifies the ID of the Storage Account where this Directory exists.
AccountId accounts.AccountId
// ShareName specifies the name of the File Share containing this Directory.
ShareName string
// DirectoryPath specifies the path representing this Directory.
DirectoryPath string
}
func NewDirectoryID(accountId accounts.AccountId, shareName, directoryPath string) DirectoryId {
return DirectoryId{
AccountId: accountId,
ShareName: shareName,
DirectoryPath: directoryPath,
}
}
func (b DirectoryId) ID() string {
return fmt.Sprintf("%s/%s/%s", b.AccountId.ID(), b.ShareName, b.DirectoryPath)
}
func (b DirectoryId) String() string {
components := []string{
fmt.Sprintf("Share Name %q", b.ShareName),
fmt.Sprintf("Account %q", b.AccountId.String()),
}
return fmt.Sprintf("Directory Path %q (%s)", b.DirectoryPath, strings.Join(components, " / "))
}
// ParseDirectoryID parses `input` into a Directory ID using a known `domainSuffix`
func ParseDirectoryID(input, domainSuffix string) (*DirectoryId, error) {
// example: https://foo.file.core.windows.net/Bar/some/directory
if input == "" {
return nil, fmt.Errorf("`input` was empty")
}
account, err := accounts.ParseAccountID(input, domainSuffix)
if err != nil {
return nil, fmt.Errorf("parsing account %q: %+v", input, err)
}
if account.SubDomainType != accounts.FileSubDomainType {
return nil, fmt.Errorf("expected the subdomain type to be %q but got %q", string(accounts.FileSubDomainType), string(account.SubDomainType))
}
uri, err := url.Parse(input)
if err != nil {
return nil, fmt.Errorf("parsing %q as a uri: %+v", input, err)
}
path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) < 2 {
return nil, fmt.Errorf("expected the path to contain at least 2 segments but got %d", len(segments))
}
shareName := segments[0]
directoryPath := strings.Join(segments[1:], "/")
return &DirectoryId{
AccountId: *account,
ShareName: shareName,
DirectoryPath: directoryPath,
}, nil
}