private async void ServerCreateImageSnapshotContextMenu_Click(object sender, RoutedEventArgs e)
        {
            lkcode.hetznercloudapi.Api.Server server = this.ServerDataGrid.SelectedItem as lkcode.hetznercloudapi.Api.Server;

            try
            {
                this.AddLogMessage(string.Format("createimage snapshot server '{0}'", server.Name));

                string imageName = await this.ShowInputAsync(
                    "Image Name",
                    "enter the image-name");

                lkcode.hetznercloudapi.Api.ServerActionResponse actionResponse = await server.CreateImage(imageName, lkcode.hetznercloudapi.Components.ServerImageType.SNAPSHOT);

                if (actionResponse.Error != null)
                {
                    this.AddLogMessage(string.Format("error: {0} ({1})", actionResponse.Error.Message, actionResponse.Error.Code));
                }
                else
                {
                    this.AddLogMessage(string.Format("success: createimage snapshot server '{0}' - actionId '{1}' - actionId '{2}'", server.Name, actionResponse.Id, actionResponse.Command));
                }
            }
            catch (Exception err)
            {
                this.AddLogMessage(string.Format("error: {0}", err.Message));
            }
        }
Exemplo n.º 2
0
        /// <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);
            }
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Deletes a server. This immediately removes the server from your account, and it is no longer accessible.
        /// </summary>
        /// <returns>the serialized ServerActionResponse</returns>
        public async Task <ServerActionResponse> Delete(string token = null)
        {
            string responseContent = await ApiCore.SendDeleteRequest(string.Format("/servers/{0}", this.Id), token : token);

            ServerActionResponse actionResponse = GetServerActionFromResponseDataEx <Objects.Server.Delete.Response>(responseContent);

            return(actionResponse);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Starts a server by turning its power on.
        /// </summary>
        /// <returns>the serialized ServerActionResponse</returns>
        public async Task <ServerActionResponse> PowerOn(string token = null)
        {
            string responseContent = await ApiCore.SendPostRequest(string.Format("/servers/{0}/actions/poweron", this.Id), token : token);

            ServerActionResponse actionResponse = GetServerActionFromResponseDataEx <Objects.Server.PostPoweron.Response>(responseContent);

            return(actionResponse);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Deletes a server. This immediately removes the server from your account, and it is no longer accessible.
        /// </summary>
        /// <returns>the serialized ServerActionResponse</returns>
        public async Task <ServerActionResponse> Delete()
        {
            string responseContent = await ApiCore.SendDeleteRequest(string.Format("/servers/{0}", this.Id));

            Objects.Server.Delete.Response response = JsonConvert.DeserializeObject <Objects.Server.Delete.Response>(responseContent);

            ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action);

            return(actionResponse);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Cuts power to the server. This forcefully stops it without giving the server operating system time to gracefully stop. May lead to data loss, equivalent to pulling the power cord. Power off should only be used when shutdown does not work.
        /// </summary>
        /// <returns>the serialized ServerActionResponse</returns>
        public async Task <ServerActionResponse> PowerOff()
        {
            string responseContent = await ApiCore.SendPostRequest(string.Format("/servers/{0}/actions/poweroff", this.Id));

            Objects.Server.PostPoweroff.Response response = JsonConvert.DeserializeObject <Objects.Server.PostPoweroff.Response>(responseContent);

            ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action);

            return(actionResponse);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Resets the root password. Only works for Linux systems that are running the qemu guest agent. Server must be powered on (state on) in order for this operation to succeed.
        /// This will generate a new password for this server and return it.
        /// If this does not succeed you can use the rescue system to netboot the server and manually change your server password by hand.
        /// </summary>
        /// <returns>the serialized ServerActionResponse</returns>
        public async Task <ServerActionResponse> ResetPassword()
        {
            string responseContent = await ApiCore.SendPostRequest(string.Format("/servers/{0}/actions/reset_password", this.Id));

            Objects.Server.ResetPassword.Response response = JsonConvert.DeserializeObject <Objects.Server.ResetPassword.Response>(responseContent);

            ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action);

            actionResponse.AdditionalActionContent = response.root_password;

            return(actionResponse);
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="responseData"></param>
        /// <returns></returns>
        private static ServerActionResponse GetServerActionFromResponseData(Objects.Server.Universal.ServerAction responseData)
        {
            ServerActionResponse serverAction = new ServerActionResponse();

            serverAction.Id       = responseData.id;
            serverAction.Command  = responseData.command;
            serverAction.Progress = responseData.progress;
            serverAction.Started  = responseData.started;
            serverAction.Status   = responseData.status;

            return(serverAction);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Requests credentials for remote access via vnc over websocket to keyboard, monitor, and mouse for a server.
        /// The provided url is valid for 1 minute, after this period a new url needs to be created to connect to the server.
        /// How long the connection is open after the initial connect is not subject to this timeout.
        /// </summary>
        /// <returns>the serialized ServerActionResponse</returns>
        public async Task <ServerActionResponse> RequestConsole()
        {
            string responseContent = await ApiCore.SendPostRequest(string.Format("/servers/{0}/actions/request_console", this.Id));

            Objects.Server.GetRequestConsole.Response response = JsonConvert.DeserializeObject <Objects.Server.GetRequestConsole.Response>(responseContent);

            ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action);

            actionResponse.AdditionalActionContent = new ServerConsoleData()
            {
                Url      = response.wss_url,
                Password = response.password
            };

            return(actionResponse);
        }
        private async void ServerResetContextMenu_Click(object sender, RoutedEventArgs e)
        {
            lkcode.hetznercloudapi.Api.Server server = this.ServerDataGrid.SelectedItem as lkcode.hetznercloudapi.Api.Server;

            try
            {
                this.AddLogMessage(string.Format("reset server '{0}'", server.Name));

                lkcode.hetznercloudapi.Api.ServerActionResponse actionResponse = await server.Reset();

                this.AddLogMessage(string.Format("success: reset server '{0}' - actionId '{1}' - actionId '{2}'", server.Name, actionResponse.Id, actionResponse.Command));
            }
            catch (Exception err)
            {
                this.AddLogMessage(string.Format("error: {0}", err.Message));
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Change reverse DNS entry for this Server
        /// Changes the hostname that will appear when getting the hostname belonging to the primary IPs (IPv4 and IPv6) of this Server.
        /// Floating IPs assigned to the Server are not affected by this.
        /// </summary>
        /// <returns></returns>
        public async Task <ServerActionResponse> ChangeRDNS(string ip, string dns_ptr)
        {
            Dictionary <string, object> arguments = new Dictionary <string, object>();

            ip      = ip.Trim();
            dns_ptr = dns_ptr.Trim();
            if (string.IsNullOrEmpty(ip) || !string.IsNullOrEmpty(dns_ptr))
            {
            }

            arguments.Add("ip", ip);
            arguments.Add("dns_ptr", dns_ptr);
            string responseContent = await ApiCore.SendPostRequest(string.Format("/servers/{0}/actions/change_dns_ptr", this.Id), arguments);

            Objects.Server.PutChangeRDNS.Response response = JsonConvert.DeserializeObject <Objects.Server.PutChangeRDNS.Response>(responseContent);

            ServerActionResponse actionResponse = GetServerActionFromResponseData(response.action);

            return(actionResponse);
        }
        private async void ServerDeleteContextMenu_Click(object sender, RoutedEventArgs e)
        {
            MessageDialogResult msgResult = await this.ShowMessageAsync(
                "Delete the Server",
                "Are you sure you want to delete the server?");

            if (msgResult == MessageDialogResult.Affirmative)
            {
                lkcode.hetznercloudapi.Api.Server server = this.ServerDataGrid.SelectedItem as lkcode.hetznercloudapi.Api.Server;

                try
                {
                    this.AddLogMessage(string.Format("delete server '{0}'", server.Name));

                    lkcode.hetznercloudapi.Api.ServerActionResponse actionResponse = await server.Delete();

                    this.AddLogMessage(string.Format("success: deleted server '{0}' - actionId '{1}' - actionId '{2}'", server.Name, actionResponse.Id, actionResponse.Command));
                }
                catch (Exception err)
                {
                    this.AddLogMessage(string.Format("error: {0}", err.Message));
                }
            }
        }
        private async void RebuildFromImageButton_Click(object sender, RoutedEventArgs e)
        {
            lkcode.hetznercloudapi.Api.Server server = this.ServerDataGrid.SelectedItem as lkcode.hetznercloudapi.Api.Server;

            try
            {
                this.AddLogMessage(string.Format("rebuild from image for server '{0}'", server.Name));

                lkcode.hetznercloudapi.Api.ServerActionResponse actionResponse = await server.RebuildImage("test-snapshot");

                if (actionResponse.Error != null)
                {
                    this.AddLogMessage(string.Format("error: {0} ({1})", actionResponse.Error.Message, actionResponse.Error.Code));
                }
                else
                {
                    this.AddLogMessage(string.Format("success: rebuild image for server '{0}' - actionId '{1}' - actionId '{2}'", server.Name, actionResponse.Id, actionResponse.Command));
                }
            }
            catch (Exception err)
            {
                this.AddLogMessage(string.Format("error: {0}", err.Message));
            }
        }
Exemplo n.º 15
0
        /// <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);
            }
        }