本文整理汇总了Python中ansible.module_utils.pycompat24.get_exception方法的典型用法代码示例。如果您正苦于以下问题:Python pycompat24.get_exception方法的具体用法?Python pycompat24.get_exception怎么用?Python pycompat24.get_exception使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ansible.module_utils.pycompat24
的用法示例。
在下文中一共展示了pycompat24.get_exception方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _unsafe_writes
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def _unsafe_writes(self, src, dest, exception):
# sadly there are some situations where we cannot ensure atomicity, but only if
# the user insists and we get the appropriate error we update the file unsafely
if exception.errno == errno.EBUSY:
#TODO: issue warning that this is an unsafe operation, but doing it cause user insists
try:
try:
out_dest = open(dest, 'wb')
in_src = open(src, 'rb')
shutil.copyfileobj(in_src, out_dest)
finally: # assuring closed files in 2.4 compatible way
if out_dest:
out_dest.close()
if in_src:
in_src.close()
except (shutil.Error, OSError, IOError):
e = get_exception()
self.fail_json(msg='Could not write data to file (%s) from (%s): %s' % (dest, src, e))
else:
self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, exception))
示例2: main
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def main():
module = AnsibleModule(
argument_spec=dict(
state = dict(choices=['present', 'absent'], required=True),
command = dict(choices=['create', 'attach'], required=True),
api_token = dict(aliases=['API_TOKEN'], no_log=True),
block_size = dict(type='int'),
volume_name = dict(type='str', required=True),
description = dict(type='str'),
region = dict(type='str', required=True),
droplet_id = dict(type='int'),
timeout = dict(type='int', default=10),
),
)
try:
handle_request(module)
except DOBlockStorageException:
e = get_exception()
module.fail_json(msg=e.message)
except KeyError:
e = get_exception()
module.fail_json(msg='Unable to load %s' % e.message)
示例3: _get_url_data
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def _get_url_data(
self, url, what=None, msg_status=None, msg_exception=None,
**kwargs):
# Compose default messages
if msg_status is None:
msg_status = "Cannot get %s" % what
if msg_exception is None:
msg_exception = "Retrieval of %s failed." % what
# Get the URL data
try:
response, info = fetch_url(
self.module, url, timeout=self.timeout, **kwargs)
if info['status'] != 200:
self.module.fail_json(msg=msg_status, details=info['msg'])
except Exception:
e = get_exception()
self.module.fail_json(msg=msg_exception, details=e.message)
return response
示例4: get_hostref
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def get_hostref(module, ssid, name, api_url, user, pwd):
all_hosts = 'storage-systems/%s/hosts' % ssid
url = api_url + all_hosts
try:
rc, data = request(url, method='GET', headers=HEADERS, url_username=user, url_password=pwd)
except Exception:
err = get_exception()
module.fail_json(msg="Failed to get hosts. Id [%s]. Error [%s]." % (ssid, str(err)))
for host in data:
if host['name'] == name:
return host['hostRef']
else:
continue
module.fail_json(msg="No host with the name %s could be found" % name)
示例5: update_hostgroup
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def update_hostgroup(module, ssid, name, api_url, user, pwd, hosts=None, new_name=None):
gid = get_hostgroup_id(module, ssid, name, api_url, user, pwd)
groups = "storage-systems/%s/host-groups/%s" % (ssid, gid)
url = api_url + groups
hostrefs = []
if hosts:
for host in hosts:
href = get_hostref(module, ssid, host, api_url, user, pwd)
hostrefs.append(href)
if new_name:
post_data = json.dumps(dict(name=new_name, hosts=hostrefs))
else:
post_data = json.dumps(dict(hosts=hostrefs))
try:
rc, data = request(url, method='POST', data=post_data, headers=HEADERS, url_username=user, url_password=pwd)
except Exception:
err = get_exception()
module.fail_json(msg="Failed to update host group. Group [%s]. Id [%s]. Error [%s]." % (gid, ssid,
str(err)))
return rc, data
示例6: group_id
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def group_id(self):
if self.group:
try:
(rc, all_groups) = request(self.url + 'storage-systems/%s/host-groups' % self.ssid,
url_password=self.pwd,
url_username=self.user, validate_certs=self.certs, headers=HEADERS)
except:
err = get_exception()
self.module.fail_json(
msg="Failed to get host groups. Array Id [%s]. Error [%s]." % (self.ssid, str(err)))
try:
group_obj = filter(lambda group: group['name'] == self.group, all_groups)[0]
return group_obj['id']
except IndexError:
self.module.fail_json(msg="No group with the name: %s exists" % self.group)
else:
# Return the value equivalent of no group
return "0000000000000000000000000000000000000000"
示例7: reassign_ports
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def reassign_ports(self, apply=True):
if not self.post_body:
self.post_body = dict(
portsToUpdate=dict()
)
for port in self.ports:
if self.port_on_diff_host(port):
self.post_body['portsToUpdate'].update(dict(
portRef=self.other_host['hostPortRef'],
hostRef=self.host_obj['id'],
# Doesnt yet address port identifier or chap secret
))
if apply:
try:
(rc, self.host_obj) = request(
self.url + 'storage-systems/%s/hosts/%s' % (self.ssid, self.host_obj['id']),
url_username=self.user, url_password=self.pwd, headers=HEADERS,
validate_certs=self.certs, method='POST', data=json.dumps(self.post_body))
except:
err = get_exception()
self.module.fail_json(
msg="Failed to reassign host port. Host Id [%s]. Array Id [%s]. Error [%s]." % (
self.host_obj['id'], self.ssid, str(err)))
示例8: update_host
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def update_host(self):
if self.ports:
if self.hostports_available:
if self.force_port_update is True:
self.reassign_ports(apply=False)
# Make sure that only ports that arent being reassigned are passed into the ports attr
self.ports = [port for port in self.ports if not self.port_on_diff_host(port)]
self.post_body['ports'] = self.ports
if self.group:
self.post_body['groupId'] = self.group_id
self.post_body['hostType'] = dict(index=self.host_type_index)
try:
(rc, self.host_obj) = request(self.url + 'storage-systems/%s/hosts/%s' % (self.ssid, self.host_obj['id']),
url_username=self.user, url_password=self.pwd, headers=HEADERS,
validate_certs=self.certs, method='POST', data=json.dumps(self.post_body))
except:
err = get_exception()
self.module.fail_json(msg="Failed to update host. Array Id [%s]. Error [%s]." % (self.ssid, str(err)))
self.module.exit_json(changed=True, **self.host_obj)
示例9: get_storage_pool
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def get_storage_pool(self, storage_pool_name):
self.debug("fetching storage pools")
# map the storage pool name to its id
try:
(rc, resp) = request(self.api_url + "/storage-systems/%s/storage-pools" % (self.ssid),
headers=dict(Accept="application/json"), url_username=self.api_usr,
url_password=self.api_pwd, validate_certs=self.validate_certs)
except Exception:
err = get_exception()
self.module.fail_json(
msg="Failed to obtain list of storage pools. Array Id [%s]. Error[%s]." % (self.ssid, str(err)))
self.debug("searching for storage pool '%s'" % storage_pool_name)
pool_detail = next(ifilter(lambda a: a['name'] == storage_pool_name, resp), None)
if pool_detail:
self.debug('found')
else:
self.debug('not found')
return pool_detail
示例10: create_volume
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def create_volume(self, pool_id, name, size_unit, size, segment_size_kb, data_assurance_enabled):
volume_add_req = dict(
name=name,
poolId=pool_id,
sizeUnit=size_unit,
size=size,
segSize=segment_size_kb,
dataAssuranceEnabled=data_assurance_enabled,
)
self.debug("creating volume '%s'" % name)
try:
(rc, resp) = request(self.api_url + "/storage-systems/%s/volumes" % (self.ssid),
data=json.dumps(volume_add_req), headers=self._post_headers, method='POST',
url_username=self.api_usr, url_password=self.api_pwd,
validate_certs=self.validate_certs,
timeout=120)
except Exception:
err = get_exception()
self.module.fail_json(
msg="Failed to create volume. Volume [%s]. Array Id [%s]. Error[%s]." % (self.name, self.ssid,
str(err)))
示例11: create_thin_volume
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def create_thin_volume(self, pool_id, name, size_unit, size, thin_volume_repo_size,
thin_volume_max_repo_size, data_assurance_enabled):
thin_volume_add_req = dict(
name=name,
poolId=pool_id,
sizeUnit=size_unit,
virtualSize=size,
repositorySize=thin_volume_repo_size,
maximumRepositorySize=thin_volume_max_repo_size,
dataAssuranceEnabled=data_assurance_enabled,
)
self.debug("creating thin-volume '%s'" % name)
try:
(rc, resp) = request(self.api_url + "/storage-systems/%s/thin-volumes" % (self.ssid),
data=json.dumps(thin_volume_add_req), headers=self._post_headers, method='POST',
url_username=self.api_usr, url_password=self.api_pwd,
validate_certs=self.validate_certs,
timeout=120)
except Exception:
err = get_exception()
self.module.fail_json(
msg="Failed to create thin volume. Volume [%s]. Array Id [%s]. Error[%s]." % (self.name,
self.ssid,
str(err)))
示例12: update_volume_properties
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def update_volume_properties(self):
update_volume_req = dict()
# conditionally add values so we ignore unspecified props
if self.volume_ssdcache_setting_changed:
update_volume_req['flashCache'] = self.ssd_cache_enabled
self.debug("updating volume properties...")
try:
(rc, resp) = request(
self.api_url + "/storage-systems/%s/%s/%s/" % (self.ssid, self.volume_resource_name,
self.volume_detail['id']),
data=json.dumps(update_volume_req), headers=self._post_headers, method='POST',
url_username=self.api_usr, url_password=self.api_pwd, validate_certs=self.validate_certs,
timeout=120)
except Exception:
err = get_exception()
self.module.fail_json(
msg="Failed to update volume properties. Volume [%s]. Array Id [%s]. Error[%s]." % (self.name,
self.ssid,
str(err)))
示例13: sp_drives
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def sp_drives(self, exclude_hotspares=True):
if not self._sp_drives_cached:
self.debug("fetching drive list...")
try:
(rc, resp) = request(self.api_url + "/storage-systems/%s/drives" % (self.ssid), method='GET',
url_username=self.api_usr, url_password=self.api_pwd,
validate_certs=self.validate_certs)
except:
err = get_exception()
pool_id = self.pool_detail['id']
self.module.exit_json(
msg="Failed to fetch disk drives. Pool id [%s]. Array id [%s]. Error[%s]." % (pool_id, self.ssid, str(err)))
sp_id = self.pool_detail['id']
if exclude_hotspares:
self._sp_drives_cached = [d for d in resp if d['currentVolumeGroupRef'] == sp_id and not d['hotSpare']]
else:
self._sp_drives_cached = [d for d in resp if d['currentVolumeGroupRef'] == sp_id]
return self._sp_drives_cached
示例14: expand_storage_pool
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def expand_storage_pool(self):
drives_to_add = self.get_expansion_candidate_drives()
self.debug("adding %s drives to storage pool..." % len(drives_to_add))
sp_expand_req = dict(
drives=drives_to_add
)
try:
request(
self.api_url + "/storage-systems/%s/storage-pools/%s/expand" % (self.ssid,
self.pool_detail['id']),
data=json.dumps(sp_expand_req), headers=self.post_headers, method='POST', url_username=self.api_usr,
url_password=self.api_pwd, validate_certs=self.validate_certs, timeout=120)
except:
err = get_exception()
pool_id = self.pool_detail['id']
self.module.exit_json(
msg="Failed to add drives to storage pool. Pool id [%s]. Array id [%s]. Error[%s]." % (pool_id,
self.ssid,
str(
err)))
# TODO: check response
# TODO: support blocking wait?
示例15: reduce_drives
# 需要导入模块: from ansible.module_utils import pycompat24 [as 别名]
# 或者: from ansible.module_utils.pycompat24 import get_exception [as 别名]
def reduce_drives(self, drive_list):
if all(drive in drive_list for drive in self.sp_drives):
# all the drives passed in are present in the system
pass
else:
self.module.fail_json(
msg="One of the drives you wish to remove does not currently exist in the storage pool you specified")
try:
(rc, resp) = request(
self.api_url + "/storage-systems/%s/storage-pools/%s/reduction" % (self.ssid,
self.pool_detail['id']),
data=json.dumps(drive_list), headers=self.post_headers, method='POST', url_username=self.api_usr,
url_password=self.api_pwd, validate_certs=self.validate_certs, timeout=120)
except:
err = get_exception()
pool_id = self.pool_detail['id']
self.module.exit_json(
msg="Failed to remove drives from storage pool. Pool id [%s]. Array id [%s]. Error[%s]." % (
pool_id, self.ssid, str(err)))