public async Task <ApplicationInsightsAccount> CreateApplicationInsightsAsync( Guid subscriptionId, string location, string resourceGroupName, string appInsightsName, string environmentName) { await RegisterProvider(subscriptionId, "Microsoft.Insights"); var accessToken = await GetAccessToken(); var token = new TokenCredentials(accessToken); var appInsightsClient = new ApplicationInsightsManagementClient(token) { SubscriptionId = subscriptionId.ToString() }; var props = new ApplicationInsightsComponent(location, "other", "other", tags: GetEnvironmentTags(environmentName)); var appInsightsComponent = await appInsightsClient.Components.CreateOrUpdateAsync(resourceGroupName, appInsightsName, props); return(new ApplicationInsightsAccount { ResourceId = appInsightsComponent.Id, Location = location, ApplicationId = appInsightsComponent.AppId, InstrumentationKey = appInsightsComponent.InstrumentationKey, ExistingResource = false, }); }
private void SetupManagementClients(RestTestFramework.MockContext context) { if (HttpMockServer.Mode == HttpRecorderMode.Record) { // This allows the use of a particular subscription if the user is associated to several // "TEST_CSM_ORGID_AUTHENTICATION=SubscriptionId=<subscription-id>" string subId = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"); RestTestFramework.TestEnvironment environment = new RestTestFramework.TestEnvironment(connectionString: subId); this.MonitorManagementClient = this.GetInsightsManagementClient(context: context, env: environment); ResourceManagementClient = this.GetResourceManagementClient(context: context, env: environment); StorageManagementClient = this.GetStorageManagementClient(context: context, env: environment); ApplicationInsightsClient = this.GetApplicationInsightsManagementClient(context: context, env: environment); } else if (HttpMockServer.Mode == HttpRecorderMode.Playback) { this.MonitorManagementClient = this.GetInsightsManagementClient(context: context, env: null); ResourceManagementClient = this.GetResourceManagementClient(context: context, env: null); StorageManagementClient = this.GetStorageManagementClient(context: context, env: null); ApplicationInsightsClient = GetApplicationInsightsManagementClient(context, env: null); } _helper.SetupManagementClients( ResourceManagementClient, this.MonitorManagementClient, StorageManagementClient, this.ApplicationInsightsClient); }
public async Task <List <ApplicationInsightsComponent> > GetApplicationInsights(string subscriptionId) { var accessToken = await GetAccessToken(); var token = new TokenCredentials(accessToken); var appInsightsClient = new ApplicationInsightsManagementClient(token) { SubscriptionId = subscriptionId }; var components = await appInsightsClient.Components.ListWithHttpMessagesAsync(); return(components.Body.OrderBy(ins => ins.Name).ToList()); }
public async void CreateAppInsightsTest() { Env.Load("../../../../../.env"); var client = new ApplicationInsightsManagementClient(new DefaultAzureMgmtCredential()); client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); var component = new ApplicationInsightsComponent(Environment.GetEnvironmentVariable("AZURE_REGION"), "web", "web"); var name = Environment.GetEnvironmentVariable("APPINSIGHTS_NAME") + Guid.NewGuid().ToString("n").Substring(0, 8); component = await client.Components.CreateOrUpdateAsync(Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP"), name, component); Assert.NotNull(component.CreationDate); }
public async Task <string> CreateApplicationInsightsApiKey( Guid subscriptionId, ApplicationInsightsAccount appInsights, string apiKeyName) { var accessToken = await GetAccessToken(); var token = new TokenCredentials(accessToken); var appInsightsClient = new ApplicationInsightsManagementClient(token) { SubscriptionId = subscriptionId.ToString() }; var scope = $"/subscriptions/{subscriptionId}/resourcegroups/{appInsights.ResourceGroupName}/providers/Microsoft.insights/components/{appInsights.Name}/api"; var apiKeyRequestProps = new APIKeyRequest($"RenderMgr-{apiKeyName}", new List <string>(new[] { scope })); var key = await appInsightsClient.APIKeys.CreateAsync( appInsights.ResourceGroupName, appInsights.Name, apiKeyRequestProps); return(key.ApiKey); }
public async void CreateAndDeleteAppInsightsTest() { Env.Load("../../../../../.env"); var baseName = Environment.GetEnvironmentVariable("AZURE_BASE_NAME"); var rgName = string.Format("{0}rg", baseName); // App Insights var client = new ApplicationInsightsManagementClient(new AzureIdentityCredentialAdapter()); client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); var component = new ApplicationInsightsComponent("westus", "web", "web"); var aiName = "appinsightsname" + Guid.NewGuid().ToString("n").Substring(0, 8); component = await client.Components.CreateOrUpdateAsync(rgName, aiName, component); Assert.NotNull(component.CreationDate); await client.Components.DeleteAsync(rgName, aiName); }
public async Task DeleteApplicationInsightsAsync(Guid subscriptionId, string resourceGroupName, string appInsightsName) { var accessToken = await GetAccessToken(); var token = new TokenCredentials(accessToken); var appInsightsClient = new ApplicationInsightsManagementClient(token) { SubscriptionId = subscriptionId.ToString() }; try { await appInsightsClient.Components.DeleteAsync(resourceGroupName, appInsightsName); } catch (CloudException cEx) { if (cEx.Response?.StatusCode != HttpStatusCode.NotFound) { throw; } } }
protected void DeleteAComponent(ApplicationInsightsManagementClient insightsClient, string resourceGroupName, string componentName) { var deleteComponentResponse = insightsClient .Components .DeleteWithHttpMessagesAsync( resourceGroupName, componentName) .GetAwaiter() .GetResult(); //get component again, should get an exception Assert.Throws <CloudException>(() => { var getComponentResponse = insightsClient .Components .GetWithHttpMessagesAsync( resourceGroupName, componentName) .GetAwaiter() .GetResult(); }); }
public async Task <List <ApplicationInsightsComponent> > GetApplicationInsights(string subscriptionId) { var cacheKey = CacheKeys.MakeKey(CacheKeys.AppInsightsList, subscriptionId); var cached = HttpContext.Session.Get <List <ApplicationInsightsComponent> >(cacheKey); if (cached != null) { return(cached); } var accessToken = await GetAccessToken(); var token = new TokenCredentials(accessToken); var appInsightsClient = new ApplicationInsightsManagementClient(token) { SubscriptionId = subscriptionId }; var components = await appInsightsClient.Components.ListWithHttpMessagesAsync(); var insights = components.Body.OrderBy(ins => ins.Name).ToList(); return(HttpContext.Session.Set(cacheKey, insights)); }
public ApplicationInsightsMgmtClient( string subscriptionId, RestClient restClient ) { // We need to initialize new RestClient so that we // extract RootHttpHandler and DelegatingHandlers out of it. var applicationInsightsRestClient = RestClient .Configure() .WithEnvironment(restClient.Environment) .WithCredentials(restClient.Credentials) //.WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders) .Build(); _applicationInsightsManagementClient = new ApplicationInsightsManagementClient( restClient.Credentials, applicationInsightsRestClient.RootHttpHandler, applicationInsightsRestClient.Handlers.ToArray() ) { SubscriptionId = subscriptionId }; }
static void Main(string[] args) { var clientId = ""; var clientSecret = ""; var tenantId = ""; var subscriptionId = ""; var groupName = ""; var location = Region.USSouthCentral; var appName = "test-app001"; var credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal(clientId , clientSecret , tenantId , AzureEnvironment.AzureGlobalCloud ); var templatePath = @"https://rdstorage10789078.blob.core.windows.net/testdata/template.json"; //var paramPath = @"https://rdstorage10789078.blob.core.windows.net/testdata/param.json"; //string p = System.IO.File.ReadAllText(@"C:\Users\vedarisi\source\repos\ArmDeployer\ArmDeployer\param.json"); // var azure = Microsoft.Azure.Management.Fluent.Azure var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithSubscription(subscriptionId); // .WithDefaultSubscription(subscriptionId); Console.WriteLine("Creating AI Instance"); Console.WriteLine("Deploying the template..."); var pJObject = JObject.FromObject(new Parameters(appName, "web", "East US")); try { var deployment = azure.Deployments.Define("mytestDeployment") .WithExistingResourceGroup(groupName) .WithTemplateLink(templatePath, "1.0.0.0") .WithParameters(pJObject) //.WithParametersLink(paramPath, "1.0.0.0") .WithMode(Microsoft.Azure.Management.ResourceManager.Fluent.Models.DeploymentMode.Incremental) .Create(); using (var appInsightsManagementClient = new ApplicationInsightsManagementClient(credentials)) { appInsightsManagementClient.SubscriptionId = subscriptionId; var component = appInsightsManagementClient.Components.Get(groupName, appName); Console.WriteLine(component.InstrumentationKey); } } catch (Exception exp) { Console.WriteLine(exp.Message); } Console.WriteLine("Complete Deployment..."); Console.WriteLine("Press enter to Come out of the program..."); Console.ReadLine(); }
protected AzureOperationResponse <ApplicationInsightsComponent> CreateAComponent(ApplicationInsightsManagementClient insightsClient, string resourceGroupName, string componentName) { ApplicationInsightsComponent bodyParameter = GetCreateComponentProperties(componentName); //"create new component"; var createdComponentResponse = insightsClient .Components .CreateOrUpdateWithHttpMessagesAsync( resourceGroupName, componentName, insightProperties: bodyParameter) .GetAwaiter() .GetResult(); //verify Assert.Equal(bodyParameter.Name, createdComponentResponse.Body.Name); Assert.Equal(bodyParameter.RequestSource, createdComponentResponse.Body.RequestSource); Assert.Equal(bodyParameter.FlowType, createdComponentResponse.Body.FlowType); Assert.Equal(bodyParameter.Kind, createdComponentResponse.Body.Kind); return(createdComponentResponse); }