Skip to content

Commit

Permalink
Release azure-resourcemanager 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-Fluent committed Dec 2, 2020
1 parent 8d293b4 commit d4ba107
Show file tree
Hide file tree
Showing 4 changed files with 1,039 additions and 737 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ languages:
products:
- azure
extensions:
services: Eventhub
services: Eventhubs
platforms: java
---

# Getting Started with Eventhub - Manage Event Hub Events - in Java #
# Getting Started with Eventhubs - Manage Event Hub Events - in Java #


Stream Azure Service Logs and Metrics for consumption through Event Hub.
Expand All @@ -23,7 +23,7 @@ extensions:

To run this sample:

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).
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).

git clone https://github.com/Azure-Samples/eventhub-java-manage-event-hub-events.git

Expand All @@ -33,9 +33,11 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut

## More information ##

[http://azure.com/java](http://azure.com/java)
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).

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

If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).

---

Expand Down
34 changes: 15 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.microsoft.azure.management.eventhub.samples.ManageEventHubEvents</mainClass>
<mainClass>com.azure.resourcemanager.eventhubs.samples.ManageEventHubEvents</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -40,7 +41,7 @@
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.microsoft.azure.management.eventhub.samples.ManageEventHubEvents.java</mainClass>
<mainClass>com.azure.resourcemanager.eventhubs.samples.ManageEventHubEvents.java</mainClass>
</manifest>
</archive>
</configuration>
Expand All @@ -51,24 +52,19 @@
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.36.3</version>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.management.eventhub.samples;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.cosmosdb.CosmosDBAccount;
import com.microsoft.azure.management.cosmosdb.DatabaseAccountKind;
import com.microsoft.azure.management.eventhub.EventHubNamespace;
import com.microsoft.azure.management.eventhub.EventHubNamespaceAuthorizationRule;
import com.microsoft.azure.management.monitor.DiagnosticSetting;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.samples.Utils;
import com.microsoft.rest.LogLevel;
import org.joda.time.Period;

import java.io.File;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.resourcemanager.eventhubs.samples;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.cosmos.models.CosmosDBAccount;
import com.azure.resourcemanager.cosmos.models.DatabaseAccountKind;
import com.azure.resourcemanager.eventhubs.models.EventHubNamespace;
import com.azure.resourcemanager.eventhubs.models.EventHubNamespaceAuthorizationRule;
import com.azure.resourcemanager.monitor.models.DiagnosticSetting;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.samples.Utils;

import java.time.Duration;

/**
* Stream Azure Service Logs and Metrics for consumption through Event Hub.
Expand All @@ -29,31 +29,29 @@ public class ManageEventHubEvents {
/**
* Main function which runs the actual sample.
*
* @param azure instance of the azure client
* @param azureResourceManager instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final String storageAccountConnectionStringFormat = "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net";
public static boolean runSample(AzureResourceManager azureResourceManager) {
final Region region = Region.US_EAST;
final String rgName = Utils.createRandomName("rgEvHb");
final String namespaceName = Utils.createRandomName("ns");
final String storageAccountName = Utils.createRandomName("stg");
final String rgName = Utils.randomResourceName(azureResourceManager, "rgEvHb", 24);
final String namespaceName = Utils.randomResourceName(azureResourceManager, "ns", 24);
final String eventHubName = "FirstEventHub";
String diagnosticSettingId = null;

try {
//=============================================================
// Creates a Cosmos DB.
//
CosmosDBAccount docDb = azure.cosmosDBAccounts()
.define(namespaceName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withKind(DatabaseAccountKind.MONGO_DB)
.withEventualConsistency()
.withWriteReplication(Region.US_WEST)
.withReadReplication(Region.US_CENTRAL)
.create();
CosmosDBAccount docDb = azureResourceManager.cosmosDBAccounts()
.define(namespaceName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withKind(DatabaseAccountKind.MONGO_DB)
.withEventualConsistency()
.withWriteReplication(Region.US_WEST)
.withReadReplication(Region.US_CENTRAL)
.create();

System.out.println("Created a DocumentDb instance.");
Utils.print(docDb);
Expand All @@ -63,12 +61,12 @@ public static boolean runSample(Azure azure) {

System.out.println("Creating event hub namespace and event hub");

EventHubNamespace namespace = azure.eventHubNamespaces()
.define(namespaceName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withNewEventHub(eventHubName)
.create();
EventHubNamespace namespace = azureResourceManager.eventHubNamespaces()
.define(namespaceName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withNewEventHub(eventHubName)
.create();

System.out.println(String.format("Created event hub namespace %s and event hub %s ", namespace.name(), eventHubName));
System.out.println();
Expand All @@ -80,8 +78,8 @@ public static boolean runSample(Azure azure) {

System.out.println("Retrieving the namespace authorization rule");

EventHubNamespaceAuthorizationRule eventHubAuthRule = azure.eventHubNamespaces().authorizationRules()
.getByName(namespace.resourceGroupName(), namespace.name(), "RootManageSharedAccessKey");
EventHubNamespaceAuthorizationRule eventHubAuthRule = azureResourceManager.eventHubNamespaces().authorizationRules()
.getByName(namespace.resourceGroupName(), namespace.name(), "RootManageSharedAccessKey");

System.out.println("Namespace authorization rule Retrieved");

Expand All @@ -92,14 +90,14 @@ public static boolean runSample(Azure azure) {
System.out.println("Enabling diagnostics events of a cosmosdb to stream to event hub");

// Store Id of created Diagnostic settings only for clean-up
DiagnosticSetting ds = azure.diagnosticSettings()
.define("DiaEventHub")
.withResource(docDb.id())
.withEventHub(eventHubAuthRule.id(), eventHubName)
.withLog("DataPlaneRequests", 0)
.withLog("MongoRequests", 0)
.withMetric("AllMetrics", Period.minutes(5), 0)
.create();
DiagnosticSetting ds = azureResourceManager.diagnosticSettings()
.define("DiaEventHub")
.withResource(docDb.id())
.withEventHub(eventHubAuthRule.id(), eventHubName)
.withLog("DataPlaneRequests", 0)
.withLog("MongoRequests", 0)
.withMetric("AllMetrics", Duration.ofMinutes(5), 0)
.create();

Utils.print(ds);
diagnosticSettingId = ds.id();
Expand All @@ -110,49 +108,47 @@ public static boolean runSample(Azure azure) {
// Listen for events from event hub using Event Hub dataplane APIs.

return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
if (diagnosticSettingId != null) {
System.out.println("Deleting Diagnostic Setting: " + diagnosticSettingId);
azure.diagnosticSettings().deleteById(diagnosticSettingId);
azureResourceManager.diagnosticSettings().deleteById(diagnosticSettingId);
}
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
azureResourceManager.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}

return false;
}

/**
* Main entry point.
*
* @param args the parameters
*/
public static void main(String[] args) {
try {
//=============================================================
// Authenticate

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
final TokenCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.build();

Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.withLogLevel(HttpLogDetailLevel.BASIC)
.authenticate(credential, profile)
.withDefaultSubscription();

// Print selected subscription
System.out.println("Selected subscription: " + azure.subscriptionId());
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());

runSample(azure);
runSample(azureResourceManager);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
Expand Down
Loading

0 comments on commit d4ba107

Please sign in to comment.