/// <summary> /// Rebuilds a server overwriting its disk with the content of an image, thereby destroying all data on the target server /// The image can either be one you have created earlier(backup or snapshot image) or it can be a completely fresh system image provided by us.You can get a list of all available images with GET /images. /// Your server will automatically be powered off before the rebuild command executes. /// </summary> /// <param name="image">ID or name of image to rebuilt from.</param> /// <returns>the serialized ServerActionResponse</returns> public async Task <ServerActionResponse> RebuildImage(string image) { Dictionary <string, object> arguments = new Dictionary <string, object>(); if (!string.IsNullOrEmpty(image.Trim()) && !string.IsNullOrWhiteSpace(image.Trim())) { arguments.Add("image", image); } string responseContent = await ApiCore.SendPostRequest(string.Format("/servers/{0}/actions/rebuild", this.Id), arguments); JObject responseObject = JObject.Parse(responseContent); if (responseObject["error"] != null) { // error Objects.Server.Universal.ErrorResponse error = JsonConvert.DeserializeObject <Objects.Server.Universal.ErrorResponse>(responseContent); ServerActionResponse response = new ServerActionResponse(); response.Error = GetErrorFromResponseData(error); return(response); } else { // success Objects.Server.PostRebuild.Response response = JsonConvert.DeserializeObject <Objects.Server.PostRebuild.Response>(responseContent); ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action); //actionResponse.AdditionalActionContent = GetServerImageFromResponseData(response.image); return(actionResponse); } }
private static ServerActionResponse GetServerActionFromResponseDataEx <TSuccessResponse>( string responseContent, Action <ServerActionResponse, TSuccessResponse> onSuccess = null, Action <ServerActionResponse, Objects.Server.Universal.ErrorResponse> onError = null) where TSuccessResponse : Objects.Server.Universal.ISuccessResponse { JObject responseObject = JObject.Parse(responseContent); if (responseObject["error"] != null) { // error Objects.Server.Universal.ErrorResponse error = JsonConvert.DeserializeObject <Objects.Server.Universal.ErrorResponse>(responseContent); ServerActionResponse response = new ServerActionResponse { Error = GetErrorFromResponseData(error) }; onError?.Invoke(response, error); return(response); } else { // success TSuccessResponse response = JsonConvert.DeserializeObject <TSuccessResponse>(responseContent); ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action); onSuccess?.Invoke(actionResponse, response); return(actionResponse); } }
/// <summary> /// /// </summary> /// <param name="errorResponse"></param> /// <returns></returns> private static Error GetErrorFromResponseData(Objects.Server.Universal.ErrorResponse errorResponse) { Error error = new Error(); error.Message = errorResponse.error.message; error.Code = errorResponse.error.code; return(error); }
/// <summary> /// saves the server-model /// </summary> /// <returns></returns> public async Task <ServerActionResponse> SaveAsync(Image image, bool startAfterCreate = true, string[] sshKeys = null, string userData = "", long locationId = -1, long datacenterId = -1) { this.validateRequiredServerDataForSave(image); this.validateOptionalServerDataForSave(); Dictionary <string, object> arguments = new Dictionary <string, object>(); // required fields arguments.Add("name", this.Name); arguments.Add("server_type", this.ServerType.Id.ToString()); arguments.Add("image", image.Id); // optional fields arguments.Add("start_after_create", startAfterCreate.ToString()); // optional field: ssh-key if (sshKeys != null && sshKeys.Length > 0) { arguments.Add("ssh_keys", JsonConvert.SerializeObject(sshKeys)); } // optional field: user-data if (!string.IsNullOrEmpty(userData) && !string.IsNullOrWhiteSpace(userData)) { arguments.Add("user_data", userData); } // optional field: location if (locationId > 0) { arguments.Add("location", locationId.ToString()); } // optional field: datacenter if (datacenterId > 0) { arguments.Add("datacenter", datacenterId.ToString()); } string responseContent = await ApiCore.SendPostRequest(string.Format("/servers"), arguments); JObject responseObject = JObject.Parse(responseContent); if (responseObject["error"] != null) { // error Objects.Server.Universal.ErrorResponse error = JsonConvert.DeserializeObject <Objects.Server.Universal.ErrorResponse>(responseContent); ServerActionResponse response = new ServerActionResponse(); response.Error = GetErrorFromResponseData(error); return(response); } else { // success Objects.Server.Create.Response response = JsonConvert.DeserializeObject <Objects.Server.Create.Response>(responseContent); Server server = GetServerFromResponseData(response.server); this.setCreatedServer(server); ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action); actionResponse.AdditionalActionContent = response.root_password; return(actionResponse); } }