Use subscription id from client set

This commit is contained in:
Paul Meyer 2020-03-25 20:11:51 +00:00
parent 6131301fdd
commit f3703a775f
8 changed files with 28 additions and 29 deletions

View File

@ -367,7 +367,6 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
if config.FromScratch {
steps = append(steps,
&StepCreateNewDisk{
SubscriptionID: info.SubscriptionID,
ResourceGroup: info.ResourceGroupName,
DiskName: config.TemporaryOSDiskName,
DiskSizeGB: config.OSDiskSizeGB,
@ -388,7 +387,6 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
}
steps = append(steps,
&StepCreateNewDisk{
SubscriptionID: info.SubscriptionID,
ResourceGroup: info.ResourceGroupName,
DiskName: config.TemporaryOSDiskName,
DiskSizeGB: config.OSDiskSizeGB,
@ -406,11 +404,9 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
steps = append(steps,
&StepVerifySourceDisk{
SourceDiskResourceID: config.Source,
SubscriptionID: info.SubscriptionID,
Location: info.Location,
},
&StepCreateNewDisk{
SubscriptionID: info.SubscriptionID,
ResourceGroup: info.ResourceGroupName,
DiskName: config.TemporaryOSDiskName,
DiskSizeGB: config.OSDiskSizeGB,

View File

@ -154,8 +154,7 @@ func Test_buildsteps(t *testing.T) {
for _, s := range steps {
if s, ok := s.(*StepVerifySourceDisk); ok {
if s.SourceDiskResourceID == "diskresourceid" &&
s.Location == info.Location &&
s.SubscriptionID == info.SubscriptionID {
s.Location == info.Location {
return
}
t.Errorf("found misconfigured StepVerifySourceDisk: %+v", s)

View File

@ -15,7 +15,7 @@ import (
var _ multistep.Step = &StepCreateNewDisk{}
type StepCreateNewDisk struct {
SubscriptionID, ResourceGroup, DiskName string
ResourceGroup, DiskName string
DiskSizeGB int32 // optional, ignored if 0
DiskStorageAccountType string // from compute.DiskStorageAccountTypes
HyperVGeneration string
@ -33,7 +33,7 @@ func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) mu
ui := state.Get("ui").(packer.Ui)
diskResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s",
s.SubscriptionID,
azcli.SubscriptionID(),
s.ResourceGroup,
s.DiskName)
state.Put("os_disk_resource_id", diskResourceID)
@ -67,7 +67,7 @@ func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) mu
disk.CreationData.ImageReference = &compute.ImageDiskReference{
ID: to.StringPtr(fmt.Sprintf(
"/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/artifacttypes/vmimage/offers/%s/skus/%s/versions/%s",
s.SubscriptionID, s.Location, s.PlatformImage.Publisher, s.PlatformImage.Offer, s.PlatformImage.Sku, s.PlatformImage.Version)),
azcli.SubscriptionID(), s.Location, s.PlatformImage.Publisher, s.PlatformImage.Offer, s.PlatformImage.Sku, s.PlatformImage.Version)),
}
}

View File

@ -17,7 +17,6 @@ import (
func TestStepCreateNewDisk_Run(t *testing.T) {
type fields struct {
SubscriptionID string
ResourceGroup string
DiskName string
DiskSizeGB int32
@ -37,7 +36,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
{
name: "HappyPathDiskSource",
fields: fields{
SubscriptionID: "SubscriptionID",
ResourceGroup: "ResourceGroupName",
DiskName: "TemporaryOSDiskName",
DiskSizeGB: 42,
@ -68,7 +66,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
{
name: "HappyPathDiskSource",
fields: fields{
SubscriptionID: "SubscriptionID",
ResourceGroup: "ResourceGroupName",
DiskName: "TemporaryOSDiskName",
DiskStorageAccountType: string(compute.StandardLRS),
@ -105,7 +102,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := StepCreateNewDisk{
SubscriptionID: tt.fields.SubscriptionID,
ResourceGroup: tt.fields.ResourceGroup,
DiskName: tt.fields.DiskName,
DiskSizeGB: tt.fields.DiskSizeGB,
@ -136,6 +132,7 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{
DisksClientMock: m,
SubscriptionIDMock: "SubscriptionID",
})
state.Put("ui", packer.TestUi(t))

View File

@ -15,7 +15,6 @@ import (
)
type StepVerifySourceDisk struct {
SubscriptionID string
SourceDiskResourceID string
Location string
}
@ -34,10 +33,10 @@ func (s StepVerifySourceDisk) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionHalt
}
if !strings.EqualFold(resource.SubscriptionID, s.SubscriptionID) {
if !strings.EqualFold(resource.SubscriptionID, azcli.SubscriptionID()) {
err := fmt.Errorf("Source disk resource %q is in a different subscription than this VM (%q). "+
"Packer does not know how to handle that.",
s.SourceDiskResourceID, s.SubscriptionID)
s.SourceDiskResourceID, azcli.SubscriptionID())
log.Printf("StepVerifySourceDisk.Run: error: %+v", err)
state.Put("error", err)
ui.Error(err.Error())

View File

@ -18,7 +18,6 @@ import (
func Test_StepVerifySourceDisk_Run(t *testing.T) {
type fields struct {
SubscriptionID string
SourceDiskResourceID string
Location string
@ -38,7 +37,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
{
name: "HappyPath",
fields: fields{
SubscriptionID: "subid1",
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
Location: "westus2",
@ -50,7 +48,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
{
name: "NotAResourceID",
fields: fields{
SubscriptionID: "subid1",
SourceDiskResourceID: "/other",
Location: "westus2",
@ -63,7 +60,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
{
name: "DiskNotFound",
fields: fields{
SubscriptionID: "subid1",
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
Location: "westus2",
@ -76,7 +72,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
{
name: "NotADisk",
fields: fields{
SubscriptionID: "subid1",
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/images/image1",
Location: "westus2",
@ -88,7 +83,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
{
name: "OtherSubscription",
fields: fields{
SubscriptionID: "subid1",
SourceDiskResourceID: "/subscriptions/subid2/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
Location: "westus2",
@ -101,7 +95,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
{
name: "OtherLocation",
fields: fields{
SubscriptionID: "subid1",
SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1",
Location: "eastus",
@ -115,7 +108,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := StepVerifySourceDisk{
SubscriptionID: tt.fields.SubscriptionID,
SourceDiskResourceID: tt.fields.SourceDiskResourceID,
Location: tt.fields.Location,
}
@ -134,6 +126,7 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{
DisksClientMock: m,
SubscriptionIDMock: "subid1",
})
state.Put("ui", ui)

View File

@ -20,6 +20,9 @@ type AzureClientSet interface {
VirtualMachineImagesClient() VirtualMachineImagesClientAPI
PollClient() autorest.Client
// SubscriptionID returns the subscription ID that this client set was created for
SubscriptionID() string
}
var subscriptionPathRegex = regexp.MustCompile(`/subscriptions/([[:xdigit:]]{8}(-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12})`)
@ -50,6 +53,10 @@ func new(c Config, say func(string)) (*azureClientSet, error) {
}, nil
}
func (s azureClientSet) SubscriptionID() string {
return s.subscriptionID
}
func (s azureClientSet) configureAutorestClient(c *autorest.Client) {
c.AddToUserAgent(useragent.String())
c.Authorizer = s.authorizer

View File

@ -5,6 +5,8 @@ import (
"github.com/Azure/go-autorest/autorest"
)
var _ AzureClientSet = &AzureClientSetMock{}
// AzureClientSetMock provides a generic mock for AzureClientSet
type AzureClientSetMock struct {
DisksClientMock computeapi.DisksClientAPI
@ -13,6 +15,7 @@ type AzureClientSetMock struct {
VirtualMachinesClientMock computeapi.VirtualMachinesClientAPI
PollClientMock autorest.Client
MetadataClientMock MetadataClientAPI
SubscriptionIDMock string
}
// DisksClient returns a DisksClientAPI
@ -44,3 +47,8 @@ func (m *AzureClientSetMock) PollClient() autorest.Client {
func (m *AzureClientSetMock) MetadataClient() MetadataClientAPI {
return m.MetadataClientMock
}
// SubscriptionID returns SubscriptionIDMock
func (m *AzureClientSetMock) SubscriptionID() string {
return m.SubscriptionIDMock
}