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
|
|
|
|
|
|
|
|
subscriptionID string
|
|
|
|
|
|
|
|
tokenRequester *tokenRequester
|
|
|
|
httpClient *retryablehttp.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(creds *AzureResourceManagerCredentials) (*Client, error) {
|
|
|
|
defaultLogger := log.New(ioutil.Discard, "", 0)
|
|
|
|
|
|
|
|
httpClient := retryablehttp.NewClient()
|
|
|
|
httpClient.Logger = defaultLogger
|
|
|
|
|
|
|
|
tr := newTokenRequester(httpClient, creds.ClientID, creds.ClientSecret, creds.TenantID)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
subscriptionID: creds.SubscriptionID,
|
|
|
|
httpClient: httpClient,
|
|
|
|
tokenRequester: tr,
|
|
|
|
logger: defaultLogger,
|
|
|
|
}, 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,
|
|
|
|
}
|
|
|
|
}
|