PEP8 compliance

I seriously hope I didn't fuck something up.
This commit is contained in:
Niccolò Maggioni 2017-03-17 12:26:46 +01:00
parent 9c9c2f51ab
commit 90d75824b7
No known key found for this signature in database
GPG Key ID: 4874B0C841E33264
1 changed files with 27 additions and 14 deletions

View File

@ -26,6 +26,7 @@
# { "groups": ["utility", "databases"], "a": false, "b": true } # { "groups": ["utility", "databases"], "a": false, "b": true }
import urllib import urllib
try: try:
import json import json
except ImportError: except ImportError:
@ -38,10 +39,12 @@ from six import iteritems
from ansible.module_utils.urls import open_url from ansible.module_utils.urls import open_url
class ProxmoxNodeList(list): class ProxmoxNodeList(list):
def get_names(self): def get_names(self):
return [node['node'] for node in self] return [node['node'] for node in self]
class ProxmoxVM(dict): class ProxmoxVM(dict):
def get_variables(self): def get_variables(self):
variables = {} variables = {}
@ -49,6 +52,7 @@ class ProxmoxVM(dict):
variables['proxmox_' + key] = value variables['proxmox_' + key] = value
return variables return variables
class ProxmoxVMList(list): class ProxmoxVMList(list):
def __init__(self, data=[], pxmxver=0.0): def __init__(self, data=[], pxmxver=0.0):
self.ver = pxmxver self.ver = pxmxver
@ -72,18 +76,22 @@ class ProxmoxVMList(list):
return variables return variables
class ProxmoxPoolList(list): class ProxmoxPoolList(list):
def get_names(self): def get_names(self):
return [pool['poolid'] for pool in self] return [pool['poolid'] for pool in self]
class ProxmoxVersion(dict): class ProxmoxVersion(dict):
def get_version(self): def get_version(self):
return float(self['version']) return float(self['version'])
class ProxmoxPool(dict): class ProxmoxPool(dict):
def get_members_name(self): def get_members_name(self):
return [member['name'] for member in self['members'] if member['template'] != 1] return [member['name'] for member in self['members'] if member['template'] != 1]
class ProxmoxAPI(object): class ProxmoxAPI(object):
def __init__(self, options, config_path): def __init__(self, options, config_path):
self.options = options self.options = options
@ -112,9 +120,11 @@ class ProxmoxAPI(object):
if not options.url: if not options.url:
raise Exception('Missing mandatory parameter --url (or PROXMOX_URL or "url" key in config file).') raise Exception('Missing mandatory parameter --url (or PROXMOX_URL or "url" key in config file).')
elif not options.username: elif not options.username:
raise Exception('Missing mandatory parameter --username (or PROXMOX_USERNAME or "username" key in config file).') raise Exception(
'Missing mandatory parameter --username (or PROXMOX_USERNAME or "username" key in config file).')
elif not options.password: elif not options.password:
raise Exception('Missing mandatory parameter --password (or PROXMOX_PASSWORD or "password" key in config file).') raise Exception(
'Missing mandatory parameter --password (or PROXMOX_PASSWORD or "password" key in config file).')
def auth(self): def auth(self):
request_path = '{}api2/json/access/ticket'.format(self.options.url) request_path = '{}api2/json/access/ticket'.format(self.options.url)
@ -124,9 +134,8 @@ class ProxmoxAPI(object):
'password': self.options.password, 'password': self.options.password,
}) })
data = json.load(open_url(request_path, data=request_params, data = json.load(open_url(request_path, data=request_params,
validate_certs=self.options.validate)) validate_certs=self.options.validate))
self.credentials = { self.credentials = {
'ticket': data['data']['ticket'], 'ticket': data['data']['ticket'],
@ -138,7 +147,7 @@ class ProxmoxAPI(object):
headers = {'Cookie': 'PVEAuthCookie={}'.format(self.credentials['ticket'])} headers = {'Cookie': 'PVEAuthCookie={}'.format(self.credentials['ticket'])}
request = open_url(request_path, data=data, headers=headers, request = open_url(request_path, data=data, headers=headers,
validate_certs=self.options.validate) validate_certs=self.options.validate)
response = json.load(request) response = json.load(request)
return response['data'] return response['data']
@ -179,6 +188,7 @@ class ProxmoxAPI(object):
def version(self): def version(self):
return ProxmoxVersion(self.get('api2/json/version')) return ProxmoxVersion(self.get('api2/json/version'))
def main_list(options, config_path): def main_list(options, config_path):
results = { results = {
'all': { 'all': {
@ -205,15 +215,15 @@ def main_list(options, config_path):
results['all']['hosts'] += openvz_list.get_names() results['all']['hosts'] += openvz_list.get_names()
results['_meta']['hostvars'].update(openvz_list.get_variables()) results['_meta']['hostvars'].update(openvz_list.get_variables())
# Merge QEMU and Containers lists from this node # Merge QEMU and Containers lists from this node
node_hostvars = qemu_list.get_variables().copy() node_hostvars = qemu_list.get_variables().copy()
if proxmox_api.version().get_version() >= 4.0: if proxmox_api.version().get_version() >= 4.0:
node_hostvars.update(lxc_list.get_variables()) node_hostvars.update(lxc_list.get_variables())
else: else:
node_hostvars.update(openvz_list.get_variables()) node_hostvars.update(openvz_list.get_variables())
# Check only VM/containers from the current node # Check only VM/containers from the current node
for vm in node_hostvars: for vm in node_hostvars:
vmid = results['_meta']['hostvars'][vm]['proxmox_vmid'] vmid = results['_meta']['hostvars'][vm]['proxmox_vmid']
try: try:
type = results['_meta']['hostvars'][vm]['proxmox_type'] type = results['_meta']['hostvars'][vm]['proxmox_type']
@ -242,15 +252,15 @@ def main_list(options, config_path):
} }
results[group]['hosts'] += [vm] results[group]['hosts'] += [vm]
# Create group 'running' # Create group 'running'
# so you can: --limit 'running' # so you can: --limit 'running'
status = results['_meta']['hostvars'][vm]['proxmox_status'] status = results['_meta']['hostvars'][vm]['proxmox_status']
if status == 'running': if status == 'running':
if 'running' not in results: if 'running' not in results:
results['running'] = { results['running'] = {
'hosts': [] 'hosts': []
} }
results['running']['hosts'] += [vm] results['running']['hosts'] += [vm]
results['_meta']['hostvars'][vm].update(metadata) results['_meta']['hostvars'][vm].update(metadata)
@ -262,6 +272,7 @@ def main_list(options, config_path):
return results return results
def main_host(options, config_path): def main_host(options, config_path):
proxmox_api = ProxmoxAPI(options, config_path) proxmox_api = ProxmoxAPI(options, config_path)
proxmox_api.auth() proxmox_api.auth()
@ -274,6 +285,7 @@ def main_host(options, config_path):
return {} return {}
def main(): def main():
config_path = os.path.join( config_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), os.path.dirname(os.path.abspath(__file__)),
@ -315,5 +327,6 @@ def main():
print(json.dumps(data, indent=indent)) print(json.dumps(data, indent=indent))
if __name__ == '__main__': if __name__ == '__main__':
main() main()