| 1 |
package entities |
| 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 Entity |
| 12 |
// This can be useful when, for example, you're using this as a unique identifier |
| 13 |
func (client Client) GetResourceID(accountName, tableName, partitionKey, rowKey string) string { |
| 14 |
domain := endpoints.GetTableEndpoint(client.BaseURI, accountName) |
| 15 |
return fmt.Sprintf("%s/%s(PartitionKey='%s',RowKey='%s')", domain, tableName, partitionKey, rowKey) |
| 16 |
} |
| 17 |
|
| 18 |
type ResourceID struct { |
| 19 |
AccountName string |
| 20 |
TableName string |
| 21 |
PartitionKey string |
| 22 |
RowKey string |
| 23 |
} |
| 24 |
|
| 25 |
// ParseResourceID parses the specified Resource ID and returns an object which |
| 26 |
// can be used to look up the specified Entity within the specified Table |
| 27 |
func ParseResourceID(id string) (*ResourceID, error) { |
| 28 |
// example: https://account1.table.core.chinacloudapi.cn/table1(PartitionKey='partition1',RowKey='row1') |
| 29 |
if id == "" { |
| 30 |
return nil, fmt.Errorf("`id` was empty") |
| 31 |
} |
| 32 |
|
| 33 |
uri, err := url.Parse(id) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("Error parsing ID as a URL: %s", err) |
| 36 |
} |
| 37 |
|
| 38 |
accountName, err := endpoints.GetAccountNameFromEndpoint(uri.Host) |
| 39 |
if err != nil { |
| 40 |
return nil, fmt.Errorf("Error parsing Account Name: %s", err) |
| 41 |
} |
| 42 |
|
| 43 |
// assume there a `Table('')` |
| 44 |
path := strings.TrimPrefix(uri.Path, "/") |
| 45 |
if !strings.Contains(uri.Path, "(") || !strings.HasSuffix(uri.Path, ")") { |
| 46 |
return nil, fmt.Errorf("Expected the Table Name to be in the format `tables(PartitionKey='',RowKey='')` but got %q", path) |
| 47 |
} |
| 48 |
|
| 49 |
// NOTE: honestly this could probably be a RegEx, but this seemed like the simplest way to |
| 50 |
// allow these two fields to be specified in either order |
| 51 |
indexOfBracket := strings.IndexByte(path, '(') |
| 52 |
tableName := path[0:indexOfBracket] |
| 53 |
|
| 54 |
// trim off the brackets |
| 55 |
temp := strings.TrimPrefix(path, fmt.Sprintf("%s(", tableName)) |
| 56 |
temp = strings.TrimSuffix(temp, ")") |
| 57 |
|
| 58 |
dictionary := strings.Split(temp, ",") |
| 59 |
partitionKey := "" |
| 60 |
rowKey := "" |
| 61 |
for _, v := range dictionary { |
| 62 |
split := strings.Split(v, "=") |
| 63 |
if len(split) != 2 { |
| 64 |
return nil, fmt.Errorf("Expected 2 segments but got %d for %q", len(split), v) |
| 65 |
} |
| 66 |
|
| 67 |
key := split[0] |
| 68 |
value := strings.TrimSuffix(strings.TrimPrefix(split[1], "'"), "'") |
| 69 |
if strings.EqualFold(key, "PartitionKey") { |
| 70 |
partitionKey = value |
| 71 |
} else if strings.EqualFold(key, "RowKey") { |
| 72 |
rowKey = value |
| 73 |
} else { |
| 74 |
return nil, fmt.Errorf("Unexpected Key %q", key) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if partitionKey == "" { |
| 79 |
return nil, fmt.Errorf("Expected a PartitionKey but didn't get one") |
| 80 |
} |
| 81 |
if rowKey == "" { |
| 82 |
return nil, fmt.Errorf("Expected a RowKey but didn't get one") |
| 83 |
} |
| 84 |
|
| 85 |
return &ResourceID{ |
| 86 |
AccountName: *accountName, |
| 87 |
TableName: tableName, |
| 88 |
PartitionKey: partitionKey, |
| 89 |
RowKey: rowKey, |
| 90 |
}, nil |
| 91 |
} |