def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), value=dict(type='raw'), parameter_type=dict(default='string', choices=[ 'string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json' ]), ), argument_spec=dict( state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), updated_name=dict(), ), required_if=( ['state', 'present_with_defaults', ['value']], ['state', 'present', ['value']], ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('common_parameters', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if entity and 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') # Convert values according to their corresponding parameter_type if entity and 'parameter_type' not in entity: entity['parameter_type'] = 'string' entity_dict['value'] = parameter_value_to_str( entity_dict['value'], entity_dict['parameter_type']) if entity and 'value' in entity: entity['value'] = parameter_value_to_str( entity['value'], entity.get('parameter_type', 'string')) changed = module.ensure_entity_state('common_parameters', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanSettingModule(foreman_spec=dict( name=dict(required=True), value=dict(type='raw'), ), ) # TODO Maybe refactor this into a EntityMixin module.foreman_spec.update( _foreman_spec_helper( dict(entity=dict(type='entity', flat_name='id', resource_type='settings', failsafe=False, thin=False, ensure=False), ))[0]) module.foreman_params['entity'] = module.foreman_params['name'] with module.api_connection(): entity = module.lookup_entity('entity') if 'value' not in module.foreman_params: module.foreman_params['value'] = entity['default'] or '' settings_type = entity['settings_type'] new_value = module.foreman_params['value'] # Allow to pass integers as string if settings_type == 'integer': new_value = int(new_value) module.foreman_params['value'] = parameter_value_to_str( new_value, settings_type) old_value = entity['value'] entity['value'] = parameter_value_to_str(old_value, settings_type) entity = module.ensure_entity('settings', module.foreman_params, entity, state='present') if entity: # Fake the not serialized input value as output entity['value'] = new_value module.exit_json(foreman_setting=entity)
def ensure_override_values(self, entity, expected_override_values): if expected_override_values is not None: parameter_type = entity.get('parameter_type', 'string') scope = {'smart_class_parameter_id': entity['id']} if not self.desired_absent: current_override_values = { override_value['match']: override_value for override_value in entity.get('override_values', []) } desired_override_values = { override_value['match']: override_value for override_value in expected_override_values } for match in desired_override_values: desired_override_value = desired_override_values[match] if 'value' in desired_override_value: desired_override_value[ 'value'] = parameter_value_to_str( desired_override_value['value'], parameter_type) current_override_value = current_override_values.pop( match, None) if current_override_value: current_override_value[ 'value'] = parameter_value_to_str( current_override_value['value'], parameter_type) self.ensure_entity( 'override_values', desired_override_value, current_override_value, state="present", foreman_spec=override_value_foreman_spec, params=scope) for current_override_value in current_override_values.values(): self.ensure_entity( 'override_values', None, current_override_value, state="absent", foreman_spec=override_value_foreman_spec, params=scope)
def main(): module = ForemanCommonParameterModule( foreman_spec=dict( name=dict(required=True), value=dict(type='raw'), parameter_type=dict(default='string', choices=[ 'string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json' ]), ), argument_spec=dict( state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), updated_name=dict(), ), required_if=( ['state', 'present_with_defaults', ['value']], ['state', 'present', ['value']], ), entity_resolve=False, ) module_params = module.clean_params() with module.api_connection(): entity = module.find_resource_by_name('common_parameters', name=module_params['name'], failsafe=True) if not module.desired_absent: # Convert values according to their corresponding parameter_type if entity and 'parameter_type' not in entity: entity['parameter_type'] = 'string' module_params['value'] = parameter_value_to_str( module_params['value'], module_params['parameter_type']) if entity and 'value' in entity: entity['value'] = parameter_value_to_str( entity['value'], entity.get('parameter_type', 'string')) module.run(module_params=module_params, entity=entity)
def main(): module = ForemanAnsibleModule(argument_spec=dict( name=dict(required=True), value=dict(type='raw'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('settings', entity_dict['name'], failsafe=False) if 'value' not in entity_dict: entity_dict['value'] = entity['default'] or '' settings_type = entity['settings_type'] new_value = entity_dict['value'] # Allow to pass integers as string if settings_type == 'integer': new_value = int(new_value) entity_dict['value'] = parameter_value_to_str(new_value, settings_type) old_value = entity['value'] entity['value'] = parameter_value_to_str(old_value, settings_type) changed, entity = module.ensure_entity('settings', entity_dict, entity, state='present', entity_spec=entity_spec) if entity: # Fake the not serialized input value as output entity['value'] = new_value module.exit_json(changed=changed, foreman_setting=entity)
def main(): module = ForemanSmartClassParameterModule( argument_spec=dict( puppetclass_name=dict(required=True), parameter=dict(required=True), state=dict(default='present', choices=['present_with_defaults', 'present']), ), foreman_spec=dict( parameter_type=dict(choices=[ 'string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json', 'none' ]), validator_type=dict(choices=['list', 'regexp']), validator_rule=dict(), description=dict(), default_value=dict(type='raw'), omit=dict(type='bool'), override=dict(type='bool'), merge_default=dict(type='bool'), merge_overrides=dict(type='bool'), avoid_duplicates=dict(type='bool'), required=dict(type='bool'), hidden_value=dict(type='bool'), override_value_order=dict(type='list', elements='str'), # tried nested_list here but, if using nested_list, override_values are not part of loaded entity. # override_values=dict(type='nested_list', elements='dict', foreman_spec=override_value_foreman_spec), override_values=dict(type='list', elements='dict'), ), # smart_class_parameters are created on puppetclass import and cannot be created/deleted from API, # so if we don't find it, it's an error. entity_opts=dict(failsafe=False), ) module_params = module.foreman_params if module_params.get('parameter_type', 'string') not in ['array', 'hash']: if 'merge_default' in module_params or 'merge_overrides' in module_params: module.fail_json( msg= "merge_default or merge_overrides can be used only with array or hash parameter_type" ) if module_params.get( 'parameter_type', 'string') != 'array' and 'avoid_duplicates' in module_params: module.fail_json( msg="avoid_duplicates can be used only with array parameter_type") search = "puppetclass_name={0} and parameter={1}".format( module_params['puppetclass_name'], module_params['parameter']) override_values = module_params.pop('override_values', None) if 'override_value_order' in module_params: module_params['override_value_order'] = '\n'.join( module_params['override_value_order']) if 'parameter_type' in module_params and module_params[ 'parameter_type'] == 'none': module_params['parameter_type'] = '' with module.api_connection(): entity = module.find_resource('smart_class_parameters', search=search) module.set_entity('entity', entity) # When override is set to false, foreman API don't accept parameter_type and all 'override options' have to be set to false if present if not module_params.get('override', False): module_params['parameter_type'] = '' for override_option in [ 'merge_default', 'merge_overrides', 'avoid_duplicates' ]: if override_option in entity and entity[override_option]: module_params[override_option] = False # Foreman API returns 'hidden_value?' instead of 'hidden_value' this is a bug ? if 'hidden_value?' in entity: entity['hidden_value'] = entity.pop('hidden_value?') if 'default_value' in module_params: module_params['default_value'] = parameter_value_to_str( module_params['default_value'], module_params.get('parameter_type', 'string')) if 'default_value' in entity: entity['default_value'] = parameter_value_to_str( entity['default_value'], entity.get('parameter_type', 'string')) entity = module.run() module.ensure_override_values(entity, override_values)