Servers
=======

Get server
----------

The easiest way to retrieve a specific server is by its unique ID:

.. code-block:: php

  $server = $service->server('{serverId}');


List servers
------------

You can list servers in two different ways:

-  return an *overview* of each server (ID, name and links)
-  return *detailed information* for each server

Knowing which option to use might help save unnecessary bandwidth and
reduce latency.

.. code-block:: php

  // overview
  $servers = $service->serverList();

  // detailed
  $servers = $service->serverList(true);

URL parameters for filtering servers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+
| Name                     | Description                                                                                                                                                                                                                                                                                                        | Type                                            |
+==========================+====================================================================================================================================================================================================================================================================================================================+=================================================+
| image                    | The image ID                                                                                                                                                                                                                                                                                                       | string                                          |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+
| flavor                   | The flavor ID                                                                                                                                                                                                                                                                                                      | string                                          |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+
| name                     | The server name                                                                                                                                                                                                                                                                                                    | string                                          |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+
| status                   | The server status. Servers contain a status attribute that indicates the current server state. You can filter on the server status when you complete a list servers request, and the server status is returned in the response body. For a full list, please consult ``OpenCloud\Compute\Constants\ServerState``   | string                                          |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+
| changes-since            | Value for checking for changes since a previous request                                                                                                                                                                                                                                                            | A valid ISO 8601 dateTime (2011-01-24T17:08Z)   |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+
| RAX-SI:image_schedule    | If scheduled images enabled or not. If the value is TRUE, the list contains all servers that have an image schedule resource set on them. If the value is set to FALSE, the list contains all servers that do not have an image schedule.                                                                          | bool                                            |
+--------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+

`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Compute/list_servers.php>`_


Create server
-------------

Using an image
~~~~~~~~~~~~~~

Now we're ready to create our instance:

.. code-block:: php

  $server = $compute->server();

  $server->create(array(
      'name'     => 'My lovely server',
      'imageId'  => '{imageId}',
      'flavorId' => '{flavorId}',
  ));

It's always best to be defensive when executing functionality over HTTP;
you can achieve this best by wrapping calls in a try/catch block. It
allows you to debug your failed operations in a graceful and efficient
manner.

`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Compute/create_server.php>`_


Using a bootable volume
~~~~~~~~~~~~~~~~~~~~~~~

Firstly we need to find our volume using their IDs.

.. code-block:: php

  $bootableVolume = $client->volumeService()->volume('{volumeId}');

Now we're ready to create our instance:

.. code-block:: php

  $server = $compute->server();

  $response = $server->create(array(
      'name'     => 'My lovely server',
      'volume'   => $bootableVolume,
      'flavorId' => '{flavorId}'
  ));

It's always best to be defensive when executing functionality over HTTP;
you can achieve this best by wrapping calls in a try/catch block. It
allows you to debug your failed operations in a graceful and efficient
manner.

`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Compute/create_server_with_bootable_volume.php>`_


Create parameters
~~~~~~~~~~~~~~~~~

 
| Name                        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | Type      | Required                     |
 
| name                        | The server name. The name that you specify in a create request becomes the initial host name of the server. After the server is built, if you change the server name in the API or change the host name directly, the names are not kept in sync.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | string    | Yes                          |
 
| flavor                      | A populated ``OpenCloud\Compute\Resource\Flavor`` object representing your chosen flavor                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | object    | Yes                          |
 
| image                       | A populated ``OpenCloud\Compute\Resource\Image`` object representing your chosen image                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | object    | No, if volume is specified   |
 
| volume                      | A populated ``OpenCloud\Volume\Resource\Volume`` object representing your chosen bootable volume                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | object    | No, if image is specified    |
 
| volumeDeleteOnTermination   | ``true`` if the bootable volume should be deleted when the server is terminated; ``false``, otherwise                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | boolean   | No; default = ``false``      |
 
| OS-DCF:diskConfig           | The disk configuration value. You can use two options: ``AUTO`` or ``MANUAL``. \ ``AUTO`` means the server is built with a single partition the size of the target flavor disk. The file system is automatically adjusted to fit the entire partition. This keeps things simple and automated. ``AUTO`` is valid only for images and servers with a single partition that use the EXT3 file system. This is the default setting for applicable Rackspace base images.\ ``MANUAL`` means the server is built using whatever partition scheme and file system is in the source image. If the target flavor disk is larger, the remaining disk space is left unpartitioned. This enables images to have non-EXT3 file systems, multiple partitions, and so on, and enables you to manage the disk configuration.   | string    | No                           |
 
| networks                    | An array of populated ``OpenCloud\Compute\Resource\Network`` objects that indicate which networks your instance resides in.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | array     | No                           |
 
| metadata                    | An array of arbitrary data (key-value pairs) that adds additional meaning to your server.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | array     | No                           |
 
| keypair                     | You can install a registered keypair onto your newly created instance, thereby providing scope for keypair-based authentication.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | array     | No                           |
 
| personality                 | Files that you can upload to your newly created instance's filesystem.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | array     | No                           |
 

Creating a server with keypairs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In order to provision an instance with a saved keypair (allowing you to SSH
in without passwords), you create your server using the same operation
as usual, with one extra parameter:

.. code-block:: php

  $server = $compute->server();

  $server->create(array(
      'name'     => 'New server',
      'imageId'  => '{imageId}',
      'flavorId' => '{flavorId}',
      'keypair'  => 'main_key'
  ));

So, as you can see, you specify the **name** of an existing keypair that
you previously created on the API.

`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Compute/create_server_with_keypair.php>`_


Creating a server with personality files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Before you execute the create operation, you can add "personality" files
to your ``OpenCloud\Compute\Resource\Server`` object. These files are
structured as a flat array.

.. code-block:: php

  $server->addFile('/var/test_file', 'FILE CONTENT');

As you can see, the first parameter represents the filename, and the
second is a string representation of its content. When the server is
created these files will be created on its local filesystem. For more
information about server personality files, please consult the `official
documentation <http://docs.rackspace.com/servers/api/v2/cs-devguide/content/Server_Personality-d1e2543.html>`__.

Update server
-------------

You can update certain attributes of an existing server instance. These
attributes are detailed in the next section.

.. code-block:: php

  $server->update(array(
     'name' => 'NEW SERVER NAME'
  ));


`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Compute/update_server.php>`_


Updatable attributes
~~~~~~~~~~~~~~~~~~~~

+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| name         | description                                                                                                                                      |
+==============+==================================================================================================================================================+
| name         | The name of the server. If you edit the server name, the server host name does not change. Also, server names are not guaranteed to be unique.   |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| accessIPv4   | The IP version 4 address.                                                                                                                        |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| accessIPv6   | The IP version 6 address.                                                                                                                        |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------+

Delete server
-------------

.. code-block:: php

  $server->delete();

`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/Compute/delete_server.php>`_
