Skip to content

Commit 209f0c4

Browse files
author
Adam Coulter
committed
fixed problem with subscription_id
1 parent a589a93 commit 209f0c4

File tree

5 files changed

+28
-52
lines changed

5 files changed

+28
-52
lines changed

main.tf

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@ resource "shell_script" "subscription" {
1616
delete = ". ./subscription.ps1; Delete"
1717
}
1818
environment = {
19-
name = var.name
20-
tenant = var.tenant_id
21-
type = local.type_codes[var.type]
19+
name = var.name
20+
type = local.type_codes[var.type]
2221
}
2322
}
24-
25-
data "azurerm_client_config" "current" {}
26-
27-
resource "azurerm_role_assignment" "owners" {
28-
for_each = toset(var.principal_ids)
29-
scope = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
30-
role_definition_name = "Owner"
31-
principal_id = each.key
32-
}

outputs.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
data "azurerm_subscription" "main" {
2+
subscription_id = shell_script.subscription.output.id
3+
}
4+
15
output "subscription_id" {
2-
value = shell_script.subscription.output.id
6+
value = data.azurerm_subscription.main.subscription_id
37
description = "Subscription ID GUID"
48
}
59

610
output "id" {
7-
value = "/subscriptions/${shell_script.subscription.output.id}"
11+
value = data.azurerm_subscription.main.id
812
description = "Azure resource model subscription path"
913
}
1014

1115
output "name" {
12-
value = shell_script.subscription.output.name
16+
value = data.azurerm_subscription.main.display_name
1317
description = "Name of the subscription"
1418
}

subscription.ps1

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
# CRUD an Azure Subscription
33
#
44
# Requires the following environment variables:
5-
# - $env:tenant
65
# - $env:name
76
# - $env:type - string, either "MS-AZR-0148P" (dev/test) or "MS-AZR-0017P" (normal/prod)
87
#
98

109
$credential = New-Object System.Management.Automation.PSCredential ($env:azsub_client_id, (ConvertTo-SecureString $env:azsub_client_secret -AsPlainText -Force))
11-
Connect-AzAccount -Credential $credential -Tenant $env:tenant -ServicePrincipal
10+
Connect-AzAccount -Credential $credential -Tenant $env:azsub_tenant_id -ServicePrincipal
1211

1312
$ErrorActionPreference = 'Stop'
1413
$old_state = [System.IO.File]::OpenText("/dev/stdin").ReadToEnd() | ConvertFrom-Json
15-
if($null -ne $old_state) {
14+
if ($null -ne $old_state) {
1615
Write-Output "Old state:"
1716
$old_state | ConvertTo-Json | Write-Output
1817
}
@@ -21,7 +20,7 @@ function Create {
2120

2221
# Check if subscription name already used in this tenant
2322
Write-Output "Checking for existing subscription with name $env:name..."
24-
$subscription = Get-AzSubscription -TenantId $env:tenant -SubscriptionName $env:name -ErrorAction SilentlyContinue
23+
$subscription = Get-AzSubscription -TenantId $env:azsub_tenant_id -SubscriptionName $env:name -ErrorAction SilentlyContinue
2524
$subscription | ConvertTo-Json | Write-Output
2625

2726
# Create new subscription
@@ -31,7 +30,7 @@ function Create {
3130
$account | ConvertTo-Json | Write-Output
3231
Write-Output "Creating subscription..."
3332
try {
34-
$subscription = New-AzSubscription -OfferType $env:type -Name $env:name -EnrollmentAccountObjectId $account[0].ObjectId -ErrorAction Stop
33+
$subscription = New-AzSubscription -OfferType $env:type -Name $env:name -EnrollmentAccountObjectId $account[0].ObjectId -ErrorAction Stop
3534
}
3635
catch {
3736
Write-Error "Error: Error when attempting to update subscription name: $($_.Exception.Response)"
@@ -46,16 +45,16 @@ function Create {
4645
}
4746

4847
# Emit refreshed state
49-
@{ id = $subscription.Id; tenant = $subscription.TenantId; name = $subscription.Name } | ConvertTo-Json | Write-Output
48+
@{ id = $subscription.Id; name = $subscription.Name } | ConvertTo-Json | Write-Output
5049

5150

5251
}
5352

5453
function Read {
55-
$subscription = Get-AzSubscription -TenantId $old_state.tenant -SubscriptionId $old_state.id -ErrorAction Stop
54+
$subscription = Get-AzSubscription -TenantId $env:azsub_tenant_id -SubscriptionId $old_state.id -ErrorAction Stop
5655

5756
# Emit refreshed state
58-
@{ id = $subscription.Id; tenant = $subscription.TenantId; name = $subscription.Name } | ConvertTo-Json | Write-Output
57+
@{ id = $subscription.Id; name = $subscription.Name } | ConvertTo-Json | Write-Output
5958
}
6059

6160
function Update {
@@ -86,7 +85,7 @@ function Update {
8685

8786
# Emit refreshed state
8887
Write-Host "New state:"
89-
@{ id = $old_state.id; tenant = $env:tenant; name = $env:name } | ConvertTo-Json | Write-Output
88+
@{ id = $old_state.id; name = $env:name } | ConvertTo-Json | Write-Output
9089
}
9190

9291
function Delete {

variables.tf

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,3 @@ variable "type" {
1212
error_message = "Allowed values for subscription type are \"Prod\" or \"DevTest\"."
1313
}
1414
}
15-
16-
variable "principal_ids" {
17-
type = list(string)
18-
description = "List of principal_ids to give the owner role on this subscription."
19-
default = []
20-
}
21-
22-
variable "tenant_id" {
23-
type = string
24-
description = "Guid of the Azure tenant to create the subscription in."
25-
}
26-
27-
variable "client_id" {
28-
type = string
29-
description = "Service principal to provision the subscription using."
30-
}
31-
32-
variable "client_secret" {
33-
type = string
34-
description = "Service principal secret to provision the subscription using."
35-
}

versions.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
terraform {
22
required_version = ">= 0.13"
33
required_providers {
4-
azurerm = "~> 2.19"
4+
azurerm = "~> 2.19"
5+
external = "~> 1.2"
56
shell = {
67
source = "scottwinkler/shell"
78
version = "=1.7.2"
@@ -11,17 +12,20 @@ terraform {
1112

1213
provider "azurerm" {
1314
features {}
14-
subscription_id = shell_script.subscription.output.id
15-
client_id = var.client_id
16-
client_secret = var.client_secret
17-
tenant_id = var.tenant_id
15+
}
16+
17+
data "azurerm_client_config" "current" {}
18+
19+
data "external" "az_client_config" {
20+
program = ["pwsh", "-command", "@{tenant=$env:ARM_TENANT_ID;client=$env:ARM_CLIENT_ID;secret=$env:ARM_CLIENT_SECRET} | ConvertTo-Json | Write-Output"]
1821
}
1922

2023
provider "shell" {
2124
interpreter = ["pwsh", "-command"]
2225

2326
sensitive_environment = {
24-
azsub_client_id = var.client_id
25-
azsub_client_secret = var.client_secret
27+
azsub_client_id = data.external.az_client_config.result.client
28+
azsub_client_secret = data.external.az_client_config.result.secret
29+
azsub_tenant_id = data.azurerm_client_config.current.tenant_id
2630
}
2731
}

0 commit comments

Comments
 (0)