# Keypairs

## Setup

The first thing to do is pass in your credentials and instantiate a Rackspace client:

    <?php

    require 'vendor/autoload.php';

    use OpenCloud\Rackspace;

    $client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
      'username' => '{username}',
      'apiKey'   => '{apiKey}',
    ));

Now, set up the Auto Scale service:

    $service = $client->computeService('{catalogType}', '{region}', '{urlType}');

{catalogType} is the name of the service, as it appears in the service catalog. For Rackspace users, this will default to cloudServersOpenStack; for OpenStack users, you must set your own value since it can depend on your environment setup.

{region} is the Compute region the service will operate in. For Rackspace users, you can select one of the following from the supported regions page.

{urlType} is the type of URL to use, depending on what endpoints your catalog provides. For Rackspace, you may use either internalURL or publicURL. The former will execute HTTP transactions over the internal Rackspace network, reducing latency and the overall bandwidth cost - the caveat is that all of your resources must be in same region. publicURL, however, which is the default, will operate over the public Internet and is to be used for multi-region installations.

## Generate a new keypair

This operation creates a new keypair under a provided name; the public key value is automatically generated for you.

    // Instantiate empty object
    $keypair = $service->keypair();

    // Send to API
    $keypair->create(array(
       'name' => 'jamie_keypair_1'
    ));

    // Save these!
    $pubKey = $keypair->getPublicKey();
    $priKey = $keypair->getPrivateKey();

## Upload existing keypair

This operation creates a new keypair according to a provided name and public key value. This is useful when the public key already exists on your local filesystem.

    $keypair = $service->keypair();

    // $key needs to be the string content of the key file, not the filename
    $content = file_get_contents('~/.ssh/id_rsa.pub');

    $keypair->create(array(
       'name'      => 'main_key',
       'publicKey' => $content
    ));

## List keypairs

To list all existing keypairs:

    $keys = $service->listKeypairs();

    foreach ($keys as $key) {

    }

## Delete keypairs

To delete a specific keypair:

    $keypair->delete();