| 1 |
package filesystems |
| 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, shareName string) string { |
| 14 |
domain := endpoints.GetDataLakeStoreEndpoint(client.BaseURI, accountName) |
| 15 |
return fmt.Sprintf("%s/%s", domain, shareName) |
| 16 |
} |
| 17 |
|
| 18 |
type ResourceID struct { |
| 19 |
AccountName string |
| 20 |
DirectoryName string |
| 21 |
} |
| 22 |
|
| 23 |
// ParseResourceID parses the specified Resource ID and returns an object |
| 24 |
// which can be used to interact with the Data Lake Storage FileSystem API's |
| 25 |
func ParseResourceID(id string) (*ResourceID, error) { |
| 26 |
// example: https://foo.dfs.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 |
directoryName := strings.TrimPrefix(uri.Path, "/") |
| 42 |
return &ResourceID{ |
| 43 |
AccountName: *accountName, |
| 44 |
DirectoryName: directoryName, |
| 45 |
}, nil |
| 46 |
} |