|
1 | 1 | import cuid from 'cuid'
|
2 | 2 | import { isEmpty } from 'lodash'
|
3 |
| -import { AzureVirtualMachineScaleSet } from '../../types/generated' |
| 3 | +import { |
| 4 | + VirtualMachineScaleSetOSProfile, |
| 5 | + VirtualMachineScaleSetStorageProfile, |
| 6 | + VirtualMachineScaleSetNetworkProfile, |
| 7 | + VirtualMachineScaleSetExtensionProfile, |
| 8 | +} from '@azure/arm-compute' |
| 9 | +import { |
| 10 | + AzureVirtualMachineScaleSet, |
| 11 | + AzureVirtualMachineScaleSetOsProfile, |
| 12 | + AzureVirtualMachineScaleSetStorageProfile, |
| 13 | + AzureVirtualMachineScaleSetNetworkProfile, |
| 14 | + AzureVirtualMachineScaleSetExtension, |
| 15 | +} from '../../types/generated' |
4 | 16 | import { formatTagsFromMap } from '../../utils/format'
|
5 | 17 | import { RawAzureVirtualMachineScaleSet } from './data'
|
6 | 18 |
|
| 19 | +const formatOsProfile = ( |
| 20 | + osProfile?: VirtualMachineScaleSetOSProfile |
| 21 | +): AzureVirtualMachineScaleSetOsProfile => { |
| 22 | + if (isEmpty(osProfile)) { |
| 23 | + return {} |
| 24 | + } |
| 25 | + |
| 26 | + const { |
| 27 | + computerNamePrefix, |
| 28 | + adminUsername, |
| 29 | + allowExtensionOperations, |
| 30 | + windowsConfiguration, |
| 31 | + linuxConfiguration, |
| 32 | + secrets, |
| 33 | + } = osProfile |
| 34 | + |
| 35 | + return { |
| 36 | + computerNamePrefix, |
| 37 | + adminUsername, |
| 38 | + allowExtensionOperations, |
| 39 | + windowsConfiguration: windowsConfiguration |
| 40 | + ? { |
| 41 | + additionalUnattendContent: |
| 42 | + windowsConfiguration.additionalUnattendContent?.map(auc => ({ |
| 43 | + id: cuid(), |
| 44 | + ...auc, |
| 45 | + })) || [], |
| 46 | + winRM: windowsConfiguration.winRM |
| 47 | + ? { |
| 48 | + listeners: |
| 49 | + windowsConfiguration.winRM.listeners?.map(l => ({ |
| 50 | + id: cuid(), |
| 51 | + ...l, |
| 52 | + })) || [], |
| 53 | + } |
| 54 | + : {}, |
| 55 | + ...windowsConfiguration, |
| 56 | + } |
| 57 | + : {}, |
| 58 | + linuxConfiguration: linuxConfiguration |
| 59 | + ? { |
| 60 | + ssh: linuxConfiguration.ssh |
| 61 | + ? { |
| 62 | + publicKeys: |
| 63 | + linuxConfiguration.ssh.publicKeys?.map(pk => ({ |
| 64 | + id: cuid(), |
| 65 | + ...pk, |
| 66 | + })) || [], |
| 67 | + } |
| 68 | + : {}, |
| 69 | + } |
| 70 | + : {}, |
| 71 | + secrets: |
| 72 | + secrets?.map(s => ({ |
| 73 | + id: cuid(), |
| 74 | + sourceVault: { |
| 75 | + id: s.sourceVault?.id || cuid(), |
| 76 | + }, |
| 77 | + vaultCertificates: |
| 78 | + s.vaultCertificates?.map(vc => ({ |
| 79 | + id: cuid(), |
| 80 | + ...vc, |
| 81 | + })) || [], |
| 82 | + })) || [], |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const formatStorageProfile = ( |
| 87 | + storageProfile?: VirtualMachineScaleSetStorageProfile |
| 88 | +): AzureVirtualMachineScaleSetStorageProfile => { |
| 89 | + if (isEmpty(storageProfile)) { |
| 90 | + return {} |
| 91 | + } |
| 92 | + |
| 93 | + const { imageReference, osDisk } = storageProfile |
| 94 | + |
| 95 | + return { |
| 96 | + imageReference: imageReference |
| 97 | + ? { |
| 98 | + id: imageReference.id || cuid(), |
| 99 | + publisher: imageReference.publisher, |
| 100 | + offer: imageReference.offer, |
| 101 | + sku: imageReference.sku, |
| 102 | + version: imageReference.version, |
| 103 | + } |
| 104 | + : {}, |
| 105 | + osDisk: osDisk |
| 106 | + ? { |
| 107 | + caching: osDisk.caching, |
| 108 | + createOption: osDisk.createOption, |
| 109 | + osType: osDisk.osType, |
| 110 | + diskSizeGB: osDisk.diskSizeGB, |
| 111 | + writeAcceleratorEnabled: osDisk.writeAcceleratorEnabled, |
| 112 | + managedDisk: osDisk.managedDisk |
| 113 | + ? { |
| 114 | + storageAccountType: osDisk.managedDisk.storageAccountType, |
| 115 | + } |
| 116 | + : {}, |
| 117 | + } |
| 118 | + : {}, |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +const formatNetworkProfile = ( |
| 123 | + networkProfile?: VirtualMachineScaleSetNetworkProfile |
| 124 | +): AzureVirtualMachineScaleSetNetworkProfile => { |
| 125 | + if (isEmpty(networkProfile)) { |
| 126 | + return {} |
| 127 | + } |
| 128 | + |
| 129 | + const { networkInterfaceConfigurations = [] } = networkProfile |
| 130 | + |
| 131 | + return { |
| 132 | + networkInterfaceConfigurations: |
| 133 | + networkInterfaceConfigurations?.map( |
| 134 | + ({ |
| 135 | + id, |
| 136 | + ipConfigurations = [], |
| 137 | + networkSecurityGroup, |
| 138 | + ...networkInterface |
| 139 | + }) => { |
| 140 | + return { |
| 141 | + id: id || cuid(), |
| 142 | + ...networkInterface, |
| 143 | + networkSecurityGroup: { |
| 144 | + id: networkSecurityGroup?.id || cuid(), |
| 145 | + }, |
| 146 | + ipConfigurations: |
| 147 | + ipConfigurations?.map( |
| 148 | + ({ |
| 149 | + subnet, |
| 150 | + publicIPAddressConfiguration, |
| 151 | + applicationGatewayBackendAddressPools, |
| 152 | + applicationSecurityGroups, |
| 153 | + loadBalancerBackendAddressPools, |
| 154 | + loadBalancerInboundNatPools, |
| 155 | + ...ipConfiguration |
| 156 | + }) => ({ |
| 157 | + id: cuid(), |
| 158 | + ...ipConfiguration, |
| 159 | + subnetId: subnet?.id, |
| 160 | + applicationGatewayBackendAddressPools: |
| 161 | + applicationGatewayBackendAddressPools?.map(agb => ({ |
| 162 | + id: agb.id || cuid(), |
| 163 | + ...agb, |
| 164 | + })) || [], |
| 165 | + applicationSecurityGroups: |
| 166 | + applicationSecurityGroups?.map(asg => ({ |
| 167 | + id: asg.id || cuid(), |
| 168 | + ...asg, |
| 169 | + })) || [], |
| 170 | + loadBalancerBackendAddressPools: |
| 171 | + loadBalancerBackendAddressPools?.map(lbb => ({ |
| 172 | + id: lbb.id || cuid(), |
| 173 | + ...lbb, |
| 174 | + })) || [], |
| 175 | + loadBalancerInboundNatPools: |
| 176 | + loadBalancerInboundNatPools?.map(lbi => ({ |
| 177 | + id: lbi.id || cuid(), |
| 178 | + ...lbi, |
| 179 | + })) || [], |
| 180 | + }) |
| 181 | + ) || [], |
| 182 | + } |
| 183 | + } |
| 184 | + ) || [], |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +const formatExtensionProfile = ( |
| 189 | + extensionProfile?: VirtualMachineScaleSetExtensionProfile |
| 190 | +): AzureVirtualMachineScaleSetExtension[] => { |
| 191 | + if (isEmpty(extensionProfile)) { |
| 192 | + return [] |
| 193 | + } |
| 194 | + |
| 195 | + const { extensions: extensionsList = [] } = extensionProfile |
| 196 | + |
| 197 | + return ( |
| 198 | + extensionsList?.map(e => ({ |
| 199 | + id: e.id || cuid(), |
| 200 | + name: e.name, |
| 201 | + forceUpdateTag: e.forceUpdateTag, |
| 202 | + type: e.type, |
| 203 | + typeHandlerVersion: e.typeHandlerVersion, |
| 204 | + typePropertiesType: e.typePropertiesType, |
| 205 | + publisher: e.publisher, |
| 206 | + provisioningState: e.provisioningState, |
| 207 | + provisionAfterExtensions: e.provisionAfterExtensions, |
| 208 | + autoUpgradeMinorVersion: e.autoUpgradeMinorVersion, |
| 209 | + enableAutomaticUpgrade: e.enableAutomaticUpgrade, |
| 210 | + settings: e.settings ? JSON.stringify(e.settings) : '', |
| 211 | + })) || [] |
| 212 | + ) |
| 213 | +} |
| 214 | + |
7 | 215 | export default ({
|
8 | 216 | service,
|
9 | 217 | account,
|
@@ -46,64 +254,27 @@ export default ({
|
46 | 254 | }
|
47 | 255 |
|
48 | 256 | // Setting Extension Profile
|
49 |
| - let extensions = [] |
| 257 | + let extensions: AzureVirtualMachineScaleSetExtension[] = [] |
50 | 258 | if (!isEmpty(extensionProfile)) {
|
51 |
| - const { extensions: extensionsList = [] } = extensionProfile |
52 |
| - extensions = extensionsList.map(({ settings, ...extension }) => ({ |
53 |
| - id: cuid(), |
54 |
| - ...extension, |
55 |
| - settings: JSON.stringify(settings), |
56 |
| - })) |
| 259 | + extensions = formatExtensionProfile(extensionProfile) |
57 | 260 | }
|
58 | 261 |
|
59 | 262 | // Setting OS Profile
|
60 | 263 | let os = {}
|
61 | 264 | if (!isEmpty(osProfile)) {
|
62 |
| - const { |
63 |
| - computerNamePrefix, |
64 |
| - adminUsername, |
65 |
| - linuxConfiguration = {}, |
66 |
| - windowsConfiguration = {}, |
67 |
| - secrets = [], |
68 |
| - } = osProfile |
69 |
| - os = { |
70 |
| - computerNamePrefix, |
71 |
| - adminUsername, |
72 |
| - linuxConfiguration, |
73 |
| - windowsConfiguration, |
74 |
| - secrets, |
75 |
| - } |
| 265 | + os = formatOsProfile(osProfile) |
76 | 266 | }
|
77 | 267 |
|
78 | 268 | // Setting Network Profile
|
79 | 269 | let network = {}
|
80 | 270 | if (!isEmpty(networkProfile)) {
|
81 |
| - const { networkInterfaceConfigurations = [] } = networkProfile |
82 |
| - |
83 |
| - const networkInterfaces = networkInterfaceConfigurations.map( |
84 |
| - ({ ipConfigurations = [], ...networkInterface }) => { |
85 |
| - return { |
86 |
| - id: cuid(), |
87 |
| - ...networkInterface, |
88 |
| - ipConfigurations: ipConfigurations.map( |
89 |
| - ({ subnet, ...ipConfiguration }) => ({ |
90 |
| - id: cuid(), |
91 |
| - ...ipConfiguration, |
92 |
| - subnetId: subnet?.id, |
93 |
| - }) |
94 |
| - ), |
95 |
| - } |
96 |
| - } |
97 |
| - ) |
98 |
| - |
99 |
| - network = { networkInterfaceConfigurations: networkInterfaces } |
| 271 | + network = formatNetworkProfile(networkProfile) |
100 | 272 | }
|
101 | 273 |
|
102 | 274 | // Setting Storage Profile
|
103 | 275 | let storage = {}
|
104 | 276 | if (!isEmpty(storageProfile)) {
|
105 |
| - const { imageReference = {}, osDisk = {} } = storageProfile |
106 |
| - storage = { imageReference, osDisk } |
| 277 | + storage = formatStorageProfile(storageProfile) |
107 | 278 | }
|
108 | 279 |
|
109 | 280 | return {
|
|
0 commit comments