public async Task <JsonResult> UpdateAdminSettings(EndpointModel newSettings)
        {
            this.ValidateInput(newSettings);

            ResourceProvider storageSampleResourceProvider;
            string           errorMessage = string.Empty;

            try
            {
                //Check if resource provider is already registered or not
                storageSampleResourceProvider = await ClientFactory.AdminManagementClient.GetResourceProviderAsync(StorageSampleClient.RegisteredServiceName, Guid.Empty.ToString());
            }
            catch (ManagementClientException exception)
            {
                // 404 means the Storage Sample resource provider is not yet configured, return an empty record.
                if (exception.StatusCode == HttpStatusCode.NotFound)
                {
                    storageSampleResourceProvider = null;
                }
                else
                {
                    //Just throw if there is any other type of exception is encountered
                    throw;
                }
            }

            if (storageSampleResourceProvider != null)
            {
                //Resource provider already registered so lets update endpoint
                storageSampleResourceProvider.AdminEndpoint        = newSettings.ToAdminEndpoint();
                storageSampleResourceProvider.TenantEndpoint       = newSettings.ToTenantEndpoint();
                storageSampleResourceProvider.NotificationEndpoint = newSettings.ToNotificationEndpoint();
                storageSampleResourceProvider.UsageEndpoint        = newSettings.ToUsageEndpoint();
            }
            else
            {
                //Resource provider not registered yet so lets register new one now
                storageSampleResourceProvider = new ResourceProvider()
                {
                    Name                    = StorageSampleClient.RegisteredServiceName,
                    DisplayName             = "Storage Sample",
                    InstanceDisplayName     = "Storage Sample",
                    Enabled                 = true,
                    PassThroughEnabled      = true,
                    AllowAnonymousAccess    = false,
                    AdminEndpoint           = newSettings.ToAdminEndpoint(),
                    TenantEndpoint          = newSettings.ToTenantEndpoint(),
                    NotificationEndpoint    = newSettings.ToNotificationEndpoint(),
                    UsageEndpoint           = newSettings.ToUsageEndpoint(),
                    MaxQuotaUpdateBatchSize = 3 // Check link http://technet.microsoft.com/en-us/library/dn520926(v=sc.20).aspx
                };
            }

            var testList = new ResourceProviderVerificationTestList()
            {
                new ResourceProviderVerificationTest()
                {
                    TestUri = new Uri(StorageSampleAdminController.adminAPIUri + StorageSampleClient.AdminSettings),
                    IsAdmin = true
                }
            };

            try
            {
                // Resource Provider Verification to ensure given endpoint and username/password is correct
                // Only validate the admin RP since we don't have a tenant subscription to do it.
                var result = await ClientFactory.AdminManagementClient.VerifyResourceProviderAsync(storageSampleResourceProvider, testList);

                if (result.HasFailures)
                {
                    throw new HttpException("Invalid endpoint or bad username/password");
                }
            }
            catch (ManagementClientException ex)
            {
                throw new HttpException("Invalid endpoint or bad username/password " + ex.Message.ToString());
            }

            //Finally Create Or Update resource provider
            Task <ResourceProvider> rpTask = (string.IsNullOrEmpty(storageSampleResourceProvider.Name) || String.IsNullOrEmpty(storageSampleResourceProvider.InstanceId))
                                                ? ClientFactory.AdminManagementClient.CreateResourceProviderAsync(storageSampleResourceProvider)
                                                : ClientFactory.AdminManagementClient.UpdateResourceProviderAsync(storageSampleResourceProvider.Name, storageSampleResourceProvider.InstanceId, storageSampleResourceProvider);

            try
            {
                await rpTask;
            }
            catch (ManagementClientException e)
            {
                throw e;
            }

            return(this.Json(newSettings));
        }
        public async Task <JsonResult> UpdateAdminSettings(EndpointModel newSettings)
        {
            this.ValidateInput(newSettings);

            ResourceProvider cmpWapExtensionResourceProvider;
            var errorMessage = string.Empty;

            try
            {
                //***********************************************************

                const string rsn = CmpWapExtensionClient.RegisteredServiceName;
                var          es  = Guid.Empty.ToString();

                var amc = ClientFactory.AdminManagementClient;

                cmpWapExtensionResourceProvider =
                    await amc.GetResourceProviderAsync(rsn, es);

                //***********************************************************

                //Check if resource provider is already registered or not

                /*CmpWapExtensionResourceProvider =
                 *  await
                 *      ClientFactory.AdminManagementClient.GetResourceProviderAsync(
                 *          CmpWapExtensionClient.RegisteredServiceName, Guid.Empty.ToString());*/
            }
            catch (ManagementClientException exception)
            {
                // 404 means the CmpWapExtension resource provider is not yet configured, return an empty record.
                if (exception.StatusCode == HttpStatusCode.NotFound)
                {
                    cmpWapExtensionResourceProvider = null;
                }
                else
                {
                    //Just throw if there is any other type of exception is encountered
                    throw;
                }
            }
            catch (Exception ex)
            {
                var XX = ex.Message;
                throw;
            }

            if (cmpWapExtensionResourceProvider != null)
            {
                //Resource provider already registered so lets update endpoint
                cmpWapExtensionResourceProvider.AdminEndpoint        = newSettings.ToAdminEndpoint();
                cmpWapExtensionResourceProvider.TenantEndpoint       = newSettings.ToTenantEndpoint();
                cmpWapExtensionResourceProvider.NotificationEndpoint = newSettings.ToNotificationEndpoint();
                cmpWapExtensionResourceProvider.UsageEndpoint        = newSettings.ToUsageEndpoint();
            }
            else
            {
                //Resource provider not registered yet so lets register new one now
                cmpWapExtensionResourceProvider = new ResourceProvider()
                {
                    Name                 = CmpWapExtensionClient.RegisteredServiceName,
                    DisplayName          = "Cmp Wap Extension",
                    InstanceDisplayName  = CmpWapExtensionClient.RegisteredServiceName + " Instance",
                    Enabled              = true,
                    PassThroughEnabled   = true,
                    AllowAnonymousAccess = false,
                    AdminEndpoint        = newSettings.ToAdminEndpoint(),
                    TenantEndpoint       = newSettings.ToTenantEndpoint(),
                    NotificationEndpoint = newSettings.ToNotificationEndpoint(),
                    UsageEndpoint        = newSettings.ToUsageEndpoint()
                };
            }

            var testList = new ResourceProviderVerificationTestList()
            {
                new ResourceProviderVerificationTest()
                {
                    TestUri = new Uri(CmpWapExtensionAdminController.AdminApiUri +
                                      CmpWapExtensionClient.AdminSettings),
                    IsAdmin = true
                }
            };

            try
            {
                // Resource Provider Verification to ensure given endpoint and username/password is correct
                // Only validate the admin RP since we don't have a tenant subscription to do it.
                var result = await ClientFactory.AdminManagementClient.VerifyResourceProviderAsync(
                    cmpWapExtensionResourceProvider, testList);

                if (result.HasFailures)
                {
                    throw new HttpException("Invalid endpoint or bad username/password");
                }
            }
            catch (ManagementClientException ex)
            {
                throw new HttpException("Invalid endpoint or bad username/password " + ex.Message.ToString());
            }

            //Finally Create Or Update resource provider
            var rpTask = (string.IsNullOrEmpty(cmpWapExtensionResourceProvider.Name) ||
                          String.IsNullOrEmpty(cmpWapExtensionResourceProvider.InstanceId))
                    ? ClientFactory.AdminManagementClient.CreateResourceProviderAsync(cmpWapExtensionResourceProvider)
                        : ClientFactory.AdminManagementClient.UpdateResourceProviderAsync(cmpWapExtensionResourceProvider.Name,
                                                                                          cmpWapExtensionResourceProvider.InstanceId, cmpWapExtensionResourceProvider);

            try
            {
                await rpTask;
            }
            catch (ManagementClientException e)
            {
                throw;
            }

            var adminSettings = new AdminSettings()
            {
                EndpointAddress = newSettings.EndpointAddress,
                Username        = newSettings.Username,
                Password        = newSettings.Password
            };

            await ClientFactory.CmpWapExtensionClient.UpdateAdminSettingsAsync(adminSettings);

            return(this.Json(newSettings));
        }