2016-02-03 19:00:55 +01:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2016-10-31 13:19:20 +01:00
|
|
|
"net/http"
|
2016-11-01 11:15:37 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-retryablehttp"
|
2016-02-03 19:00:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
logger *log.Logger
|
|
|
|
|
2017-01-04 13:00:14 +01:00
|
|
|
subscriptionID string
|
|
|
|
resourceManagerEndpoint string
|
2016-02-03 19:00:55 +01:00
|
|
|
|
|
|
|
tokenRequester *tokenRequester
|
|
|
|
httpClient *retryablehttp.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(creds *AzureResourceManagerCredentials) (*Client, error) {
|
|
|
|
defaultLogger := log.New(ioutil.Discard, "", 0)
|
|
|
|
|
|
|
|
httpClient := retryablehttp.NewClient()
|
|
|
|
httpClient.Logger = defaultLogger
|
|
|
|
|
2017-01-04 13:00:14 +01:00
|
|
|
if creds.ResourceManagerEndpoint == "" {
|
|
|
|
creds.ResourceManagerEndpoint = defaultResourceManagerEndpoint
|
|
|
|
}
|
|
|
|
if creds.ActiveDirectoryEndpoint == "" {
|
|
|
|
creds.ActiveDirectoryEndpoint = defaultActiveDirectoryEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := newTokenRequester(httpClient, creds)
|
2016-02-03 19:00:55 +01:00
|
|
|
|
|
|
|
return &Client{
|
2017-01-04 13:00:14 +01:00
|
|
|
subscriptionID: creds.SubscriptionID,
|
|
|
|
resourceManagerEndpoint: creds.ResourceManagerEndpoint,
|
|
|
|
httpClient: httpClient,
|
|
|
|
tokenRequester: tr,
|
|
|
|
logger: defaultLogger,
|
2016-02-03 19:00:55 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-01 11:15:37 +01:00
|
|
|
func (c *Client) SetRequestLoggingHook(hook func(*log.Logger, *http.Request, int)) {
|
2016-05-07 02:36:25 +02:00
|
|
|
c.httpClient.RequestLogHook = hook
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:00:55 +01:00
|
|
|
func (c *Client) SetLogger(newLogger *log.Logger) {
|
|
|
|
c.logger = newLogger
|
|
|
|
c.httpClient.Logger = newLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) NewRequest() *Request {
|
|
|
|
return &Request{
|
|
|
|
client: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) NewRequestForURI(resourceURI string) *Request {
|
|
|
|
return &Request{
|
|
|
|
URI: &resourceURI,
|
|
|
|
client: c,
|
|
|
|
}
|
|
|
|
}
|