parent
b256c77423
commit
91cb3e2833
|
@ -4,14 +4,14 @@ import (
|
|||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
logger *log.Logger
|
||||
|
||||
BaseURL string
|
||||
subscriptionID string
|
||||
|
||||
tokenRequester *tokenRequester
|
||||
|
@ -27,7 +27,6 @@ func NewClient(creds *AzureResourceManagerCredentials) (*Client, error) {
|
|||
tr := newTokenRequester(httpClient, creds.ClientID, creds.ClientSecret, creds.TenantID)
|
||||
|
||||
return &Client{
|
||||
BaseURL: "https://management.azure.com",
|
||||
subscriptionID: creds.SubscriptionID,
|
||||
httpClient: httpClient,
|
||||
tokenRequester: tr,
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package azure
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Endpoints struct {
|
||||
resourceManagerEndpointUrl string
|
||||
activeDirectoryEndpointUrl string
|
||||
}
|
||||
|
||||
var (
|
||||
ChineseEndpoints = Endpoints{"https://management.chinacloudapi.cn", "https://login.chinacloudapi.cn"}
|
||||
DefaultEndpoints = Endpoints{"https://management.azure.com", "https://login.microsoftonline.com"}
|
||||
GermanEndpoints = Endpoints{"https://management.microsoftazure.de", "https://login.microsoftonline.de"}
|
||||
USGovEndpoints = Endpoints{"https://management.usgovcloudapi.net", "https://login.microsoftonline.com"}
|
||||
)
|
||||
|
||||
func GetEndpointsForLocation(location string) Endpoints {
|
||||
location = strings.Replace(strings.ToLower(location), " ", "", -1)
|
||||
|
||||
switch location {
|
||||
case GermanyCentral, GermanyEast:
|
||||
return GermanEndpoints
|
||||
case ChinaEast, ChinaNorth:
|
||||
return ChineseEndpoints
|
||||
case USGovIowa, USGovVirginia:
|
||||
return USGovEndpoints
|
||||
default:
|
||||
return DefaultEndpoints
|
||||
}
|
||||
}
|
||||
|
||||
func GetEndpointsForCommand(command APICall) Endpoints {
|
||||
locationField := reflect.Indirect(reflect.ValueOf(command)).FieldByName("Location")
|
||||
if locationField.IsValid() {
|
||||
location := locationField.Interface().(string)
|
||||
return GetEndpointsForLocation(location)
|
||||
}
|
||||
|
||||
return DefaultEndpoints
|
||||
}
|
|
@ -18,9 +18,16 @@ const (
|
|||
JapanWest = "japanwest"
|
||||
BrazilSouth = "brazilsouth"
|
||||
AustraliaEast = "australiaeast"
|
||||
Australia = "australia"
|
||||
AustraliaSouthEast = "australiasoutheast"
|
||||
CentralIndia = "centralindia"
|
||||
SouthIndia = "southindia"
|
||||
WestIndia = "westindia"
|
||||
ChinaEast = "chinaeast"
|
||||
ChinaNorth = "chinanorth"
|
||||
UKSouth = "uksouth"
|
||||
UKWest = "ukwest"
|
||||
CanadaCentral = "canadacentral"
|
||||
CanadaEast = "canadaeast"
|
||||
GermanyCentral = "germanycentral"
|
||||
GermanyEast = "germanyeast"
|
||||
)
|
||||
|
|
|
@ -75,7 +75,8 @@ func (request *Request) pollForAsynchronousResponse(acceptedResponse *http.Respo
|
|||
return nil, err
|
||||
}
|
||||
|
||||
err = request.client.tokenRequester.addAuthorizationToRequest(req)
|
||||
endpoints := GetEndpointsForCommand(request.Command)
|
||||
err = request.client.tokenRequester.addAuthorizationToRequest(req, endpoints)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -115,10 +116,11 @@ func defaultARMRequestSerialize(body interface{}) (io.ReadSeeker, error) {
|
|||
func (request *Request) Execute() (*Response, error) {
|
||||
apiInfo := request.Command.APIInfo()
|
||||
|
||||
endpoints := GetEndpointsForCommand(request.Command)
|
||||
|
||||
var urlString string
|
||||
|
||||
// Base URL should already be validated by now so Parse is safe without error handling
|
||||
urlObj, _ := url.Parse(request.client.BaseURL)
|
||||
urlObj, _ := url.Parse(endpoints.resourceManagerEndpointUrl)
|
||||
|
||||
// Determine whether to use the URLPathFunc or the URI explicitly set in the request
|
||||
if request.URI == nil {
|
||||
|
@ -164,7 +166,7 @@ func (request *Request) Execute() (*Response, error) {
|
|||
req.Header.Add("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
err = request.client.tokenRequester.addAuthorizationToRequest(req)
|
||||
err = request.client.tokenRequester.addAuthorizationToRequest(req, endpoints)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -43,8 +44,8 @@ func newTokenRequester(client *retryablehttp.Client, clientID, clientSecret, ten
|
|||
// addAuthorizationToRequest adds an Authorization header to an http.Request, having ensured
|
||||
// that the token is sufficiently fresh. This may invoke network calls, so should not be
|
||||
// relied on to return quickly.
|
||||
func (tr *tokenRequester) addAuthorizationToRequest(request *retryablehttp.Request) error {
|
||||
token, err := tr.getUsableToken()
|
||||
func (tr *tokenRequester) addAuthorizationToRequest(request *retryablehttp.Request, endpoints Endpoints) error {
|
||||
token, err := tr.getUsableToken(endpoints)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error obtaining authorization token: %s", err)
|
||||
}
|
||||
|
@ -53,7 +54,7 @@ func (tr *tokenRequester) addAuthorizationToRequest(request *retryablehttp.Reque
|
|||
return nil
|
||||
}
|
||||
|
||||
func (tr *tokenRequester) getUsableToken() (*token, error) {
|
||||
func (tr *tokenRequester) getUsableToken(endpoints Endpoints) (*token, error) {
|
||||
tr.l.Lock()
|
||||
defer tr.l.Unlock()
|
||||
|
||||
|
@ -61,7 +62,7 @@ func (tr *tokenRequester) getUsableToken() (*token, error) {
|
|||
return tr.currentToken, nil
|
||||
}
|
||||
|
||||
newToken, err := tr.refreshToken()
|
||||
newToken, err := tr.refreshToken(endpoints)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error refreshing token: %s", err)
|
||||
}
|
||||
|
@ -70,14 +71,14 @@ func (tr *tokenRequester) getUsableToken() (*token, error) {
|
|||
return newToken, nil
|
||||
}
|
||||
|
||||
func (tr *tokenRequester) refreshToken() (*token, error) {
|
||||
oauthURL := fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/%s?api-version=1.0", tr.tenantID, "token")
|
||||
func (tr *tokenRequester) refreshToken(endpoints Endpoints) (*token, error) {
|
||||
oauthURL := fmt.Sprintf("%s/%s/oauth2/%s?api-version=1.0", endpoints.activeDirectoryEndpointUrl, tr.tenantID, "token")
|
||||
|
||||
v := url.Values{}
|
||||
v.Set("client_id", tr.clientID)
|
||||
v.Set("client_secret", tr.clientSecret)
|
||||
v.Set("grant_type", "client_credentials")
|
||||
v.Set("resource", "https://management.azure.com/")
|
||||
v.Set("resource", strings.TrimSuffix(endpoints.resourceManagerEndpointUrl, "/")+"/")
|
||||
|
||||
var newToken token
|
||||
response, err := tr.httpClient.PostForm(oauthURL, v)
|
||||
|
|
|
@ -6,7 +6,7 @@ type GetDatabaseResponse struct {
|
|||
ID *string `mapstructure:"id"`
|
||||
Name *string `mapstructure:"name"`
|
||||
Location *string `mapstructure:"location"`
|
||||
Tags *map[string]string `mapstructure:"tags"`
|
||||
Tags *map[string]*string `mapstructure:"tags"`
|
||||
Kind *string `mapstructure:"kind"`
|
||||
DatabaseID *string `mapstructure:"databaseId"`
|
||||
DatabaseName *string `mapstructure:"databaseName"`
|
||||
|
|
|
@ -1451,34 +1451,34 @@
|
|||
"revisionTime": "2016-09-29T21:48:42Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "oPpOfZn11Ef6DWOoETxSW9Venzs=",
|
||||
"checksumSHA1": "tRK0n/cIjBCYjnumPiP9cCS2CnA=",
|
||||
"path": "github.com/jen20/riviera/azure",
|
||||
"revision": "1159d86fc8144abafeb29ee7c5a3e2e85af336ba",
|
||||
"revisionTime": "2016-06-30T14:11:29Z"
|
||||
"revision": "04d3aa8ac7df6c05dbfeaf5344f754f31a7e711f",
|
||||
"revisionTime": "2016-11-01T10:00:17Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "ncdT+1PFEF5ly0niXuQc9/pKzT0=",
|
||||
"path": "github.com/jen20/riviera/dns",
|
||||
"revision": "1159d86fc8144abafeb29ee7c5a3e2e85af336ba",
|
||||
"revisionTime": "2016-06-30T14:11:29Z"
|
||||
"revision": "04d3aa8ac7df6c05dbfeaf5344f754f31a7e711f",
|
||||
"revisionTime": "2016-11-01T10:00:17Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "zVXx6ha3bt0N4ukRbRHXjSl91S4=",
|
||||
"path": "github.com/jen20/riviera/search",
|
||||
"revision": "1159d86fc8144abafeb29ee7c5a3e2e85af336ba",
|
||||
"revisionTime": "2016-06-30T14:11:29Z"
|
||||
"revision": "04d3aa8ac7df6c05dbfeaf5344f754f31a7e711f",
|
||||
"revisionTime": "2016-11-01T10:00:17Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "KfquDaeBPGchw92QnojlJFsJKgk=",
|
||||
"checksumSHA1": "mD+brnqSfkLheYdGzTUqUi6VWgw=",
|
||||
"path": "github.com/jen20/riviera/sql",
|
||||
"revision": "1159d86fc8144abafeb29ee7c5a3e2e85af336ba",
|
||||
"revisionTime": "2016-06-30T14:11:29Z"
|
||||
"revision": "04d3aa8ac7df6c05dbfeaf5344f754f31a7e711f",
|
||||
"revisionTime": "2016-11-01T10:00:17Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "nKUCquNpJ9ifHgkXoT4K3Xar6R8=",
|
||||
"path": "github.com/jen20/riviera/storage",
|
||||
"revision": "1159d86fc8144abafeb29ee7c5a3e2e85af336ba",
|
||||
"revisionTime": "2016-06-30T14:11:29Z"
|
||||
"revision": "04d3aa8ac7df6c05dbfeaf5344f754f31a7e711f",
|
||||
"revisionTime": "2016-11-01T10:00:17Z"
|
||||
},
|
||||
{
|
||||
"comment": "0.2.2-2-gc01cf91",
|
||||
|
|
Loading…
Reference in New Issue