~jtv/gwacl/storage-tool-command-line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2013 Canonical Ltd.  This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).

package gwacl

import (
    "fmt"
    "net/url"
    "strings"
)

// APIEndpoint describes the base URL for accesing Windows Azure's APIs.
//
// Azure will have subdomains on this URL's domain, such as blob.<domain> for
// storage, with further sub-domains for storage accounts; management.<domain>
// for the management API; and possibly more such as queue.<domain>,
// table.<domain>.  APIEndpoint defines methods to obtain these URLs.
type APIEndpoint string

// GetEndpoint returns the API endpoint for the given location.  This is
// hard-coded, so some guesswork may be involved.
func GetEndpoint(location string) APIEndpoint {
    if strings.Contains(location, "China") {
        // Mainland China is a special case.  It has its own endpoint.
        return "https://core.chinacloudapi.cn/"
    }

    // The rest of the world shares a single endpoint.
    return "https://core.windows.net/"
}

// prefixHost prefixes the hostname part of a URL with a subdomain.  For
// example, prefixHost("foo", "http://example.com") becomes
// "http://foo.example.com".
//
// The URL must be well-formed, and contain a hostname.
func prefixHost(host, originalURL string) string {
    parsedURL, err := url.Parse(originalURL)
    if err != nil {
        panic(fmt.Errorf("failed to parse URL %s - %v", originalURL, err))
    }
    if parsedURL.Host == "" {
        panic(fmt.Errorf("no hostname in URL '%s'", originalURL))
    }
    // Escape manually.  Strangely, turning a url.URL into a string does not
    // do this for you.
    parsedURL.Host = url.QueryEscape(host) + "." + parsedURL.Host
    return parsedURL.String()
}

// ManagementAPI returns the URL for the endpoint's management API.
func (endpoint APIEndpoint) ManagementAPI() string {
    return prefixHost("management", string(endpoint))
}

// BlobStorageAPI returns the base URL for the endpoint's blob storage API.
//
// Actual storage API requests are made to subdomains of this URL.  To address
// a particular storage account, prefix it as a subdomain to the hostname
// portion of this URL.
func (endpoint APIEndpoint) BlobStorageAPI() string {
    return prefixHost("blob", string(endpoint))
}