def main(): module = AnsibleModule(argument_spec=dict( view=dict(choices=['topics', 'subscriptions'], default='topics'), topic=dict(required=False), state=dict(choices=['list'], default='list'), service_account_email=dict(), credentials_file=dict(), project_id=dict(), ), ) if module._name in ('gcpubsub_facts', 'community.general.gcpubsub_facts'): module.deprecate( "The 'gcpubsub_facts' module has been renamed to 'gcpubsub_info'", version='3.0.0', collection_name='community.general') # was Ansible 2.13 if not HAS_PYTHON26: module.fail_json( msg="GCE module requires python's 'ast' module, python v2.6+") if not HAS_GOOGLE_CLOUD_PUBSUB: module.fail_json(msg="Please install google-cloud-pubsub library.") CLIENT_MINIMUM_VERSION = '0.22.0' if not check_min_pkg_version('google-cloud-pubsub', CLIENT_MINIMUM_VERSION): module.fail_json( msg="Please install google-cloud-pubsub library version %s" % CLIENT_MINIMUM_VERSION) mod_params = {} mod_params['state'] = module.params.get('state') mod_params['topic'] = module.params.get('topic') mod_params['view'] = module.params.get('view') creds, params = get_google_cloud_credentials(module) pubsub_client = pubsub.Client(project=params['project_id'], credentials=creds, use_gax=False) pubsub_client.user_agent = 'ansible-pubsub-0.1' json_output = {} if mod_params['view'] == 'topics': json_output['topics'] = list_func(pubsub_client.list_topics()) elif mod_params['view'] == 'subscriptions': if mod_params['topic']: t = pubsub_client.topic(mod_params['topic']) json_output['subscriptions'] = list_func(t.list_subscriptions()) else: json_output['subscriptions'] = list_func( pubsub_client.list_subscriptions()) json_output['changed'] = False json_output.update(mod_params) module.exit_json(**json_output)
def main(): module = AnsibleModule(argument_spec=dict( instance_id=dict(type='str', required=True), state=dict(type='str', default='present', choices=['absent', 'present']), database_name=dict(type='str'), configuration=dict(type='str', required=True), node_count=dict(type='int', default=1), instance_display_name=dict(type='str'), force_instance_delete=dict(type='bool', default=False), service_account_email=dict(type='str'), credentials_file=dict(type='str'), project_id=dict(type='str'), ), ) if not HAS_PYTHON26: module.fail_json( msg="GCE module requires python's 'ast' module, python v2.6+") if not HAS_GOOGLE_CLOUD_SPANNER: module.fail_json(msg="Please install google-cloud-spanner.") if not check_min_pkg_version(CLOUD_CLIENT, CLOUD_CLIENT_MINIMUM_VERSION): module.fail_json(msg="Please install %s client version %s" % (CLOUD_CLIENT, CLOUD_CLIENT_MINIMUM_VERSION)) mod_params = {} mod_params['state'] = module.params.get('state') mod_params['instance_id'] = module.params.get('instance_id') mod_params['database_name'] = module.params.get('database_name') mod_params['configuration'] = module.params.get('configuration') mod_params['node_count'] = module.params.get('node_count', None) mod_params['instance_display_name'] = module.params.get( 'instance_display_name') mod_params['force_instance_delete'] = module.params.get( 'force_instance_delete') creds, params = get_google_cloud_credentials(module) spanner_client = spanner.Client(project=params['project_id'], credentials=creds, user_agent=CLOUD_CLIENT_USER_AGENT) changed = False json_output = {} i = None if mod_params['instance_id']: config_name = get_spanner_configuration_name( mod_params['configuration'], params['project_id']) i = spanner_client.instance(mod_params['instance_id'], configuration_name=config_name) d = None if mod_params['database_name']: # TODO(supertom): support DDL ddl_statements = '' d = i.database(mod_params['database_name'], ddl_statements) if mod_params['state'] == 'absent': # Remove the most granular resource. If database is specified # we remove it. If only instance is specified, that is what is removed. if d is not None and d.exists(): d.drop() changed = True else: if i.exists(): if mod_params['force_instance_delete']: i.delete() else: module.fail_json(msg=(( "Cannot delete Spanner instance: " "'force_instance_delete' argument not specified"))) changed = True elif mod_params['state'] == 'present': if not i.exists(): i = spanner_client.instance( mod_params['instance_id'], configuration_name=config_name, display_name=mod_params['instance_display_name'], node_count=mod_params['node_count'] or 1) i.create() changed = True else: # update instance i.reload() inst_prev_vals = {} if i.display_name != mod_params['instance_display_name']: inst_prev_vals['instance_display_name'] = i.display_name i.display_name = mod_params['instance_display_name'] if mod_params['node_count']: if i.node_count != mod_params['node_count']: inst_prev_vals['node_count'] = i.node_count i.node_count = mod_params['node_count'] if inst_prev_vals: changed = instance_update(i) json_output['updated'] = changed json_output['previous_values'] = {'instance': inst_prev_vals} if d: if not d.exists(): d.create() d.reload() changed = True json_output['changed'] = changed json_output.update(mod_params) module.exit_json(**json_output)
def main(): module = AnsibleModule( argument_spec=dict( topic=dict(type='str', required=True), state=dict(type='str', default='present', choices=['absent', 'present']), publish=dict(type='list'), subscription=dict(type='dict'), service_account_email=dict(type='str'), credentials_file=dict(type='str'), project_id=dict(type='str'), ), ) if not HAS_PYTHON26: module.fail_json( msg="GCE module requires python's 'ast' module, python v2.6+") if not HAS_GOOGLE_CLOUD_PUBSUB: module.fail_json(msg="Please install google-cloud-pubsub library.") if not check_min_pkg_version(CLOUD_CLIENT, CLOUD_CLIENT_MINIMUM_VERSION): module.fail_json(msg="Please install %s client version %s" % (CLOUD_CLIENT, CLOUD_CLIENT_MINIMUM_VERSION)) mod_params = {} mod_params['publish'] = module.params.get('publish') mod_params['state'] = module.params.get('state') mod_params['topic'] = module.params.get('topic') mod_params['subscription'] = module.params.get('subscription') creds, params = get_google_cloud_credentials(module) pubsub_client = pubsub.Client(project=params['project_id'], credentials=creds, use_gax=False) pubsub_client.user_agent = CLOUD_CLIENT_USER_AGENT changed = False json_output = {} t = None if mod_params['topic']: t = pubsub_client.topic(mod_params['topic']) s = None if mod_params['subscription']: # Note: default ack deadline cannot be changed without deleting/recreating subscription s = t.subscription(mod_params['subscription']['name'], ack_deadline=mod_params['subscription'].get('ack_deadline', None), push_endpoint=mod_params['subscription'].get('push_endpoint', None)) if mod_params['state'] == 'absent': # Remove the most granular resource. If subscription is specified # we remove it. If only topic is specified, that is what is removed. # Note that a topic can be removed without first removing the subscription. # TODO(supertom): Enhancement: Provide an option to only delete a topic # if there are no subscriptions associated with it (which the API does not support). if s is not None: if s.exists(): s.delete() changed = True else: if t.exists(): t.delete() changed = True elif mod_params['state'] == 'present': if not t.exists(): t.create() changed = True if s: if not s.exists(): s.create() s.reload() changed = True else: # Subscription operations # TODO(supertom): if more 'update' operations arise, turn this into a function. s.reload() push_endpoint = mod_params['subscription'].get('push_endpoint', None) if push_endpoint is not None: if push_endpoint != s.push_endpoint: if push_endpoint == 'None': push_endpoint = None s.modify_push_configuration(push_endpoint=push_endpoint) s.reload() changed = push_endpoint == s.push_endpoint if 'pull' in mod_params['subscription']: if s.push_endpoint is not None: module.fail_json(msg="Cannot pull messages, push_endpoint is configured.") (json_output['pulled_messages'], changed) = pull_messages( mod_params['subscription']['pull'], s) # publish messages to the topic if mod_params['publish'] and len(mod_params['publish']) > 0: changed = publish_messages(mod_params['publish'], t) json_output['changed'] = changed json_output.update(mod_params) module.exit_json(**json_output)
def test_check_minimum_pkg_version(self, mockobj): self.assertTrue(check_min_pkg_version('foobar', '0.4.0')) self.assertTrue(check_min_pkg_version('foobar', '0.5.0')) self.assertFalse(check_min_pkg_version('foobar', '0.6.0'))