1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

fix pep8 for cloudscale_server (#30974)

This commit is contained in:
René Moser 2017-09-27 17:39:02 +02:00 committed by ansibot
parent 56fe9499d1
commit b444332412
2 changed files with 39 additions and 66 deletions

View file

@ -36,70 +36,56 @@ options:
state: state:
description: description:
- State of the server - State of the server
required: False
default: running default: running
choices: ['running', 'stopped', 'absent'] choices: [ running, stopped, absent ]
name: name:
description: description:
- Name of the Server - Name of the Server.
- Either C(name) or C(uuid) are required. These options are mutually exclusive. - Either C(name) or C(uuid) are required. These options are mutually exclusive.
required: False
uuid: uuid:
description: description:
- UUID of the server - UUID of the server.
- Either C(name) or C(uuid) are required. These options are mutually exclusive. - Either C(name) or C(uuid) are required. These options are mutually exclusive.
required: False
flavor: flavor:
description: description:
- Flavor of the server - Flavor of the server.
required: False
image: image:
description: description:
- Image used to create the server - Image used to create the server.
required: False
volume_size_gb: volume_size_gb:
description: description:
- Size of the root volume in GB - Size of the root volume in GB.
required: False
default: 10 default: 10
bulk_volume_size_gb: bulk_volume_size_gb:
description: description:
- Size of the bulk storage volume in GB - Size of the bulk storage volume in GB.
required: False - No bulk storage volume if not set.
default: null (no bulk storage volume)
ssh_keys: ssh_keys:
description: description:
- List of SSH public keys - List of SSH public keys.
- Use the full content of your .pub file here. - Use the full content of your .pub file here.
required: False
use_public_network: use_public_network:
description: description:
- Attach a public network interface to the server - Attach a public network interface to the server.
required: False
default: True default: True
use_private_network: use_private_network:
description: description:
- Attach a private network interface to the server - Attach a private network interface to the server.
required: False
default: False default: False
use_ipv6: use_ipv6:
description: description:
- Enable IPv6 on the public network interface - Enable IPv6 on the public network interface.
required: False
default: True default: True
anti_affinity_with: anti_affinity_with:
description: description:
- UUID of another server to create an anti-affinity group with - UUID of another server to create an anti-affinity group with.
required: False
user_data: user_data:
description: description:
- Cloud-init configuration (cloud-config) data to use for the server. - Cloud-init configuration (cloud-config) data to use for the server.
required: False
api_token: api_token:
description: description:
- cloudscale.ch API token. - cloudscale.ch API token.
- This can also be passed in the CLOUDSCALE_API_TOKEN environment variable. - This can also be passed in the CLOUDSCALE_API_TOKEN environment variable.
required: False
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -221,7 +207,7 @@ from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
API_URL = 'https://api.cloudscale.ch/v1/' API_URL = 'https://api.cloudscale.ch/v1/'
TIMEOUT_WAIT = 30 TIMEOUT_WAIT = 30
ALLOWED_STATES = ('running', ALLOWED_STATES = ('running',
'stopped', 'stopped',
@ -248,7 +234,7 @@ class AnsibleCloudscaleServer(object):
if uuid: if uuid:
# Look for server by UUID if given # Look for server by UUID if given
if s['uuid'] == uuid: if s['uuid'] == uuid:
self.info = self._transform_state(s) self.info = self._transform_state(s)
break break
else: else:
# Look for server by name # Look for server by name
@ -261,9 +247,8 @@ class AnsibleCloudscaleServer(object):
self._module.fail_json(msg="More than one server with name '%s' exists. " self._module.fail_json(msg="More than one server with name '%s' exists. "
"Use the 'uuid' parameter to identify the server" % name) "Use the 'uuid' parameter to identify the server" % name)
def _get(self, api_call): def _get(self, api_call):
resp, info = fetch_url(self._module, API_URL+api_call, headers=self._auth_header) resp, info = fetch_url(self._module, API_URL + api_call, headers=self._auth_header)
if info['status'] == 200: if info['status'] == 200:
return json.loads(resp.read()) return json.loads(resp.read())
@ -271,14 +256,13 @@ class AnsibleCloudscaleServer(object):
self._module.fail_json(msg='Failure while calling the cloudscale.ch API with GET for ' self._module.fail_json(msg='Failure while calling the cloudscale.ch API with GET for '
'"%s": %s' % (api_call, info['body'])) '"%s": %s' % (api_call, info['body']))
def _post(self, api_call, data=None): def _post(self, api_call, data=None):
if data is not None: if data is not None:
data = urlencode(data) data = urlencode(data)
resp, info = fetch_url(self._module, resp, info = fetch_url(self._module,
API_URL+api_call, API_URL + api_call,
headers = self._auth_header, headers=self._auth_header,
method='POST', method='POST',
data=data) data=data)
@ -290,11 +274,10 @@ class AnsibleCloudscaleServer(object):
self._module.fail_json(msg='Failure while calling the cloudscale.ch API with POST for ' self._module.fail_json(msg='Failure while calling the cloudscale.ch API with POST for '
'"%s": %s' % (api_call, info['body'])) '"%s": %s' % (api_call, info['body']))
def _delete(self, api_call): def _delete(self, api_call):
resp, info = fetch_url(self._module, resp, info = fetch_url(self._module,
API_URL+api_call, API_URL + api_call,
headers = self._auth_header, headers=self._auth_header,
method='DELETE') method='DELETE')
if info['status'] == 204: if info['status'] == 204:
@ -303,7 +286,6 @@ class AnsibleCloudscaleServer(object):
self._module.fail_json(msg='Failure while calling the cloudscale.ch API with DELETE for ' self._module.fail_json(msg='Failure while calling the cloudscale.ch API with DELETE for '
'"%s": %s' % (api_call, info['body'])) '"%s": %s' % (api_call, info['body']))
@staticmethod @staticmethod
def _transform_state(server): def _transform_state(server):
if 'status' in server: if 'status' in server:
@ -313,16 +295,15 @@ class AnsibleCloudscaleServer(object):
server['state'] = 'absent' server['state'] = 'absent'
return server return server
def update_info(self): def update_info(self):
# If we don't have a UUID (yet) there is nothing to update # If we don't have a UUID (yet) there is nothing to update
if not 'uuid' in self.info: if 'uuid' not in self.info:
return return
# Can't use _get here because we want to handle 404 # Can't use _get here because we want to handle 404
resp, info = fetch_url(self._module, resp, info = fetch_url(self._module,
API_URL+'servers/'+self.info['uuid'], API_URL + 'servers/' + self.info['uuid'],
headers=self._auth_header) headers=self._auth_header)
if info['status'] == 200: if info['status'] == 200:
self.info = self._transform_state(json.loads(resp.read())) self.info = self._transform_state(json.loads(resp.read()))
@ -334,7 +315,6 @@ class AnsibleCloudscaleServer(object):
self._module.fail_json(msg='Failure while calling the cloudscale.ch API for ' self._module.fail_json(msg='Failure while calling the cloudscale.ch API for '
'update_info: %s' % info['body']) 'update_info: %s' % info['body'])
def wait_for_state(self, states): def wait_for_state(self, states):
start = datetime.now() start = datetime.now()
while datetime.now() - start < timedelta(seconds=TIMEOUT_WAIT): while datetime.now() - start < timedelta(seconds=TIMEOUT_WAIT):
@ -346,14 +326,13 @@ class AnsibleCloudscaleServer(object):
self._module.fail_json(msg='Timeout while waiting for a state change on server %s to states %s. Current state is %s' self._module.fail_json(msg='Timeout while waiting for a state change on server %s to states %s. Current state is %s'
% (self.info['name'], states, self.info['state'])) % (self.info['name'], states, self.info['state']))
def create_server(self): def create_server(self):
data = self._module.params.copy() data = self._module.params.copy()
# check for required parameters to create a server # check for required parameters to create a server
missing_parameters = [] missing_parameters = []
for p in ('name', 'ssh_keys', 'image', 'flavor'): for p in ('name', 'ssh_keys', 'image', 'flavor'):
if not p in data or not data[p]: if p not in data or not data[p]:
missing_parameters.append(p) missing_parameters.append(p)
if len(missing_parameters) > 0: if len(missing_parameters) > 0:
@ -361,7 +340,7 @@ class AnsibleCloudscaleServer(object):
' '.join(missing_parameters)) ' '.join(missing_parameters))
# Sanitize data dictionary # Sanitize data dictionary
for k,v in data.items(): for k, v in data.items():
# Remove items not relevant to the create server call # Remove items not relevant to the create server call
if k in ('api_token', 'uuid', 'state'): if k in ('api_token', 'uuid', 'state'):
@ -376,44 +355,39 @@ class AnsibleCloudscaleServer(object):
self.info = self._transform_state(self._post('servers', data)) self.info = self._transform_state(self._post('servers', data))
self.wait_for_state(('running', )) self.wait_for_state(('running', ))
def delete_server(self): def delete_server(self):
self._delete('servers/%s' % self.info['uuid']) self._delete('servers/%s' % self.info['uuid'])
self.wait_for_state(('absent', )) self.wait_for_state(('absent', ))
def start_server(self): def start_server(self):
self._post('servers/%s/start' % self.info['uuid']) self._post('servers/%s/start' % self.info['uuid'])
self.wait_for_state(('running', )) self.wait_for_state(('running', ))
def stop_server(self): def stop_server(self):
self._post('servers/%s/stop' % self.info['uuid']) self._post('servers/%s/stop' % self.info['uuid'])
self.wait_for_state(('stopped', )) self.wait_for_state(('stopped', ))
def list_servers(self): def list_servers(self):
return self._get('servers') return self._get('servers')
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
state = dict(default='running', state=dict(default='running', choices=ALLOWED_STATES),
choices=ALLOWED_STATES), name=dict(),
name = dict(), uuid=dict(),
uuid = dict(), flavor=dict(),
flavor = dict(), image=dict(),
image = dict(), volume_size_gb=dict(type='int', default=10),
volume_size_gb = dict(type='int', default=10), bulk_volume_size_gb=dict(type='int'),
bulk_volume_size_gb = dict(type='int'), ssh_keys=dict(type='list'),
ssh_keys = dict(type='list'), use_public_network=dict(type='bool', default=True),
use_public_network = dict(type='bool', default=True), use_private_network=dict(type='bool', default=False),
use_private_network = dict(type='bool', default=False), use_ipv6=dict(type='bool', default=True),
use_ipv6 = dict(type='bool', default=True), anti_affinity_with=dict(),
anti_affinity_with = dict(), user_data=dict(),
user_data = dict(), api_token=dict(no_log=True),
api_token = dict(no_log=True),
), ),
required_one_of=(('name', 'uuid'),), required_one_of=(('name', 'uuid'),),
mutually_exclusive=(('name', 'uuid'),), mutually_exclusive=(('name', 'uuid'),),

View file

@ -70,7 +70,6 @@ lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork.py lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork.py
lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_facts.py lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_facts.py
lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py
lib/ansible/modules/cloud/cloudscale/cloudscale_server.py
lib/ansible/modules/cloud/cloudstack/cs_instance.py lib/ansible/modules/cloud/cloudstack/cs_instance.py
lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py
lib/ansible/modules/cloud/cloudstack/_cs_nic.py lib/ansible/modules/cloud/cloudstack/_cs_nic.py