From 85725c8339336a305989f05c99912a0e15a5d6ba Mon Sep 17 00:00:00 2001 From: Jenna Goldstrich Date: Thu, 22 Aug 2024 17:26:35 -0700 Subject: [PATCH 1/2] Add Subscription ID and Tenant ID to build variables --- .web-docs/components/builder/arm/README.md | 4 +++ builder/azure/arm/builder.go | 6 ++++- builder/azure/arm/step_set_generated_data.go | 27 ++++++++++++++++++++ docs/builders/arm.mdx | 4 +++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 builder/azure/arm/step_set_generated_data.go diff --git a/.web-docs/components/builder/arm/README.md b/.web-docs/components/builder/arm/README.md index 6f8dfd8f..5e56f6bc 100644 --- a/.web-docs/components/builder/arm/README.md +++ b/.web-docs/components/builder/arm/README.md @@ -737,6 +737,10 @@ The generated variables available for this builder are: shared images the resulting name will point to the actual source used to create the said version. building the AMI. +- `SubscriptionID` - The ID of the Azure Subscription where the build takes place. + +- `TenantID` - The ID of the Azure Tenant where the build takes place. + Usage example: **HCL2** diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index 836a0ab7..874c50a3 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -59,7 +59,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { b.setTemplateParameters(b.stateBag) b.setImageParameters(b.stateBag) - generatedDataKeys := []string{"SourceImageName"} + generatedDataKeys := []string{"SourceImageName", "TenantID", "SubscriptionID"} return generatedDataKeys, warnings, nil } @@ -390,6 +390,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) ) } steps = append(steps, + &StepSetGeneratedData{ + GeneratedData: generatedData, + Config: &b.config, + }, NewStepValidateTemplate(azureClient, ui, &b.config, deploymentName, getVirtualMachineDeploymentFunction), NewStepDeployTemplate(azureClient, ui, &b.config, deploymentName, getVirtualMachineDeploymentFunction, VirtualMachineTemplate), NewStepGetIPAddress(azureClient, ui, endpointConnectType), diff --git a/builder/azure/arm/step_set_generated_data.go b/builder/azure/arm/step_set_generated_data.go new file mode 100644 index 00000000..0c2d16d2 --- /dev/null +++ b/builder/azure/arm/step_set_generated_data.go @@ -0,0 +1,27 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package arm + +import ( + "context" + + "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata" +) + +type StepSetGeneratedData struct { + GeneratedData *packerbuilderdata.GeneratedData + Config *Config +} + +func (s *StepSetGeneratedData) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + + s.GeneratedData.Put("TenantID", s.Config.ClientConfig.TenantID) + s.GeneratedData.Put("SubscriptionID", s.Config.ClientConfig.SubscriptionID) + return multistep.ActionContinue +} + +func (s *StepSetGeneratedData) Cleanup(state multistep.StateBag) { + // No cleanup... +} diff --git a/docs/builders/arm.mdx b/docs/builders/arm.mdx index 90b2a13a..e175e1c1 100644 --- a/docs/builders/arm.mdx +++ b/docs/builders/arm.mdx @@ -150,6 +150,10 @@ The generated variables available for this builder are: shared images the resulting name will point to the actual source used to create the said version. building the AMI. +- `SubscriptionID` - The ID of the Azure Subscription where the build takes place. + +- `TenantID` - The ID of the Azure Tenant where the build takes place. + Usage example: **HCL2** From 916cb5aa46fc3f59ebe05d7e1804182cb7c33a19 Mon Sep 17 00:00:00 2001 From: Jenna Goldstrich Date: Thu, 22 Aug 2024 18:13:47 -0700 Subject: [PATCH 2/2] Add docs --- .web-docs/components/builder/arm/README.md | 6 +++++- builder/azure/arm/builder.go | 11 ++++++----- docs/builders/arm.mdx | 6 +++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.web-docs/components/builder/arm/README.md b/.web-docs/components/builder/arm/README.md index 5e56f6bc..b8045ddc 100644 --- a/.web-docs/components/builder/arm/README.md +++ b/.web-docs/components/builder/arm/README.md @@ -765,6 +765,8 @@ post-processor "manifest" { strip_path = true custom_data = { source_image_name = "${build.SourceImageName}" + tenant_id = "${build.TenantID}" + subscription_id = "${build.SubscriptionID}" } } ``` @@ -777,7 +779,9 @@ post-processor "manifest" { "output": "manifest.json", "strip_path": true, "custom_data": { - "source_image_name": "{{ build `SourceImageName` }}" + "source_image_name": "{{ build `SourceImageName` }}", + "tenant_id": "{{ build `TenantID` }}", + "subscription_id": "{{ build `SubscriptionID` }}" } } ] diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index 874c50a3..868817f4 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -390,10 +390,6 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) ) } steps = append(steps, - &StepSetGeneratedData{ - GeneratedData: generatedData, - Config: &b.config, - }, NewStepValidateTemplate(azureClient, ui, &b.config, deploymentName, getVirtualMachineDeploymentFunction), NewStepDeployTemplate(azureClient, ui, &b.config, deploymentName, getVirtualMachineDeploymentFunction, VirtualMachineTemplate), NewStepGetIPAddress(azureClient, ui, endpointConnectType), @@ -440,7 +436,12 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) NewStepCaptureImage(azureClient, ui), NewStepPublishToSharedImageGallery(azureClient, ui, &b.config), ) - + steps = append([]multistep.Step{ + &StepSetGeneratedData{ + GeneratedData: generatedData, + Config: &b.config, + }, + }, steps...) steps = append(steps, captureSteps...) if b.config.PackerDebug { diff --git a/docs/builders/arm.mdx b/docs/builders/arm.mdx index e175e1c1..cbef5f37 100644 --- a/docs/builders/arm.mdx +++ b/docs/builders/arm.mdx @@ -178,6 +178,8 @@ post-processor "manifest" { strip_path = true custom_data = { source_image_name = "${build.SourceImageName}" + tenant_id = "${build.TenantID}" + subscription_id = "${build.SubscriptionID}" } } ``` @@ -190,7 +192,9 @@ post-processor "manifest" { "output": "manifest.json", "strip_path": true, "custom_data": { - "source_image_name": "{{ build `SourceImageName` }}" + "source_image_name": "{{ build `SourceImageName` }}", + "tenant_id": "{{ build `TenantID` }}", + "subscription_id": "{{ build `SubscriptionID` }}" } } ]