/// <summary> /// Create a new server. /// </summary> /// <param name="deploymentConfiguration"> /// The configuration that the new server will be deployed with. /// </param> /// <param name="cancellationToken"> /// An optional cancellation token that can be used to cancel the request. /// </param> /// <returns> /// The Id of the new server. /// </returns> public async Task <Guid> CreateServer(ServerDeploymentConfiguration deploymentConfiguration, CancellationToken cancellationToken = default(CancellationToken)) { if (deploymentConfiguration == null) { throw new ArgumentNullException(nameof(deploymentConfiguration)); } Guid organizationId = await GetOrganizationId(); HttpRequest createServer = Requests.Server.CreateServer.WithTemplateParameter("organizationId", organizationId); using (HttpResponseMessage response = await _httpClient.PostAsJsonAsync(createServer, deploymentConfiguration, cancellationToken)) { ApiResponseV2 apiResponse = await response.ReadContentAsApiResponseV2(); if (apiResponse.ResponseCode != ApiResponseCodeV2.InProgress) { throw CloudControlException.FromApiV2Response(apiResponse, response.StatusCode); } string serverId = apiResponse.InfoMessages.GetByName("serverId"); if (String.IsNullOrWhiteSpace(serverId)) { throw new CloudControlException("Received an unexpected response from the CloudControl API (missing 'serverId' message)."); } return(new Guid(serverId)); } }
/// <summary> /// Apply an image to the server deployment configuration. /// </summary> /// <param name="deploymentConfiguration"> /// The server deployment configuration. /// </param> /// <param name="image"> /// The image to apply. /// </param> public static void ApplyImage(this ServerDeploymentConfiguration deploymentConfiguration, Image image) { if (deploymentConfiguration == null) { throw new ArgumentNullException(nameof(deploymentConfiguration)); } if (image == null) { throw new ArgumentNullException(nameof(image)); } deploymentConfiguration.ImageId = image.Id; deploymentConfiguration.MemoryGB = image.MemoryGB; deploymentConfiguration.CPU.UpdateFrom(image.CPU); // Create or update disk configuration. Dictionary <int, VirtualMachineDisk> serverDisks = deploymentConfiguration.Disks.ToDictionary(disk => disk.ScsiUnitId); foreach (VirtualMachineDisk imageDisk in image.Disks) { VirtualMachineDisk serverDisk; if (!serverDisks.TryGetValue(imageDisk.ScsiUnitId, out serverDisk)) { serverDisk = imageDisk.Clone(withId: false); deploymentConfiguration.Disks.Add(serverDisk); } serverDisk.SizeGB = imageDisk.SizeGB; serverDisk.Speed = imageDisk.Speed; } }