Skip to content

Commit

Permalink
Added SSH Key Management
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Birkner committed Dec 2, 2016
1 parent 0d6d267 commit 7cfb3aa
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/Nitrapi/SSHKeys/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Nitrapi\SSHKeys;

use Nitrapi\Nitrapi;

class Manager
{
/**
* @var Nitrapi
*/
private $api;

public function __construct(Nitrapi $api)
{
$this->api = $api;
}

/**
* Returns all your SSH Public Keys
*
* @return array
*/
public function getPublicKeys()
{
$url = "user/ssh_keys";
$keys = [];
foreach ($this->api->dataGet($url)['keys'] as $key) {
$keys[] = new SSHKey($this->api, $key);
}
return $keys;
}

/**
* Uploads a new SSH Public Key
*
* @param $key
* @param bool $enabled
* @return bool
*/
public function uploadPublicKey($key, $enabled = true)
{
$url = "user/ssh_keys";
$this->api->dataPost($url, [
'key' => $key,
'enabled' => ($enabled ? 'true' : 'false')
]);

return true;
}

}
98 changes: 98 additions & 0 deletions lib/Nitrapi/SSHKeys/SSHKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Nitrapi\SSHKeys;

use Nitrapi\Nitrapi;

class SSHKey {

/**
* @var Nitrapi
*/
private $api;

/**
* @var array
*/
private $data;

public function __construct(Nitrapi $api, array $data) {
$this->api = $api;
$this->data = $data;
$this->data['full_public_key'] = $this->data['type'] . ' ' . $this->data['public_key'] . ' ' . $this->data['comment'];
}

/**
* Returns the full SSH public key
*
* @return string
*/
public function getPublicKey()
{
return $this->data['full_public_key'];
}

/**
* Updates the existing SSH public key
*
* @return SSHKey
*/
public function setPublicKey($key)
{
$this->data['full_public_key'] = $key;
$this->doUpdate();
return $this;
}

/**
* Returns true if the key is enabled
*
* @return bool
*/
public function isEnabled()
{
return (bool)$this->data['enabled'];
}

/**
* Returns true if the key is enabled
*
* @return SSHKey
*/
public function setEnabled($enabled = true)
{
$this->data['enabled'] = $enabled;
$this->doUpdate();
return $this;
}

/**
* Deletes this SSH public key
*
* @return bool
*/
public function doDelete()
{
$url = "user/ssh_keys/" . $this->data['id'];
$this->api->dataDelete($url);

return true;
}

/**
* Updates this SSH public key in database
*
* @return $this
*/
private function doUpdate()
{
$url = "user/ssh_keys/" . $this->data['id'];
$this->api->dataPost($url, [
'key' => $this->getPublicKey(),
'enabled' => ($this->isEnabled() ? 'true' : 'false')
]);

return $this;
}

}

0 comments on commit 7cfb3aa

Please sign in to comment.