Skip to content

Commit 751f216

Browse files
mgmt, add convenience code for DiskEncryptionSet (Azure#28936)
* disk encryption set api * implementation * session records * checkstyle * changelog * Update ComputeManagementTest.java revert unnecessary changes * remove used imports * Update sdk/resourcemanager/azure-resourcemanager-compute/CHANGELOG.md Co-authored-by: Weidong Xu <weidxu@microsoft.com> * change name of rbac grant methods, add javadoc * session-records * fix compilation * rename withRBACBasedAccess to withRoleBasedAccess Co-authored-by: Weidong Xu <weidxu@microsoft.com>
1 parent 1fc5055 commit 751f216

File tree

16 files changed

+2279
-1269
lines changed

16 files changed

+2279
-1269
lines changed

sdk/resourcemanager/azure-resourcemanager-compute/CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
### Features Added
66

7-
### Breaking Changes
8-
9-
### Bugs Fixed
7+
- Supported `DiskEncryptionSet` for disk encryption set.
108

119
### Other Changes
1210

sdk/resourcemanager/azure-resourcemanager-compute/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm.implementation=ALL-UNNAMED
4545
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm.collection.implementation=ALL-UNNAMED
4646
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.arm.models.implementation=ALL-UNNAMED
47+
--add-exports com.azure.resourcemanager.resources/com.azure.resourcemanager.resources.fluentcore.model.implementation=ALL-UNNAMED
4748

4849
--add-opens com.azure.resourcemanager.authorization/com.azure.resourcemanager.authorization=ALL-UNNAMED
4950
--add-opens com.azure.resourcemanager.msi/com.azure.resourcemanager.msi=ALL-UNNAMED

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/ComputeManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
import com.azure.core.credential.TokenCredential;
77
import com.azure.core.http.HttpPipeline;
8+
import com.azure.core.management.profile.AzureProfile;
9+
import com.azure.resourcemanager.authorization.AuthorizationManager;
810
import com.azure.resourcemanager.compute.fluent.ComputeManagementClient;
9-
import com.azure.resourcemanager.compute.implementation.ComputeManagementClientBuilder;
1011
import com.azure.resourcemanager.compute.implementation.AvailabilitySetsImpl;
12+
import com.azure.resourcemanager.compute.implementation.ComputeManagementClientBuilder;
1113
import com.azure.resourcemanager.compute.implementation.ComputeSkusImpl;
1214
import com.azure.resourcemanager.compute.implementation.ComputeUsagesImpl;
15+
import com.azure.resourcemanager.compute.implementation.DiskEncryptionSetsImpl;
1316
import com.azure.resourcemanager.compute.implementation.DisksImpl;
1417
import com.azure.resourcemanager.compute.implementation.GalleriesImpl;
1518
import com.azure.resourcemanager.compute.implementation.GalleryImageVersionsImpl;
@@ -24,6 +27,7 @@
2427
import com.azure.resourcemanager.compute.models.AvailabilitySets;
2528
import com.azure.resourcemanager.compute.models.ComputeSkus;
2629
import com.azure.resourcemanager.compute.models.ComputeUsages;
30+
import com.azure.resourcemanager.compute.models.DiskEncryptionSets;
2731
import com.azure.resourcemanager.compute.models.Disks;
2832
import com.azure.resourcemanager.compute.models.Galleries;
2933
import com.azure.resourcemanager.compute.models.GalleryImageVersions;
@@ -34,11 +38,9 @@
3438
import com.azure.resourcemanager.compute.models.VirtualMachineImages;
3539
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSets;
3640
import com.azure.resourcemanager.compute.models.VirtualMachines;
37-
import com.azure.resourcemanager.authorization.AuthorizationManager;
3841
import com.azure.resourcemanager.network.NetworkManager;
3942
import com.azure.resourcemanager.resources.fluentcore.arm.AzureConfigurable;
4043
import com.azure.resourcemanager.resources.fluentcore.arm.Manager;
41-
import com.azure.core.management.profile.AzureProfile;
4244
import com.azure.resourcemanager.resources.fluentcore.arm.implementation.AzureConfigurableImpl;
4345
import com.azure.resourcemanager.resources.fluentcore.utils.HttpPipelineProvider;
4446
import com.azure.resourcemanager.storage.StorageManager;
@@ -66,6 +68,7 @@ public final class ComputeManager extends Manager<ComputeManagementClient> {
6668
private Galleries galleries;
6769
private GalleryImages galleryImages;
6870
private GalleryImageVersions galleryImageVersions;
71+
private DiskEncryptionSets diskEncryptionSets;
6972

7073
/** @return the storage manager */
7174
public StorageManager storageManager() {
@@ -264,4 +267,12 @@ public GalleryImageVersions galleryImageVersions() {
264267
}
265268
return galleryImageVersions;
266269
}
270+
271+
/** @return the disk encryption set management entry point */
272+
public DiskEncryptionSets diskEncryptionSets() {
273+
if (diskEncryptionSets == null) {
274+
diskEncryptionSets = new DiskEncryptionSetsImpl(this);
275+
}
276+
return diskEncryptionSets;
277+
}
267278
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.AuthorizationManager;
7+
import com.azure.resourcemanager.authorization.utils.RoleAssignmentHelper;
8+
9+
/**
10+
* Utility class to set Managed Service Identity (MSI) property on a disk encryption set,
11+
* create role assignments for the service principal associated with the disk encryption set.
12+
*/
13+
class DiskEncryptionSetMsiHandler extends RoleAssignmentHelper {
14+
/**
15+
* Creates DiskEncryptionSetMsiHandler.
16+
*
17+
* @param authorizationManager the graph rbac manager
18+
* @param diskEncryptionSet disk encryption set
19+
*/
20+
DiskEncryptionSetMsiHandler(AuthorizationManager authorizationManager, DiskEncryptionSetImpl diskEncryptionSet) {
21+
super(authorizationManager, diskEncryptionSet.taskGroup(), diskEncryptionSet.idProvider());
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.compute.ComputeManager;
7+
import com.azure.resourcemanager.compute.fluent.DiskEncryptionSetsClient;
8+
import com.azure.resourcemanager.compute.fluent.models.DiskEncryptionSetInner;
9+
import com.azure.resourcemanager.compute.models.DiskEncryptionSet;
10+
import com.azure.resourcemanager.compute.models.DiskEncryptionSets;
11+
import com.azure.resourcemanager.resources.fluentcore.arm.collection.implementation.TopLevelModifiableResourcesImpl;
12+
13+
public class DiskEncryptionSetsImpl
14+
extends TopLevelModifiableResourcesImpl<
15+
DiskEncryptionSet,
16+
DiskEncryptionSetImpl,
17+
DiskEncryptionSetInner,
18+
DiskEncryptionSetsClient,
19+
ComputeManager>
20+
implements DiskEncryptionSets {
21+
public DiskEncryptionSetsImpl(ComputeManager manager) {
22+
super(manager.serviceClient().getDiskEncryptionSets(), manager);
23+
}
24+
25+
@Override
26+
public DiskEncryptionSet.DefinitionStages.Blank define(String name) {
27+
return wrapModel(name);
28+
}
29+
30+
@Override
31+
protected DiskEncryptionSetImpl wrapModel(String name) {
32+
DiskEncryptionSetInner inner = new DiskEncryptionSetInner();
33+
return new DiskEncryptionSetImpl(name, inner, manager());
34+
}
35+
36+
@Override
37+
protected DiskEncryptionSetImpl wrapModel(DiskEncryptionSetInner inner) {
38+
if (inner == null) {
39+
return null;
40+
}
41+
return new DiskEncryptionSetImpl(inner.name(), inner, manager());
42+
}
43+
}

0 commit comments

Comments
 (0)