Skip to content

Commit

Permalink
Add CosmosDB example
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Jan 14, 2020
1 parent 2eed9ac commit bc652f1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The `DefaultAzureMgmtCredential` class allows you to use all the goodness of `Az

Example usage:

Add Nuget dependencies:

### Application Insights

```
dotnet add package JonGallant.Azure.Identity.Extensions
Expand All @@ -28,4 +29,21 @@ using Microsoft.Azure.Management.ApplicationInsights.Management;
var appInsightsClient = new ApplicationInsightsManagementClient(new DefaultAzureMgmtCredential());
```


### CosmosDB

```
dotnet add package JonGallant.Azure.Identity.Extensions
dotnet add package Microsoft.Azure.Management.CosmosDB --version 1.0.1
```

Use DefaultAzureMgmtCredential in place of ServiceClientCredential:
```csharp
using JonGallant.Azure.Identity.Extensions;
using Microsoft.Azure.Management.CosmosDB;
using Microsoft.Azure.Management.CosmosDB.Models;

var client = new CosmosDBManagementClient(new DefaultAzureMgmtCredential());
```

More to come soon. Please file a GitHub issue with any questions/suggestions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public void CreateAppInsightsTest()
{
Env.Load("../../../.env");

var appInsightsClient = new ApplicationInsightsManagementClient(new DefaultAzureMgmtCredential());
appInsightsClient.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var client = new ApplicationInsightsManagementClient(new DefaultAzureMgmtCredential());
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");

var appInsightsComponent = new ApplicationInsightsComponent("westus", "web", "web");
var appInsightsName = Environment.GetEnvironmentVariable("APPINSIGHTS_NAME") + Guid.NewGuid().ToString("n").Substring(0, 8);

appInsightsComponent = appInsightsClient.Components.CreateOrUpdate(Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP"), appInsightsName, appInsightsComponent);
var component = new ApplicationInsightsComponent(Environment.GetEnvironmentVariable("AZURE_REGION"), "web", "web");
var name = Environment.GetEnvironmentVariable("APPINSIGHTS_NAME") + Guid.NewGuid().ToString("n").Substring(0, 8);

Assert.NotNull(appInsightsComponent.CreationDate);
component = client.Components.CreateOrUpdate(Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP"), name, component);

Assert.NotNull(component.CreationDate);
}
}
}
34 changes: 34 additions & 0 deletions net/JonGallant.Azure.Identity.Extensions.Tests/CosmosDBTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using DotNetEnv;
using Microsoft.Azure.Management.CosmosDB;
using Microsoft.Azure.Management.CosmosDB.Models;
using System;
using System.Collections.Generic;
using Xunit;

namespace JonGallant.Azure.Identity.Extensions.Tests
{
public class CosmosDBTests
{
[Fact]
public void CreateCosmosDBTest()
{
Env.Load("../../../.env");


var client = new CosmosDBManagementClient(new DefaultAzureMgmtCredential());
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");

var name = Environment.GetEnvironmentVariable("COSMOSDB_NAME") + Guid.NewGuid().ToString("n").Substring(0, 8);

var parameters = new DatabaseAccountCreateUpdateParameters
{
Location = Environment.GetEnvironmentVariable("AZURE_REGION"),
Locations = new List<Location> { new Location(locationName: Environment.GetEnvironmentVariable("AZURE_REGION")) }
};

var results = client.DatabaseAccounts.CreateOrUpdate(Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP"), name, parameters);

Assert.Equal(results.Name, name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="DotNetEnv" Version="1.4.0" />
<PackageReference Include="Microsoft.Azure.Management.ApplicationInsights" Version="0.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.CosmosDB" Version="1.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
Expand Down

0 comments on commit bc652f1

Please sign in to comment.