| 1 | package paths |
| 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 Data Lake Storage FileSystem |
| 12 | // This can be useful when, for example, you're using this as a unique identifier |
| 13 | func (client Client) GetResourceID(accountName, fileSystemName, path string) string { |
| 14 | domain := endpoints.GetDataLakeStoreEndpoint(client.BaseURI, accountName) |
| 15 | return fmt.Sprintf("%s/%s/%s", domain, fileSystemName, path) |
| 16 | } |
| 17 | |
| 18 | type ResourceID struct { |
| 19 | AccountName string |
| 20 | FileSystemName string |
| 21 | Path string |
| 22 | } |
| 23 | |
| 24 | // ParseResourceID parses the specified Resource ID and returns an object |
| 25 | // which can be used to interact with the Data Lake Storage FileSystem API's |
| 26 | func ParseResourceID(id string) (*ResourceID, error) { |
| 27 | // example: https://foo.dfs.core.windows.net/Bar |
| 28 | if id == "" { |
| 29 | return nil, fmt.Errorf("`id` was empty") |
| 30 | } |
| 31 | |
| 32 | uri, err := url.Parse(id) |
| 33 | if err != nil { |
| 34 | return nil, fmt.Errorf("Error parsing ID as a URL: %s", err) |
| 35 | } |
| 36 | |
| 37 | accountName, err := endpoints.GetAccountNameFromEndpoint(uri.Host) |
| 38 | if err != nil { |
| 39 | return nil, fmt.Errorf("Error parsing Account Name: %s", err) |
| 40 | } |
| 41 | |
| 42 | fileSystemAndPath := strings.TrimPrefix(uri.Path, "/") |
| 43 | separatorIndex := strings.Index(fileSystemAndPath, "/") |
| 44 | var fileSystem, path string |
| 45 | if separatorIndex < 0 { |
| 46 | fileSystem = fileSystemAndPath |
| 47 | path = "" |
| 48 | } else { |
| 49 | fileSystem = fileSystemAndPath[0:separatorIndex] |
| 50 | path = fileSystemAndPath[separatorIndex+1:] |
| 51 | } |
| 52 | return &ResourceID{ |
| 53 | AccountName: *accountName, |
| 54 | FileSystemName: fileSystem, |
| 55 | Path: path, |
| 56 | }, nil |
| 57 | } |