-
Notifications
You must be signed in to change notification settings - Fork 41
Additional endpoints - storagebox & key #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
glorpen
wants to merge
8
commits into
aszlig:master
Choose a base branch
from
glorpen:additional_endpoints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c030784
Add support for StorageBoxes
pajowu cd3e4d7
Apply suggestions from code review (Part 1)
pajowu a206ff7
Apply suggestions from code review (Part 2)
pajowu 2728cbb
Merge remote-tracking branch 'pajowu/pajowu/storagebox'
glorpen b9d0ac5
fill storagebox id for servers
glorpen 8e0ebb3
add support for key endpoint
glorpen 66aa3e5
remove unneeded underscores
glorpen c3a80b3
remove unicode string markers
glorpen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import datetime | ||
|
|
||
|
|
||
| class Key: | ||
| name: str | ||
| fingerprint: str | ||
| size: int | ||
| data: str | ||
| created_at: datetime.datetime | ||
|
|
||
| def __init__(self, conn, fingerprint: str = None, data: dict = None): | ||
| super(Key, self).__init__() | ||
| self._conn = conn | ||
|
|
||
| if data is None: | ||
| self.fingerprint = fingerprint | ||
| else: | ||
| self.update_info(data) | ||
|
|
||
| def update_info(self, result=None): | ||
| if result is None: | ||
| result = self._conn.request("get", f"/key/{self.fingerprint}") | ||
|
|
||
| data = result["key"] | ||
|
|
||
| for key in ("name", "fingerprint", "size", "data"): | ||
| setattr(self, key, data[key]) | ||
| self.created_at = datetime.datetime.strptime(data['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ') | ||
|
|
||
| def rename(self, new_name: str): | ||
| return self._conn.request('POST', f"/key/{self.fingerprint}", {"name": new_name}) | ||
|
|
||
| def __repr__(self): | ||
| return f"<Key {self.name}:{self.fingerprint}>" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
|
|
||
| from base64 import b64encode | ||
|
|
||
| from hetzner.key import Key | ||
|
|
||
| try: | ||
| from httplib import BadStatusLine, ResponseNotReady | ||
| except ImportError: | ||
|
|
@@ -17,6 +19,7 @@ | |
|
|
||
| from hetzner import WebRobotError, RobotError | ||
| from hetzner.server import Server | ||
| from hetzner.storagebox import StorageBox | ||
| from hetzner.rdns import ReverseDNSManager | ||
| from hetzner.failover import FailoverManager | ||
| from hetzner.util.http import ValidatedHTTPSConnection | ||
|
|
@@ -427,10 +430,38 @@ def get(self, ip): | |
| def __iter__(self): | ||
| return iter([Server(self.conn, s) for s in self.conn.get('/server')]) | ||
|
|
||
| class StorageBoxManager(object): | ||
| def __init__(self, conn): | ||
| self.conn = conn | ||
|
|
||
| def get(self, id_): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've used |
||
| """ | ||
| Get storage boxes by providing its main id | ||
| """ | ||
| return StorageBox(self.conn, self.conn.get('/storagebox/{0}'.format(id_))) | ||
|
|
||
| def __iter__(self): | ||
| return iter([StorageBox(self.conn, s) for s in self.conn.get('/storagebox')]) | ||
|
|
||
|
|
||
| class KeysManager(object): | ||
| def __init__(self, conn): | ||
| self._conn = conn | ||
|
|
||
| def __iter__(self): | ||
| return iter([Key(self._conn, data=k) for k in self._conn.get('/key')]) | ||
|
|
||
| def delete(self, fingerprint: str): | ||
| return self._conn.request('DELETE', f"/key/{fingerprint}") | ||
|
|
||
| def add(self, name: str, data: str): | ||
| return Key(self._conn, data=self._conn.request('POST', "/key", {"name": name, "data": data})["key"]) | ||
|
|
||
| class Robot(object): | ||
| def __init__(self, user, passwd): | ||
| self.conn = RobotConnection(user, passwd) | ||
| self.servers = ServerManager(self.conn) | ||
| self.storageboxes = StorageBoxManager(self.conn) | ||
| self.rdns = ReverseDNSManager(self.conn) | ||
| self.failover = FailoverManager(self.conn, self.servers) | ||
| self.keys = KeysManager(self.conn) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import logging | ||
|
|
||
| from datetime import datetime | ||
|
|
||
| __all__ = ['StorageBox', 'SubAccount', 'SubAccountManager'] | ||
|
|
||
|
|
||
| class SubAccount(object): | ||
| def __init__(self, conn, box_id, result): | ||
| self.conn = conn | ||
| self.box_id = box_id | ||
| self.update_info(result) | ||
|
|
||
| def update_info(self, result): | ||
| """ | ||
| Update the information of the subaccount. | ||
| """ | ||
| data = result['subaccount'] | ||
|
|
||
| self.username = data['username'] | ||
| self.accountid = data['accountid'] | ||
| self.server = data['server'] | ||
| self.homedirectory = data['homedirectory'] | ||
| self.samba = data['samba'] | ||
| self.ssh = data['ssh'] | ||
| self.external_reachability = data['external_reachability'] | ||
| self.webdav = data['webdav'] | ||
| self.readonly = data['readonly'] | ||
| self.createtime = datetime.strptime(data['createtime'], '%Y-%m-%d %H:%M:%S') | ||
| self.comment = data['comment'] | ||
|
|
||
| def update(self, homedirectory, samba, ssh, external_reachability, webdav, readonly, comment): | ||
| path = f'/storagebox/{self.box_id}/subaccount/{self.username}' | ||
| data = {'homedirectory': homedirectory, | ||
| 'samba': samba, | ||
| 'ssh': ssh, | ||
| 'external_reachability': external_reachability, | ||
| 'webdav': webdav, | ||
| 'readonly': readonly, | ||
| 'comment': comment} | ||
| return self.conn.put(path, data) | ||
|
|
||
| def reset_password(self): | ||
| data = f'/storagebox/{self.box_id}/subaccount/{self.username}/password' | ||
| result = self.conn.post(data, None) | ||
| return result['password'] | ||
|
|
||
| def delete(self): | ||
| self.conn.delete('/storagebox/{0}/subaccount/{1}'.format(self.box_id, self.username)) | ||
|
|
||
| def __repr__(self): | ||
| return "<SubAccount {0}>".format(self.username) | ||
|
|
||
|
|
||
| class SubAccountManager(object): | ||
| def __init__(self, conn, box_id): | ||
| self.conn = conn | ||
| self.box_id = box_id | ||
|
|
||
| def create(self, homedirectory, samba, ssh, external_reachability, webdav, readonly, comment): | ||
| result = self.conn.post('/storagebox/{0}/subaccount'.format(self.box_id), | ||
| {'homedirectory': homedirectory, | ||
| 'samba': samba, | ||
| 'ssh': ssh, | ||
| 'external_reachability': external_reachability, | ||
| 'webdav': webdav, | ||
| 'readonly': readonly, | ||
| 'comment': comment}) | ||
|
|
||
| return result | ||
|
|
||
| def delete(self, username): | ||
| self.conn.delete('/storagebox/{0}/subaccount/{1}'.format(self.box_id, username)) | ||
|
|
||
| def __iter__(self): | ||
| return iter([SubAccount(self.conn, self.box_id, s) for s in self.conn.get('/storagebox/{0}/subaccount'.format(self.box_id))]) | ||
|
|
||
|
|
||
| class StorageBox(object): | ||
| def __init__(self, conn, result): | ||
| self.conn = conn | ||
| self.update_info(result) | ||
| self.subaccounts = SubAccountManager(self.conn, self.id_) | ||
| self.logger = logging.getLogger("StorageBox #{0}".format(self.id_)) | ||
|
|
||
| def update_info(self, result=None): | ||
| """ | ||
| Updates the information of the current SubAccount instance either by | ||
| sending a new GET request or by parsing the response given by result. | ||
| """ | ||
| if result is None: | ||
| result = self.conn.get('/storagebox/{0}'.format(self.id_)) | ||
| data = result['storagebox'] | ||
|
|
||
| self.id_ = data['id'] | ||
| self.login = data['login'] | ||
| self.name = data['name'] | ||
| self.product = data['product'] | ||
| self.cancelled = data['cancelled'] | ||
| self.locked = data['locked'] | ||
| self.location = data['location'] | ||
| self.linked_server = data['linked_server'] | ||
| self.paid_until = datetime.strptime(data['paid_until'], '%Y-%m-%d') | ||
| if 'disk_quota' in data: | ||
| self.disk_quota = data['disk_quota'] | ||
| self.disk_usage = data['disk_usage'] | ||
| self.disk_usage_data = data['disk_usage_data'] | ||
| self.disk_usage_snapshots = data['disk_usage_snapshots'] | ||
| self.webdav = data['webdav'] | ||
| self.samba = data['samba'] | ||
| self.ssh = data['ssh'] | ||
| self.external_reachability = data['external_reachability'] | ||
| self.zfs = data['zfs'] | ||
| self.server = data['server'] | ||
| self.host_system = data['host_system'] | ||
|
|
||
| def __repr__(self): | ||
| return "<{0} (#{1} {2})>".format(self.login, self.id_, self.product) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding type hints while at the same time using deprecated stuff such as
class Foo(object):andsuper(Key, self)is somewhat inconsistent. However, since even I nowadays use type hints and more modern features in other projects, I'm wondering whether it's time to even drop support for Python 2 for this project. That however has to happen for a new major version, though.So either we stick here to being 2.x compatible and merge this pull request or we use Python 3.x features (consistently) here but wait for the next major release.