Skip to content

Commit

Permalink
Merge pull request #8 from clumsical/master
Browse files Browse the repository at this point in the history
SSHKeys
  • Loading branch information
koalalorenzo committed Jul 4, 2013
2 parents 8532b13 + 518a8da commit 840d86d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
19 changes: 18 additions & 1 deletion digitalocean/Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .Size import Size
from .Image import Image
from .Domain import Domain
from .SSHKey import SSHKey


class Manager(object):
Expand All @@ -18,7 +19,7 @@ def __call_api(self, path, params=dict()):
r = requests.get("https://api.digitalocean.com/%s" % path, params=payload)
data = r.json()
self.call_response = data
if data['status'] != "OK":
if data['status'] != "OK":
raise Exception(data[u'error_message'])
return data

Expand Down Expand Up @@ -138,3 +139,19 @@ def get_all_domains(self):
domain.api_key = self.api_key
domains.append(domain)
return domains

def get_all_sshkeys(self):
"""
This function returns a list of SSHKey object.
"""
data = self.__call_api("/ssh_keys/")
ssh_keys = list()
for jsoned in data['ssh_keys']:
ssh_key = SSHKey()
ssh_key.id = jsoned['id']
ssh_key.name = jsoned['name']
ssh_key.client_id = self.client_id
ssh_key.api_key = self.api_key
ssh_keys.append(ssh_key)
return ssh_keys

63 changes: 63 additions & 0 deletions digitalocean/SSHKey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import requests

class SSHKey(object):
def __init__(self, client_id="", api_key="", *args, **kwargs):

self.client_id = client_id
self.api_key = api_key

self.id = ""
self.name = None
self.ssh_pub_key = None

#Setting the attribute values
for attr in kwargs.keys():
setattr(self,attr,kwargs[attr])

def __call_api(self, path, params=dict()):
payload = {'client_id': self.client_id, 'api_key': self.api_key}
payload.update(params)
r = requests.get("https://api.digitalocean.com/ssh_keys/%s%s" % ( self.id, path ), params=payload)
print r.url
data = r.json()
self.call_response = data
if data['status'] != "OK":
raise Exception(data[u'error_message'])

return data

def load(self):
ssh_key = self.__call_api("")['ssh_key']
self.ssh_pub_key = ssh_key['ssh_pub_key']
self.name = ssh_key['name']
self.id = ssh_key['id']

def create(self):
"""
Create the SSH Key
"""
data = {
"name": self.name,
"ssh_pub_key": self.ssh_pub_key,
}
data = self.__call_api("/new/", data)
if data:
self.id = data['ssh_key']['id']

def edit(self):
"""
Edit the SSH Key
"""
data = {
"name": self.name,
"ssh_pub_key": self.ssh_pub_key,
}
data = self.__call_api("/edit/", data)
if data:
self.id = data['ssh_key']['id']

def destroy(self):
"""
Destroy the SSH Key
"""
self.__call_api("/destroy/")

0 comments on commit 840d86d

Please sign in to comment.