2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-04-06 04:12:58 -04:00
|
|
|
"context"
|
2016-04-21 19:50:03 -04:00
|
|
|
"encoding/json"
|
2018-04-05 14:28:46 -04:00
|
|
|
"fmt"
|
2016-04-21 19:50:03 -04:00
|
|
|
"math"
|
|
|
|
"net/http"
|
2016-07-07 20:28:47 -04:00
|
|
|
"net/url"
|
2016-04-21 19:50:03 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2019-07-26 18:20:32 -04:00
|
|
|
"time"
|
2016-04-21 19:50:03 -04:00
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
2019-06-07 16:47:37 -04:00
|
|
|
newCompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
|
2020-03-18 16:38:38 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2018-02-14/keyvault"
|
2018-04-06 04:12:58 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-01-01/network"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
|
|
|
|
armStorage "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-10-01/storage"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/Azure/azure-sdk-for-go/storage"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest"
|
2017-05-29 00:06:09 -04:00
|
|
|
"github.com/Azure/go-autorest/autorest/adal"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/useragent"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common"
|
2020-11-09 20:09:32 -05:00
|
|
|
"github.com/hashicorp/packer/builder/azure/version"
|
2016-04-21 19:50:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
EnvPackerLogAzureMaxLen = "PACKER_LOG_AZURE_MAXLEN"
|
|
|
|
)
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
type AzureClient struct {
|
|
|
|
storage.BlobStorageClient
|
2016-04-21 19:50:03 -04:00
|
|
|
resources.DeploymentsClient
|
2017-11-06 00:16:58 -05:00
|
|
|
resources.DeploymentOperationsClient
|
2016-04-21 19:50:03 -04:00
|
|
|
resources.GroupsClient
|
|
|
|
network.PublicIPAddressesClient
|
2016-06-30 19:51:52 -04:00
|
|
|
network.InterfacesClient
|
|
|
|
network.SubnetsClient
|
|
|
|
network.VirtualNetworksClient
|
2019-10-08 17:56:43 -04:00
|
|
|
network.SecurityGroupsClient
|
2017-05-29 00:06:09 -04:00
|
|
|
compute.ImagesClient
|
2016-04-21 19:50:03 -04:00
|
|
|
compute.VirtualMachinesClient
|
|
|
|
common.VaultClient
|
|
|
|
armStorage.AccountsClient
|
2018-04-06 04:12:58 -04:00
|
|
|
compute.DisksClient
|
2018-11-05 18:48:22 -05:00
|
|
|
compute.SnapshotsClient
|
2019-06-14 17:54:27 -04:00
|
|
|
newCompute.GalleryImageVersionsClient
|
2019-06-18 01:34:26 -04:00
|
|
|
newCompute.GalleryImagesClient
|
2016-04-21 19:50:03 -04:00
|
|
|
|
|
|
|
InspectorMaxLength int
|
|
|
|
Template *CaptureTemplate
|
2017-06-08 20:57:59 -04:00
|
|
|
LastError azureErrorResponse
|
2020-03-18 16:38:38 -04:00
|
|
|
VaultClientDelete keyvault.VaultsClient
|
2016-04-21 19:50:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCaptureResponse(body string) *CaptureTemplate {
|
|
|
|
var operation CaptureOperation
|
|
|
|
err := json.Unmarshal([]byte(body), &operation)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if operation.Properties != nil && operation.Properties.Output != nil {
|
|
|
|
return operation.Properties.Output
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(chrboum): This method is a hack. It was written to work around this issue
|
|
|
|
// (https://github.com/Azure/azure-sdk-for-go/issues/307) and to an extent this
|
|
|
|
// issue (https://github.com/Azure/azure-rest-api-specs/issues/188).
|
|
|
|
//
|
|
|
|
// Capturing a VM is a long running operation that requires polling. There are
|
|
|
|
// couple different forms of polling, and the end result of a poll operation is
|
|
|
|
// discarded by the SDK. It is expected that any discarded data can be re-fetched,
|
|
|
|
// so discarding it has minimal impact. Unfortunately, there is no way to re-fetch
|
|
|
|
// the template returned by a capture call that I am aware of.
|
|
|
|
//
|
|
|
|
// If the second issue were fixed the VM ID would be included when GET'ing a VM. The
|
|
|
|
// VM ID could be used to locate the captured VHD, and captured template.
|
|
|
|
// Unfortunately, the VM ID is not included so this method cannot be used either.
|
|
|
|
//
|
|
|
|
// This code captures the template and saves it to the client (the AzureClient type).
|
|
|
|
// It expects that the capture API is called only once, or rather you only care that the
|
|
|
|
// last call's value is important because subsequent requests are not persisted. There
|
|
|
|
// is no care given to multiple threads writing this value because for our use case
|
|
|
|
// it does not matter.
|
|
|
|
func templateCapture(client *AzureClient) autorest.RespondDecorator {
|
|
|
|
return func(r autorest.Responder) autorest.Responder {
|
|
|
|
return autorest.ResponderFunc(func(resp *http.Response) error {
|
|
|
|
body, bodyString := handleBody(resp.Body, math.MaxInt64)
|
|
|
|
resp.Body = body
|
|
|
|
|
|
|
|
captureTemplate := getCaptureResponse(bodyString)
|
|
|
|
if captureTemplate != nil {
|
|
|
|
client.Template = captureTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Respond(resp)
|
|
|
|
})
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2017-06-08 20:57:59 -04:00
|
|
|
func errorCapture(client *AzureClient) autorest.RespondDecorator {
|
|
|
|
return func(r autorest.Responder) autorest.Responder {
|
|
|
|
return autorest.ResponderFunc(func(resp *http.Response) error {
|
|
|
|
body, bodyString := handleBody(resp.Body, math.MaxInt64)
|
|
|
|
resp.Body = body
|
|
|
|
|
|
|
|
errorResponse := newAzureErrorResponse(bodyString)
|
|
|
|
if errorResponse != nil {
|
|
|
|
client.LastError = *errorResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Respond(resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
// WAITING(chrboum): I have logged https://github.com/Azure/azure-sdk-for-go/issues/311 to get this
|
|
|
|
// method included in the SDK. It has been accepted, and I'll cut over to the official way
|
|
|
|
// once it ships.
|
|
|
|
func byConcatDecorators(decorators ...autorest.RespondDecorator) autorest.RespondDecorator {
|
|
|
|
return func(r autorest.Responder) autorest.Responder {
|
|
|
|
return autorest.DecorateResponder(r, decorators...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:13:32 -04:00
|
|
|
func NewAzureClient(subscriptionID, sigSubscriptionID, resourceGroupName, storageAccountName string,
|
|
|
|
cloud *azure.Environment, sharedGalleryTimeout time.Duration, pollingDuration time.Duration,
|
2017-05-28 03:38:45 -04:00
|
|
|
servicePrincipalToken, servicePrincipalTokenVault *adal.ServicePrincipalToken) (*AzureClient, error) {
|
2016-04-21 19:50:03 -04:00
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
var azureClient = &AzureClient{}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
maxlen := getInspectorMaxLength()
|
|
|
|
|
2016-07-20 14:23:51 -04:00
|
|
|
azureClient.DeploymentsClient = resources.NewDeploymentsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.DeploymentsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient.DeploymentsClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.DeploymentsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.DeploymentsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.DeploymentsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.DeploymentsClient.Client.PollingDuration = pollingDuration
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-11-06 00:16:58 -05:00
|
|
|
azureClient.DeploymentOperationsClient = resources.NewDeploymentOperationsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
|
|
|
azureClient.DeploymentOperationsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.DeploymentOperationsClient.RequestInspector = withInspection(maxlen)
|
|
|
|
azureClient.DeploymentOperationsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.DeploymentOperationsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.DeploymentOperationsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.DeploymentOperationsClient.Client.PollingDuration = pollingDuration
|
2017-11-06 00:16:58 -05:00
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
azureClient.DisksClient = compute.NewDisksClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-11-06 00:16:58 -05:00
|
|
|
azureClient.DisksClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.DisksClient.RequestInspector = withInspection(maxlen)
|
|
|
|
azureClient.DisksClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.DisksClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.DisksClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.DisksClient.Client.PollingDuration = pollingDuration
|
2017-11-06 00:16:58 -05:00
|
|
|
|
2016-07-20 14:23:51 -04:00
|
|
|
azureClient.GroupsClient = resources.NewGroupsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.GroupsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient.GroupsClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.GroupsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.GroupsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.GroupsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.GroupsClient.Client.PollingDuration = pollingDuration
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2017-05-29 00:06:09 -04:00
|
|
|
azureClient.ImagesClient = compute.NewImagesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
|
|
|
azureClient.ImagesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.ImagesClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.ImagesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.ImagesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.ImagesClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.ImagesClient.Client.PollingDuration = pollingDuration
|
2017-05-29 00:06:09 -04:00
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
azureClient.InterfacesClient = network.NewInterfacesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.InterfacesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-06-30 19:51:52 -04:00
|
|
|
azureClient.InterfacesClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.InterfacesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.InterfacesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.InterfacesClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.InterfacesClient.Client.PollingDuration = pollingDuration
|
2016-06-30 19:51:52 -04:00
|
|
|
|
|
|
|
azureClient.SubnetsClient = network.NewSubnetsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.SubnetsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-06-30 19:51:52 -04:00
|
|
|
azureClient.SubnetsClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.SubnetsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.SubnetsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.SubnetsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.SubnetsClient.Client.PollingDuration = pollingDuration
|
2016-06-30 19:51:52 -04:00
|
|
|
|
|
|
|
azureClient.VirtualNetworksClient = network.NewVirtualNetworksClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.VirtualNetworksClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-06-30 19:51:52 -04:00
|
|
|
azureClient.VirtualNetworksClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.VirtualNetworksClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.VirtualNetworksClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.VirtualNetworksClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.VirtualNetworksClient.Client.PollingDuration = pollingDuration
|
2016-06-30 19:51:52 -04:00
|
|
|
|
2019-10-08 17:56:43 -04:00
|
|
|
azureClient.SecurityGroupsClient = network.NewSecurityGroupsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
|
|
|
azureClient.SecurityGroupsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.SecurityGroupsClient.RequestInspector = withInspection(maxlen)
|
|
|
|
azureClient.SecurityGroupsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.SecurityGroupsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.SecurityGroupsClient.UserAgent)
|
2019-10-08 17:56:43 -04:00
|
|
|
|
2016-07-20 14:23:51 -04:00
|
|
|
azureClient.PublicIPAddressesClient = network.NewPublicIPAddressesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.PublicIPAddressesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient.PublicIPAddressesClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.PublicIPAddressesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.PublicIPAddressesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.PublicIPAddressesClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.PublicIPAddressesClient.Client.PollingDuration = pollingDuration
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-07-20 14:23:51 -04:00
|
|
|
azureClient.VirtualMachinesClient = compute.NewVirtualMachinesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.VirtualMachinesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient.VirtualMachinesClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.VirtualMachinesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), templateCapture(azureClient), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.VirtualMachinesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.VirtualMachinesClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.VirtualMachinesClient.Client.PollingDuration = pollingDuration
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2018-11-05 18:48:22 -05:00
|
|
|
azureClient.SnapshotsClient = compute.NewSnapshotsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
|
|
|
azureClient.SnapshotsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.SnapshotsClient.RequestInspector = withInspection(maxlen)
|
2018-11-06 14:17:03 -05:00
|
|
|
azureClient.SnapshotsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.SnapshotsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.SnapshotsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.SnapshotsClient.Client.PollingDuration = pollingDuration
|
2018-11-05 18:48:22 -05:00
|
|
|
|
2016-07-20 14:23:51 -04:00
|
|
|
azureClient.AccountsClient = armStorage.NewAccountsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.AccountsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient.AccountsClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.AccountsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.AccountsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.AccountsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.AccountsClient.Client.PollingDuration = pollingDuration
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2019-06-14 17:54:27 -04:00
|
|
|
azureClient.GalleryImageVersionsClient = newCompute.NewGalleryImageVersionsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
|
|
|
azureClient.GalleryImageVersionsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.GalleryImageVersionsClient.RequestInspector = withInspection(maxlen)
|
|
|
|
azureClient.GalleryImageVersionsClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.GalleryImageVersionsClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.GalleryImageVersionsClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.GalleryImageVersionsClient.Client.PollingDuration = sharedGalleryTimeout
|
|
|
|
azureClient.GalleryImageVersionsClient.SubscriptionID = sigSubscriptionID
|
2019-06-14 17:54:27 -04:00
|
|
|
|
2019-06-18 01:34:26 -04:00
|
|
|
azureClient.GalleryImagesClient = newCompute.NewGalleryImagesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
|
|
|
|
azureClient.GalleryImagesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.GalleryImagesClient.RequestInspector = withInspection(maxlen)
|
|
|
|
azureClient.GalleryImagesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.GalleryImagesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.GalleryImagesClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.GalleryImagesClient.Client.PollingDuration = pollingDuration
|
|
|
|
azureClient.GalleryImagesClient.SubscriptionID = sigSubscriptionID
|
2019-06-18 01:34:26 -04:00
|
|
|
|
2016-07-07 20:28:47 -04:00
|
|
|
keyVaultURL, err := url.Parse(cloud.KeyVaultEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
azureClient.VaultClient = common.NewVaultClient(*keyVaultURL)
|
2017-05-28 03:38:45 -04:00
|
|
|
azureClient.VaultClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalTokenVault)
|
2016-04-21 19:50:03 -04:00
|
|
|
azureClient.VaultClient.RequestInspector = withInspection(maxlen)
|
2017-06-08 20:57:59 -04:00
|
|
|
azureClient.VaultClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.VaultClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.VaultClient.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.VaultClient.Client.PollingDuration = pollingDuration
|
2016-04-21 19:50:03 -04:00
|
|
|
|
2020-03-18 16:38:38 -04:00
|
|
|
// This client is different than the above because it manages the vault
|
|
|
|
// itself rather than the contents of the vault.
|
|
|
|
azureClient.VaultClientDelete = keyvault.NewVaultsClient(subscriptionID)
|
2017-11-30 03:11:17 -05:00
|
|
|
azureClient.VaultClientDelete.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
|
|
|
|
azureClient.VaultClientDelete.RequestInspector = withInspection(maxlen)
|
|
|
|
azureClient.VaultClientDelete.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
|
2020-11-09 20:09:32 -05:00
|
|
|
azureClient.VaultClientDelete.UserAgent = fmt.Sprintf("%s %s", useragent.String(version.AzurePluginVersion.FormattedVersion()), azureClient.VaultClientDelete.UserAgent)
|
2020-09-03 11:13:32 -04:00
|
|
|
azureClient.VaultClientDelete.Client.PollingDuration = pollingDuration
|
2017-11-30 03:11:17 -05:00
|
|
|
|
2017-06-07 18:01:10 -04:00
|
|
|
// If this is a managed disk build, this should be ignored.
|
|
|
|
if resourceGroupName != "" && storageAccountName != "" {
|
2018-04-06 04:12:58 -04:00
|
|
|
accountKeys, err := azureClient.AccountsClient.ListKeys(context.TODO(), resourceGroupName, storageAccountName)
|
2017-06-07 18:01:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storageClient, err := storage.NewClient(
|
|
|
|
storageAccountName,
|
|
|
|
*(*accountKeys.Keys)[0].Value,
|
|
|
|
cloud.StorageEndpointSuffix,
|
|
|
|
storage.DefaultAPIVersion,
|
|
|
|
true /*useHttps*/)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
azureClient.BlobStorageClient = storageClient.GetBlobService()
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return azureClient, nil
|
|
|
|
}
|
2016-04-21 19:50:03 -04:00
|
|
|
|
|
|
|
func getInspectorMaxLength() int64 {
|
|
|
|
value, ok := os.LookupEnv(EnvPackerLogAzureMaxLen)
|
|
|
|
if !ok {
|
|
|
|
return math.MaxInt64
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|