def main(): module = ForemanTaxonomicEntityAnsibleModule(entity_spec=dict( name=dict(required=True), description=dict(), filters=dict(type='nested_list', entity_spec=filter_entity_spec), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('roles', name=entity_dict['name'], failsafe=True) entity_dict = module.handle_taxonomy_params(entity_dict) filters = entity_dict.pop("filters", None) new_entity = module.ensure_entity('roles', entity_dict, entity) if not module.desired_absent and filters is not None: scope = {'role_id': new_entity['id']} if entity: current_filters = [ module.show_resource('filters', filter['id']) for filter in entity['filters'] ] else: current_filters = [] desired_filters = copy.deepcopy(filters) for desired_filter in desired_filters: # search for an existing filter for current_filter in current_filters: if desired_filter['search'] == current_filter['search']: if set(desired_filter['permissions']) == set( perm['name'] for perm in current_filter['permissions']): current_filters.remove(current_filter) break else: desired_filter['permissions'] = module.find_resources_by_name( 'permissions', desired_filter['permissions'], thin=True) module.ensure_entity('filters', desired_filter, None, params=scope, state='present', entity_spec=filter_entity_spec) for current_filter in current_filters: module.ensure_entity('filters', None, {'id': current_filter['id']}, params=scope, state='absent', entity_spec=filter_entity_spec) module.exit_json()
def main(): module = ForemanTaxonomicEntityAnsibleModule(entity_spec=dict( login=dict(required=True, aliases=['name']), firstname=dict(required=False), lastname=dict(required=False), mail=dict(required=False), description=dict(required=False), admin=dict(required=False, type='bool', default=False), user_password=dict(required=False, no_log=True, flat_name='password'), default_location=dict(required=False, type='entity', flat_name='default_location_id'), default_organization=dict(required=False, type='entity', flat_name='default_organization_id'), auth_source=dict(required=False, type='entity', flat_name='auth_source_id'), timezone=dict(required=False, choices=timezone_list), locale=dict(required=False, choices=locale_list), roles=dict(required=False, type='entity_list', flat_name='role_ids'), ), ) entity_dict = module.clean_params() module.connect() search = 'login="******"'.format(entity_dict['login']) entity = module.find_resource('users', search, failsafe=True) entity_dict = module.handle_taxonomy_params(entity_dict) if not module.desired_absent: if 'mail' not in entity_dict: if not entity: module.fail_json( msg= "The 'mail' parameter is required when creating a new user." ) else: entity_dict['mail'] = entity['mail'] if 'default_location' in entity_dict: entity_dict['default_location'] = module.find_resource_by_title( 'locations', entity_dict['default_location'], thin=True) if 'default_organization' in entity_dict: entity_dict['default_organization'] = module.find_resource_by_name( 'organizations', entity_dict['default_organization'], thin=True) if 'auth_source' in entity_dict: entity_dict['auth_source'] = module.find_resource_by_name( 'auth_sources', entity_dict['auth_source'], thin=True) if 'roles' in entity_dict: entity_dict['roles'] = module.find_resources_by_name( 'roles', entity_dict['roles'], thin=True) module.ensure_entity('users', entity_dict, entity) module.exit_json(entity_dict=entity_dict)
def main(): module = ForemanTaxonomicEntityAnsibleModule( argument_spec=dict(updated_name=dict(), ), entity_spec=dict( name=dict(required=True), description=dict(), network_type=dict(choices=['IPv4', 'IPv6'], default='IPv4'), dns_primary=dict(), dns_secondary=dict(), domains=dict(type='entity_list', flat_name='domain_ids'), gateway=dict(), network=dict(required=True), cidr=dict(type='int'), mask=dict(), from_ip=dict(flat_name='from'), to_ip=dict(flat_name='to'), boot_mode=dict(choices=['DHCP', 'Static'], default='DHCP'), ipam=dict( choices=['DHCP', 'Internal DB', 'Random DB', 'EUI-64', 'None'], default='DHCP'), dhcp_proxy=dict(type='entity', flat_name='dhcp_id'), httpboot_proxy=dict(type='entity', flat_name='httpboot_id'), tftp_proxy=dict(type='entity', flat_name='tftp_id'), discovery_proxy=dict(type='entity', flat_name='discovery_id'), dns_proxy=dict(type='entity', flat_name='dns_id'), template_proxy=dict(type='entity', flat_name='template_id'), remote_execution_proxies=dict( type='entity_list', flat_name='remote_execution_proxy_ids'), vlanid=dict(type='int'), mtu=dict(type='int'), parameters=dict(type='nested_list', entity_spec=parameter_entity_spec), ), required_one_of=[['cidr', 'mask']], ) if not HAS_IPADDRESS: module.fail_json(msg='The ipaddress Python module is required', exception=IPADDRESS_IMP_ERR) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('subnets', entity_dict['name'], failsafe=True) entity_dict = module.handle_taxonomy_params(entity_dict) if not module.desired_absent: if entity and 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') if entity_dict['network_type'] == 'IPv4': IPNetwork = ipaddress.IPv4Network else: IPNetwork = ipaddress.IPv6Network if 'mask' in entity_dict and 'cidr' not in entity_dict: entity_dict['cidr'] = IPNetwork( u'%s/%s' % (entity_dict['network'], entity_dict['mask'])).prefixlen elif 'mask' not in entity_dict and 'cidr' in entity_dict: entity_dict['mask'] = str( IPNetwork( u'%s/%s' % (entity_dict['network'], entity_dict['cidr'])).netmask) if 'domains' in entity_dict: entity_dict['domains'] = module.find_resources( 'domains', entity_dict['domains'], thin=True) for feature in ('dhcp_proxy', 'httpboot_proxy', 'tftp_proxy', 'discovery_proxy', 'dns_proxy', 'template_proxy'): if feature in entity_dict: entity_dict[feature] = module.find_resource_by_name( 'smart_proxies', entity_dict[feature], thin=True) if 'remote_execution_proxies' in entity_dict: entity_dict[ 'remote_execution_proxies'] = module.find_resources_by_name( 'smart_proxies', entity_dict['remote_execution_proxies'], thin=True) parameters = entity_dict.get('parameters') subnet = module.ensure_entity('subnets', entity_dict, entity) if subnet: scope = {'subnet_id': subnet['id']} module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json()