[cloud] Added account selection to PubNub BLOCKS (#23160)
* . switched from 'user.id' to 'account.id' in REST API calls. + added ability to select desired account (by name or identifier) from list of accounts to which authorized user have access. * + added account option addition version. * Remove pubnub_blocks from PEP8-legacy list
This commit is contained in:
parent
15bd7e48da
commit
9a43603761
2 changed files with 84 additions and 20 deletions
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
#
|
#
|
||||||
# PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks
|
# PubNub Real-time Cloud-Hosted Push API and Push Notification Client
|
||||||
|
# Frameworks
|
||||||
# Copyright (C) 2016 PubNub Inc.
|
# Copyright (C) 2016 PubNub Inc.
|
||||||
# http://www.pubnub.com/
|
# http://www.pubnub.com/
|
||||||
# http://www.pubnub.com/terms
|
# http://www.pubnub.com/terms
|
||||||
|
@ -24,7 +25,6 @@ ANSIBLE_METADATA = {'metadata_version': '1.0',
|
||||||
'supported_by': 'community'}
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: pubnub_blocks
|
module: pubnub_blocks
|
||||||
|
@ -61,6 +61,13 @@ options:
|
||||||
gathered artifacts and pass them to this parameter.
|
gathered artifacts and pass them to this parameter.
|
||||||
required: false
|
required: false
|
||||||
default: {}
|
default: {}
|
||||||
|
account:
|
||||||
|
description:
|
||||||
|
- "Name of PubNub account for from which C(application) will be used to
|
||||||
|
manage blocks."
|
||||||
|
- "User\'s account will be used if value not set or empty."
|
||||||
|
required: false
|
||||||
|
version_added: '2.4'
|
||||||
application:
|
application:
|
||||||
description:
|
description:
|
||||||
- "Name of target PubNub application for which blocks configuration on
|
- "Name of target PubNub application for which blocks configuration on
|
||||||
|
@ -226,37 +233,47 @@ import os
|
||||||
|
|
||||||
# Import module snippets
|
# Import module snippets
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
# noinspection PyProtectedMember
|
||||||
from ansible.module_utils._text import *
|
from ansible.module_utils._text import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Import PubNub BLOCKS client.
|
# Import PubNub BLOCKS client.
|
||||||
from pubnub_blocks_client import Account, Application, Keyset, Block
|
from pubnub_blocks_client import User, Account, Owner, Application, Keyset
|
||||||
from pubnub_blocks_client import EventHandler
|
from pubnub_blocks_client import Block, EventHandler
|
||||||
import pubnub_blocks_client.exceptions as exceptions
|
import pubnub_blocks_client.exceptions as exceptions
|
||||||
HAS_PUBNUB_BLOCKS_CLIENT = True
|
HAS_PUBNUB_BLOCKS_CLIENT = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_PUBNUB_BLOCKS_CLIENT = False
|
HAS_PUBNUB_BLOCKS_CLIENT = False
|
||||||
|
User = None
|
||||||
|
Account = None
|
||||||
|
Owner = None
|
||||||
|
Application = None
|
||||||
|
Keyset = None
|
||||||
|
Block = None
|
||||||
|
EventHandler = None
|
||||||
|
exceptions = None
|
||||||
|
|
||||||
|
|
||||||
def pubnub_account(module):
|
def pubnub_user(module):
|
||||||
"""Create and configure account if it is possible.
|
"""Create and configure user model if it possible.
|
||||||
|
|
||||||
:type module: AnsibleModule
|
:type module: AnsibleModule
|
||||||
:param module: Reference on module which contain module launch
|
:param module: Reference on module which contain module launch
|
||||||
information and status report methods.
|
information and status report methods.
|
||||||
|
|
||||||
:rtype: Account
|
:rtype: User
|
||||||
:return: Reference on initialized and ready to use account or 'None' in
|
:return: Reference on initialized and ready to use user or 'None' in
|
||||||
case if not all required information has been passed to block.
|
case if not all required information has been passed to block.
|
||||||
"""
|
"""
|
||||||
account = None
|
user = None
|
||||||
params = module.params
|
params = module.params
|
||||||
|
|
||||||
if params.get('cache') and params['cache'].get('module_cache'):
|
if params.get('cache') and params['cache'].get('module_cache'):
|
||||||
account = Account()
|
cache = params['cache']['module_cache']
|
||||||
account.restore(cache=copy.deepcopy(params['cache']['module_cache']))
|
user = User()
|
||||||
|
user.restore(cache=copy.deepcopy(cache['pnm_user']))
|
||||||
elif params.get('email') and params.get('password'):
|
elif params.get('email') and params.get('password'):
|
||||||
account = Account(email=params.get('email'),
|
user = User(email=params.get('email'), password=params.get('password'))
|
||||||
password=params.get('password'))
|
|
||||||
else:
|
else:
|
||||||
err_msg = 'It looks like not account credentials has been passed or ' \
|
err_msg = 'It looks like not account credentials has been passed or ' \
|
||||||
'\'cache\' field doesn\'t have result of previous module ' \
|
'\'cache\' field doesn\'t have result of previous module ' \
|
||||||
|
@ -264,6 +281,37 @@ def pubnub_account(module):
|
||||||
module.fail_json(msg='Missing account credentials.',
|
module.fail_json(msg='Missing account credentials.',
|
||||||
description=err_msg, changed=False)
|
description=err_msg, changed=False)
|
||||||
|
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
def pubnub_account(module, user):
|
||||||
|
"""Create and configure account if it is possible.
|
||||||
|
|
||||||
|
:type module: AnsibleModule
|
||||||
|
:param module: Reference on module which contain module launch
|
||||||
|
information and status report methods.
|
||||||
|
:type user: User
|
||||||
|
:param user: Reference on authorized user for which one of accounts
|
||||||
|
should be used during manipulations with block.
|
||||||
|
|
||||||
|
:rtype: Account
|
||||||
|
:return: Reference on initialized and ready to use account or 'None' in
|
||||||
|
case if not all required information has been passed to block.
|
||||||
|
"""
|
||||||
|
params = module.params
|
||||||
|
if params.get('account'):
|
||||||
|
account_name = params.get('account')
|
||||||
|
account = user.account(name=params.get('account'))
|
||||||
|
if account is None:
|
||||||
|
err_frmt = 'It looks like there is no \'{0}\' account for ' \
|
||||||
|
'authorized user. Please make sure what correct ' \
|
||||||
|
'name has been passed during module configuration.'
|
||||||
|
module.fail_json(msg='Missing account.',
|
||||||
|
description=err_frmt.format(account_name),
|
||||||
|
changed=False)
|
||||||
|
else:
|
||||||
|
account = user.accounts()[0]
|
||||||
|
|
||||||
return account
|
return account
|
||||||
|
|
||||||
|
|
||||||
|
@ -444,8 +492,16 @@ def _failure_title_from_exception(exception):
|
||||||
failure.
|
failure.
|
||||||
"""
|
"""
|
||||||
title = 'General REST API access error.'
|
title = 'General REST API access error.'
|
||||||
if exception.code == exceptions.PN_AUTHORIZATION_WRONG_CREDENTIALS:
|
if exception.code == exceptions.PN_AUTHORIZATION_MISSING_CREDENTIALS:
|
||||||
title = 'Authorization error (wrong credentials).'
|
title = 'Authorization error: missing credentials.'
|
||||||
|
elif exception.code == exceptions.PN_AUTHORIZATION_WRONG_CREDENTIALS:
|
||||||
|
title = 'Authorization error: wrong credentials.'
|
||||||
|
elif exception.code == exceptions.PN_USER_INSUFFICIENT_RIGHTS:
|
||||||
|
title = 'API access error: insufficient access rights.'
|
||||||
|
elif exception.code == exceptions.PN_API_ACCESS_TOKEN_EXPIRED:
|
||||||
|
title = 'API access error: time token expired.'
|
||||||
|
elif exception.code == exceptions.PN_KEYSET_BLOCK_EXISTS:
|
||||||
|
title = 'Block create did fail: block with same name already exists).'
|
||||||
elif exception.code == exceptions.PN_KEYSET_BLOCKS_FETCH_DID_FAIL:
|
elif exception.code == exceptions.PN_KEYSET_BLOCKS_FETCH_DID_FAIL:
|
||||||
title = 'Unable fetch list of blocks for keyset.'
|
title = 'Unable fetch list of blocks for keyset.'
|
||||||
elif exception.code == exceptions.PN_BLOCK_CREATE_DID_FAIL:
|
elif exception.code == exceptions.PN_BLOCK_CREATE_DID_FAIL:
|
||||||
|
@ -458,6 +514,8 @@ def _failure_title_from_exception(exception):
|
||||||
title = 'Block start/stop did fail.'
|
title = 'Block start/stop did fail.'
|
||||||
elif exception.code == exceptions.PN_EVENT_HANDLER_MISSING_FIELDS:
|
elif exception.code == exceptions.PN_EVENT_HANDLER_MISSING_FIELDS:
|
||||||
title = 'Event handler creation did fail: missing fields.'
|
title = 'Event handler creation did fail: missing fields.'
|
||||||
|
elif exception.code == exceptions.PN_BLOCK_EVENT_HANDLER_EXISTS:
|
||||||
|
title = 'Event handler creation did fail: missing fields.'
|
||||||
elif exception.code == exceptions.PN_EVENT_HANDLER_CREATE_DID_FAIL:
|
elif exception.code == exceptions.PN_EVENT_HANDLER_CREATE_DID_FAIL:
|
||||||
title = 'Event handler creation did fail.'
|
title = 'Event handler creation did fail.'
|
||||||
elif exception.code == exceptions.PN_EVENT_HANDLER_UPDATE_DID_FAIL:
|
elif exception.code == exceptions.PN_EVENT_HANDLER_UPDATE_DID_FAIL:
|
||||||
|
@ -493,6 +551,7 @@ def main():
|
||||||
fields = dict(
|
fields = dict(
|
||||||
email=dict(default='', required=False, type='str'),
|
email=dict(default='', required=False, type='str'),
|
||||||
password=dict(default='', required=False, type='str', no_log=True),
|
password=dict(default='', required=False, type='str', no_log=True),
|
||||||
|
account=dict(default='', required=False, type='str'),
|
||||||
application=dict(required=True, type='str'),
|
application=dict(required=True, type='str'),
|
||||||
keyset=dict(required=True, type='str'),
|
keyset=dict(required=True, type='str'),
|
||||||
state=dict(default='present', type='str',
|
state=dict(default='present', type='str',
|
||||||
|
@ -509,8 +568,10 @@ def main():
|
||||||
|
|
||||||
params = module.params
|
params = module.params
|
||||||
|
|
||||||
|
# Authorize user.
|
||||||
|
user = pubnub_user(module)
|
||||||
# Initialize PubNub account instance.
|
# Initialize PubNub account instance.
|
||||||
account = pubnub_account(module)
|
account = pubnub_account(module, user=user)
|
||||||
# Try fetch application with which module should work.
|
# Try fetch application with which module should work.
|
||||||
application = pubnub_application(module, account=account)
|
application = pubnub_application(module, account=account)
|
||||||
# Try fetch keyset with which module should work.
|
# Try fetch keyset with which module should work.
|
||||||
|
@ -548,18 +609,22 @@ def main():
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
try:
|
try:
|
||||||
account.save()
|
account.save()
|
||||||
except (exceptions.AccountError, exceptions.KeysetError,
|
except (exceptions.APIAccessError, exceptions.KeysetError,
|
||||||
exceptions.BlockError, exceptions.EventHandlerError,
|
exceptions.BlockError, exceptions.EventHandlerError,
|
||||||
exceptions.GeneralPubNubError) as exc:
|
exceptions.GeneralPubNubError) as exc:
|
||||||
|
module_cache = dict(account)
|
||||||
|
module_cache.update(dict(pnm_user=dict(user)))
|
||||||
exc_msg = _failure_title_from_exception(exc)
|
exc_msg = _failure_title_from_exception(exc)
|
||||||
exc_descr = exc.message if hasattr(exc, 'message') else exc.args[0]
|
exc_descr = exc.message if hasattr(exc, 'message') else exc.args[0]
|
||||||
module.fail_json(msg=exc_msg, description=exc_descr,
|
module.fail_json(msg=exc_msg, description=exc_descr,
|
||||||
changed=account.changed,
|
changed=account.changed,
|
||||||
module_cache=dict(account))
|
module_cache=module_cache)
|
||||||
|
|
||||||
# Report module execution results.
|
# Report module execution results.
|
||||||
|
module_cache = dict(account)
|
||||||
|
module_cache.update(dict(pnm_user=dict(user)))
|
||||||
changed_will_change = account.changed or account.will_change
|
changed_will_change = account.changed or account.will_change
|
||||||
module.exit_json(changed=changed_will_change, module_cache=dict(account))
|
module.exit_json(changed=changed_will_change, module_cache=module_cache)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -344,7 +344,6 @@ lib/ansible/modules/cloud/profitbricks/profitbricks_datacenter.py
|
||||||
lib/ansible/modules/cloud/profitbricks/profitbricks_nic.py
|
lib/ansible/modules/cloud/profitbricks/profitbricks_nic.py
|
||||||
lib/ansible/modules/cloud/profitbricks/profitbricks_volume.py
|
lib/ansible/modules/cloud/profitbricks/profitbricks_volume.py
|
||||||
lib/ansible/modules/cloud/profitbricks/profitbricks_volume_attachments.py
|
lib/ansible/modules/cloud/profitbricks/profitbricks_volume_attachments.py
|
||||||
lib/ansible/modules/cloud/pubnub/pubnub_blocks.py
|
|
||||||
lib/ansible/modules/cloud/rackspace/rax_cbs_attachments.py
|
lib/ansible/modules/cloud/rackspace/rax_cbs_attachments.py
|
||||||
lib/ansible/modules/cloud/rackspace/rax_cdb.py
|
lib/ansible/modules/cloud/rackspace/rax_cdb.py
|
||||||
lib/ansible/modules/cloud/rackspace/rax_clb.py
|
lib/ansible/modules/cloud/rackspace/rax_clb.py
|
||||||
|
|
Loading…
Reference in a new issue