From d2e593de378e24b476482bfe1ca61594d30f4391 Mon Sep 17 00:00:00 2001 From: Christopher Boumenot Date: Mon, 5 Mar 2018 01:27:52 -0800 Subject: [PATCH 1/2] azure: support for marketplace plan information --- builder/azure/arm/config.go | 14 ++ builder/azure/arm/config_test.go | 51 ++++++ builder/azure/arm/template_factory.go | 4 + ..._factory_test.TestPlanInfo01.approved.json | 170 +++++++++++++++++ ..._factory_test.TestPlanInfo02.approved.json | 171 ++++++++++++++++++ builder/azure/arm/template_factory_test.go | 39 ++++ builder/azure/common/template/template.go | 8 + .../azure/common/template/template_builder.go | 31 ++++ website/source/docs/builders/azure.html.md | 11 ++ 9 files changed, 499 insertions(+) create mode 100644 builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json create mode 100644 builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index afa5f62a7..e8ed6bd81 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -115,6 +115,12 @@ type Config struct { // Additional Disks AdditionalDiskSize []int32 `mapstructure:"disk_additional_size"` + // Plan Info + PlanName string `mapstructure:"plan_name"` + PlanProduct string `mapstructure:"plan_product"` + PlanPublisher string `mapstructure:"plan_publisher"` + PlanPromotionCode string `mapstructure:"plan_promotion_code"` + // Runtime Values UserName string Password string @@ -647,6 +653,14 @@ func assertRequiredParametersSet(c *Config, errs *packer.MultiError) { errs = packer.MultiErrorAppend(errs, fmt.Errorf("If virtual_network_subnet_name is specified, so must virtual_network_name")) } + ///////////////////////////////////////////// + // Plan Info + if c.PlanName != "" || c.PlanProduct != "" || c.PlanPublisher != "" || c.PlanPromotionCode != "" { + if c.PlanName == "" || c.PlanProduct == "" || c.PlanPublisher == "" { + errs = packer.MultiErrorAppend(errs, fmt.Errorf("if either plan_name, plan_product, plan_publisher, or plan_promotion_code are defined then plan_name, plan_product, and plan_publisher must be defined")) + } + } + ///////////////////////////////////////////// // OS if strings.EqualFold(c.OSType, constants.Target_Linux) { diff --git a/builder/azure/arm/config_test.go b/builder/azure/arm/config_test.go index d69c24a33..b56a260e3 100644 --- a/builder/azure/arm/config_test.go +++ b/builder/azure/arm/config_test.go @@ -1097,6 +1097,57 @@ func TestConfigAdditionalDiskOverrideDefault(t *testing.T) { } } +// Test that configuration handles plan info +// +// The use of plan info requires that the following three properties are set. +// +// 1. plan_name +// 2. plan_product +// 3. plan_publisher +func TestPlanInfoConfiguration(t *testing.T) { + config := map[string]interface{}{ + "capture_name_prefix": "ignore", + "capture_container_name": "ignore", + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "storage_account": "ignore", + "resource_group_name": "ignore", + "subscription_id": "ignore", + "os_type": "linux", + "communicator": "none", + } + + config["plan_name"] = "--plan-name--" + _, _, err := newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject the use of plan_name without plan_product and plan_publisher") + } + + config["plan_product"] = "--plan-product--" + _, _, err = newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject the use of plan_name and plan_product without plan_publisher") + } + + config["plan_publisher"] = "--plan-publisher--" + c, _, err := newConfig(config, getPackerConfiguration()) + if err != nil { + t.Fatalf("expected config to accept a complete plan configuration: %s", err) + } + + if c.PlanName != "--plan-name--" { + t.Fatalf("Expected PlanName to be '--plan-name--', but got %q", c.PlanName) + } + if c.PlanProduct != "--plan-product--" { + t.Fatalf("Expected PlanProduct to be '--plan-product--', but got %q", c.PlanProduct) + } + if c.PlanPublisher != "--plan-publisher--" { + t.Fatalf("Expected PlanPublisher to be '--plan-publisher--, but got %q", c.PlanPublisher) + } +} + func getArmBuilderConfiguration() map[string]string { m := make(map[string]string) for _, v := range requiredConfigValues { diff --git a/builder/azure/arm/template_factory.go b/builder/azure/arm/template_factory.go index ebc7ef91c..a68b13f22 100644 --- a/builder/azure/arm/template_factory.go +++ b/builder/azure/arm/template_factory.go @@ -81,6 +81,10 @@ func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) builder.SetCustomData(config.customData) } + if config.PlanName != "" { + builder.SetPlanInfo(config.PlanName, config.PlanProduct, config.PlanPublisher, config.PlanPromotionCode) + } + if config.VirtualNetworkName != "" && DefaultPrivateVirtualNetworkWithPublicIp != config.PrivateVirtualNetworkWithPublicIp { builder.SetPrivateVirtualNetworWithPublicIp( config.VirtualNetworkResourceGroupName, diff --git a/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json b/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json new file mode 100644 index 000000000..9af488f98 --- /dev/null +++ b/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json @@ -0,0 +1,170 @@ +{ + "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json", + "contentVersion": "1.0.0.0", + "parameters": { + "adminPassword": { + "type": "string" + }, + "adminUsername": { + "type": "string" + }, + "dnsNameForPublicIP": { + "type": "string" + }, + "osDiskName": { + "type": "string" + }, + "storageAccountBlobEndpoint": { + "type": "string" + }, + "vmName": { + "type": "string" + }, + "vmSize": { + "type": "string" + } + }, + "resources": [ + { + "apiVersion": "[variables('publicIPAddressApiVersion')]", + "location": "[variables('location')]", + "name": "[variables('publicIPAddressName')]", + "properties": { + "dnsSettings": { + "domainNameLabel": "[parameters('dnsNameForPublicIP')]" + }, + "publicIPAllocationMethod": "[variables('publicIPAddressType')]" + }, + "type": "Microsoft.Network/publicIPAddresses" + }, + { + "apiVersion": "[variables('virtualNetworksApiVersion')]", + "location": "[variables('location')]", + "name": "[variables('virtualNetworkName')]", + "properties": { + "addressSpace": { + "addressPrefixes": [ + "[variables('addressPrefix')]" + ] + }, + "subnets": [ + { + "name": "[variables('subnetName')]", + "properties": { + "addressPrefix": "[variables('subnetAddressPrefix')]" + } + } + ] + }, + "type": "Microsoft.Network/virtualNetworks" + }, + { + "apiVersion": "[variables('networkInterfacesApiVersion')]", + "dependsOn": [ + "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]", + "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]" + ], + "location": "[variables('location')]", + "name": "[variables('nicName')]", + "properties": { + "ipConfigurations": [ + { + "name": "ipconfig", + "properties": { + "privateIPAllocationMethod": "Dynamic", + "publicIPAddress": { + "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]" + }, + "subnet": { + "id": "[variables('subnetRef')]" + } + } + } + ] + }, + "type": "Microsoft.Network/networkInterfaces" + }, + { + "apiVersion": "[variables('apiVersion')]", + "dependsOn": [ + "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]" + ], + "location": "[variables('location')]", + "name": "[parameters('vmName')]", + "plan": { + "name": "planName00", + "product": "planProduct00", + "publisher": "planPublisher00" + }, + "properties": { + "diagnosticsProfile": { + "bootDiagnostics": { + "enabled": false + } + }, + "hardwareProfile": { + "vmSize": "[parameters('vmSize')]" + }, + "networkProfile": { + "networkInterfaces": [ + { + "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]" + } + ] + }, + "osProfile": { + "adminPassword": "[parameters('adminPassword')]", + "adminUsername": "[parameters('adminUsername')]", + "computerName": "[parameters('vmName')]", + "linuxConfiguration": { + "ssh": { + "publicKeys": [ + { + "keyData": "", + "path": "[variables('sshKeyPath')]" + } + ] + } + } + }, + "storageProfile": { + "imageReference": { + "offer": "ignored00", + "publisher": "ignored00", + "sku": "ignored00", + "version": "latest" + }, + "osDisk": { + "caching": "ReadWrite", + "createOption": "FromImage", + "name": "osdisk", + "vhd": { + "uri": "[concat(parameters('storageAccountBlobEndpoint'),variables('vmStorageAccountContainerName'),'/', parameters('osDiskName'),'.vhd')]" + } + } + } + }, + "type": "Microsoft.Compute/virtualMachines" + } + ], + "variables": { + "addressPrefix": "10.0.0.0/16", + "apiVersion": "2017-03-30", + "location": "[resourceGroup().location]", + "managedDiskApiVersion": "2017-03-30", + "networkInterfacesApiVersion": "2017-04-01", + "nicName": "packerNic", + "publicIPAddressApiVersion": "2017-04-01", + "publicIPAddressName": "packerPublicIP", + "publicIPAddressType": "Dynamic", + "sshKeyPath": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]", + "subnetAddressPrefix": "10.0.0.0/24", + "subnetName": "packerSubnet", + "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]", + "virtualNetworkName": "packerNetwork", + "virtualNetworkResourceGroup": "[resourceGroup().name]", + "virtualNetworksApiVersion": "2017-04-01", + "vmStorageAccountContainerName": "images", + "vnetID": "[resourceId(variables('virtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]" + } +} \ No newline at end of file diff --git a/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json b/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json new file mode 100644 index 000000000..3cd802107 --- /dev/null +++ b/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json @@ -0,0 +1,171 @@ +{ + "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json", + "contentVersion": "1.0.0.0", + "parameters": { + "adminPassword": { + "type": "string" + }, + "adminUsername": { + "type": "string" + }, + "dnsNameForPublicIP": { + "type": "string" + }, + "osDiskName": { + "type": "string" + }, + "storageAccountBlobEndpoint": { + "type": "string" + }, + "vmName": { + "type": "string" + }, + "vmSize": { + "type": "string" + } + }, + "resources": [ + { + "apiVersion": "[variables('publicIPAddressApiVersion')]", + "location": "[variables('location')]", + "name": "[variables('publicIPAddressName')]", + "properties": { + "dnsSettings": { + "domainNameLabel": "[parameters('dnsNameForPublicIP')]" + }, + "publicIPAllocationMethod": "[variables('publicIPAddressType')]" + }, + "type": "Microsoft.Network/publicIPAddresses" + }, + { + "apiVersion": "[variables('virtualNetworksApiVersion')]", + "location": "[variables('location')]", + "name": "[variables('virtualNetworkName')]", + "properties": { + "addressSpace": { + "addressPrefixes": [ + "[variables('addressPrefix')]" + ] + }, + "subnets": [ + { + "name": "[variables('subnetName')]", + "properties": { + "addressPrefix": "[variables('subnetAddressPrefix')]" + } + } + ] + }, + "type": "Microsoft.Network/virtualNetworks" + }, + { + "apiVersion": "[variables('networkInterfacesApiVersion')]", + "dependsOn": [ + "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]", + "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]" + ], + "location": "[variables('location')]", + "name": "[variables('nicName')]", + "properties": { + "ipConfigurations": [ + { + "name": "ipconfig", + "properties": { + "privateIPAllocationMethod": "Dynamic", + "publicIPAddress": { + "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]" + }, + "subnet": { + "id": "[variables('subnetRef')]" + } + } + } + ] + }, + "type": "Microsoft.Network/networkInterfaces" + }, + { + "apiVersion": "[variables('apiVersion')]", + "dependsOn": [ + "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]" + ], + "location": "[variables('location')]", + "name": "[parameters('vmName')]", + "plan": { + "name": "planName00", + "product": "planProduct00", + "promotionCode": "planPromotionCode00", + "publisher": "planPublisher00" + }, + "properties": { + "diagnosticsProfile": { + "bootDiagnostics": { + "enabled": false + } + }, + "hardwareProfile": { + "vmSize": "[parameters('vmSize')]" + }, + "networkProfile": { + "networkInterfaces": [ + { + "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]" + } + ] + }, + "osProfile": { + "adminPassword": "[parameters('adminPassword')]", + "adminUsername": "[parameters('adminUsername')]", + "computerName": "[parameters('vmName')]", + "linuxConfiguration": { + "ssh": { + "publicKeys": [ + { + "keyData": "", + "path": "[variables('sshKeyPath')]" + } + ] + } + } + }, + "storageProfile": { + "imageReference": { + "offer": "ignored00", + "publisher": "ignored00", + "sku": "ignored00", + "version": "latest" + }, + "osDisk": { + "caching": "ReadWrite", + "createOption": "FromImage", + "name": "osdisk", + "vhd": { + "uri": "[concat(parameters('storageAccountBlobEndpoint'),variables('vmStorageAccountContainerName'),'/', parameters('osDiskName'),'.vhd')]" + } + } + } + }, + "type": "Microsoft.Compute/virtualMachines" + } + ], + "variables": { + "addressPrefix": "10.0.0.0/16", + "apiVersion": "2017-03-30", + "location": "[resourceGroup().location]", + "managedDiskApiVersion": "2017-03-30", + "networkInterfacesApiVersion": "2017-04-01", + "nicName": "packerNic", + "publicIPAddressApiVersion": "2017-04-01", + "publicIPAddressName": "packerPublicIP", + "publicIPAddressType": "Dynamic", + "sshKeyPath": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]", + "subnetAddressPrefix": "10.0.0.0/24", + "subnetName": "packerSubnet", + "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]", + "virtualNetworkName": "packerNetwork", + "virtualNetworkResourceGroup": "[resourceGroup().name]", + "virtualNetworksApiVersion": "2017-04-01", + "vmStorageAccountContainerName": "images", + "vnetID": "[resourceId(variables('virtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]" + } +} \ No newline at end of file diff --git a/builder/azure/arm/template_factory_test.go b/builder/azure/arm/template_factory_test.go index 7909a0cee..292c56389 100644 --- a/builder/azure/arm/template_factory_test.go +++ b/builder/azure/arm/template_factory_test.go @@ -523,3 +523,42 @@ func TestKeyVaultDeployment03(t *testing.T) { t.Fatal(err) } } + +func TestPlanInfo01(t *testing.T) { + planInfo := map[string]interface{}{ + "plan_name": "planName00", + "plan_product": "planProduct00", + "plan_publisher": "planPublisher00", + } + + c, _, _ := newConfig(planInfo, getArmBuilderConfiguration(), getPackerConfiguration()) + deployment, err := GetVirtualMachineDeployment(c) + if err != nil { + t.Fatal(err) + } + + err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) + if err != nil { + t.Fatal(err) + } +} + +func TestPlanInfo02(t *testing.T) { + planInfo := map[string]interface{}{ + "plan_name": "planName00", + "plan_product": "planProduct00", + "plan_publisher": "planPublisher00", + "plan_promotion_code": "planPromotionCode00", + } + + c, _, _ := newConfig(planInfo, getArmBuilderConfiguration(), getPackerConfiguration()) + deployment, err := GetVirtualMachineDeployment(c) + if err != nil { + t.Fatal(err) + } + + err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) + if err != nil { + t.Fatal(err) + } +} diff --git a/builder/azure/common/template/template.go b/builder/azure/common/template/template.go index 070ca3392..d6273fa46 100644 --- a/builder/azure/common/template/template.go +++ b/builder/azure/common/template/template.go @@ -30,11 +30,19 @@ type Resource struct { Type *string `json:"type"` Location *string `json:"location,omitempty"` DependsOn *[]string `json:"dependsOn,omitempty"` + Plan *Plan `json:"plan,omitempty"` Properties *Properties `json:"properties,omitempty"` Tags *map[string]*string `json:"tags,omitempty"` Resources *[]Resource `json:"resources,omitempty"` } +type Plan struct { + Name *string `json:"name"` + Product *string `json:"product"` + Publisher *string `json:"publisher"` + PromotionCode *string `json:"promotionCode,omitempty"` +} + type OSDiskUnion struct { OsType compute.OperatingSystemTypes `json:"osType,omitempty"` OsState compute.OperatingSystemStateTypes `json:"osState,omitempty"` diff --git a/builder/azure/common/template/template_builder.go b/builder/azure/common/template/template_builder.go index 835aff096..a05b66ab3 100644 --- a/builder/azure/common/template/template_builder.go +++ b/builder/azure/common/template/template_builder.go @@ -179,6 +179,26 @@ func (s *TemplateBuilder) SetImageUrl(imageUrl string, osType compute.OperatingS return nil } +func (s *TemplateBuilder) SetPlanInfo(name, product, publisher, promotionCode string) error { + var promotionCodeVal *string = nil + if promotionCode != "" { + promotionCodeVal = to.StringPtr(promotionCode) + } + + for i, x := range *s.template.Resources { + if strings.EqualFold(*x.Type, resourceVirtualMachine) { + (*s.template.Resources)[i].Plan = &Plan{ + Name: to.StringPtr(name), + Product: to.StringPtr(product), + Publisher: to.StringPtr(publisher), + PromotionCode: promotionCodeVal, + } + } + } + + return nil +} + func (s *TemplateBuilder) SetOSDiskSizeGB(diskSizeGB int32) error { resource, err := s.getResourceByType(resourceVirtualMachine) if err != nil { @@ -302,6 +322,17 @@ func (s *TemplateBuilder) getResourceByType(t string) (*Resource, error) { return nil, fmt.Errorf("template: could not find a resource of type %s", t) } +func (s *TemplateBuilder) getResourceByType2(t string) (**Resource, error) { + for _, x := range *s.template.Resources { + if strings.EqualFold(*x.Type, t) { + p := &x + return &p, nil + } + } + + return nil, fmt.Errorf("template: could not find a resource of type %s", t) +} + func (s *TemplateBuilder) setVariable(name string, value string) { (*s.template.Variables)[name] = value } diff --git a/website/source/docs/builders/azure.html.md b/website/source/docs/builders/azure.html.md index b7d937acd..94e0d0a0c 100644 --- a/website/source/docs/builders/azure.html.md +++ b/website/source/docs/builders/azure.html.md @@ -162,6 +162,17 @@ Providing `temp_resource_group_name` or `location` in combination with `build_re `Linux` this configures an SSH authorized key. For `Windows` this configures a WinRM certificate. +- `plan_name` (string) The plan name. This setting (`plan_product`, `plan_publisher`, and `plan_promotion_code`) are + only needed for Marketplace images. Please refer to [Deploy an image with Marketplace terms](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cli-ps-findimage#deploy-an-image-with-marketplace-terms) for more details. + +- `plan_product` (string) The plan product. See `plan_name` for more information. + +- `plan_publisher` (string) Specifies the produce of the image from the Marketplace. This value is the same value as + Offer (`image_offer`). See `plan_name` for more information. + +- `plan_promotion_code` (string) Some Marketplace images use a promotion code. See `plan_name` for more + information. + - `temp_compute_name` (string) temporary name assigned to the VM. If this value is not set, a random value will be assigned. Knowing the resource group and VM name allows one to execute commands to update the VM during a Packer build, e.g. attach a resource disk to the VM. From 1ef491d4c8969385f0030af919cbc12f0a125de2 Mon Sep 17 00:00:00 2001 From: Christopher Boumenot Date: Thu, 8 Mar 2018 22:39:23 -0800 Subject: [PATCH 2/2] incorporate reviewer feedback --- builder/azure/arm/config.go | 27 +++-- builder/azure/arm/config_test.go | 98 +++++++++++++++++-- builder/azure/arm/template_factory.go | 4 +- ..._factory_test.TestPlanInfo01.approved.json | 24 +++++ ..._factory_test.TestPlanInfo02.approved.json | 28 ++++++ builder/azure/arm/template_factory_test.go | 21 ++-- examples/azure/marketplace_plan_info.json | 51 ++++++++++ website/source/docs/builders/azure.html.md | 33 +++++-- 8 files changed, 253 insertions(+), 33 deletions(-) create mode 100644 examples/azure/marketplace_plan_info.json diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index e8ed6bd81..df507930a 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -57,6 +57,13 @@ var ( reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe) ) +type PlanInformation struct { + PlanName string `mapstructure:"plan_name"` + PlanProduct string `mapstructure:"plan_product"` + PlanPublisher string `mapstructure:"plan_publisher"` + PlanPromotionCode string `mapstructure:"plan_promotion_code"` +} + type Config struct { common.PackerConfig `mapstructure:",squash"` @@ -107,6 +114,7 @@ type Config struct { VirtualNetworkResourceGroupName string `mapstructure:"virtual_network_resource_group_name"` CustomDataFile string `mapstructure:"custom_data_file"` customData string + PlanInfo PlanInformation `mapstructure:"plan_info"` // OS OSType string `mapstructure:"os_type"` @@ -115,12 +123,6 @@ type Config struct { // Additional Disks AdditionalDiskSize []int32 `mapstructure:"disk_additional_size"` - // Plan Info - PlanName string `mapstructure:"plan_name"` - PlanProduct string `mapstructure:"plan_product"` - PlanPublisher string `mapstructure:"plan_publisher"` - PlanPromotionCode string `mapstructure:"plan_promotion_code"` - // Runtime Values UserName string Password string @@ -655,9 +657,18 @@ func assertRequiredParametersSet(c *Config, errs *packer.MultiError) { ///////////////////////////////////////////// // Plan Info - if c.PlanName != "" || c.PlanProduct != "" || c.PlanPublisher != "" || c.PlanPromotionCode != "" { - if c.PlanName == "" || c.PlanProduct == "" || c.PlanPublisher == "" { + if c.PlanInfo.PlanName != "" || c.PlanInfo.PlanProduct != "" || c.PlanInfo.PlanPublisher != "" || c.PlanInfo.PlanPromotionCode != "" { + if c.PlanInfo.PlanName == "" || c.PlanInfo.PlanProduct == "" || c.PlanInfo.PlanPublisher == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("if either plan_name, plan_product, plan_publisher, or plan_promotion_code are defined then plan_name, plan_product, and plan_publisher must be defined")) + } else { + if c.AzureTags == nil { + c.AzureTags = make(map[string]*string) + } + + c.AzureTags["PlanInfo"] = &c.PlanInfo.PlanName + c.AzureTags["PlanProduct"] = &c.PlanInfo.PlanProduct + c.AzureTags["PlanPublisher"] = &c.PlanInfo.PlanPublisher + c.AzureTags["PlanPromotionCode"] = &c.PlanInfo.PlanPromotionCode } } diff --git a/builder/azure/arm/config_test.go b/builder/azure/arm/config_test.go index b56a260e3..c7758fb91 100644 --- a/builder/azure/arm/config_test.go +++ b/builder/azure/arm/config_test.go @@ -1119,32 +1119,112 @@ func TestPlanInfoConfiguration(t *testing.T) { "communicator": "none", } - config["plan_name"] = "--plan-name--" + planInfo := map[string]string{ + "plan_name": "--plan-name--", + } + config["plan_info"] = planInfo + _, _, err := newConfig(config, getPackerConfiguration()) if err == nil { t.Fatal("expected config to reject the use of plan_name without plan_product and plan_publisher") } - config["plan_product"] = "--plan-product--" + planInfo["plan_product"] = "--plan-product--" _, _, err = newConfig(config, getPackerConfiguration()) if err == nil { t.Fatal("expected config to reject the use of plan_name and plan_product without plan_publisher") } - config["plan_publisher"] = "--plan-publisher--" + planInfo["plan_publisher"] = "--plan-publisher--" c, _, err := newConfig(config, getPackerConfiguration()) if err != nil { t.Fatalf("expected config to accept a complete plan configuration: %s", err) } - if c.PlanName != "--plan-name--" { - t.Fatalf("Expected PlanName to be '--plan-name--', but got %q", c.PlanName) + if c.PlanInfo.PlanName != "--plan-name--" { + t.Fatalf("Expected PlanName to be '--plan-name--', but got %q", c.PlanInfo.PlanName) } - if c.PlanProduct != "--plan-product--" { - t.Fatalf("Expected PlanProduct to be '--plan-product--', but got %q", c.PlanProduct) + if c.PlanInfo.PlanProduct != "--plan-product--" { + t.Fatalf("Expected PlanProduct to be '--plan-product--', but got %q", c.PlanInfo.PlanProduct) } - if c.PlanPublisher != "--plan-publisher--" { - t.Fatalf("Expected PlanPublisher to be '--plan-publisher--, but got %q", c.PlanPublisher) + if c.PlanInfo.PlanPublisher != "--plan-publisher--" { + t.Fatalf("Expected PlanPublisher to be '--plan-publisher--, but got %q", c.PlanInfo.PlanPublisher) + } +} + +func TestPlanInfoPromotionCode(t *testing.T) { + config := map[string]interface{}{ + "capture_name_prefix": "ignore", + "capture_container_name": "ignore", + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "storage_account": "ignore", + "resource_group_name": "ignore", + "subscription_id": "ignore", + "os_type": "linux", + "communicator": "none", + "plan_info": map[string]string{ + "plan_name": "--plan-name--", + "plan_product": "--plan-product--", + "plan_publisher": "--plan-publisher--", + "plan_promotion_code": "--plan-promotion-code--", + }, + } + + c, _, err := newConfig(config, getPackerConfiguration()) + if err != nil { + t.Fatalf("expected config to accept plan_info configuration, but got %s", err) + } + + if c.PlanInfo.PlanName != "--plan-name--" { + t.Fatalf("Expected PlanName to be '--plan-name--', but got %q", c.PlanInfo.PlanName) + } + if c.PlanInfo.PlanProduct != "--plan-product--" { + t.Fatalf("Expected PlanProduct to be '--plan-product--', but got %q", c.PlanInfo.PlanProduct) + } + if c.PlanInfo.PlanPublisher != "--plan-publisher--" { + t.Fatalf("Expected PlanPublisher to be '--plan-publisher--, but got %q", c.PlanInfo.PlanPublisher) + } + if c.PlanInfo.PlanPromotionCode != "--plan-promotion-code--" { + t.Fatalf("Expected PlanPublisher to be '--plan-promotion-code----, but got %q", c.PlanInfo.PlanPromotionCode) + } +} + +// plan_info defines 3 or 4 tags based on plan data. +// The user can define up to 15 tags. If the combination of these two +// exceeds the max tag amount, the builder should reject the configuration. +func TestPlanInfoTooManyTagsErrors(t *testing.T) { + exactMaxNumberOfTags := map[string]string{} + for i := 0; i < 15; i++ { + exactMaxNumberOfTags[fmt.Sprintf("tag%.2d", i)] = "ignored" + } + + config := map[string]interface{}{ + "capture_name_prefix": "ignore", + "capture_container_name": "ignore", + "image_offer": "ignore", + "image_publisher": "ignore", + "image_sku": "ignore", + "location": "ignore", + "storage_account": "ignore", + "resource_group_name": "ignore", + "subscription_id": "ignore", + "os_type": "linux", + "communicator": "none", + "azure_tags": exactMaxNumberOfTags, + "plan_info": map[string]string{ + "plan_name": "--plan-name--", + "plan_product": "--plan-product--", + "plan_publisher": "--plan-publisher--", + "plan_promotion_code": "--plan-promotion-code--", + }, + } + + _, _, err := newConfig(config, getPackerConfiguration()) + if err == nil { + t.Fatal("expected config to reject configuration due to excess tags") } } diff --git a/builder/azure/arm/template_factory.go b/builder/azure/arm/template_factory.go index a68b13f22..843580fd2 100644 --- a/builder/azure/arm/template_factory.go +++ b/builder/azure/arm/template_factory.go @@ -81,8 +81,8 @@ func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) builder.SetCustomData(config.customData) } - if config.PlanName != "" { - builder.SetPlanInfo(config.PlanName, config.PlanProduct, config.PlanPublisher, config.PlanPromotionCode) + if config.PlanInfo.PlanName != "" { + builder.SetPlanInfo(config.PlanInfo.PlanName, config.PlanInfo.PlanProduct, config.PlanInfo.PlanPublisher, config.PlanInfo.PlanPromotionCode) } if config.VirtualNetworkName != "" && DefaultPrivateVirtualNetworkWithPublicIp != config.PrivateVirtualNetworkWithPublicIp { diff --git a/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json b/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json index 9af488f98..74546c490 100644 --- a/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json +++ b/builder/azure/arm/template_factory_test.TestPlanInfo01.approved.json @@ -35,6 +35,12 @@ }, "publicIPAllocationMethod": "[variables('publicIPAddressType')]" }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "", + "PlanPublisher": "planPublisher00" + }, "type": "Microsoft.Network/publicIPAddresses" }, { @@ -56,6 +62,12 @@ } ] }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "", + "PlanPublisher": "planPublisher00" + }, "type": "Microsoft.Network/virtualNetworks" }, { @@ -82,6 +94,12 @@ } ] }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "", + "PlanPublisher": "planPublisher00" + }, "type": "Microsoft.Network/networkInterfaces" }, { @@ -144,6 +162,12 @@ } } }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "", + "PlanPublisher": "planPublisher00" + }, "type": "Microsoft.Compute/virtualMachines" } ], diff --git a/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json b/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json index 3cd802107..8f93bb530 100644 --- a/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json +++ b/builder/azure/arm/template_factory_test.TestPlanInfo02.approved.json @@ -35,6 +35,13 @@ }, "publicIPAllocationMethod": "[variables('publicIPAddressType')]" }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "planPromotionCode00", + "PlanPublisher": "planPublisher00", + "dept": "engineering" + }, "type": "Microsoft.Network/publicIPAddresses" }, { @@ -56,6 +63,13 @@ } ] }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "planPromotionCode00", + "PlanPublisher": "planPublisher00", + "dept": "engineering" + }, "type": "Microsoft.Network/virtualNetworks" }, { @@ -82,6 +96,13 @@ } ] }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "planPromotionCode00", + "PlanPublisher": "planPublisher00", + "dept": "engineering" + }, "type": "Microsoft.Network/networkInterfaces" }, { @@ -145,6 +166,13 @@ } } }, + "tags": { + "PlanInfo": "planName00", + "PlanProduct": "planProduct00", + "PlanPromotionCode": "planPromotionCode00", + "PlanPublisher": "planPublisher00", + "dept": "engineering" + }, "type": "Microsoft.Compute/virtualMachines" } ], diff --git a/builder/azure/arm/template_factory_test.go b/builder/azure/arm/template_factory_test.go index 292c56389..a6d31d7d9 100644 --- a/builder/azure/arm/template_factory_test.go +++ b/builder/azure/arm/template_factory_test.go @@ -526,9 +526,11 @@ func TestKeyVaultDeployment03(t *testing.T) { func TestPlanInfo01(t *testing.T) { planInfo := map[string]interface{}{ - "plan_name": "planName00", - "plan_product": "planProduct00", - "plan_publisher": "planPublisher00", + "plan_info": map[string]string{ + "plan_name": "planName00", + "plan_product": "planProduct00", + "plan_publisher": "planPublisher00", + }, } c, _, _ := newConfig(planInfo, getArmBuilderConfiguration(), getPackerConfiguration()) @@ -545,10 +547,15 @@ func TestPlanInfo01(t *testing.T) { func TestPlanInfo02(t *testing.T) { planInfo := map[string]interface{}{ - "plan_name": "planName00", - "plan_product": "planProduct00", - "plan_publisher": "planPublisher00", - "plan_promotion_code": "planPromotionCode00", + "azure_tags": map[string]string{ + "dept": "engineering", + }, + "plan_info": map[string]string{ + "plan_name": "planName00", + "plan_product": "planProduct00", + "plan_publisher": "planPublisher00", + "plan_promotion_code": "planPromotionCode00", + }, } c, _, _ := newConfig(planInfo, getArmBuilderConfiguration(), getPackerConfiguration()) diff --git a/examples/azure/marketplace_plan_info.json b/examples/azure/marketplace_plan_info.json new file mode 100644 index 000000000..4891a9e6b --- /dev/null +++ b/examples/azure/marketplace_plan_info.json @@ -0,0 +1,51 @@ +{ + "variables": { + "client_id": "{{env `ARM_CLIENT_ID`}}", + "client_secret": "{{env `ARM_CLIENT_SECRET`}}", + "resource_group": "{{env `ARM_RESOURCE_GROUP`}}", + "storage_account": "{{env `ARM_STORAGE_ACCOUNT`}}", + "subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}" + }, + "builders": [{ + "type": "azure-arm", + + "client_id": "{{user `client_id`}}", + "client_secret": "{{user `client_secret`}}", + "resource_group_name": "{{user `resource_group`}}", + "storage_account": "{{user `storage_account`}}", + "subscription_id": "{{user `subscription_id`}}", + + "capture_container_name": "images", + "capture_name_prefix": "packer", + + "os_type": "Linux", + "image_publisher": "Canonical", + "image_offer": "UbuntuServer", + "image_sku": "16.04-LTS", + + "azure_tags": { + "dept": "engineering", + "task": "image deployment" + }, + + "plan_info": { + "plan_name": "rabbitmq", + "plan_product": "rabbitmq", + "plan_publisher": "bitnami" + }, + + "location": "West US", + "vm_size": "Standard_DS2_v2" + }], + "provisioners": [{ + "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'", + "inline": [ + "apt-get update", + "apt-get upgrade -y", + + "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync" + ], + "inline_shebang": "/bin/sh -x", + "type": "shell" + }] +} diff --git a/website/source/docs/builders/azure.html.md b/website/source/docs/builders/azure.html.md index 94e0d0a0c..13b2b46a3 100644 --- a/website/source/docs/builders/azure.html.md +++ b/website/source/docs/builders/azure.html.md @@ -162,16 +162,35 @@ Providing `temp_resource_group_name` or `location` in combination with `build_re `Linux` this configures an SSH authorized key. For `Windows` this configures a WinRM certificate. -- `plan_name` (string) The plan name. This setting (`plan_product`, `plan_publisher`, and `plan_promotion_code`) are - only needed for Marketplace images. Please refer to [Deploy an image with Marketplace terms](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cli-ps-findimage#deploy-an-image-with-marketplace-terms) for more details. +- `plan_info` (object) - Used for creating images from Marketplace images. Please refer to [Deploy an image with + Marketplace terms](https://aka.ms/azuremarketplaceapideployment) for more details. Not all Marketplace images + support programmatic deployment, and support is controlled by the image publisher. -- `plan_product` (string) The plan product. See `plan_name` for more information. + An example plan_info object is defined below. -- `plan_publisher` (string) Specifies the produce of the image from the Marketplace. This value is the same value as - Offer (`image_offer`). See `plan_name` for more information. + ```json + { + "plan_info": { + "plan_name": "rabbitmq", + "plan_product": "rabbitmq", + "plan_publisher": "bitnami" + } + } + ``` -- `plan_promotion_code` (string) Some Marketplace images use a promotion code. See `plan_name` for more - information. + `plan_name` (string) - The plan name, required. + `plan_product` (string) - The plan product, required. + `plan_publisher` (string) - The plan publisher, required. + `plan_promotion_code` (string) - Some images accept a promotion code, optional. + + Images created from the Marketplace with `plan_info` **must** specify `plan_info` whenever the image is deployed. + The builder automatically adds tags to the image to ensure this information is not lost. The following tags are + added. + + 1. PlanName + 1. PlanProduct + 1. PlanPublisher + 1. PlanPromotionCode - `temp_compute_name` (string) temporary name assigned to the VM. If this value is not set, a random value will be assigned. Knowing the resource group and VM name allows one to execute commands to update the VM during a Packer