Merge "Flake8 update - library/glance"
This commit is contained in:
commit
3eae97a77d
@ -13,7 +13,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
DOCUMENTATION='''
|
DOCUMENTATION = """
|
||||||
---
|
---
|
||||||
module: glance
|
module: glance
|
||||||
short_description:
|
short_description:
|
||||||
@ -56,16 +56,16 @@ options:
|
|||||||
- 1 (default)
|
- 1 (default)
|
||||||
- 2
|
- 2
|
||||||
author: Hugh Saunders
|
author: Hugh Saunders
|
||||||
'''
|
"""
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = """
|
||||||
# Create an image
|
# Create an image
|
||||||
- name: Ensure cirros image
|
- name: Ensure cirros image
|
||||||
glance:
|
glance:
|
||||||
command: 'image-create'
|
command: 'image-create'
|
||||||
openrc_path: /root/openrc
|
openrc_path: /root/openrc
|
||||||
image_name: cirros
|
image_name: cirros
|
||||||
image_url: 'https://launchpad.net/cirros/trunk/0.3.2/+download/cirros-0.3.2-source.tar.gz'
|
image_url: 'https://example-domain.com/cirros-0.3.2-source.tar.gz'
|
||||||
image_container_format: bare
|
image_container_format: bare
|
||||||
image_disk_format: qcow2
|
image_disk_format: qcow2
|
||||||
image_is_public: True
|
image_is_public: True
|
||||||
@ -75,15 +75,17 @@ EXAMPLES = '''
|
|||||||
glance:
|
glance:
|
||||||
command: 'image-list'
|
command: 'image-list'
|
||||||
openrc_path: /root/openrc
|
openrc_path: /root/openrc
|
||||||
'''
|
"""
|
||||||
|
|
||||||
|
|
||||||
import re
|
|
||||||
import keystoneclient.v2_0.client as ksclient
|
|
||||||
import glanceclient.client as glclient
|
import glanceclient.client as glclient
|
||||||
|
import keystoneclient.v2_0.client as ksclient
|
||||||
|
|
||||||
|
|
||||||
COMMAND_MAP = {'image-list': 'list_images',
|
COMMAND_MAP = {'image-list': 'list_images',
|
||||||
'image-create': 'create_image'}
|
'image-create': 'create_image'}
|
||||||
|
|
||||||
|
|
||||||
class ManageGlance(object):
|
class ManageGlance(object):
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
self.state_change = False
|
self.state_change = False
|
||||||
@ -99,17 +101,18 @@ class ManageGlance(object):
|
|||||||
rc=2, msg=str(e))
|
rc=2, msg=str(e))
|
||||||
|
|
||||||
def _parse_openrc(self):
|
def _parse_openrc(self):
|
||||||
""" Get credentials from an openrc file """
|
"""Get credentials from an openrc file."""
|
||||||
openrc_path = self.module.params['openrc_path']
|
openrc_path = self.module.params['openrc_path']
|
||||||
line_re = re.compile('^export (?P<key>OS_\w*)=(?P<value>[^\n]*)')
|
line_re = re.compile('^export (?P<key>OS_\w*)=(?P<value>[^\n]*)')
|
||||||
with open(openrc_path) as openrc:
|
with open(openrc_path) as openrc:
|
||||||
matches = [line_re.match(l) for l in openrc]
|
matches = [line_re.match(l) for l in openrc]
|
||||||
return dict(
|
return dict(
|
||||||
(g.groupdict()['key'], g.groupdict()['value'])
|
(g.groupdict()['key'], g.groupdict()['value'])
|
||||||
for g in matches if g)
|
for g in matches if g
|
||||||
|
)
|
||||||
|
|
||||||
def _keystone_authenticate(self):
|
def _keystone_authenticate(self):
|
||||||
""" Authenticate with Keystone """
|
"""Authenticate with Keystone."""
|
||||||
openrc = self._parse_openrc()
|
openrc = self._parse_openrc()
|
||||||
self.keystone = ksclient.Client(username=openrc['OS_USERNAME'],
|
self.keystone = ksclient.Client(username=openrc['OS_USERNAME'],
|
||||||
password=openrc['OS_PASSWORD'],
|
password=openrc['OS_PASSWORD'],
|
||||||
@ -117,65 +120,83 @@ class ManageGlance(object):
|
|||||||
auth_url=openrc['OS_AUTH_URL'])
|
auth_url=openrc['OS_AUTH_URL'])
|
||||||
|
|
||||||
def _init_glance(self):
|
def _init_glance(self):
|
||||||
""" Create glance client object using token and url from keystone """
|
"""Create glance client object using token and url from keystone."""
|
||||||
openrc = self._parse_openrc()
|
openrc = self._parse_openrc()
|
||||||
p = self.module.params
|
p = self.module.params
|
||||||
v = p['api_version']
|
v = p['api_version']
|
||||||
ep = self.keystone.service_catalog.url_for(service_type='image',
|
ep = self.keystone.service_catalog.url_for(
|
||||||
endpoint_type=openrc['OS_ENDPOINT_TYPE'])
|
service_type='image',
|
||||||
|
endpoint_type=openrc['OS_ENDPOINT_TYPE']
|
||||||
|
)
|
||||||
|
|
||||||
self.glance = glclient.Client(endpoint='%s/v%s' % (ep, v),
|
self.glance = glclient.Client(
|
||||||
token=self.keystone.get_token(self.keystone.session))
|
endpoint='%s/v%s' % (ep, v),
|
||||||
|
token=self.keystone.get_token(self.keystone.session)
|
||||||
|
)
|
||||||
|
|
||||||
def route(self):
|
def route(self):
|
||||||
""" Run the command specified by the command parameter """
|
"""Run the command specified by the command parameter."""
|
||||||
getattr(self, COMMAND_MAP[self.module.params['command']])()
|
getattr(self, COMMAND_MAP[self.module.params['command']])()
|
||||||
|
|
||||||
def _get_image_facts(self):
|
def _get_image_facts(self):
|
||||||
""" Helper function to format image list as a dictionary """
|
"""Helper function to format image list as a dictionary."""
|
||||||
p = self.module.params
|
p = self.module.params
|
||||||
v = p['api_version']
|
v = p['api_version']
|
||||||
if v == '1':
|
if v == '1':
|
||||||
return dict((i.name, i.to_dict()) for i in self.glance.images.list())
|
return dict(
|
||||||
|
(i.name, i.to_dict()) for i in self.glance.images.list()
|
||||||
|
)
|
||||||
elif v == '2':
|
elif v == '2':
|
||||||
return dict((i.name, i) for i in self.glance.images.list())
|
return dict(
|
||||||
|
(i.name, i) for i in self.glance.images.list()
|
||||||
|
)
|
||||||
|
|
||||||
def list_images(self):
|
def list_images(self):
|
||||||
""" Get information about available glance images and return
|
"""Get information about available glance images.
|
||||||
as a fact dictionary glance_images
|
|
||||||
|
Returns as a fact dictionary glance_images
|
||||||
"""
|
"""
|
||||||
self.module.exit_json(
|
self.module.exit_json(
|
||||||
changed=self.state_change,
|
changed=self.state_change,
|
||||||
ansible_facts=dict(glance_images=self._get_image_facts()))
|
ansible_facts=dict(glance_images=self._get_image_facts()))
|
||||||
|
|
||||||
def create_image(self):
|
def create_image(self):
|
||||||
""" Create a glance image that references a remote url """
|
"""Create a glance image that references a remote url."""
|
||||||
p = self.module.params
|
p = self.module.params
|
||||||
v = p['api_version']
|
v = p['api_version']
|
||||||
name = p['image_name']
|
image_name = p['image_name']
|
||||||
image_opts=dict(name=name,
|
image_opts = dict(
|
||||||
|
name=image_name,
|
||||||
disk_format=p['image_disk_format'],
|
disk_format=p['image_disk_format'],
|
||||||
container_format=p['image_container_format'],
|
container_format=p['image_container_format'],
|
||||||
copy_from=p['image_url'])
|
copy_from=p['image_url']
|
||||||
|
)
|
||||||
if v == '1':
|
if v == '1':
|
||||||
image_opts['is_public'] = p['image_is_public']
|
image_opts['is_public'] = p['image_is_public']
|
||||||
elif v == '2':
|
elif v == '2':
|
||||||
if p['image_is_public'] == True:
|
if p['image_is_public']:
|
||||||
vis = 'public'
|
vis = 'public'
|
||||||
else:
|
else:
|
||||||
vis = 'private'
|
vis = 'private'
|
||||||
image_opts['visibility'] = vis
|
image_opts['visibility'] = vis
|
||||||
|
|
||||||
images = {i.name for i in self.glance.images.list()}
|
images = {i.name for i in self.glance.images.list()}
|
||||||
if name in images:
|
if image_name in images:
|
||||||
self.module.exit_json(changed=self.state_change,
|
self.module.exit_json(
|
||||||
ansible_facts=dict(glance_images=self._get_image_facts()))
|
changed=self.state_change,
|
||||||
|
ansible_facts=dict(
|
||||||
|
glance_images=self._get_image_facts()
|
||||||
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.glance.images.create(**image_opts)
|
self.glance.images.create(**image_opts)
|
||||||
self.state_change = True
|
self.state_change = True
|
||||||
self.module.exit_json(
|
self.module.exit_json(
|
||||||
changed=self.state_change,
|
changed=self.state_change,
|
||||||
ansible_facts=dict(glance_images=self._get_image_facts()))
|
ansible_facts=dict(
|
||||||
|
glance_images=self._get_image_facts()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Loading…
Reference in New Issue
Block a user