public async Task<JsonResult> GetAdminSettings()
        {
            try
            {
                var resourceProvider = await ClientFactory.AdminManagementClient.GetResourceProviderAsync
                                                            (StorageSampleClient.RegisteredServiceName, Guid.Empty.ToString());

                this.StorageSampleServiceEndPoint = EndpointModel.FromResourceProviderEndpoint(resourceProvider.AdminEndpoint);
                return this.JsonDataSet(this.StorageSampleServiceEndPoint);
            }
            catch (ManagementClientException managementException)
            {
                // 404 means the Storage Sample resource provider is not yet configured, return an empty record.
                if (managementException.StatusCode == HttpStatusCode.NotFound)
                {
                    return this.JsonDataSet(new EndpointModel());
                }

                //Just throw if there is any other type of exception is encountered
                throw;
            }
        }
        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);
        }
        private void ValidateInput(EndpointModel newSettings)
        {
            if (newSettings == null)
            {
                throw new ArgumentNullException("newSettings");
            }

            if (String.IsNullOrEmpty(newSettings.EndpointAddress))
            {
                throw new ArgumentNullException("EndpointAddress");
            }

            if (String.IsNullOrEmpty(newSettings.Username))
            {
                throw new ArgumentNullException("Username");
            }

            // Note: We do not run validation on password, as password is not null, only when a change is required.
        }