Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
<PropertyGroup>
<NetSdkBuildTargetsDir Condition=" Exists('$(BuildAssetsDir)') ">$(BuildAssetsDir)\targets</NetSdkBuildTargetsDir>
<NetSdkBuildTargetsDir Condition=" Exists('$(SdkBuildToolsDir)') AND '$(NetSdkBuildTargetsDir)' == '' ">$(SdkBuildToolsDir)\targets</NetSdkBuildTargetsDir>
<NetSdkBuildTargetsDir Condition=" '$(NetSdkBuildTargetsDir)' == '' ">$(LibraryToolsFolder)\buildTargets</NetSdkBuildTargetsDir>
<!-- <NetSdkBuildTargetsDir Condition=" '$(NetSdkBuildTargetsDir)' == '' ">$(LibraryToolsFolder)\buildTargets</NetSdkBuildTargetsDir> -->

<NetSdkBuildToolsDir Condition="Exists('$(BuildAssetsDir)')">$(BuildAssetsDir)</NetSdkBuildToolsDir>
<NetSdkBuildToolsDir Condition="Exists('$(SdkBuildToolsDir)')">$(SdkBuildToolsDir)</NetSdkBuildToolsDir>
<NetSdkBuildToolsDir Condition=" '$(NetSdkBuildToolsDir)' == '' ">$(LibraryToolsFolder)\buildTargets</NetSdkBuildToolsDir>
<!-- <NetSdkBuildToolsDir Condition=" '$(NetSdkBuildToolsDir)' == '' ">$(LibraryToolsFolder)\buildTargets</NetSdkBuildToolsDir> -->
</PropertyGroup>

<Import Project="$(NetSdkBuildTargetsDir)\common.Build.props"/>
<Import Project="$(NetSdkBuildTargetsDir)\common.NugetPackage.props"/>
<Import Condition="Exists('$(NetSdkBuildTargetsDir)\common.Build.props')" Project="$(NetSdkBuildTargetsDir)\common.Build.props"/>
<Import Condition="Exists('$(NetSdkBuildTargetsDir)\common.NugetPackage.props')" Project="$(NetSdkBuildTargetsDir)\common.NugetPackage.props"/>
</Project>
4 changes: 2 additions & 2 deletions build.proj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project DefaultTargets="RunTests" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Init" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="Directory.Build.props" />
<Import Project="test.props" />
<Import Project="AzSdk.props" />
Expand All @@ -11,6 +11,6 @@
<Target Name="PublishNuget" DependsOnTargets="$(PublishNugetDependsOn)" />
<Target Name="CreateNugetPackage" DependsOnTargets="$(PackageNugetDependsOn)" />
<Target Name="RunTests" DependsOnTargets="$(RunTestProjectsDependsOn)" />
<Target Name="SignNuget" DependsOnTargets="$(SignNugetDependsOn)" />
<Target Name="SignNuget" DependsOnTargets="$(SignNugetDependsOn)" />
<Target Name="Help" DependsOnTargets="$(HelpDependsOn)" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.


using ApplicationInsights.Tests.Helpers;
using Microsoft.Azure.Management.ApplicationInsights.Management.Models;
using Microsoft.Rest.Azure;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Linq;
using System.Net;
using Xunit;

namespace ApplicationInsights.Tests.Scenarios
{
public class APIKeysTests : TestBase
{
private const string ResourceGroupName = "swaggertest";
private RecordedDelegatingHandler handler;


public APIKeysTests()
: base()
{
handler = new RecordedDelegatingHandler { SubsequentStatusCodeToReturn = HttpStatusCode.OK };
}

[Fact]
[Trait("Category", "Scenario")]
public void CreateGetListUpdateDeleteAPIKeys()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var insightsClient = this.GetAppInsightsManagementClient(context, handler);

//prepare a component
this.CreateAComponent(insightsClient, ResourceGroupName, nameof(CreateGetListUpdateDeleteAPIKeys));

//create an API key
var apiKeyProperties = GetCreateAPIKeyProperties(ResourceGroupName, nameof(CreateGetListUpdateDeleteAPIKeys));
var createAPIKeyResponse = insightsClient
.APIKeys
.CreateWithHttpMessagesAsync(
ResourceGroupName,
nameof(CreateGetListUpdateDeleteAPIKeys),
apiKeyProperties)
.GetAwaiter()
.GetResult();
AreEqual(apiKeyProperties, createAPIKeyResponse.Body);

//get all API keys
var getAllAPIKeys = insightsClient
.APIKeys
.ListWithHttpMessagesAsync(
ResourceGroupName,
nameof(CreateGetListUpdateDeleteAPIKeys))
.GetAwaiter()
.GetResult();

Assert.Equal(1, getAllAPIKeys.Body.Count());
AreEqual(apiKeyProperties, getAllAPIKeys.Body.ElementAt(0));

string fullkeyId = getAllAPIKeys.Body.ElementAt(0).Id;
string keyId = fullkeyId.Split('/')[10];

//get specif API key
var getAPIKey = insightsClient
.APIKeys
.GetWithHttpMessagesAsync(
ResourceGroupName,
nameof(CreateGetListUpdateDeleteAPIKeys),
keyId)
.GetAwaiter()
.GetResult();

AreEqual(apiKeyProperties, getAPIKey.Body);

//delete the API key
var deleteAPIKeyResponse = insightsClient
.APIKeys
.DeleteWithHttpMessagesAsync(
ResourceGroupName,
nameof(CreateGetListUpdateDeleteAPIKeys),
keyId)
.GetAwaiter()
.GetResult();

//get API again, should get an NOT found exception
Assert.Throws(typeof(CloudException), () =>
{
getAPIKey = insightsClient
.APIKeys
.GetWithHttpMessagesAsync(
ResourceGroupName,
nameof(CreateGetListUpdateDeleteAPIKeys),
keyId)
.GetAwaiter()
.GetResult();
});

//clean up component
this.DeleteAComponent(insightsClient, ResourceGroupName, nameof(CreateGetListUpdateDeleteAPIKeys));
}
}

private static void AreEqual(APIKeyRequest request, ApplicationInsightsComponentAPIKey response)
{
Assert.Equal(request.Name, response.Name, ignoreCase: true);
Assert.True(response.LinkedReadProperties.Count >= request.LinkedReadProperties.Count);
Assert.True(response.LinkedWriteProperties.Count >= request.LinkedWriteProperties.Count);
foreach (var readaccess in request.LinkedReadProperties)
{
Assert.True(response.LinkedReadProperties.Any(r => r == readaccess));
}

foreach (var writeaccess in request.LinkedWriteProperties)
{
Assert.True(response.LinkedWriteProperties.Any(w => w == writeaccess));
}
}

private APIKeyRequest GetCreateAPIKeyProperties(string resourceGroupName, string componentName)
{
return new APIKeyRequest()
{
Name = "test",
LinkedReadProperties = new string[] {
$"/subscriptions/{this.SubscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/components/{componentName}/api",
$"/subscriptions/{this.SubscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/components/{componentName}/agentconfig"
},
LinkedWriteProperties = new string[]
{
$"/subscriptions/{this.SubscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/components/{componentName}/annotations"
}
};
}

private static ApplicationInsightsComponent GetCreateComponentProperties()
{
return new ApplicationInsightsComponent(
name: nameof(CreateGetListUpdateDeleteAPIKeys),
location: "South Central US",
kind: "web",
applicationType: "web",
applicationId: nameof(CreateGetListUpdateDeleteAPIKeys),
flowType: "Bluefield",
requestSource: "rest"
);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ namespace ApplicationInsights.Tests.Scenarios
{
public class TestBase
{
protected bool IsRecording { get; set; }
protected string SubscriptionId { get; set; }

public TestBase()
{
this.IsRecording = false;
}


Expand All @@ -36,10 +35,9 @@ protected ApplicationInsightsManagementClient GetAppInsightsManagementClient(Moc

if (string.Equals(testMode, "record", StringComparison.OrdinalIgnoreCase))
{
this.IsRecording = true;
string subId = Environment.GetEnvironmentVariable("AZURE_TEST_SUBSCRIPTIONID");

subId = string.IsNullOrWhiteSpace(subId) ? "b90b0dec-9b9a-4778-a84e-4ffb73bb17f6" : subId;
this.SubscriptionId = subId;

TestEnvironment env = new TestEnvironment(connectionString: "SubscriptionId=" + subId);
client = context.GetServiceClient<ApplicationInsightsManagementClient>(
Expand All @@ -48,6 +46,7 @@ protected ApplicationInsightsManagementClient GetAppInsightsManagementClient(Moc
}
else
{
this.SubscriptionId = "b90b0dec-9b9a-4778-a84e-4ffb73bb17f6";
client = context.GetServiceClient<ApplicationInsightsManagementClient>(
handlers: handler ?? new RecordedDelegatingHandler { SubsequentStatusCodeToReturn = System.Net.HttpStatusCode.OK });
}
Expand Down
Loading