Skip to content

Commit d58d0ba

Browse files
committed
Release azure-resourcemanager 2.0.0
1 parent ef679be commit d58d0ba

File tree

6 files changed

+1889
-1619
lines changed

6 files changed

+1889
-1619
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extensions:
2525

2626
To run this sample:
2727

28-
Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-libraries-for-java/blob/master/AUTH.md).
28+
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/docs/AUTH.md).
2929

3030
git clone https://github.com/Azure-Samples/monitor-java-query-metrics-activitylogs.git
3131

@@ -35,9 +35,11 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut
3535

3636
## More information ##
3737

38-
[http://azure.com/java](http://azure.com/java)
38+
For general documentation as well as quickstarts on how to use Azure Management Libraries for Java, please see [here](https://aka.ms/azsdk/java/mgmt).
3939

40-
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
40+
Start to develop applications with Java on Azure [here](http://azure.com/java).
41+
42+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).
4143

4244
---
4345

pom.xml

+21-15
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
<artifactId>exec-maven-plugin</artifactId>
1616
<version>1.4.0</version>
1717
<configuration>
18-
<mainClass>com.microsoft.azure.management.monitor.samples.QueryMetricsAndActivityLogs</mainClass>
18+
<mainClass>com.azure.resourcemanager.monitor.samples.QueryMetricsAndActivityLogs</mainClass>
19+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
1920
</configuration>
2021
</plugin>
2122
<plugin>
2223
<artifactId>maven-compiler-plugin</artifactId>
23-
<version>3.0</version>
24+
<version>3.8.1</version>
2425
<configuration>
25-
<source>1.7</source>
26-
<target>1.7</target>
26+
<source>1.8</source>
27+
<target>1.8</target>
2728
</configuration>
2829
</plugin>
2930
<plugin>
@@ -40,7 +41,7 @@
4041
</descriptorRefs>
4142
<archive>
4243
<manifest>
43-
<mainClass>com.microsoft.azure.management.monitor.samples.QueryMetricsAndActivityLogs.java</mainClass>
44+
<mainClass>com.azure.resourcemanager.monitor.samples.QueryMetricsAndActivityLogs.java</mainClass>
4445
</manifest>
4546
</archive>
4647
</configuration>
@@ -51,24 +52,29 @@
5152
</build>
5253
<dependencies>
5354
<dependency>
54-
<groupId>com.microsoft.azure</groupId>
55-
<artifactId>azure</artifactId>
56-
<version>1.36.3</version>
55+
<groupId>com.azure.resourcemanager</groupId>
56+
<artifactId>azure-resourcemanager</artifactId>
57+
<version>2.0.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.azure</groupId>
61+
<artifactId>azure-identity</artifactId>
62+
<version>1.1.3</version>
5763
</dependency>
5864
<dependency>
5965
<groupId>commons-net</groupId>
6066
<artifactId>commons-net</artifactId>
61-
<version>3.3</version>
67+
<version>3.6</version>
6268
</dependency>
6369
<dependency>
64-
<groupId>commons-lang</groupId>
65-
<artifactId>commons-lang</artifactId>
66-
<version>2.6</version>
70+
<groupId>com.azure</groupId>
71+
<artifactId>azure-storage-blob</artifactId>
72+
<version>12.8.0</version>
6773
</dependency>
6874
<dependency>
69-
<groupId>org.apache.commons</groupId>
70-
<artifactId>commons-lang3</artifactId>
71-
<version>3.7</version>
75+
<groupId>commons-io</groupId>
76+
<artifactId>commons-io</artifactId>
77+
<version>2.6</version>
7278
</dependency>
7379
</dependencies>
7480
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.monitor.samples;
5+
6+
import com.azure.core.credential.TokenCredential;
7+
import com.azure.core.http.HttpClient;
8+
import com.azure.core.http.policy.HttpLogDetailLevel;
9+
import com.azure.core.http.rest.PagedIterable;
10+
import com.azure.core.management.AzureEnvironment;
11+
import com.azure.identity.DefaultAzureCredentialBuilder;
12+
import com.azure.resourcemanager.AzureResourceManager;
13+
import com.azure.resourcemanager.monitor.models.EventData;
14+
import com.azure.resourcemanager.monitor.models.Metric;
15+
import com.azure.resourcemanager.monitor.models.MetricCollection;
16+
import com.azure.resourcemanager.monitor.models.MetricDefinition;
17+
import com.azure.resourcemanager.monitor.models.MetricValue;
18+
import com.azure.resourcemanager.monitor.models.TimeSeriesElement;
19+
import com.azure.resourcemanager.monitor.fluent.models.MetadataValueInner;
20+
import com.azure.core.management.Region;
21+
import com.azure.core.management.profile.AzureProfile;
22+
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
23+
import com.azure.resourcemanager.samples.Utils;
24+
import com.azure.resourcemanager.storage.models.AccessTier;
25+
import com.azure.resourcemanager.storage.models.StorageAccount;
26+
import com.azure.resourcemanager.storage.models.StorageAccountKey;
27+
import com.azure.storage.blob.BlobClient;
28+
import com.azure.storage.blob.BlobContainerClient;
29+
import com.azure.storage.blob.BlobContainerClientBuilder;
30+
import com.azure.storage.blob.BlobServiceClient;
31+
import com.azure.storage.blob.BlobServiceClientBuilder;
32+
import com.azure.storage.blob.models.BlobAnalyticsLogging;
33+
import com.azure.storage.blob.models.BlobMetrics;
34+
import com.azure.storage.blob.models.BlobRetentionPolicy;
35+
import com.azure.storage.blob.models.BlobServiceProperties;
36+
import com.azure.storage.blob.specialized.BlockBlobClient;
37+
import org.apache.commons.io.IOUtils;
38+
39+
import java.io.ByteArrayInputStream;
40+
import java.io.IOException;
41+
import java.io.InputStream;
42+
import java.time.Duration;
43+
import java.time.OffsetDateTime;
44+
import java.util.List;
45+
46+
/**
47+
* This sample shows examples of retrieving metrics and activity logs for Storage Account.
48+
* - List all metric definitions available for a storage account
49+
* - Retrieve and show metrics for the past 7 days for Transactions where
50+
* - Api name was 'PutBlob' and
51+
* - response type was 'Success' and
52+
* - Geo type was 'Primary'
53+
* - Retrieve and show all activity logs for the past 7 days for the same Storage account.
54+
*/
55+
public final class QueryMetricsAndActivityLogs {
56+
57+
/**
58+
* Main function which runs the actual sample.
59+
* @param azureResourceManager instance of the azure client
60+
* @return true if sample runs successfully
61+
*/
62+
public static boolean runSample(AzureResourceManager azureResourceManager) throws IOException {
63+
final String storageAccountName = Utils.randomResourceName(azureResourceManager, "saMonitor", 20);
64+
final String rgName = Utils.randomResourceName(azureResourceManager, "rgMonitor", 20);
65+
66+
try {
67+
// ============================================================
68+
// Create a storage account
69+
70+
System.out.println("Creating a Storage Account");
71+
72+
StorageAccount storageAccount = azureResourceManager.storageAccounts().define(storageAccountName)
73+
.withRegion(Region.US_EAST)
74+
.withNewResourceGroup(rgName)
75+
.withBlobStorageAccountKind()
76+
.withAccessTier(AccessTier.COOL)
77+
.create();
78+
79+
System.out.println("Created a Storage Account:");
80+
Utils.print(storageAccount);
81+
82+
List<StorageAccountKey> storageAccountKeys = storageAccount.getKeys();
83+
final String storageConnectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s",
84+
storageAccount.name(),
85+
storageAccountKeys.get(0).value());
86+
87+
// Add some blob transaction events
88+
addBlobTransactions(storageConnectionString, storageAccount.manager().httpPipeline().getHttpClient());
89+
90+
OffsetDateTime recordDateTime = OffsetDateTime.now();
91+
// get metric definitions for storage account.
92+
for (MetricDefinition metricDefinition : azureResourceManager.metricDefinitions().listByResource(storageAccount.id())) {
93+
// find metric definition for Transactions
94+
if (metricDefinition.name().localizedValue().equalsIgnoreCase("transactions")) {
95+
// get metric records
96+
MetricCollection metricCollection = metricDefinition.defineQuery()
97+
.startingFrom(recordDateTime.minusDays(7))
98+
.endsBefore(recordDateTime)
99+
.withAggregation("Average")
100+
.withInterval(Duration.ofMinutes(5))
101+
.withOdataFilter("apiName eq 'PutBlob' and responseType eq 'Success' and geoType eq 'Primary'")
102+
.execute();
103+
104+
System.out.println("Metrics for '" + storageAccount.id() + "':");
105+
System.out.println("Namespacse: " + metricCollection.namespace());
106+
System.out.println("Query time: " + metricCollection.timespan());
107+
System.out.println("Time Grain: " + metricCollection.interval());
108+
System.out.println("Cost: " + metricCollection.cost());
109+
110+
for (Metric metric : metricCollection.metrics()) {
111+
System.out.println("\tMetric: " + metric.name().localizedValue());
112+
System.out.println("\tType: " + metric.type());
113+
System.out.println("\tUnit: " + metric.unit());
114+
System.out.println("\tTime Series: ");
115+
for (TimeSeriesElement timeElement : metric.timeseries()) {
116+
System.out.println("\t\tMetadata: ");
117+
for (MetadataValueInner metadata : timeElement.metadatavalues()) {
118+
System.out.println("\t\t\t" + metadata.name().localizedValue() + ": " + metadata.value());
119+
}
120+
System.out.println("\t\tData: ");
121+
for (MetricValue data : timeElement.data()) {
122+
System.out.println("\t\t\t" + data.timestamp()
123+
+ " : (Min) " + data.minimum()
124+
+ " : (Max) " + data.maximum()
125+
+ " : (Avg) " + data.average()
126+
+ " : (Total) " + data.total()
127+
+ " : (Count) " + data.count());
128+
}
129+
}
130+
}
131+
break;
132+
}
133+
}
134+
135+
// get activity logs for the same period.
136+
PagedIterable<EventData> logs = azureResourceManager.activityLogs().defineQuery()
137+
.startingFrom(recordDateTime.minusDays(7))
138+
.endsBefore(recordDateTime)
139+
.withAllPropertiesInResponse()
140+
.filterByResource(storageAccount.id())
141+
.execute();
142+
143+
System.out.println("Activity logs for the Storage Account:");
144+
145+
for (EventData event : logs) {
146+
if (event.eventName() != null) {
147+
System.out.println("\tEvent: " + event.eventName().localizedValue());
148+
}
149+
if (event.operationName() != null) {
150+
System.out.println("\tOperation: " + event.operationName().localizedValue());
151+
}
152+
System.out.println("\tCaller: " + event.caller());
153+
System.out.println("\tCorrelationId: " + event.correlationId());
154+
System.out.println("\tSubscriptionId: " + event.subscriptionId());
155+
}
156+
157+
return true;
158+
} finally {
159+
if (azureResourceManager.resourceGroups().getByName(rgName) != null) {
160+
System.out.println("Deleting Resource Group: " + rgName);
161+
azureResourceManager.resourceGroups().beginDeleteByName(rgName);
162+
System.out.println("Deleted Resource Group: " + rgName);
163+
} else {
164+
System.out.println("Did not create any resources in Azure. No clean up is necessary");
165+
}
166+
}
167+
}
168+
169+
/**
170+
* Main entry point.
171+
* @param args the parameters
172+
*/
173+
public static void main(String[] args) {
174+
try {
175+
176+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
177+
final TokenCredential credential = new DefaultAzureCredentialBuilder()
178+
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
179+
.build();
180+
181+
AzureResourceManager azureResourceManager = AzureResourceManager
182+
.configure()
183+
.withLogLevel(HttpLogDetailLevel.BASIC)
184+
.authenticate(credential, profile)
185+
.withDefaultSubscription();
186+
187+
// Print selected subscription
188+
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());
189+
190+
runSample(azureResourceManager);
191+
} catch (Exception e) {
192+
System.out.println(e.getMessage());
193+
e.printStackTrace();
194+
}
195+
}
196+
197+
private static void addBlobTransactions(String storageConnectionString, HttpClient httpClient) throws IOException {
198+
// Get the script to upload
199+
//
200+
try (InputStream scriptFileAsStream = QueryMetricsAndActivityLogs.class.getResourceAsStream("/install_apache.sh")) {
201+
202+
// Get the size of the stream
203+
//
204+
byte[] scriptFileBytes = IOUtils.toByteArray(scriptFileAsStream);
205+
ByteArrayInputStream scriptFileStream = new ByteArrayInputStream(scriptFileBytes);
206+
int fileSize = scriptFileBytes.length;
207+
208+
// Upload the script file as block blob
209+
//
210+
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
211+
.connectionString(storageConnectionString)
212+
.containerName("scripts")
213+
.httpClient(httpClient)
214+
.buildClient();
215+
216+
blobContainerClient.create();
217+
218+
// Get the service properties.
219+
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
220+
.connectionString(storageConnectionString)
221+
.httpClient(httpClient)
222+
.buildClient();
223+
BlobServiceProperties serviceProps = blobServiceClient.getProperties();
224+
225+
// configure Storage logging and metrics
226+
BlobAnalyticsLogging logProps = new BlobAnalyticsLogging()
227+
.setRead(true)
228+
.setWrite(true)
229+
.setRetentionPolicy(new BlobRetentionPolicy()
230+
.setEnabled(true)
231+
.setDays(2))
232+
.setVersion("1.0");
233+
serviceProps.setLogging(logProps);
234+
235+
BlobMetrics metricProps = new BlobMetrics()
236+
.setEnabled(true)
237+
.setIncludeApis(true)
238+
.setRetentionPolicy(new BlobRetentionPolicy()
239+
.setEnabled(true)
240+
.setDays(2))
241+
.setVersion("1.0");
242+
serviceProps.setHourMetrics(metricProps);
243+
serviceProps.setMinuteMetrics(metricProps);
244+
245+
// Set the default service version to be used for anonymous requests.
246+
serviceProps.setDefaultServiceVersion("2015-04-05");
247+
248+
// Set the service properties.
249+
blobServiceClient.setProperties(serviceProps);
250+
251+
BlobClient blobClient = blobContainerClient.getBlobClient("install_apache.sh");
252+
BlockBlobClient blockBlobClient = blobClient.getBlockBlobClient();
253+
blockBlobClient.upload(scriptFileStream, fileSize);
254+
255+
// give sometime for the infrastructure to process the records and fit into time grain.
256+
ResourceManagerUtils.sleep(Duration.ofMinutes(6));
257+
}
258+
}
259+
}

0 commit comments

Comments
 (0)