Esempio n. 1
0
        /// <summary>
        /// Processes creating web test resources.
        /// </summary>
        /// <param name="name">Web test name.</param>
        /// <param name="url">Web test URL.</param>
        /// <param name="authType"><see cref="AuthType"/> value.</param>
        /// <param name="accessToken">Access token value.</param>
        /// <param name="testType"><see cref="TestType"/> value. Default is <c>TestType.UriPingTest</c>.</param>
        /// <returns>Returns <c>True</c>; if processed successfully; otherwise returns <c>False</c>.</returns>
        public async Task <bool> ProcessAsync(string name, string url, AuthType authType = AuthType.None, string accessToken = null, TestType testType = TestType.UrlPingTest)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException(nameof(url));
            }

            var credentials = await this.GetCredentialsAsync().ConfigureAwait(false);

            this._resourceManagementClient = new ResourceManagementClient(credentials);
            this._insightsManagementClient = new InsightsManagementClient(credentials);

            var insightsResource = await this.GetInsightsResourceAsync(this._resourceManagementClient).ConfigureAwait(false);

            var webTest = this._webTests.SingleOrDefault(p => p.TestType == testType);

            if (webTest == null)
            {
                return(false);
            }

            var webTestResource = await this.CreateOrUpdateWebTestAsync(name, url, authType, accessToken, webTest, this._resourceManagementClient, insightsResource).ConfigureAwait(false);

            var result = await this.CreateOrUpdateAlertsAsync(name, webTest, this._insightsManagementClient, webTestResource, insightsResource).ConfigureAwait(false);

            return(result);
        }
        /// <summary>
        /// Dispose the resources
        /// </summary>
        /// <param name="disposing">Indicates whether the managed resources should be disposed or not</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (this.insightsManagementClient != null)
                {
                    this.insightsManagementClient.Dispose();
                    this.insightsManagementClient = null;
                }

                this.disposed = true;
            }
        }
        /// <summary>
        /// Dispose the resources
        /// </summary>
        /// <param name="disposing">Indicates whether the managed resources should be disposed or not</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (this.insightsManagementClient != null)
                {
                    this.insightsManagementClient.Dispose();
                    this.insightsManagementClient = null;
                }

                this.disposed = true;
            }
        }
        /// <summary>
        /// Dispose the resources
        /// </summary>
        /// <param name="disposing">Indicates whether the managed resources should be disposed or not</param>
        protected override void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (this.insightsManagementClient != null)
                {
                    this.insightsManagementClient.Dispose();
                    this.insightsManagementClient = null;
                }

                this.disposed = true;
            }
            base.Dispose(disposing);
        }
        /// <summary>
        /// Dispose the resources
        /// </summary>
        /// <param name="disposing">Indicates whether the managed resources should be disposed or not</param>
        protected override void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (this.insightsManagementClient != null)
                {
                    this.insightsManagementClient.Dispose();
                    this.insightsManagementClient = null;
                }

                this.disposed = true;
            }
            base.Dispose(disposing);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates or updates alert resource.
        /// </summary>
        /// <param name="name">Name of the web test.</param>
        /// <param name="webTest"><see cref="WebTestElement"/> instance from configuration.</param>
        /// <param name="client"><see cref="IInsightsManagementClient"/> instance.</param>
        /// <param name="webTestResource"><see cref="ResourceBaseExtended"/> instance as a Web Test resource.</param>
        /// <param name="insightsResource"><see cref="ResourceBaseExtended"/> instance as an Application Insights resource.</param>
        /// <returns>Returns <c>True</c>, if the web test resource creted/updated successfully; otherwise returns <c>False</c>.</returns>
        public async Task <bool> CreateOrUpdateAlertsAsync(string name, WebTestElement webTest, IInsightsManagementClient client, ResourceBaseExtended webTestResource, ResourceBaseExtended insightsResource)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (webTest == null)
            {
                throw new ArgumentNullException(nameof(webTest));
            }

            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (webTestResource == null)
            {
                throw new ArgumentNullException(nameof(webTestResource));
            }

            if (insightsResource == null)
            {
                throw new ArgumentNullException(nameof(insightsResource));
            }

#if !TEST
            Console.WriteLine($"Processing alert for {name} started ...");
#endif
            var parameters = (new RuleCreateOrUpdateParameters()
            {
                Location = InsightsAlertResourceLocation
            })
                             .AddTags(webTestResource, insightsResource)
                             .AddProperties(name, webTest, webTestResource, insightsResource);

            var result = await client.AlertOperations.CreateOrUpdateRuleAsync(this._appInsights.ResourceGroup, parameters).ConfigureAwait(false);

            if (!IsAcceptableHttpStatusCode(result.StatusCode, new[] { HttpStatusCode.OK, HttpStatusCode.Created }))
            {
                throw new HttpResponseException(result.StatusCode);
            }

#if !TEST
            Console.WriteLine($"Processing alert for {name} completed");
#endif
            return(true);
        }