Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subscription ID and Tenant ID to build variables #439

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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