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

View File

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

View File

@ -15,10 +15,10 @@ import (
var _ multistep.Step = &StepCreateNewDisk{} var _ multistep.Step = &StepCreateNewDisk{}
type StepCreateNewDisk struct { type StepCreateNewDisk struct {
SubscriptionID, ResourceGroup, DiskName string ResourceGroup, DiskName string
DiskSizeGB int32 // optional, ignored if 0 DiskSizeGB int32 // optional, ignored if 0
DiskStorageAccountType string // from compute.DiskStorageAccountTypes DiskStorageAccountType string // from compute.DiskStorageAccountTypes
HyperVGeneration string HyperVGeneration string
Location string Location string
PlatformImage *client.PlatformImage PlatformImage *client.PlatformImage
@ -33,7 +33,7 @@ func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) mu
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
diskResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s", diskResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s",
s.SubscriptionID, azcli.SubscriptionID(),
s.ResourceGroup, s.ResourceGroup,
s.DiskName) s.DiskName)
state.Put("os_disk_resource_id", diskResourceID) 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{ disk.CreationData.ImageReference = &compute.ImageDiskReference{
ID: to.StringPtr(fmt.Sprintf( ID: to.StringPtr(fmt.Sprintf(
"/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/artifacttypes/vmimage/offers/%s/skus/%s/versions/%s", "/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) { func TestStepCreateNewDisk_Run(t *testing.T) {
type fields struct { type fields struct {
SubscriptionID string
ResourceGroup string ResourceGroup string
DiskName string DiskName string
DiskSizeGB int32 DiskSizeGB int32
@ -37,7 +36,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
{ {
name: "HappyPathDiskSource", name: "HappyPathDiskSource",
fields: fields{ fields: fields{
SubscriptionID: "SubscriptionID",
ResourceGroup: "ResourceGroupName", ResourceGroup: "ResourceGroupName",
DiskName: "TemporaryOSDiskName", DiskName: "TemporaryOSDiskName",
DiskSizeGB: 42, DiskSizeGB: 42,
@ -68,7 +66,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
{ {
name: "HappyPathDiskSource", name: "HappyPathDiskSource",
fields: fields{ fields: fields{
SubscriptionID: "SubscriptionID",
ResourceGroup: "ResourceGroupName", ResourceGroup: "ResourceGroupName",
DiskName: "TemporaryOSDiskName", DiskName: "TemporaryOSDiskName",
DiskStorageAccountType: string(compute.StandardLRS), DiskStorageAccountType: string(compute.StandardLRS),
@ -105,7 +102,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := StepCreateNewDisk{ s := StepCreateNewDisk{
SubscriptionID: tt.fields.SubscriptionID,
ResourceGroup: tt.fields.ResourceGroup, ResourceGroup: tt.fields.ResourceGroup,
DiskName: tt.fields.DiskName, DiskName: tt.fields.DiskName,
DiskSizeGB: tt.fields.DiskSizeGB, DiskSizeGB: tt.fields.DiskSizeGB,
@ -135,7 +131,8 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{ state.Put("azureclient", &client.AzureClientSetMock{
DisksClientMock: m, DisksClientMock: m,
SubscriptionIDMock: "SubscriptionID",
}) })
state.Put("ui", packer.TestUi(t)) state.Put("ui", packer.TestUi(t))

View File

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

View File

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

View File

@ -20,6 +20,9 @@ type AzureClientSet interface {
VirtualMachineImagesClient() VirtualMachineImagesClientAPI VirtualMachineImagesClient() VirtualMachineImagesClientAPI
PollClient() autorest.Client 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})`) 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 }, nil
} }
func (s azureClientSet) SubscriptionID() string {
return s.subscriptionID
}
func (s azureClientSet) configureAutorestClient(c *autorest.Client) { func (s azureClientSet) configureAutorestClient(c *autorest.Client) {
c.AddToUserAgent(useragent.String()) c.AddToUserAgent(useragent.String())
c.Authorizer = s.authorizer c.Authorizer = s.authorizer

View File

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