Skip to content
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

Small adjustment to support Proxmox mail gateway REST API #82

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion proxmoxer/backends/base_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def __init__(self, content, status_code):

class ProxmoxBaseSSHSession(object):

def __init__(self, platform='pve'):
self.platform = platform

def _exec(self, cmd):
raise NotImplementedError()

Expand All @@ -41,7 +44,7 @@ def request(self, method, url, data=None, params=None, headers=None):
data['tmpfilename'] = tmp_filename

translated_data = ' '.join(["-{0} {1}".format(k, v) for k, v in chain(data.items(), params.items())])
full_cmd = 'pvesh {0}'.format(' '.join(filter(None, (cmd, url, translated_data))))
full_cmd = '{0}sh {1}'.format(self.platform, ' '.join(filter(None, (cmd, url, translated_data))))

stdout, stderr = self._exec(full_cmd)
match = lambda s: re.match('\d\d\d [a-zA-Z]', s)
Expand Down
14 changes: 9 additions & 5 deletions proxmoxer/backends/https.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, base_url, username, password, verify_ssl=False):
if response_data is None:
raise AuthenticationError("Couldn't authenticate user: {0} to {1}".format(username, base_url + "/access/ticket"))

self.pve_auth_cookie = response_data["ticket"]
self.auth_cookie = response_data["ticket"]
self.csrf_prevention_token = response_data["CSRFPreventionToken"]

def __call__(self, r):
Expand All @@ -60,7 +60,7 @@ class ProxmoxHTTPTokenAuth(ProxmoxHTTPAuth):
may be used instead of passing username/password.
"""
def __init__(self, auth_token, csrf_token):
self.pve_auth_cookie = auth_token
self.auth_cookie = auth_token
self.csrf_prevention_token = csrf_token


Expand Down Expand Up @@ -112,7 +112,10 @@ def request(self, method, url, params=None, data=None, headers=None, cookies=Non

class Backend(object):
def __init__(self, host, user, password, port=8006, verify_ssl=True,
mode='json', timeout=5, auth_token=None, csrf_token=None):
mode='json', timeout=5, auth_token=None, csrf_token=None,
platform='pve'
):
self.platform = platform
if ':' in host:
host, host_port = host.split(':')
port = host_port if host_port.isdigit() else port
Expand All @@ -131,7 +134,8 @@ def get_session(self):
session = ProxmoxHttpSession()
session.verify = self.verify_ssl
session.auth = self.auth
session.cookies = cookiejar_from_dict({"PVEAuthCookie": self.auth.pve_auth_cookie})
cookie_name = '{}AuthCookie'.format(self.platform.upper())
session.cookies = cookiejar_from_dict({cookie_name: self.auth.auth_cookie})
session.headers['Connection'] = 'keep-alive'
session.headers["accept"] = self.get_serializer().get_accept_types()
return session
Expand All @@ -145,4 +149,4 @@ def get_serializer(self):

def get_tokens(self):
"""Return the in-use auth and csrf tokens."""
return self.auth.pve_auth_cookie, self.auth.csrf_prevention_token
return self.auth.auth_cookie, self.auth.csrf_prevention_token
12 changes: 9 additions & 3 deletions proxmoxer/backends/openssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def __init__(self, host,
timeout=5,
forward_ssh_agent=False,
sudo=False,
identity_file=None):
identity_file=None,
platform='pve'):
super(ProxmoxOpenSSHSession, self).__init__(platform=platform)
self.host = host
self.username = username
self.configfile = configfile
Expand All @@ -47,11 +49,15 @@ def upload_file_obj(self, file_obj, remote_path):


class Backend(BaseBackend):
def __init__(self, host, user, configfile=None, port=22, timeout=5, forward_ssh_agent=False, sudo=False, identity_file=None):
def __init__(self, host, user, configfile=None,
port=22, timeout=5, forward_ssh_agent=False,
sudo=False, identity_file=None, platform='pve'):

self.session = ProxmoxOpenSSHSession(host, user,
configfile=configfile,
port=port,
timeout=timeout,
forward_ssh_agent=forward_ssh_agent,
sudo=sudo,
identity_file=identity_file)
identity_file=identity_file,
platform=platform)
13 changes: 10 additions & 3 deletions proxmoxer/backends/ssh_paramiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def __init__(self, host,
private_key_file=None,
port=22,
timeout=5,
sudo=False):
sudo=False,
platform='pve'):

super(ProxmoxParamikoSession, self).__init__(platform=platform)
self.host = host
self.username = username
self.password = password
Expand Down Expand Up @@ -67,12 +70,16 @@ def upload_file_obj(self, file_obj, remote_path):


class Backend(BaseBackend):
def __init__(self, host, user, password=None, private_key_file=None, port=22, timeout=5, sudo=False):
def __init__(self, host, user, password=None,
private_key_file=None, port=22, timeout=5,
sudo=False, platform='pve'):

self.session = ProxmoxParamikoSession(host, user,
password=password,
private_key_file=private_key_file,
port=port,
timeout=timeout,
sudo=sudo)
sudo=sudo,
platform=platform)


6 changes: 6 additions & 0 deletions proxmoxer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ def get_tokens(self):
return None, None

return self._backend.get_tokens()

class ProxmoxMGAPI(ProxmoxAPI):

def __init__(self, host, *args, **kwargs):
kwargs['platform'] = 'pmg'
super(ProxmoxMGAPI, self).__init__(host, *args, **kwargs)