-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathr-vm.tf
202 lines (160 loc) · 5.94 KB
/
r-vm.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
resource "azurerm_windows_virtual_machine" "main" {
name = local.name
location = var.location
resource_group_name = var.resource_group_name
network_interface_ids = [azurerm_network_interface.main.id]
size = var.vm_size
license_type = var.license_type
tags = merge(local.default_tags, local.default_vm_tags, var.extra_tags)
source_image_id = var.vm_image_id
dynamic "source_image_reference" {
for_each = var.vm_image_id == null ? [0] : []
content {
offer = var.vm_image.offer
publisher = var.vm_image.publisher
sku = var.vm_image.sku
version = var.vm_image.version
}
}
dynamic "plan" {
for_each = var.vm_plan[*]
content {
name = var.vm_plan.name
product = var.vm_plan.product
publisher = var.vm_plan.publisher
}
}
availability_set_id = var.availability_set != null ? var.availability_set.id : null
zone = var.zone_id
boot_diagnostics {
storage_account_uri = "https://${var.diagnostics_storage_account_name}.blob.core.windows.net"
}
os_disk {
name = local.os_disk_name
caching = var.os_disk_caching
storage_account_type = var.os_disk_storage_account_type
disk_size_gb = var.os_disk_size_gb
}
encryption_at_host_enabled = var.encryption_at_host_enabled
vm_agent_platform_updates_enabled = var.vm_agent_platform_updates_enabled
vtpm_enabled = var.vtpm_enabled
disk_controller_type = var.disk_controller_type
dynamic "additional_capabilities" {
for_each = var.ultra_ssd_enabled[*]
content {
ultra_ssd_enabled = var.ultra_ssd_enabled
}
}
dynamic "identity" {
for_each = local.identity[*]
content {
type = local.identity.type
identity_ids = local.identity.identity_ids
}
}
computer_name = local.hostname
admin_username = var.admin_username
admin_password = var.admin_password
custom_data = base64encode(local.custom_data_content)
user_data = var.user_data
dynamic "secret" {
for_each = var.key_vault[*]
content {
key_vault_id = var.key_vault.id
certificate {
url = one(azurerm_key_vault_certificate.main[*].secret_id)
store = "My"
}
}
}
dynamic "additional_unattend_content" {
for_each = var.key_vault != null ? local.additional_unattend_content : {}
content {
setting = additional_unattend_content.key
content = additional_unattend_content.value
}
}
priority = var.spot_instance_enabled ? "Spot" : "Regular"
max_bid_price = var.spot_instance_enabled ? var.spot_instance_max_bid_price : null
eviction_policy = var.spot_instance_enabled ? var.spot_instance_eviction_policy : null
provision_vm_agent = true
enable_automatic_updates = true
patch_mode = var.patch_mode
patch_assessment_mode = var.patch_mode == "AutomaticByPlatform" ? var.patch_mode : "ImageDefault"
hotpatching_enabled = var.hotpatching_enabled
bypass_platform_safety_checks_on_user_schedule_enabled = var.patch_mode == "AutomaticByPlatform"
reboot_setting = var.patch_mode == "AutomaticByPlatform" ? var.patching_reboot_setting : null
}
moved {
from = azurerm_windows_virtual_machine.vm
to = azurerm_windows_virtual_machine.main
}
resource "terraform_data" "winrm_connection_test" {
count = var.public_ip_enabled && var.key_vault != null ? 1 : 0
triggers_replace = [
azurerm_windows_virtual_machine.main.id,
]
connection {
type = "winrm"
host = one(azurerm_public_ip.main[*].ip_address)
port = 5986
https = true
user = var.admin_username
password = var.admin_password
timeout = "3m"
# NOTE: if you're using a real certificate, rather than a self-signed one, you'll want this set to `false`/to remove this.
insecure = true
}
provisioner "remote-exec" {
inline = [
"cd C:\\claranet",
"dir",
]
}
depends_on = [
azurerm_public_ip.main,
azurerm_network_interface.main,
azurerm_windows_virtual_machine.main,
]
}
resource "azapi_resource_action" "main" {
count = var.os_disk_tagging_enabled ? 1 : 0
type = "Microsoft.Compute/disks@2022-03-02"
resource_id = data.azurerm_managed_disk.vm_os_disk.id
method = "PATCH"
body = {
tags = merge(local.default_tags, var.extra_tags, var.os_disk_extra_tags)
}
}
resource "azurerm_managed_disk" "main" {
for_each = var.storage_data_disk_config
location = var.location
resource_group_name = var.resource_group_name
name = coalesce(each.value.name, data.azurecaf_name.disk[each.key].result)
zone = can(regex("_zrs$", lower(each.value.storage_account_type))) ? null : var.zone_id
storage_account_type = each.value.storage_account_type
create_option = each.value.create_option
disk_size_gb = each.value.disk_size_gb
source_resource_id = contains(["Copy", "Restore"], each.value.create_option) ? each.value.source_resource_id : null
tags = merge(local.default_tags, var.extra_tags, each.value.extra_tags)
}
moved {
from = azurerm_managed_disk.disk
to = azurerm_managed_disk.main
}
resource "azurerm_virtual_machine_data_disk_attachment" "main" {
for_each = var.storage_data_disk_config
managed_disk_id = azurerm_managed_disk.main[each.key].id
virtual_machine_id = azurerm_windows_virtual_machine.main.id
lun = coalesce(each.value.lun, index(keys(var.storage_data_disk_config), each.key))
caching = each.value.caching
}
# To be consistent with `linux-vm`
moved {
from = azurerm_virtual_machine_data_disk_attachment.disk_attach
to = azurerm_virtual_machine_data_disk_attachment.data_disk_attachment
}
moved {
from = azurerm_virtual_machine_data_disk_attachment.data_disk_attachment
to = azurerm_virtual_machine_data_disk_attachment.main
}