From d2e593de378e24b476482bfe1ca61594d30f4391 Mon Sep 17 00:00:00 2001 From: Christopher Boumenot Date: Mon, 5 Mar 2018 01:27:52 -0800 Subject: [PATCH] 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.