Skip to content
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

Merged
merged 15 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions builder/azure/TODO.md

This file was deleted.

108 changes: 16 additions & 92 deletions builder/azure/arm/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package arm
import (
"bytes"
"fmt"
"log"
"net/url"
"path"
"strings"

"github.com/hashicorp/packer-plugin-azure/builder/azure/common/constants"
Expand All @@ -20,8 +17,7 @@ const (
)

type AdditionalDiskArtifact struct {
AdditionalDiskUri string
AdditionalDiskUriReadOnlySas string
AdditionalDiskUri string
}

type Artifact struct {
Expand All @@ -32,8 +28,6 @@ type Artifact struct {
StorageAccountLocation string
OSDiskUri string
TemplateUri string
OSDiskUriReadOnlySas string
TemplateUriReadOnlySas string

// Managed Image
ManagedImageResourceGroupName string
Expand All @@ -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) {
Copy link
Contributor

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.

res := Artifact{
ManagedImageResourceGroupName: resourceGroup,
ManagedImageName: name,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I was to create an Artifact without this constructor artifact := &Artifact{//...} how would I know the values to be used for OSDiskUri and TemplateUri?

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 /?
If so maybe we use the url package for joining URL strings.

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 != ""
}
Expand Down Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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

Expand Down
Loading
Loading