<commit_before><?php

/**
 * Vultr.com Curl Adapter
 *
 * NOTE: part of this code was extracted from
 * https://github.com/usefulz/vultr-api-client, updated for PSR compliance and
 * extended with new API calls.
 *
 * @package Vultr
 * @version 1.0
 * @author  https://github.com/malc0mn - https://github.com/usefulz
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 * @see     https://github.com/malc0mn/vultr-api-client
 */

namespace Vultr\ApiCall;

class Server extends AbstractApiCall
{
    /**
     * Changes the application of a virtual machine.
     *
     * All data will be permanently lost.
     *
     * @see https://www.vultr.com/api/#server_app_change
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     * @param integer $appId    Application to use. See getAppChangeList().
     *
     * @return integer HTTP response code
     */
    public function appChange($serverId, $appId)
    {
        $args = [
            'SUBID' => (int) $serverId,
            'APPID' => (int) $appId,
        ];

        return $this->adapter->post('server/app_change', $args, true);
    }

    /**
     * Retrieves a list of applications to which a virtual machine can be
     * changed.
     *
     * Only virtual machines originally launched as an application can be
     * switched to other applications. Always check against this list before
     * trying to switch applications because it is not possible to switch
     * between every application combination.
     *
     * @see https://www.vultr.com/api/#server_app_change_list
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getAppChangeList($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        return $this->adapter->get('server/app_change_list', $args);
    }

    /**
     * Changes the operating system of a virtual machine.
     *
     * All data will be permanently lost.
     *
     * @see https://www.vultr.com/api/#server_os_change
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     * @param integer $osId     Operating system to use. See getOsChangeList().
     *
     * @return integer HTTP response code
     */
    public function osChange($serverId, $osId)
    {
        $args = [
            'SUBID' => (int) $serverId,
            'OSID' => (int) $osId,
        ];

        return $this->adapter->post('server/os_change', $args, true);
    }

    /**
     * Retrieves a list of operating systems to which a virtual machine can be
     * changed.
     *
     * @see https://www.vultr.com/api/#server_os_change_list
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getOsChangeList($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        return $this->adapter->get('server/os_change_list', $args);
    }


    /**
     * Upgrade the plan of a virtual machine.
     *
     * The virtual machine will be rebooted upon a successful upgrade.
     *
     * @see https://www.vultr.com/api/#server_upgrade_plan
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     * @param integer $planId   The new plan. See getUpgradePlanList().
     *
     * @return integer HTTP response code
     */
    public function upgradePlan($serverId, $planId)
    {
        $args = [
            'SUBID' => (int) $serverId,
            'VPSPLANID' => (int) $planId,
        ];

        return $this->adapter->post('server/upgrade_plan', $args, true);
    }

    /**
     * Retrieve a list of the VPSPLANIDs for which a virtual machine can be
     * upgraded.
     *
     * An empty response array means that there are currently no upgrades
     * available.
     *
     * @see https://www.vultr.com/api/#server_upgrade_plan_list
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getUpgradePlanList($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        return $this->adapter->get('server/upgrade_plan_list', $args);
    }

    /**
     * List all active or pending virtual machines on the current account.
     *
     * The 'status' field represents the status of the subscription and will be
     * one of: pending | active | suspended | closed.
     * If the status is 'active', you can check 'power_status' to determine if
     * the VPS is powered on or not. When status is 'active', you may also use
     * 'server_state' for a more detailed status of: none | locked |
     * installingbooting | isomounting | ok.
     * The API does not provide any way to determine if the initial installation
     * has completed or not. The 'v6_network', 'v6_main_ip', and
     * 'v6_network_size' fields are deprecated in favor of 'v6_networks'.
     *
     * @see https://www.vultr.com/api/#server_server_list
     *
     * @param integer $subscriptionId (optional) Unique identifier of a
     * subscription. Only the subscription object will be returned.
     * @param string  $tag            (optional) A tag string. Only subscription
     * objects with this tag will be returned.
     *
     * @return array
     */
    public function getList($subscriptionId = null, $tag = null)
    {
        $args = [];

        if ($subscriptionId !== null) {
            $args['SUBID'] = $subscriptionId;
        }

        if ($tag !== null) {
            $args['tag'] = $tag;
        }

        return $this->adapter->get('server/list', $args);
    }

    /**
     * Retrieves the (base64 encoded) user-data for this subscription.
     *
     * @see https://www.vultr.com/api/#server_get_user_data
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getUserData($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        $userData = $this->adapter->get('server/get_user_data', $args);

        return base64_decode($userData['userdata']);
    }

    /**
     * Sets the cloud-init user-data for this subscription.
     *<commit_msg>Aligned output of server detail data with the rest of the API client.<commit_after><?php

/**
 * Vultr.com Curl Adapter
 *
 * NOTE: part of this code was extracted from
 * https://github.com/usefulz/vultr-api-client, updated for PSR compliance and
 * extended with new API calls.
 *
 * @package Vultr
 * @version 1.0
 * @author  https://github.com/malc0mn - https://github.com/usefulz
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 * @see     https://github.com/malc0mn/vultr-api-client
 */

namespace Vultr\ApiCall;

class Server extends AbstractApiCall
{
    /**
     * Changes the application of a virtual machine.
     *
     * All data will be permanently lost.
     *
     * @see https://www.vultr.com/api/#server_app_change
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     * @param integer $appId    Application to use. See getAppChangeList().
     *
     * @return integer HTTP response code
     */
    public function appChange($serverId, $appId)
    {
        $args = [
            'SUBID' => (int) $serverId,
            'APPID' => (int) $appId,
        ];

        return $this->adapter->post('server/app_change', $args, true);
    }

    /**
     * Retrieves a list of applications to which a virtual machine can be
     * changed.
     *
     * Only virtual machines originally launched as an application can be
     * switched to other applications. Always check against this list before
     * trying to switch applications because it is not possible to switch
     * between every application combination.
     *
     * @see https://www.vultr.com/api/#server_app_change_list
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getAppChangeList($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        return $this->adapter->get('server/app_change_list', $args);
    }

    /**
     * Changes the operating system of a virtual machine.
     *
     * All data will be permanently lost.
     *
     * @see https://www.vultr.com/api/#server_os_change
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     * @param integer $osId     Operating system to use. See getOsChangeList().
     *
     * @return integer HTTP response code
     */
    public function osChange($serverId, $osId)
    {
        $args = [
            'SUBID' => (int) $serverId,
            'OSID' => (int) $osId,
        ];

        return $this->adapter->post('server/os_change', $args, true);
    }

    /**
     * Retrieves a list of operating systems to which a virtual machine can be
     * changed.
     *
     * @see https://www.vultr.com/api/#server_os_change_list
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getOsChangeList($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        return $this->adapter->get('server/os_change_list', $args);
    }


    /**
     * Upgrade the plan of a virtual machine.
     *
     * The virtual machine will be rebooted upon a successful upgrade.
     *
     * @see https://www.vultr.com/api/#server_upgrade_plan
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     * @param integer $planId   The new plan. See getUpgradePlanList().
     *
     * @return integer HTTP response code
     */
    public function upgradePlan($serverId, $planId)
    {
        $args = [
            'SUBID' => (int) $serverId,
            'VPSPLANID' => (int) $planId,
        ];

        return $this->adapter->post('server/upgrade_plan', $args, true);
    }

    /**
     * Retrieve a list of the VPSPLANIDs for which a virtual machine can be
     * upgraded.
     *
     * An empty response array means that there are currently no upgrades
     * available.
     *
     * @see https://www.vultr.com/api/#server_upgrade_plan_list
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getUpgradePlanList($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        return $this->adapter->get('server/upgrade_plan_list', $args);
    }

    /**
     * List all active or pending virtual machines on the current account.
     *
     * The 'status' field represents the status of the subscription and will be
     * one of: pending | active | suspended | closed.
     * If the status is 'active', you can check 'power_status' to determine if
     * the VPS is powered on or not. When status is 'active', you may also use
     * 'server_state' for a more detailed status of: none | locked |
     * installingbooting | isomounting | ok.
     * The API does not provide any way to determine if the initial installation
     * has completed or not. The 'v6_network', 'v6_main_ip', and
     * 'v6_network_size' fields are deprecated in favor of 'v6_networks'.
     *
     * @see https://www.vultr.com/api/#server_server_list
     *
     * @param integer $subscriptionId (optional) Unique identifier of a
     * subscription. Only the subscription object will be returned.
     * @param string  $tag            (optional) A tag string. Only subscription
     * objects with this tag will be returned.
     *
     * @return array
     */
    public function getList($subscriptionId = null, $tag = null)
    {
        $args = [];

        if ($subscriptionId !== null) {
            $args['SUBID'] = $subscriptionId;
        }

        if ($tag !== null) {
            $args['tag'] = $tag;
        }

        $servers = $this->adapter->get('server/list', $args);

        if ($subscriptionId !== null) {
            return isset($servers[$subscriptionId])? $servers[$subscriptionId] : false;
        }

        return $servers;
    }

    /**
     * Retrieves the (base64 encoded) user-data for this subscription.
     *
     * @see https://www.vultr.com/api/#server_get_user_data
     *
     * @param integer $serverId Unique identifier for this subscription. These
     * can be found using the getList() call.
     *
     * @return array
     */
    public function getUserData($serverId)
    {
        $args = ['SUBID' => (int) $serverId];

        $userData = $this->adapter->get('server/get_user_data', $args);

        return base64_decode($userData['userdata']);
    }

    /**
     * Sets the cloud-init user-data for this subscription.
     *
{'fraction_non_alphanumeric': 0.11206961218213188, 'fraction_numerical': 0.0018003150551346486, 'mean_word_length': 2.321375186846039, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 24, 'lorem ipsum': 0, 'www.': 18, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4855758', 'n_tokens_mistral': 3913, 'n_tokens_neox': 3850, 'n_words': 1225}