/// <summary>
        /// Creates a new location
        /// </summary>
        protected override void ExecuteCore()
        {
            if (this.MyInvocation.InvocationName.Equals("New-AzureRmManagedLocation", StringComparison.OrdinalIgnoreCase))
            {
                this.WriteWarning("Alias New-AzureRmManagedLocation will be deprecated in a future release. Please use the cmdlet name New-AzsLocation instead");
            }

            if (ShouldProcess(this.Name, VerbsCommon.New))
            {
                using (var client = this.GetAzureStackClient())
                {
                    this.WriteVerbose(Resources.CreatingNewLocation.FormatArgs(this.Name));
                    var parameters = new ManagedLocationCreateOrUpdateParameters()
                    {
                        Location = new Location()
                        {
                            DisplayName = this.DisplayName,
                            Latitude    = this.Latitude.ToString(CultureInfo.InvariantCulture),
                            Longitude   = this.Longitude.ToString(CultureInfo.InvariantCulture),
                            Name        = this.Name
                        }
                    };

                    if (client.ManagedLocations.List()
                        .Locations.Any(location => location.Name.EqualsInsensitively(parameters.Location.Name)))
                    {
                        throw new PSInvalidOperationException(
                                  Resources.LocationAlreadyExists.FormatArgs(parameters.Location.Name));
                    }

                    var result = client.ManagedLocations.CreateOrUpdate(parameters).Location;
                    WriteObject(result);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a new location
        /// </summary>
        protected override object ExecuteCore()
        {
            using (var client = this.GetAzureStackClient(this.SubscriptionId))
            {
                this.WriteVerbose(Resources.CreatingNewManagedLocation.FormatArgs(this.Name));
                var parameters = new ManagedLocationCreateOrUpdateParameters()
                {
                    Location = new Location()
                    {
                        DisplayName = this.DisplayName,
                        Latitude    = this.Latitude.ToString(CultureInfo.InvariantCulture),
                        Longitude   = this.Longitude.ToString(CultureInfo.InvariantCulture),
                        Name        = this.Name
                    }
                };

                if (client.ManagedLocations.List()
                    .Locations.Any(location => location.Name.EqualsInsensitively(parameters.Location.Name)))
                {
                    throw new PSInvalidOperationException(Resources.ManagedLocationAlreadyExists.FormatArgs(parameters.Location.Name));
                }

                return(client.ManagedLocations.CreateOrUpdate(parameters).Location);
            }
        }
 /// <summary>
 /// Updates the managed location with new values
 /// </summary>
 protected override object ExecuteCore()
 {
     using (var client = this.GetAzureStackClient(this.SubscriptionId))
     {
         this.WriteVerbose(Resources.UpdatingManagedLocation.FormatArgs(this.Location.Name));
         var parameters = new ManagedLocationCreateOrUpdateParameters(this.Location);
         return(client.ManagedLocations.CreateOrUpdate(parameters).Location);
     }
 }
        /// <summary>
        /// Updates the managed location with new values
        /// </summary>
        protected override void ExecuteCore()
        {
            if (this.MyInvocation.InvocationName.Equals("Set-AzureRmManagedLocation", StringComparison.OrdinalIgnoreCase))
            {
                this.WriteWarning("Alias Set-AzureRmManagedLocation will be deprecated in a future release. Please use the cmdlet name Set-AzsLocation instead");
            }

            if (ShouldProcess(this.Location.Name, VerbsCommon.Set))
            {
                using (var client = this.GetAzureStackClient())
                {
                    this.WriteVerbose(Resources.UpdatingLocation.FormatArgs(this.Location.Name));
                    var parameters = new ManagedLocationCreateOrUpdateParameters(this.Location);
                    var result     = client.ManagedLocations.CreateOrUpdate(parameters).Location;
                    WriteObject(result);
                }
            }
        }
        /// <summary>
        /// Create / Update the location.
        /// </summary>
        /// <param name='parameters'>
        /// Required. Location properties
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The location update result.
        /// </returns>
        public async Task <ManagedLocationCreateOrUpdateResult> CreateOrUpdateAsync(ManagedLocationCreateOrUpdateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.Location == null)
            {
                throw new ArgumentNullException("parameters.Location");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateOrUpdateAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/providers/Microsoft.Subscriptions.Admin/locations/";
            if (parameters.Location.Name != null)
            {
                url = url + Uri.EscapeDataString(parameters.Location.Name);
            }
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=" + Uri.EscapeDataString(this.Client.ApiVersion));
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Put;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Serialize Request
                string requestContent = null;
                JToken requestDoc     = null;

                JObject managedLocationCreateOrUpdateParametersValue = new JObject();
                requestDoc = managedLocationCreateOrUpdateParametersValue;

                if (parameters.Location.Id != null)
                {
                    managedLocationCreateOrUpdateParametersValue["id"] = parameters.Location.Id;
                }

                if (parameters.Location.Name != null)
                {
                    managedLocationCreateOrUpdateParametersValue["name"] = parameters.Location.Name;
                }

                if (parameters.Location.DisplayName != null)
                {
                    managedLocationCreateOrUpdateParametersValue["displayName"] = parameters.Location.DisplayName;
                }

                if (parameters.Location.Latitude != null)
                {
                    managedLocationCreateOrUpdateParametersValue["latitude"] = parameters.Location.Latitude;
                }

                if (parameters.Location.Longitude != null)
                {
                    managedLocationCreateOrUpdateParametersValue["longitude"] = parameters.Location.Longitude;
                }

                requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    ManagedLocationCreateOrUpdateResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new ManagedLocationCreateOrUpdateResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            Location locationInstance = new Location();
                            result.Location = locationInstance;

                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                locationInstance.Id = idInstance;
                            }

                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                locationInstance.Name = nameInstance;
                            }

                            JToken displayNameValue = responseDoc["displayName"];
                            if (displayNameValue != null && displayNameValue.Type != JTokenType.Null)
                            {
                                string displayNameInstance = ((string)displayNameValue);
                                locationInstance.DisplayName = displayNameInstance;
                            }

                            JToken latitudeValue = responseDoc["latitude"];
                            if (latitudeValue != null && latitudeValue.Type != JTokenType.Null)
                            {
                                string latitudeInstance = ((string)latitudeValue);
                                locationInstance.Latitude = latitudeInstance;
                            }

                            JToken longitudeValue = responseDoc["longitude"];
                            if (longitudeValue != null && longitudeValue.Type != JTokenType.Null)
                            {
                                string longitudeInstance = ((string)longitudeValue);
                                locationInstance.Longitude = longitudeInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
 /// <summary>
 /// Create / Update the location.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.IManagedLocationOperations.
 /// </param>
 /// <param name='parameters'>
 /// Required. Location properties
 /// </param>
 /// <returns>
 /// The location update result.
 /// </returns>
 public static Task <ManagedLocationCreateOrUpdateResult> CreateOrUpdateAsync(this IManagedLocationOperations operations, ManagedLocationCreateOrUpdateParameters parameters)
 {
     return(operations.CreateOrUpdateAsync(parameters, CancellationToken.None));
 }
 /// <summary>
 /// Create / Update the location.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.IManagedLocationOperations.
 /// </param>
 /// <param name='parameters'>
 /// Required. Location properties
 /// </param>
 /// <returns>
 /// The location update result.
 /// </returns>
 public static ManagedLocationCreateOrUpdateResult CreateOrUpdate(this IManagedLocationOperations operations, ManagedLocationCreateOrUpdateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IManagedLocationOperations)s).CreateOrUpdateAsync(parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }