2019-09-25 17:13:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2020-03-31 18:19:58 -04:00
|
|
|
"github.com/hashicorp/packer/helper/useragent"
|
|
|
|
|
2020-04-23 05:03:17 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute/computeapi"
|
2019-09-25 17:13:23 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AzureClientSet interface {
|
|
|
|
MetadataClient() MetadataClientAPI
|
|
|
|
|
|
|
|
DisksClient() computeapi.DisksClientAPI
|
2020-03-25 17:36:51 -04:00
|
|
|
SnapshotsClient() computeapi.SnapshotsClientAPI
|
2019-09-25 17:13:23 -04:00
|
|
|
ImagesClient() computeapi.ImagesClientAPI
|
2020-03-25 17:36:51 -04:00
|
|
|
|
|
|
|
GalleryImagesClient() computeapi.GalleryImagesClientAPI
|
|
|
|
GalleryImageVersionsClient() computeapi.GalleryImageVersionsClientAPI
|
|
|
|
|
2019-09-25 17:13:23 -04:00
|
|
|
VirtualMachinesClient() computeapi.VirtualMachinesClientAPI
|
|
|
|
VirtualMachineImagesClient() VirtualMachineImagesClientAPI
|
|
|
|
|
|
|
|
PollClient() autorest.Client
|
2020-03-25 16:11:51 -04:00
|
|
|
|
|
|
|
// SubscriptionID returns the subscription ID that this client set was created for
|
|
|
|
SubscriptionID() string
|
2019-09-25 17:13:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var subscriptionPathRegex = regexp.MustCompile(`/subscriptions/([[:xdigit:]]{8}(-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12})`)
|
|
|
|
|
|
|
|
var _ AzureClientSet = &azureClientSet{}
|
|
|
|
|
|
|
|
type azureClientSet struct {
|
|
|
|
sender autorest.Sender
|
|
|
|
authorizer autorest.Authorizer
|
|
|
|
subscriptionID string
|
|
|
|
PollingDelay time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(c Config, say func(string)) (AzureClientSet, error) {
|
2019-10-22 16:58:30 -04:00
|
|
|
return new(c, say)
|
|
|
|
}
|
|
|
|
|
|
|
|
func new(c Config, say func(string)) (*azureClientSet, error) {
|
2019-10-16 04:40:25 -04:00
|
|
|
token, err := c.GetServicePrincipalToken(say, c.CloudEnvironment().ResourceManagerEndpoint)
|
2019-09-25 17:13:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &azureClientSet{
|
|
|
|
authorizer: autorest.NewBearerAuthorizer(token),
|
|
|
|
subscriptionID: c.SubscriptionID,
|
|
|
|
sender: http.DefaultClient,
|
|
|
|
PollingDelay: time.Second,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:11:51 -04:00
|
|
|
func (s azureClientSet) SubscriptionID() string {
|
|
|
|
return s.subscriptionID
|
|
|
|
}
|
|
|
|
|
2019-09-25 17:13:23 -04:00
|
|
|
func (s azureClientSet) configureAutorestClient(c *autorest.Client) {
|
2019-10-22 16:58:30 -04:00
|
|
|
c.AddToUserAgent(useragent.String())
|
2019-09-25 17:13:23 -04:00
|
|
|
c.Authorizer = s.authorizer
|
|
|
|
c.Sender = s.sender
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s azureClientSet) MetadataClient() MetadataClientAPI {
|
2019-10-22 16:58:30 -04:00
|
|
|
return metadataClient{
|
|
|
|
s.sender,
|
|
|
|
useragent.String(),
|
|
|
|
}
|
2019-09-25 17:13:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s azureClientSet) DisksClient() computeapi.DisksClientAPI {
|
|
|
|
c := compute.NewDisksClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:36:51 -04:00
|
|
|
func (s azureClientSet) SnapshotsClient() computeapi.SnapshotsClientAPI {
|
|
|
|
c := compute.NewSnapshotsClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-09-25 17:13:23 -04:00
|
|
|
func (s azureClientSet) ImagesClient() computeapi.ImagesClientAPI {
|
|
|
|
c := compute.NewImagesClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s azureClientSet) VirtualMachinesClient() computeapi.VirtualMachinesClientAPI {
|
|
|
|
c := compute.NewVirtualMachinesClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s azureClientSet) VirtualMachineImagesClient() VirtualMachineImagesClientAPI {
|
|
|
|
c := compute.NewVirtualMachineImagesClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
2020-03-25 14:18:48 -04:00
|
|
|
return VirtualMachineImagesClient{c}
|
2019-09-25 17:13:23 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 17:36:51 -04:00
|
|
|
func (s azureClientSet) GalleryImagesClient() computeapi.GalleryImagesClientAPI {
|
|
|
|
c := compute.NewGalleryImagesClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s azureClientSet) GalleryImageVersionsClient() computeapi.GalleryImageVersionsClientAPI {
|
|
|
|
c := compute.NewGalleryImageVersionsClient(s.subscriptionID)
|
|
|
|
s.configureAutorestClient(&c.Client)
|
|
|
|
c.PollingDelay = s.PollingDelay
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-09-25 17:13:23 -04:00
|
|
|
func (s azureClientSet) PollClient() autorest.Client {
|
|
|
|
c := autorest.NewClientWithUserAgent("Packer-Azure-ClientSet")
|
|
|
|
s.configureAutorestClient(&c)
|
2020-03-31 18:19:58 -04:00
|
|
|
c.PollingDelay = time.Second * 5
|
2019-09-25 17:13:23 -04:00
|
|
|
return c
|
|
|
|
}
|