-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[V2] Migrate all builders to hashicorp/go-azure-sdk #326
Changes from 9 commits
a4471f8
c46a7ce
61152df
dc5d2d8
2d3fdc6
d491147
4d0c3ba
78b7d4b
c6731f0
e3e5955
06a217b
e1dc7d1
4a53467
ae9ef52
c5762d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
* @hashicorp/packer | ||
* @JenGoldstrich @nywilken @lbajolet-hashicorp |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,6 @@ package arm | |
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"net/url" | ||
"path" | ||
"strings" | ||
|
||
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/constants" | ||
|
@@ -20,8 +17,7 @@ const ( | |
) | ||
|
||
type AdditionalDiskArtifact struct { | ||
AdditionalDiskUri string | ||
AdditionalDiskUriReadOnlySas string | ||
AdditionalDiskUri string | ||
} | ||
|
||
type Artifact struct { | ||
|
@@ -32,8 +28,6 @@ type Artifact struct { | |
StorageAccountLocation string | ||
OSDiskUri string | ||
TemplateUri string | ||
OSDiskUriReadOnlySas string | ||
TemplateUriReadOnlySas string | ||
|
||
// Managed Image | ||
ManagedImageResourceGroupName string | ||
|
@@ -56,7 +50,7 @@ type Artifact struct { | |
StateData map[string]interface{} | ||
} | ||
|
||
func NewManagedImageArtifact(osType, resourceGroup, name, location, id, osDiskSnapshotName, dataDiskSnapshotPrefix string, generatedData map[string]interface{}, keepOSDisk bool, template *CaptureTemplate, getSasUrl func(name string) string) (*Artifact, error) { | ||
func NewManagedImageArtifact(osType, resourceGroup, name, location, id, osDiskSnapshotName, dataDiskSnapshotPrefix string, generatedData map[string]interface{}, osDiskUri string) (*Artifact, error) { | ||
res := Artifact{ | ||
ManagedImageResourceGroupName: resourceGroup, | ||
ManagedImageName: name, | ||
|
@@ -68,25 +62,8 @@ func NewManagedImageArtifact(osType, resourceGroup, name, location, id, osDiskSn | |
StateData: generatedData, | ||
} | ||
|
||
if keepOSDisk { | ||
if template == nil { | ||
log.Printf("artifact error: nil capture template") | ||
return &res, nil | ||
} | ||
|
||
if len(template.Resources) != 1 { | ||
log.Printf("artifact error: malformed capture template, expected one resource") | ||
return &res, nil | ||
} | ||
|
||
vhdUri, err := url.Parse(template.Resources[0].Properties.StorageProfile.OSDisk.Image.Uri) | ||
if err != nil { | ||
log.Printf("artifact error: Error parsing osdisk url: %s", err) | ||
return &res, nil | ||
} | ||
|
||
res.OSDiskUri = vhdUri.String() | ||
res.OSDiskUriReadOnlySas = getSasUrl(getStorageUrlPath(vhdUri)) | ||
if osDiskUri != "" { | ||
res.OSDiskUri = osDiskUri | ||
} | ||
|
||
return &res, nil | ||
|
@@ -115,80 +92,33 @@ func NewSharedImageArtifact(osType, destinationSharedImageGalleryId string, loca | |
}, nil | ||
} | ||
|
||
func NewArtifact(template *CaptureTemplate, getSasUrl func(name string) string, osType string, generatedData map[string]interface{}) (*Artifact, error) { | ||
if template == nil { | ||
return nil, fmt.Errorf("nil capture template") | ||
} | ||
|
||
if len(template.Resources) != 1 { | ||
return nil, fmt.Errorf("malformed capture template, expected one resource") | ||
} | ||
func NewArtifact(vmInternalID string, storageAccountUrl string, storageAccountLocation string, osType string, additionalDiskCount int, generatedData map[string]interface{}) (*Artifact, error) { | ||
vhdUri := fmt.Sprintf("%ssystem/Microsoft.Compute/Images/images/packer-osDisk.%s.vhd", storageAccountUrl, vmInternalID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I was to create an Artifact without this constructor Should there be functions for generating these values with valid inputs? When it comes to generating the vhdUri do we need to validate that the storageAccountUrl ends with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to close the loop - as we discuss internally we will hold on refactors to not introduce untested changes. |
||
|
||
vhdUri, err := url.Parse(template.Resources[0].Properties.StorageProfile.OSDisk.Image.Uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
templateUri, err := storageUriToTemplateUri(vhdUri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
templateUri := fmt.Sprintf("%ssystem/Microsoft.Compute/Images/images/packer-vmTemplate.%s.json", storageAccountUrl, vmInternalID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto on having a generate template URI function |
||
|
||
var additional_disks *[]AdditionalDiskArtifact | ||
if template.Resources[0].Properties.StorageProfile.DataDisks != nil { | ||
data_disks := make([]AdditionalDiskArtifact, len(template.Resources[0].Properties.StorageProfile.DataDisks)) | ||
for i, additionaldisk := range template.Resources[0].Properties.StorageProfile.DataDisks { | ||
additionalVhdUri, err := url.Parse(additionaldisk.Image.Uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data_disks[i].AdditionalDiskUri = additionalVhdUri.String() | ||
data_disks[i].AdditionalDiskUriReadOnlySas = getSasUrl(getStorageUrlPath(additionalVhdUri)) | ||
if additionalDiskCount > 0 { | ||
data_disks := make([]AdditionalDiskArtifact, additionalDiskCount) | ||
for i := 0; i < additionalDiskCount; i++ { | ||
data_disks[i].AdditionalDiskUri = fmt.Sprintf("%ssystem/Microsoft.Compute/Images/images/packer-datadisk-%d.%s.vhd", storageAccountUrl, i+1, vmInternalID) | ||
} | ||
additional_disks = &data_disks | ||
} | ||
|
||
return &Artifact{ | ||
OSType: osType, | ||
OSDiskUri: vhdUri.String(), | ||
OSDiskUriReadOnlySas: getSasUrl(getStorageUrlPath(vhdUri)), | ||
TemplateUri: templateUri.String(), | ||
TemplateUriReadOnlySas: getSasUrl(getStorageUrlPath(templateUri)), | ||
OSType: osType, | ||
OSDiskUri: vhdUri, | ||
TemplateUri: templateUri, | ||
|
||
AdditionalDisks: additional_disks, | ||
|
||
StorageAccountLocation: template.Resources[0].Location, | ||
StorageAccountLocation: storageAccountLocation, | ||
|
||
StateData: generatedData, | ||
}, nil | ||
} | ||
|
||
func getStorageUrlPath(u *url.URL) string { | ||
parts := strings.Split(u.Path, "/") | ||
return strings.Join(parts[3:], "/") | ||
} | ||
|
||
func storageUriToTemplateUri(su *url.URL) (*url.URL, error) { | ||
// packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd -> 4085bb15-3644-4641-b9cd-f575918640b4 | ||
filename := path.Base(su.Path) | ||
parts := strings.Split(filename, ".") | ||
|
||
if len(parts) < 3 { | ||
return nil, fmt.Errorf("malformed URL") | ||
} | ||
|
||
// packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd -> packer | ||
prefixParts := strings.Split(parts[0], "-") | ||
prefix := strings.Join(prefixParts[:len(prefixParts)-1], "-") | ||
|
||
templateFilename := fmt.Sprintf("%s-vmTemplate.%s.json", prefix, parts[1]) | ||
|
||
// https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.4085bb15-3644-4641-b9cd-f575918640b4.vhd" | ||
// -> | ||
// https://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-vmTemplate.4085bb15-3644-4641-b9cd-f575918640b4.json" | ||
return url.Parse(strings.Replace(su.String(), filename, templateFilename, 1)) | ||
} | ||
|
||
func (a *Artifact) isManagedImage() bool { | ||
return a.ManagedImageResourceGroupName != "" | ||
} | ||
|
@@ -249,19 +179,13 @@ func (a *Artifact) String() string { | |
if a.OSDiskUri != "" { | ||
buf.WriteString(fmt.Sprintf("OSDiskUri: %s\n", a.OSDiskUri)) | ||
} | ||
if a.OSDiskUriReadOnlySas != "" { | ||
buf.WriteString(fmt.Sprintf("OSDiskUriReadOnlySas: %s\n", a.OSDiskUriReadOnlySas)) | ||
} | ||
} else if !a.isPublishedToSIG() { | ||
buf.WriteString(fmt.Sprintf("StorageAccountLocation: %s\n", a.StorageAccountLocation)) | ||
buf.WriteString(fmt.Sprintf("OSDiskUri: %s\n", a.OSDiskUri)) | ||
buf.WriteString(fmt.Sprintf("OSDiskUriReadOnlySas: %s\n", a.OSDiskUriReadOnlySas)) | ||
buf.WriteString(fmt.Sprintf("TemplateUri: %s\n", a.TemplateUri)) | ||
buf.WriteString(fmt.Sprintf("TemplateUriReadOnlySas: %s\n", a.TemplateUriReadOnlySas)) | ||
if a.AdditionalDisks != nil { | ||
for i, additionaldisk := range *a.AdditionalDisks { | ||
buf.WriteString(fmt.Sprintf("AdditionalDiskUri (datadisk-%d): %s\n", i+1, additionaldisk.AdditionalDiskUri)) | ||
buf.WriteString(fmt.Sprintf("AdditionalDiskUriReadOnlySas (datadisk-%d): %s\n", i+1, additionaldisk.AdditionalDiskUriReadOnlySas)) | ||
} | ||
} | ||
} | ||
|
@@ -351,7 +275,7 @@ func (a *Artifact) hcpPackerRegistryMetadata() *registryimage.Image { | |
return img | ||
} | ||
|
||
// If image is a legacy VHD | ||
// If image is a VHD | ||
labels["storage_account_location"] = a.StorageAccountLocation | ||
labels["template_uri"] = a.TemplateUri | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future todo - we should rethink the number of parameters for this function.