/usr/lib/python3/dist-packages/digitalocean/SSHKey.py is in python3-digitalocean 1.13.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | # -*- coding: utf-8 -*-
from .baseapi import BaseAPI, GET, POST, DELETE, PUT
class SSHKey(BaseAPI):
def __init__(self, *args, **kwargs):
self.id = ""
self.name = None
self.public_key = None
self.fingerprint = None
super(SSHKey, self).__init__(*args, **kwargs)
@classmethod
def get_object(cls, api_token, ssh_key_id):
"""
Class method that will return a SSHKey object by ID.
"""
ssh_key = cls(token=api_token, id=ssh_key_id)
ssh_key.load()
return ssh_key
def load(self):
"""
Load the SSHKey object from DigitalOcean.
Requires either self.id or self.fingerprint to be set.
"""
identifier = None
if self.id:
identifier = self.id
elif self.fingerprint is not None:
identifier = self.fingerprint
data = self.get_data("account/keys/%s" % identifier, type=GET)
ssh_key = data['ssh_key']
# Setting the attribute values
for attr in ssh_key.keys():
setattr(self, attr, ssh_key[attr])
self.id = ssh_key['id']
def load_by_pub_key(self, public_key):
"""
This method will laod a SSHKey object from DigitalOcean
from a public_key. This method will avoid problem like
uploading the same public_key twice.
"""
data = self.get_data("account/keys/")
for jsoned in data['ssh_keys']:
if jsoned.get('public_key', "") == public_key:
self.id = jsoned['id']
self.load()
return self
return None
def create(self):
"""
Create the SSH Key
"""
input_params = {
"name": self.name,
"public_key": self.public_key,
}
data = self.get_data("account/keys/", type=POST, params=input_params)
if data:
self.id = data['ssh_key']['id']
def edit(self):
"""
Edit the SSH Key
"""
input_params = {
"name": self.name,
"public_key": self.public_key,
}
data = self.get_data(
"account/keys/%s" % self.id,
type=PUT,
params=input_params
)
if data:
self.id = data['ssh_key']['id']
def destroy(self):
"""
Destroy the SSH Key
"""
return self.get_data("account/keys/%s" % self.id, type=DELETE)
def __str__(self):
return "<SSHKey: %s %s>" % (self.id, self.name)
|