Skip to content

Commit

Permalink
Add Subscription ID and Tenant ID to build variables (#439)
Browse files Browse the repository at this point in the history
* Add Subscription ID and Tenant ID to build variables

* Add docs
  • Loading branch information
JenGoldstrich authored Aug 23, 2024
1 parent b7642d6 commit e47bee8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
10 changes: 9 additions & 1 deletion .web-docs/components/builder/arm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -761,6 +765,8 @@ post-processor "manifest" {
strip_path = true
custom_data = {
source_image_name = "${build.SourceImageName}"
tenant_id = "${build.TenantID}"
subscription_id = "${build.SubscriptionID}"
}
}
```
Expand All @@ -773,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` }}"
}
}
]
Expand Down
9 changes: 7 additions & 2 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -436,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 {
Expand Down
27 changes: 27 additions & 0 deletions builder/azure/arm/step_set_generated_data.go
Original file line number Diff line number Diff line change
@@ -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...
}
10 changes: 9 additions & 1 deletion docs/builders/arm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -174,6 +178,8 @@ post-processor "manifest" {
strip_path = true
custom_data = {
source_image_name = "${build.SourceImageName}"
tenant_id = "${build.TenantID}"
subscription_id = "${build.SubscriptionID}"
}
}
```
Expand All @@ -186,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` }}"
}
}
]
Expand Down

0 comments on commit e47bee8

Please sign in to comment.