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

stacki_host: PEP8 compliancy and documentation changes #32651

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 40 additions & 64 deletions lib/ansible/modules/remote_management/stacki/stacki_host.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2016, Hugh Ma <Hugh.Ma@flextronics.com>
# Copyright: (c) 2016, Hugh Ma <Hugh.Ma@flextronics.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}


DOCUMENTATION = '''
---
module: stacki_host
short_description: Add or remove host to stacki front-end
description:
- Use this module to add or remove hosts to a stacki front-end via API
- Use this module to add or remove hosts to a stacki front-end via API.
- U(https://github.com/StackIQ/stacki)
version_added: "2.3"
options:
Expand All @@ -43,21 +41,17 @@
prim_intf_mac:
description:
- MAC Address for the primary PXE boot network interface.
required: False
prim_intf_ip:
description:
- IP Address for the primary network interface.
required: False
prim_intf:
description:
- Name of the primary network interface.
required: False
force_install:
description:
- Set value to True to force node into install state if it already exists in stacki.
required: False

author: "Hugh Ma <Hugh.Ma@flextronics.com>"
author:
- Hugh Ma <Hugh.Ma@flextronics.com>
'''

EXAMPLES = '''
Expand Down Expand Up @@ -112,18 +106,18 @@ class StackiHost(object):

def __init__(self, module):
self.module = module
self.hostname = module.params['name']
self.rack = module.params['rack']
self.rank = module.params['rank']
self.appliance = module.params['appliance']
self.prim_intf = module.params['prim_intf']
self.prim_intf_ip = module.params['prim_intf_ip']
self.network = module.params['network']
self.prim_intf_mac = module.params['prim_intf_mac']
self.endpoint = module.params['stacki_endpoint']

auth_creds = {'USERNAME': module.params['stacki_user'],
'PASSWORD': module.params['stacki_password']}
self.hostname = module.params['name']
self.rack = module.params['rack']
self.rank = module.params['rank']
self.appliance = module.params['appliance']
self.prim_intf = module.params['prim_intf']
self.prim_intf_ip = module.params['prim_intf_ip']
self.network = module.params['network']
self.prim_intf_mac = module.params['prim_intf_mac']
self.endpoint = module.params['stacki_endpoint']

auth_creds = {'USERNAME': module.params['stacki_user'],
'PASSWORD': module.params['stacki_password']}

# Get Initial CSRF
cred_a = self.do_request(self.module, self.endpoint, method="GET")
Expand Down Expand Up @@ -161,7 +155,6 @@ def __init__(self, module):
'Content-type': 'application/json',
'Cookie': login_req.headers.get('Set-Cookie')}


def do_request(self, module, url, payload=None, headers=None, method=None):
res, info = fetch_url(module, url, data=payload, headers=headers, method=method)

Expand All @@ -170,36 +163,25 @@ def do_request(self, module, url, payload=None, headers=None, method=None):

return res


def stack_check_host(self):

res = self.do_request(self.module, self.endpoint, payload=json.dumps({"cmd": "list host"}),
headers=self.header, method="POST")
res = self.do_request(self.module, self.endpoint, payload=json.dumps({"cmd": "list host"}), headers=self.header, method="POST")

if self.hostname in res.read():
return True
else:
return False


def stack_sync(self):

self.do_request(self.module, self.endpoint, payload=json.dumps({ "cmd": "sync config"}),
headers=self.header, method="POST")

self.do_request(self.module, self.endpoint, payload=json.dumps({"cmd": "sync host config"}),
headers=self.header, method="POST")

self.do_request(self.module, self.endpoint, payload=json.dumps({"cmd": "sync config"}), headers=self.header, method="POST")
self.do_request(self.module, self.endpoint, payload=json.dumps({"cmd": "sync host config"}), headers=self.header, method="POST")

def stack_force_install(self, result):

data = dict()
changed = False

data['cmd'] = "set host boot {0} action=install" \
.format(self.hostname)
self.do_request(self.module, self.endpoint, payload=json.dumps(data),
headers=self.header, method="POST")
self.do_request(self.module, self.endpoint, payload=json.dumps(data), headers=self.header, method="POST")
changed = True

self.stack_sync()
Expand All @@ -208,29 +190,24 @@ def stack_force_install(self, result):
result['stdout'] = "api call successful".rstrip("\r\n")

def stack_add(self, result):

data = dict()
changed = False
data = dict()
changed = False

data['cmd'] = "add host {0} rack={1} rank={2} appliance={3}"\
.format(self.hostname, self.rack, self.rank, self.appliance)
self.do_request(self.module, self.endpoint, payload=json.dumps(data),
headers=self.header, method="POST")
self.do_request(self.module, self.endpoint, payload=json.dumps(data), headers=self.header, method="POST")

self.stack_sync()

result['changed'] = changed
result['stdout'] = "api call successful".rstrip("\r\n")


def stack_remove(self, result):

data = dict()
data = dict()

data['cmd'] = "remove host {0}"\
.format(self.hostname)
self.do_request(self.module, self.endpoint, payload=json.dumps(data),
headers=self.header, method="POST")
self.do_request(self.module, self.endpoint, payload=json.dumps(data), headers=self.header, method="POST")

self.stack_sync()

Expand All @@ -239,24 +216,23 @@ def stack_remove(self, result):


def main():

module = AnsibleModule(
argument_spec = dict(
state=dict(type='str', default='present', choices=['present', 'absent']),
name=dict(required=True, type='str'),
rack=dict(required=False, type='int', default=0),
rank=dict(required=False, type='int', default=0),
appliance=dict(required=False, type='str', default='backend'),
prim_intf=dict(required=False, type='str', default=None),
prim_intf_ip=dict(required=False, type='str', default=None),
network=dict(required=False, type='str', default='private'),
prim_intf_mac=dict(required=False, type='str', default=None),
stacki_user=dict(required=True, type='str', default=os.environ.get('stacki_user')),
stacki_password=dict(required=True, no_log=True, type='str', default=os.environ.get('stacki_password')),
stacki_endpoint=dict(required=True, type='str', default=os.environ.get('stacki_endpoint')),
force_install=dict(required=False, type='bool', default=False)
argument_spec=dict(
state=dict(type='str', default='present', choices=['absent', 'present']),
name=dict(type='str', required=True),
rack=dict(type='int', default=0),
rank=dict(type='int', default=0),
appliance=dict(type='str', default='backend'),
prim_intf=dict(type='str'),
prim_intf_ip=dict(type='str'),
network=dict(type='str', default='private'),
prim_intf_mac=dict(type='str'),
stacki_user=dict(type='str', required=True, default=os.environ.get('stacki_user')),
stacki_password=dict(type='str', required=True, default=os.environ.get('stacki_password'), no_log=True),
stacki_endpoint=dict(type='str', required=True, default=os.environ.get('stacki_endpoint')),
force_install=dict(type='bool', default=False),
),
supports_check_mode=False
supports_check_mode=False,
)

result = {'changed': False}
Expand Down
1 change: 0 additions & 1 deletion test/sanity/pep8/legacy-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ lib/ansible/modules/packaging/os/urpmi.py
lib/ansible/modules/packaging/os/yum.py
lib/ansible/modules/packaging/os/zypper.py
lib/ansible/modules/packaging/os/zypper_repository.py
lib/ansible/modules/remote_management/stacki/stacki_host.py
lib/ansible/modules/storage/infinidat/infini_export.py
lib/ansible/modules/storage/infinidat/infini_export_client.py
lib/ansible/modules/storage/infinidat/infini_fs.py
Expand Down