Skip to content

Commit f2f60f0

Browse files
mgmt-core, support systemData for Azure resource (Azure#18303)
1 parent e38e701 commit f2f60f0

File tree

6 files changed

+218
-24
lines changed

6 files changed

+218
-24
lines changed

sdk/core/azure-core-management/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.2.0-beta.1 (Unreleased)
44

5+
- Added `SystemData`.
56

67
## 1.1.1 (2021-02-05)
78

sdk/core/azure-core-management/src/main/java/com/azure/core/management/ProxyResource.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,20 @@
99
* The Proxy Resource model.
1010
*/
1111
public class ProxyResource {
12-
/**
13-
* Resource Id.
14-
*/
12+
1513
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
1614
private String id;
1715

18-
/**
19-
* Resource name.
20-
*/
2116
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
2217
private String name;
2318

24-
/**
25-
* Resource type.
26-
*/
2719
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
2820
private String type;
2921

3022
/**
3123
* Get the id value.
3224
*
33-
* @return the id value
25+
* @return the fully qualified resource ID for the resource.
3426
*/
3527
public String id() {
3628
return this.id;
@@ -39,7 +31,7 @@ public String id() {
3931
/**
4032
* Get the name value.
4133
*
42-
* @return the name value
34+
* @return the name of the resource.
4335
*/
4436
public String name() {
4537
return this.name;
@@ -48,7 +40,7 @@ public String name() {
4840
/**
4941
* Get the type value.
5042
*
51-
* @return the type value
43+
* @return the type of the resource.
5244
*/
5345
public String type() {
5446
return this.type;

sdk/core/azure-core-management/src/main/java/com/azure/core/management/Resource.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@
1111
* The Resource model.
1212
*/
1313
public class Resource extends ProxyResource {
14-
/**
15-
* Resource location.
16-
*/
14+
1715
@JsonProperty(required = true)
1816
private String location;
1917

20-
/**
21-
* Resource tags.
22-
*/
2318
private Map<String, String> tags;
2419

2520
/**
2621
* Get the location value.
2722
*
28-
* @return the location value
23+
* @return the geo-location where the resource live.
2924
*/
3025
public String location() {
3126
return this.location;
@@ -34,8 +29,8 @@ public String location() {
3429
/**
3530
* Set the location value.
3631
*
37-
* @param location the location value to set
38-
* @return the resource itself
32+
* @param location the geo-location where the resource live.
33+
* @return the resource itself.
3934
*/
4035
public Resource withLocation(String location) {
4136
this.location = location;
@@ -45,7 +40,7 @@ public Resource withLocation(String location) {
4540
/**
4641
* Get the tags value.
4742
*
48-
* @return the tags value
43+
* @return the tags of the resource.
4944
*/
5045
public Map<String, String> tags() {
5146
return this.tags;
@@ -54,8 +49,8 @@ public Map<String, String> tags() {
5449
/**
5550
* Set the tags value.
5651
*
57-
* @param tags the tags value to set
58-
* @return the resource itself
52+
* @param tags the tags of the resource.
53+
* @return the resource itself.
5954
*/
6055
public Resource withTags(Map<String, String> tags) {
6156
this.tags = tags;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.core.management;
5+
6+
import com.azure.core.util.ExpandableStringEnum;
7+
import com.fasterxml.jackson.annotation.JsonCreator;
8+
import java.util.Collection;
9+
10+
/** Defines values for identity type of resource author. */
11+
public final class ResourceAuthorIdentityType extends ExpandableStringEnum<ResourceAuthorIdentityType> {
12+
13+
/** Static value User for ResourceAuthorIdentityType, represents an AAD user. */
14+
public static final ResourceAuthorIdentityType USER = fromString("User");
15+
16+
/** Static value Application for ResourceAuthorIdentityType, represents an AAD application. */
17+
public static final ResourceAuthorIdentityType APPLICATION = fromString("Application");
18+
19+
/** Static value ManagedIdentity for ResourceAuthorIdentityType, represents a Managed Identity. */
20+
public static final ResourceAuthorIdentityType MANAGED_IDENTITY = fromString("ManagedIdentity");
21+
22+
/** Static value Key for ResourceAuthorIdentityType. */
23+
public static final ResourceAuthorIdentityType KEY = fromString("Key");
24+
25+
/**
26+
* Creates or finds a ResourceAuthorIdentityType from its string representation.
27+
*
28+
* @param name a name to look for.
29+
* @return the corresponding ResourceAuthorIdentityType.
30+
*/
31+
@JsonCreator
32+
public static ResourceAuthorIdentityType fromString(String name) {
33+
return fromString(name, ResourceAuthorIdentityType.class);
34+
}
35+
36+
/** @return known ResourceAuthorIdentityType values. */
37+
public static Collection<ResourceAuthorIdentityType> values() {
38+
return values(ResourceAuthorIdentityType.class);
39+
}
40+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.core.management;
5+
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
8+
import java.time.OffsetDateTime;
9+
10+
/** Metadata pertaining to creation and last modification of the resource. */
11+
public final class SystemData {
12+
13+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
14+
private String createdBy;
15+
16+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
17+
private ResourceAuthorIdentityType createdByType;
18+
19+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
20+
private OffsetDateTime createdAt;
21+
22+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
23+
private String lastModifiedBy;
24+
25+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
26+
private ResourceAuthorIdentityType lastModifiedByType;
27+
28+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
29+
private OffsetDateTime lastModifiedAt;
30+
31+
/**
32+
* Get the identity that created the resource.
33+
*
34+
* @return the identity that created the resource.
35+
*/
36+
public String createdBy() {
37+
return this.createdBy;
38+
}
39+
40+
/**
41+
* Get the type of identity that created the resource.
42+
*
43+
* @return the type of identity that created the resource.
44+
*/
45+
public ResourceAuthorIdentityType createdByType() {
46+
return this.createdByType;
47+
}
48+
49+
/**
50+
* Get the timestamp of resource creation (UTC).
51+
*
52+
* @return the timestamp of resource creation (UTC).
53+
*/
54+
public OffsetDateTime createdAt() {
55+
return this.createdAt;
56+
}
57+
58+
/**
59+
* Get the identity that last modified the resource.
60+
*
61+
* @return the identity that last modified the resource.
62+
*/
63+
public String lastModifiedBy() {
64+
return this.lastModifiedBy;
65+
}
66+
67+
/**
68+
* Get the type of identity that last modified the resource.
69+
*
70+
* @return the type of identity that last modified the resource.
71+
*/
72+
public ResourceAuthorIdentityType lastModifiedByType() {
73+
return this.lastModifiedByType;
74+
}
75+
76+
/**
77+
* Get the type of identity that last modified the resource.
78+
*
79+
* @return the timestamp of resource modification (UTC).
80+
*/
81+
public OffsetDateTime lastModifiedAt() {
82+
return this.lastModifiedAt;
83+
}
84+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.core.management;
5+
6+
import com.azure.core.management.serializer.SerializerFactory;
7+
import com.azure.core.util.serializer.SerializerAdapter;
8+
import com.azure.core.util.serializer.SerializerEncoding;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import wiremock.com.fasterxml.jackson.annotation.JsonProperty;
12+
13+
import java.io.IOException;
14+
15+
public class ResourceTests {
16+
17+
private static class ProxyResourceWithSystemData extends ProxyResource {
18+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
19+
private SystemData systemData;
20+
21+
/**
22+
* Get the systemData value.
23+
*
24+
* @return the metadata pertaining to creation and last modification of the resource.
25+
* */
26+
public SystemData systemData() {
27+
return this.systemData;
28+
}
29+
}
30+
31+
private static class ResourceWithSystemData extends Resource {
32+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
33+
private SystemData systemData;
34+
35+
/**
36+
* Get the systemData value.
37+
*
38+
* @return the metadata pertaining to creation and last modification of the resource.
39+
* */
40+
public SystemData systemData() {
41+
return this.systemData;
42+
}
43+
}
44+
45+
@Test
46+
public void testSerialization() throws IOException {
47+
String cosmosAccountJson = "{\"id\":\"/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.DocumentDB/databaseAccounts/c1weidxu\",\"name\":\"c1weidxu\",\"location\":\"West US\",\"type\":\"Microsoft.DocumentDB/databaseAccounts\",\"kind\":\"GlobalDocumentDB\",\"tags\":{\"defaultExperience\":\"Core (SQL)\",\"hidden-cosmos-mmspecial\":\"\",\"CosmosAccountType\":\"Non-Production\"},\"systemData\":{\"createdBy\":\"00000000-0000-0000-0000-000000000000\",\"createdByType\":\"Application\",\"createdAt\":\"2021-03-03T02:03:46.3387771Z\",\"lastModifiedBy\":\"johndoe@microsoft.com\",\"lastModifiedByType\":\"User\",\"lastModifiedAt\":\"2021-03-03T02:04:42.8252362Z\"},\"properties\":{\"provisioningState\":\"Succeeded\",\"documentEndpoint\":\"https://c1weidxu.documents.azure.com:443/\",\"publicNetworkAccess\":\"Enabled\",\"enableAutomaticFailover\":false,\"enableMultipleWriteLocations\":false,\"enablePartitionKeyMonitor\":false,\"isVirtualNetworkFilterEnabled\":false,\"virtualNetworkRules\":[],\"EnabledApiTypes\":\"Sql\",\"disableKeyBasedMetadataWriteAccess\":false,\"enableFreeTier\":false,\"enableAnalyticalStorage\":false,\"instanceId\":\"f5a124e6-988e-4936-8c9b-38e011c80ef4\",\"createMode\":\"Default\",\"databaseAccountOfferType\":\"Standard\",\"enableCassandraConnector\":false,\"connectorOffer\":\"\",\"consistencyPolicy\":{\"defaultConsistencyLevel\":\"Session\",\"maxIntervalInSeconds\":5,\"maxStalenessPrefix\":100},\"configurationOverrides\":{},\"writeLocations\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"documentEndpoint\":\"https://c1weidxu-westus.documents.azure.com:443/\",\"provisioningState\":\"Succeeded\",\"failoverPriority\":0,\"isZoneRedundant\":false}],\"readLocations\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"documentEndpoint\":\"https://c1weidxu-westus.documents.azure.com:443/\",\"provisioningState\":\"Succeeded\",\"failoverPriority\":0,\"isZoneRedundant\":false}],\"locations\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"documentEndpoint\":\"https://c1weidxu-westus.documents.azure.com:443/\",\"provisioningState\":\"Succeeded\",\"failoverPriority\":0,\"isZoneRedundant\":false}],\"failoverPolicies\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"failoverPriority\":0}],\"cors\":[],\"capabilities\":[],\"ipRules\":[],\"backupPolicy\":{\"type\":\"Periodic\",\"periodicModeProperties\":{\"backupIntervalInMinutes\":240,\"backupRetentionIntervalInHours\":8}}}}";
48+
49+
SerializerAdapter serializerAdapter = SerializerFactory.createDefaultManagementSerializerAdapter();
50+
ResourceWithSystemData cosmosAccountResource = serializerAdapter.deserialize(cosmosAccountJson, ResourceWithSystemData.class, SerializerEncoding.JSON);
51+
Assertions.assertEquals("/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.DocumentDB/databaseAccounts/c1weidxu", cosmosAccountResource.id());
52+
Assertions.assertEquals(Region.US_WEST, Region.fromName(cosmosAccountResource.location()));
53+
Assertions.assertEquals("Microsoft.DocumentDB/databaseAccounts", cosmosAccountResource.type());
54+
Assertions.assertEquals(3, cosmosAccountResource.tags().size());
55+
Assertions.assertNotNull(cosmosAccountResource.systemData());
56+
Assertions.assertNotNull(cosmosAccountResource.systemData().createdAt());
57+
Assertions.assertEquals("00000000-0000-0000-0000-000000000000", cosmosAccountResource.systemData().createdBy());
58+
Assertions.assertEquals(ResourceAuthorIdentityType.APPLICATION, cosmosAccountResource.systemData().createdByType());
59+
Assertions.assertNotNull(cosmosAccountResource.systemData().createdAt());
60+
Assertions.assertEquals("johndoe@microsoft.com", cosmosAccountResource.systemData().lastModifiedBy());
61+
Assertions.assertEquals(ResourceAuthorIdentityType.USER, cosmosAccountResource.systemData().lastModifiedByType());
62+
Assertions.assertNotNull(cosmosAccountResource.systemData().lastModifiedAt());
63+
64+
ProxyResourceWithSystemData proxyResource = serializerAdapter.deserialize(cosmosAccountJson, ProxyResourceWithSystemData.class, SerializerEncoding.JSON);
65+
Assertions.assertNotNull(proxyResource.systemData());
66+
Assertions.assertNotNull(proxyResource.systemData().createdAt());
67+
Assertions.assertEquals("00000000-0000-0000-0000-000000000000", proxyResource.systemData().createdBy());
68+
Assertions.assertEquals(ResourceAuthorIdentityType.APPLICATION, proxyResource.systemData().createdByType());
69+
Assertions.assertNotNull(cosmosAccountResource.systemData().createdAt());
70+
Assertions.assertEquals("johndoe@microsoft.com", cosmosAccountResource.systemData().lastModifiedBy());
71+
Assertions.assertEquals(ResourceAuthorIdentityType.USER, cosmosAccountResource.systemData().lastModifiedByType());
72+
Assertions.assertNotNull(cosmosAccountResource.systemData().lastModifiedAt());
73+
74+
String vaultJson = "{\"id\":\"/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.KeyVault/vaults/v1weidxu\",\"name\":\"v1weidxu\",\"type\":\"Microsoft.KeyVault/vaults\",\"location\":\"centralus\",\"tags\":{},\"properties\":{\"sku\":{\"family\":\"A\",\"name\":\"standard\"},\"tenantId\":\"###\",\"accessPolicies\":[],\"enabledForDeployment\":false,\"vaultUri\":\"https://v1weidxu.vault.azure.net/\",\"provisioningState\":\"Succeeded\"}}";
75+
ResourceWithSystemData vaultResource = serializerAdapter.deserialize(vaultJson, ResourceWithSystemData.class, SerializerEncoding.JSON);
76+
Assertions.assertEquals("/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.KeyVault/vaults/v1weidxu", vaultResource.id());
77+
Assertions.assertEquals(Region.US_CENTRAL, Region.fromName(vaultResource.location()));
78+
Assertions.assertEquals("Microsoft.KeyVault/vaults", vaultResource.type());
79+
Assertions.assertEquals(0, vaultResource.tags().size());
80+
Assertions.assertNull(vaultResource.systemData());
81+
}
82+
}

0 commit comments

Comments
 (0)