|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.compute.implementation; |
| 5 | + |
| 6 | +import com.azure.resourcemanager.authorization.models.BuiltInRole; |
| 7 | +import com.azure.resourcemanager.authorization.utils.RoleAssignmentHelper; |
| 8 | +import com.azure.resourcemanager.compute.ComputeManager; |
| 9 | +import com.azure.resourcemanager.compute.fluent.models.DiskEncryptionSetInner; |
| 10 | +import com.azure.resourcemanager.compute.models.DiskEncryptionSet; |
| 11 | +import com.azure.resourcemanager.compute.models.DiskEncryptionSetIdentityType; |
| 12 | +import com.azure.resourcemanager.compute.models.DiskEncryptionSetType; |
| 13 | +import com.azure.resourcemanager.compute.models.DiskEncryptionSetUpdate; |
| 14 | +import com.azure.resourcemanager.compute.models.EncryptionSetIdentity; |
| 15 | +import com.azure.resourcemanager.compute.models.KeyForDiskEncryptionSet; |
| 16 | +import com.azure.resourcemanager.compute.models.SourceVault; |
| 17 | +import com.azure.resourcemanager.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | + |
| 20 | +public class DiskEncryptionSetImpl |
| 21 | + extends GroupableResourceImpl<DiskEncryptionSet, DiskEncryptionSetInner, DiskEncryptionSetImpl, ComputeManager> |
| 22 | + implements DiskEncryptionSet, |
| 23 | + DiskEncryptionSet.Definition, |
| 24 | + DiskEncryptionSet.Update { |
| 25 | + private DiskEncryptionSetUpdate patchToUpdate = new DiskEncryptionSetUpdate(); |
| 26 | + private boolean updated; |
| 27 | + private final DiskEncryptionSetMsiHandler msiHandler; |
| 28 | + |
| 29 | + protected DiskEncryptionSetImpl(String name, DiskEncryptionSetInner innerObject, ComputeManager manager) { |
| 30 | + super(name, innerObject, manager); |
| 31 | + this.msiHandler = new DiskEncryptionSetMsiHandler(manager.authorizationManager(), this); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public String keyVaultId() { |
| 36 | + if (innerModel().activeKey() == null || innerModel().activeKey().sourceVault() == null) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + return innerModel().activeKey().sourceVault().id(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String encryptionKeyId() { |
| 44 | + if (innerModel().activeKey() == null) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + return innerModel().activeKey().keyUrl(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String systemAssignedManagedServiceIdentityPrincipalId() { |
| 52 | + if (innerModel().identity() == null || innerModel().identity().type() == DiskEncryptionSetIdentityType.NONE) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return innerModel().identity().principalId(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Boolean isAutomaticKeyRotationEnabled() { |
| 60 | + return innerModel().rotationToLatestKeyVersionEnabled(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public DiskEncryptionSetType encryptionType() { |
| 65 | + return innerModel().encryptionType(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public DiskEncryptionSetImpl withAutomaticKeyRotation() { |
| 70 | + innerModel().withRotationToLatestKeyVersionEnabled(true); |
| 71 | + if (isInUpdateMode()) { |
| 72 | + patchToUpdate.withRotationToLatestKeyVersionEnabled(true); |
| 73 | + updated = true; |
| 74 | + } |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public DiskEncryptionSetImpl withoutAutomaticKeyRotation() { |
| 80 | + innerModel().withRotationToLatestKeyVersionEnabled(false); |
| 81 | + if (isInUpdateMode()) { |
| 82 | + patchToUpdate.withRotationToLatestKeyVersionEnabled(false); |
| 83 | + updated = true; |
| 84 | + } |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public DiskEncryptionSetImpl withSystemAssignedManagedServiceIdentity() { |
| 90 | + innerModel().withIdentity(new EncryptionSetIdentity().withType(DiskEncryptionSetIdentityType.SYSTEM_ASSIGNED)); |
| 91 | + if (isInUpdateMode()) { |
| 92 | + patchToUpdate.withIdentity(innerModel().identity()); |
| 93 | + updated = true; |
| 94 | + } |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public DiskEncryptionSetImpl withoutSystemAssignedManagedServiceIdentity() { |
| 100 | + innerModel().withIdentity(new EncryptionSetIdentity().withType(DiskEncryptionSetIdentityType.NONE)); |
| 101 | + if (isInUpdateMode()) { |
| 102 | + patchToUpdate.withIdentity(innerModel().identity()); |
| 103 | + updated = true; |
| 104 | + } |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public DiskEncryptionSetImpl withExistingKeyVault(String keyVaultId) { |
| 110 | + ensureActiveKey(); |
| 111 | + innerModel().activeKey().withSourceVault(new SourceVault().withId(keyVaultId)); |
| 112 | + if (isInUpdateMode()) { |
| 113 | + ensureActiveKey(patchToUpdate); |
| 114 | + patchToUpdate.activeKey().withSourceVault(innerModel().activeKey().sourceVault()); |
| 115 | + updated = true; |
| 116 | + } |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public DiskEncryptionSetImpl withRoleBasedAccessToCurrentKeyVault(BuiltInRole builtInRole) { |
| 122 | + if (keyVaultId() != null) { |
| 123 | + msiHandler.withAccessTo(keyVaultId(), builtInRole); |
| 124 | + } |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public DiskEncryptionSetImpl withRoleBasedAccessToCurrentKeyVault() { |
| 130 | + return withRoleBasedAccessToCurrentKeyVault(BuiltInRole.KEY_VAULT_CRYPTO_SERVICE_ENCRYPTION_USER); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Mono<DiskEncryptionSet> createResourceAsync() { |
| 135 | + return manager().serviceClient().getDiskEncryptionSets().createOrUpdateAsync( |
| 136 | + resourceGroupName(), name(), innerModel() |
| 137 | + ).map(inner -> { |
| 138 | + setInner(inner); |
| 139 | + return this; |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public DiskEncryptionSetImpl update() { |
| 145 | + this.patchToUpdate = new DiskEncryptionSetUpdate(); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Mono<DiskEncryptionSet> updateResourceAsync() { |
| 151 | + if (!updated) { |
| 152 | + return Mono.just(this); |
| 153 | + } |
| 154 | + return manager().serviceClient().getDiskEncryptionSets().updateAsync( |
| 155 | + resourceGroupName(), name(), patchToUpdate |
| 156 | + ).map(inner -> { |
| 157 | + setInner(inner); |
| 158 | + this.updated = false; |
| 159 | + return this; |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + protected Mono<DiskEncryptionSetInner> getInnerAsync() { |
| 165 | + return manager().serviceClient().getDiskEncryptionSets().getByResourceGroupAsync( |
| 166 | + resourceGroupName(), name() |
| 167 | + ).map(inner -> { |
| 168 | + this.updated = false; |
| 169 | + return inner; |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public DiskEncryptionSetImpl withExistingKey(String keyId) { |
| 175 | + ensureActiveKey(); |
| 176 | + innerModel().activeKey().withKeyUrl(keyId); |
| 177 | + if (isInUpdateMode()) { |
| 178 | + ensureActiveKey(patchToUpdate); |
| 179 | + patchToUpdate.activeKey().withKeyUrl(keyId); |
| 180 | + updated = true; |
| 181 | + } |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public DiskEncryptionSetImpl withEncryptionType(DiskEncryptionSetType type) { |
| 187 | + innerModel().withEncryptionType(type); |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + RoleAssignmentHelper.IdProvider idProvider() { |
| 192 | + return new RoleAssignmentHelper.IdProvider() { |
| 193 | + @Override |
| 194 | + public String principalId() { |
| 195 | + return systemAssignedManagedServiceIdentityPrincipalId(); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public String resourceId() { |
| 200 | + return id(); |
| 201 | + } |
| 202 | + }; |
| 203 | + } |
| 204 | + |
| 205 | + private void ensureActiveKey() { |
| 206 | + if (innerModel().activeKey() == null) { |
| 207 | + innerModel().withActiveKey(new KeyForDiskEncryptionSet()); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private void ensureActiveKey(DiskEncryptionSetUpdate patchToUpdate) { |
| 212 | + if (patchToUpdate.activeKey() == null) { |
| 213 | + patchToUpdate.withActiveKey(new KeyForDiskEncryptionSet()); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private boolean isInUpdateMode() { |
| 218 | + return !isInCreateMode(); |
| 219 | + } |
| 220 | +} |
0 commit comments