Debian Codegraph @

storage/2017-07-29/queue/messages/resource_id.go

raw
1 package messages
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 Message within a Queue
12 // This can be useful when, for example, you're using this as a unique identifier
13 func (client Client) GetResourceID(accountName, queueName, messageID string) string {
14 domain := endpoints.GetQueueEndpoint(client.BaseURI, accountName)
15 return fmt.Sprintf("%s/%s/messages/%s", domain, queueName, messageID)
16 }
17
18 type ResourceID struct {
19 AccountName string
20 QueueName string
21 MessageID string
22 }
23
24 // ParseResourceID parses the specified Resource ID and returns an object
25 // which can be used to interact with the Message within a Queue
26 func ParseResourceID(id string) (*ResourceID, error) {
27 // example: https://account1.queue.core.chinacloudapi.cn/queue1/messages/message1
28
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 path := strings.TrimPrefix(uri.Path, "/")
44 segments := strings.Split(path, "/")
45 if len(segments) != 3 {
46 return nil, fmt.Errorf("Expected the path to contain 3 segments but got %d", len(segments))
47 }
48
49 queueName := segments[0]
50 messageID := segments[2]
51 return &ResourceID{
52 AccountName: *accountName,
53 MessageID: messageID,
54 QueueName: queueName,
55 }, nil
56 }