| 1 |
package tables |
| 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 Table |
| 12 |
// This can be useful when, for example, you're using this as a unique identifier |
| 13 |
func (client Client) GetResourceID(accountName, tableName string) string { |
| 14 |
domain := endpoints.GetTableEndpoint(client.BaseURI, accountName) |
| 15 |
return fmt.Sprintf("%s/Tables('%s')", domain, tableName) |
| 16 |
} |
| 17 |
|
| 18 |
type ResourceID struct { |
| 19 |
AccountName string |
| 20 |
TableName string |
| 21 |
} |
| 22 |
|
| 23 |
// ParseResourceID parses the Resource ID and returns an object which |
| 24 |
// can be used to interact with the Table within the specified Storage Account |
| 25 |
func ParseResourceID(id string) (*ResourceID, error) { |
| 26 |
// example: https://foo.table.core.windows.net/Table('foo') |
| 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 |
// assume there a `Table('')` |
| 42 |
path := strings.TrimPrefix(uri.Path, "/") |
| 43 |
if !strings.HasPrefix(path, "Tables('") || !strings.HasSuffix(path, "')") { |
| 44 |
return nil, fmt.Errorf("Expected the Table Name to be in the format `Tables('name')` but got %q", path) |
| 45 |
} |
| 46 |
|
| 47 |
// strip off the `Table('')` |
| 48 |
tableName := strings.TrimPrefix(uri.Path, "/Tables('") |
| 49 |
tableName = strings.TrimSuffix(tableName, "')") |
| 50 |
return &ResourceID{ |
| 51 |
AccountName: *accountName, |
| 52 |
TableName: tableName, |
| 53 |
}, nil |
| 54 |
} |