def find_content_view_version(module, content_view, environment=None, version=None, failsafe=False): if environment is not None: response = ContentViewVersion(content_view=content_view).search( ['content_view'], {'environment_id': environment.id}) return handle_find_response( module, response, message= "No content view version found on content view {} promoted to environment {}" .format(content_view.name, environment.name), failsafe=failsafe) elif version is not None: response = ContentViewVersion(content_view=content_view, version=version).search() return handle_find_response( module, response, message= "No content view version found on content view {} for version {}". format(content_view.name, version), failsafe=failsafe)
def test_positive_noapply_api(self): """Check if api incremental update can be done without actually applying it :id: 481c5ff2-801f-4eff-b1e0-95ea5bb37f95 :Setup: The prerequisites are already covered in the setUpClass() but for easy debug, get the content view id, Repository id and Lifecycle environment id using hammer and plug these statements on the top of the test. For example:: self.rhel_6_partial_cv = ContentView(id=38).read() self.rhva_6_repo = Repository(id=164).read() self.qe_lce = LifecycleEnvironment(id=46).read() :expectedresults: Incremental update completed with no errors and Content view has a newer version :CaseLevel: System """ # Get the content view versions and use the recent one. API always # returns the versions in ascending order so it is safe to assume the # last one in the list is the recent cv_versions = self.rhel_6_partial_cv.version # Get the applicable errata errata_list = self.get_applicable_errata(self.rhva_6_repo) self.assertGreater(len(errata_list), 0) # Apply incremental update using the first applicable errata ContentViewVersion().incremental_update(data={ 'content_view_version_environments': [{ 'content_view_version_id': cv_versions[-1].id, 'environment_ids': [self.qe_lce.id] }], 'add_content': { 'errata_ids': [errata_list[0].id] } }) # Re-read the content view to get the latest versions self.rhel_6_partial_cv = self.rhel_6_partial_cv.read() self.assertGreater( len(self.rhel_6_partial_cv.version), len(cv_versions) )
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True, no_log=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), content_view=dict(required=True), organization=dict(required=True), state=dict(default='present', choices=['present', 'absent']), version=dict(), lifecycle_environments=dict(type='list', default=['Library']), force=dict(type='bool', aliases=['force_promote'], default=False), force_yum_metadata_regeneration=dict(type='bool', default=False), synchronous=dict(type='bool', default=True), current_lifecycle_environment=dict(), ), mutually_exclusive=[['current_lifecycle_environment', 'version']], supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) set_task_timeout(3600000) # 60 minutes params_dict = dict([(k, v) for (k, v) in module.params.items() if v is not None]) server_url = module.params['server_url'] username = module.params['username'] password = module.params['password'] verify_ssl = module.params['verify_ssl'] state = module.params['state'] try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) organization = find_organization(module, params_dict['organization']) content_view = find_content_view(module, name=params_dict['content_view'], organization=organization) if 'current_lifecycle_environment' in params_dict: params_dict[ 'current_lifecycle_environment'] = find_lifecycle_environment( module, name=params_dict['current_lifecycle_environment'], organization=organization) content_view_version = find_content_view_version( module, content_view, environment=params_dict['current_lifecycle_environment']) elif 'version' in params_dict: content_view_version = find_content_view_version( module, content_view, version=params_dict['version'], failsafe=True) else: content_view_version = None changed = False if state == 'present': if content_view_version is None: kwargs = dict(data=dict()) if 'description' in params_dict: kwargs['data'].update(description=params_dict['description']) if 'force_metadata_regeneration' in params_dict: kwargs['data'].update( force_yum_metadata_regeneration=params_dict[ 'force_metadata_regeneration']) if 'version' in params_dict: kwargs['data'].update( major=map(int, str(params_dict['version']).split('.'))[0]) kwargs['data'].update( minor=map(int, str(params_dict['version']).split('.'))[1]) response = content_view.publish(params_dict['synchronous'], **kwargs) changed = True content_view_version = ContentViewVersion( id=response['output']['content_view_version_id']).read() if 'lifecycle_environments' in params_dict: lifecycle_environments = find_lifecycle_environments( module, names=params_dict['lifecycle_environments'], organization=organization) le_changed = promote_content_view_version( module, content_view_version, organization, lifecycle_environments, params_dict['synchronous'], force=params_dict['force'], force_yum_metadata_regeneration=params_dict[ 'force_yum_metadata_regeneration']) elif state == 'absent': changed = naildown_entity_state(ContentViewVersion, dict(), content_view_version, state, module) module.exit_json(changed=changed or le_changed)