2016-05-21 02:01:16 -04:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
|
2016-05-21 02:01:16 -04:00
|
|
|
|
2017-05-30 03:33:54 -04:00
|
|
|
"fmt"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/template"
|
2016-05-21 02:01:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type templateFactoryFunc func(*Config) (*resources.Deployment, error)
|
|
|
|
|
|
|
|
func GetKeyVaultDeployment(config *Config) (*resources.Deployment, error) {
|
|
|
|
params := &template.TemplateParameters{
|
|
|
|
KeyVaultName: &template.TemplateParameter{Value: config.tmpKeyVaultName},
|
|
|
|
KeyVaultSecretValue: &template.TemplateParameter{Value: config.winrmCertificate},
|
|
|
|
ObjectId: &template.TemplateParameter{Value: config.ObjectID},
|
|
|
|
TenantId: &template.TemplateParameter{Value: config.TenantID},
|
|
|
|
}
|
|
|
|
|
2016-07-29 17:17:33 -04:00
|
|
|
builder, _ := template.NewTemplateBuilder(template.KeyVault)
|
|
|
|
builder.SetTags(&config.AzureTags)
|
|
|
|
|
|
|
|
doc, _ := builder.ToJSON()
|
|
|
|
return createDeploymentParameters(*doc, params)
|
2016-05-21 02:01:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) {
|
|
|
|
params := &template.TemplateParameters{
|
|
|
|
AdminUsername: &template.TemplateParameter{Value: config.UserName},
|
|
|
|
AdminPassword: &template.TemplateParameter{Value: config.Password},
|
|
|
|
DnsNameForPublicIP: &template.TemplateParameter{Value: config.tmpComputeName},
|
2018-03-10 14:17:43 -05:00
|
|
|
NicName: &template.TemplateParameter{Value: config.tmpNicName},
|
2016-05-21 02:01:16 -04:00
|
|
|
OSDiskName: &template.TemplateParameter{Value: config.tmpOSDiskName},
|
2018-03-10 14:17:43 -05:00
|
|
|
PublicIPAddressName: &template.TemplateParameter{Value: config.tmpPublicIPAddressName},
|
|
|
|
SubnetName: &template.TemplateParameter{Value: config.tmpSubnetName},
|
2016-05-21 02:01:16 -04:00
|
|
|
StorageAccountBlobEndpoint: &template.TemplateParameter{Value: config.storageAccountBlobEndpoint},
|
2018-03-10 14:17:43 -05:00
|
|
|
VirtualNetworkName: &template.TemplateParameter{Value: config.tmpVirtualNetworkName},
|
|
|
|
VMSize: &template.TemplateParameter{Value: config.VMSize},
|
|
|
|
VMName: &template.TemplateParameter{Value: config.tmpComputeName},
|
2016-05-21 02:01:16 -04:00
|
|
|
}
|
|
|
|
|
2018-03-10 14:17:43 -05:00
|
|
|
builder, err := template.NewTemplateBuilder(template.BasicTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-21 02:01:16 -04:00
|
|
|
osType := compute.Linux
|
|
|
|
|
|
|
|
switch config.OSType {
|
|
|
|
case constants.Target_Linux:
|
|
|
|
builder.BuildLinux(config.sshAuthorizedKey)
|
|
|
|
case constants.Target_Windows:
|
|
|
|
osType = compute.Windows
|
|
|
|
builder.BuildWindows(config.tmpKeyVaultName, config.tmpWinRMCertificateUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ImageUrl != "" {
|
|
|
|
builder.SetImageUrl(config.ImageUrl, osType)
|
2017-05-30 14:25:46 -04:00
|
|
|
} else if config.CustomManagedImageName != "" {
|
2017-08-13 04:45:04 -04:00
|
|
|
builder.SetManagedDiskUrl(config.customManagedImageID, config.managedImageStorageAccountType)
|
2017-05-30 14:25:46 -04:00
|
|
|
} else if config.ManagedImageName != "" && config.ImagePublisher != "" {
|
2017-05-30 03:33:54 -04:00
|
|
|
imageID := fmt.Sprintf("/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/ArtifactTypes/vmimage/offers/%s/skus/%s/versions/%s",
|
|
|
|
config.SubscriptionID,
|
|
|
|
config.Location,
|
|
|
|
config.ImagePublisher,
|
|
|
|
config.ImageOffer,
|
|
|
|
config.ImageSku,
|
|
|
|
config.ImageVersion)
|
|
|
|
|
2017-08-13 04:45:04 -04:00
|
|
|
builder.SetManagedMarketplaceImage(config.Location, config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion, imageID, config.managedImageStorageAccountType)
|
2018-10-03 19:10:46 -04:00
|
|
|
} else if config.SharedGallerySubscription != "" && config.SharedGalleryResourceGroup != "" && config.SharedGalleryName != "" && config.SharedGalleryImageName != "" {
|
2018-09-30 16:56:44 -04:00
|
|
|
imageID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/galleries/%s/images/%s",
|
2018-10-03 19:10:46 -04:00
|
|
|
config.SharedGallerySubscription,
|
|
|
|
config.SharedGalleryResourceGroup,
|
|
|
|
config.SharedGalleryName,
|
|
|
|
config.SharedGalleryImageName)
|
|
|
|
if config.SharedGalleryImageVersion != "" {
|
2018-09-30 16:56:44 -04:00
|
|
|
imageID += fmt.Sprintf("/versions/%s",
|
2018-10-03 19:10:46 -04:00
|
|
|
config.SharedGalleryImageVersion)
|
2018-09-30 16:56:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.SetSharedGalleryImage(config.Location, imageID)
|
2016-05-21 02:01:16 -04:00
|
|
|
} else {
|
|
|
|
builder.SetMarketPlaceImage(config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion)
|
|
|
|
}
|
|
|
|
|
2016-10-12 19:24:04 -04:00
|
|
|
if config.OSDiskSizeGB > 0 {
|
|
|
|
builder.SetOSDiskSizeGB(config.OSDiskSizeGB)
|
|
|
|
}
|
|
|
|
|
2018-02-23 18:34:13 -05:00
|
|
|
if len(config.AdditionalDiskSize) > 0 {
|
|
|
|
builder.SetAdditionalDisks(config.AdditionalDiskSize, config.CustomManagedImageName != "" || (config.ManagedImageName != "" && config.ImagePublisher != ""))
|
|
|
|
}
|
|
|
|
|
2016-10-13 14:56:23 -04:00
|
|
|
if config.customData != "" {
|
|
|
|
builder.SetCustomData(config.customData)
|
|
|
|
}
|
|
|
|
|
2018-03-09 01:39:23 -05:00
|
|
|
if config.PlanInfo.PlanName != "" {
|
|
|
|
builder.SetPlanInfo(config.PlanInfo.PlanName, config.PlanInfo.PlanProduct, config.PlanInfo.PlanPublisher, config.PlanInfo.PlanPromotionCode)
|
2018-03-05 04:27:52 -05:00
|
|
|
}
|
|
|
|
|
2017-08-06 18:32:44 -04:00
|
|
|
if config.VirtualNetworkName != "" && DefaultPrivateVirtualNetworkWithPublicIp != config.PrivateVirtualNetworkWithPublicIp {
|
2018-03-13 04:14:30 -04:00
|
|
|
builder.SetPrivateVirtualNetworkWithPublicIp(
|
2017-08-06 18:32:44 -04:00
|
|
|
config.VirtualNetworkResourceGroupName,
|
|
|
|
config.VirtualNetworkName,
|
|
|
|
config.VirtualNetworkSubnetName)
|
|
|
|
} else if config.VirtualNetworkName != "" {
|
2016-06-30 19:51:52 -04:00
|
|
|
builder.SetVirtualNetwork(
|
|
|
|
config.VirtualNetworkResourceGroupName,
|
|
|
|
config.VirtualNetworkName,
|
|
|
|
config.VirtualNetworkSubnetName)
|
|
|
|
}
|
|
|
|
|
2016-07-29 17:17:33 -04:00
|
|
|
builder.SetTags(&config.AzureTags)
|
2016-05-21 02:01:16 -04:00
|
|
|
doc, _ := builder.ToJSON()
|
|
|
|
return createDeploymentParameters(*doc, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDeploymentParameters(doc string, parameters *template.TemplateParameters) (*resources.Deployment, error) {
|
|
|
|
var template map[string]interface{}
|
|
|
|
err := json.Unmarshal(([]byte)(doc), &template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := json.Marshal(*parameters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateParameters map[string]interface{}
|
|
|
|
err = json.Unmarshal(bs, &templateParameters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resources.Deployment{
|
|
|
|
Properties: &resources.DeploymentProperties{
|
|
|
|
Mode: resources.Incremental,
|
|
|
|
Template: &template,
|
|
|
|
Parameters: &templateParameters,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|