Search in sources :

Example 11 with VirtConsumerMap

use of org.candlepin.model.VirtConsumerMap in project candlepin by candlepin.

the class HypervisorResourceTest method hypervisorCheckInReportsFailuresOnCreateFailure.

@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@Test
public void hypervisorCheckInReportsFailuresOnCreateFailure() throws Exception {
    Owner owner = new Owner("admin");
    Map<String, List<GuestIdDTO>> hostGuestMap = new HashMap<>();
    String expectedHostVirtId = "test-host-id";
    hostGuestMap.put(expectedHostVirtId, new ArrayList(Arrays.asList(TestUtil.createGuestIdDTO("GUEST_A"), TestUtil.createGuestIdDTO("GUEST_B"))));
    when(consumerCurator.getHostConsumersMap(any(Owner.class), any(Set.class))).thenReturn(new VirtConsumerMap());
    when(consumerCurator.getGuestConsumersMap(any(String.class), any(Set.class))).thenReturn(new VirtConsumerMap());
    when(idCertService.generateIdentityCert(any(Consumer.class))).thenReturn(new IdentityCertificate());
    String expectedMessage = "Forced Exception.";
    RuntimeException exception = new RuntimeException(expectedMessage);
    // Simulate failure  when checking the owner
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(principal.canAccess(eq(owner), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true);
    when(consumerCurator.create(any(Consumer.class))).thenThrow(exception);
    HypervisorCheckInResult result = hypervisorResource.hypervisorUpdate(hostGuestMap, principal, owner.getKey(), true);
    List<String> failures = new ArrayList<>(result.getFailedUpdate());
    assertEquals(1, failures.size());
    assertTrue(failures.get(0).contains("Problem creating unit"));
}
Also used : Owner(org.candlepin.model.Owner) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) List(java.util.List) ArrayList(java.util.ArrayList) IdentityCertificate(org.candlepin.model.IdentityCertificate) HypervisorCheckInResult(org.candlepin.resource.dto.HypervisorCheckInResult) Test(org.junit.Test)

Example 12 with VirtConsumerMap

use of org.candlepin.model.VirtConsumerMap in project candlepin by candlepin.

the class HypervisorResourceTest method hypervisorCheckInCreatesNewConsumer.

@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@Test
public void hypervisorCheckInCreatesNewConsumer() throws Exception {
    Owner owner = new Owner("admin");
    owner.setId("test-id");
    Map<String, List<GuestIdDTO>> hostGuestMap = new HashMap<>();
    hostGuestMap.put("test-host", new ArrayList(Arrays.asList(TestUtil.createGuestIdDTO("GUEST_A"), TestUtil.createGuestIdDTO("GUEST_B"))));
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(consumerCurator.getHostConsumersMap(any(Owner.class), any(Set.class))).thenReturn(new VirtConsumerMap());
    when(consumerCurator.getGuestConsumersMap(any(String.class), any(Set.class))).thenReturn(new VirtConsumerMap());
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(principal.canAccess(eq(owner), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true);
    when(idCertService.generateIdentityCert(any(Consumer.class))).thenReturn(new IdentityCertificate());
    HypervisorCheckInResult result = hypervisorResource.hypervisorUpdate(hostGuestMap, principal, owner.getKey(), true);
    Set<Consumer> created = result.getCreated();
    assertEquals(1, created.size());
    Consumer c1 = created.iterator().next();
    assertEquals("test-host", c1.getHypervisorId().getHypervisorId());
    assertEquals(2, c1.getGuestIds().size());
    assertEquals("GUEST_A", c1.getGuestIds().get(0).getGuestId());
    assertEquals("GUEST_B", c1.getGuestIds().get(1).getGuestId());
    assertEquals("x86_64", c1.getFact("uname.machine"));
    assertEquals(this.hypervisorType.getId(), c1.getTypeId());
}
Also used : Owner(org.candlepin.model.Owner) Set(java.util.Set) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IdentityCertificate(org.candlepin.model.IdentityCertificate) HypervisorCheckInResult(org.candlepin.resource.dto.HypervisorCheckInResult) Test(org.junit.Test)

Example 13 with VirtConsumerMap

use of org.candlepin.model.VirtConsumerMap in project candlepin by candlepin.

the class HypervisorResourceTest method treatNullGuestListsAsEmptyGuestLists.

@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@Test
public void treatNullGuestListsAsEmptyGuestLists() throws Exception {
    Owner owner = new Owner("admin");
    Map<String, List<GuestIdDTO>> hostGuestMap = new HashMap<>();
    hostGuestMap.put("HYPERVISOR_A", null);
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(consumerCurator.getHostConsumersMap(any(Owner.class), any(Set.class))).thenReturn(new VirtConsumerMap());
    when(consumerCurator.getGuestConsumersMap(any(String.class), any(Set.class))).thenReturn(new VirtConsumerMap());
    when(principal.canAccess(eq(owner), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true);
    when(idCertService.generateIdentityCert(any(Consumer.class))).thenReturn(new IdentityCertificate());
    HypervisorCheckInResult result = hypervisorResource.hypervisorUpdate(hostGuestMap, principal, owner.getKey(), true);
    assertNotNull(result);
    assertNotNull(result.getCreated());
    List<Consumer> created = new ArrayList<>(result.getCreated());
    assertEquals(1, created.size());
    List<GuestId> gids = created.get(0).getGuestIds();
    assertEquals(0, gids.size());
}
Also used : Owner(org.candlepin.model.Owner) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) GuestId(org.candlepin.model.GuestId) List(java.util.List) ArrayList(java.util.ArrayList) IdentityCertificate(org.candlepin.model.IdentityCertificate) HypervisorCheckInResult(org.candlepin.resource.dto.HypervisorCheckInResult) Test(org.junit.Test)

Example 14 with VirtConsumerMap

use of org.candlepin.model.VirtConsumerMap in project candlepin by candlepin.

the class HypervisorResource method hypervisorUpdate.

/**
 * @deprecated Use the asynchronous method
 * @return HypervisorCheckInResult
 */
@ApiOperation(notes = "Updates the list of Hypervisor Guests Allows agents such as " + "virt-who to update its host list and associate the guests for each host. This is " + "typically used when a host is unable to register to candlepin via subscription" + " manager.  In situations where consumers already exist it is probably best not " + "to allow creation of new hypervisor consumers.  Most consumers do not have a" + " hypervisorId attribute, so that should be added manually when necessary by the " + "management environment. @deprecated Use the asynchronous method", value = "hypervisorUpdate")
@ApiResponses({ @ApiResponse(code = 202, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Deprecated
@Transactional
@UpdateConsumerCheckIn
@SuppressWarnings("checkstyle:indentation")
public HypervisorCheckInResult hypervisorUpdate(Map<String, List<GuestIdDTO>> hostGuestDTOMap, @Context Principal principal, @QueryParam("owner") @Verify(value = Owner.class, require = Access.READ_ONLY, subResource = SubResource.HYPERVISOR) String ownerKey, @ApiParam("specify whether or not to create missing hypervisors." + "Default is true.  If false is specified, hypervisorIds that are not found" + "will result in failed entries in the resulting HypervisorCheckInResult") @QueryParam("create_missing") @DefaultValue("true") boolean createMissing) {
    log.debug("Hypervisor check-in by principal: {}", principal);
    if (hostGuestDTOMap == null) {
        log.debug("Host/Guest mapping provided during hypervisor checkin was null.");
        throw new BadRequestException(i18n.tr("Host to guest mapping was not provided for hypervisor check-in."));
    }
    Owner owner = this.getOwner(ownerKey);
    if (owner.isAutobindDisabled()) {
        log.debug("Could not update host/guest mapping. Auto-attach is disabled for owner {}", owner.getKey());
        throw new BadRequestException(i18n.tr("Could not update host/guest mapping. Auto-attach is disabled for owner {0}.", owner.getKey()));
    }
    if (hostGuestDTOMap.remove("") != null) {
        log.warn("Ignoring empty hypervisor id");
    }
    // Maps virt hypervisor ID to registered consumer for that hypervisor, should one exist:
    VirtConsumerMap hypervisorConsumersMap = consumerCurator.getHostConsumersMap(owner, hostGuestDTOMap.keySet());
    int emptyGuestIdCount = 0;
    Set<String> allGuestIds = new HashSet<>();
    Collection<List<GuestIdDTO>> idsLists = hostGuestDTOMap.values();
    for (List<GuestIdDTO> guestIds : idsLists) {
        // See bzs 1332637, 1332635
        if (guestIds == null) {
            continue;
        }
        for (Iterator<GuestIdDTO> guestIdsItr = guestIds.iterator(); guestIdsItr.hasNext(); ) {
            String id = guestIdsItr.next().getGuestId();
            if (StringUtils.isEmpty(id)) {
                emptyGuestIdCount++;
                guestIdsItr.remove();
            } else {
                allGuestIds.add(id);
            }
        }
    }
    if (emptyGuestIdCount > 0) {
        log.warn("Ignoring {} empty/null guest id(s).", emptyGuestIdCount);
    }
    HypervisorCheckInResult result = new HypervisorCheckInResult();
    for (Entry<String, List<GuestIdDTO>> hostEntry : hostGuestDTOMap.entrySet()) {
        String hypervisorId = hostEntry.getKey();
        // See bzs 1332637, 1332635
        if (hostEntry.getValue() == null) {
            hostEntry.setValue(new ArrayList<>());
        }
        try {
            log.debug("Syncing virt host: {} ({} guest IDs)", hypervisorId, hostEntry.getValue().size());
            boolean hostConsumerCreated = false;
            boolean updatedType = false;
            // Attempt to find a consumer for the given hypervisorId
            Consumer consumer = null;
            if (hypervisorConsumersMap.get(hypervisorId) == null) {
                if (!createMissing) {
                    log.info("Unable to find hypervisor with id {} in org {}", hypervisorId, ownerKey);
                    result.failed(hypervisorId, i18n.tr("Unable to find hypervisor in org \"{0}\"", ownerKey));
                    continue;
                }
                log.debug("Registering new host consumer for hypervisor ID: {}", hypervisorId);
                consumer = createConsumerForHypervisorId(hypervisorId, owner, principal);
                hostConsumerCreated = true;
            } else {
                consumer = hypervisorConsumersMap.get(hypervisorId);
                if (!hypervisorType.getId().equals(consumer.getTypeId())) {
                    consumer.setType(hypervisorType);
                    updatedType = true;
                }
            }
            List<GuestId> guestIds = new ArrayList<>();
            guestIdResource.populateEntities(guestIds, hostEntry.getValue());
            boolean guestIdsUpdated = addGuestIds(consumer, guestIds);
            Date now = new Date();
            consumerCurator.updateLastCheckin(consumer, now);
            consumer.setLastCheckin(now);
            // Populate the result with the processed consumer.
            if (hostConsumerCreated) {
                result.created(consumer);
            } else if (guestIdsUpdated || updatedType) {
                result.updated(consumer);
            } else {
                result.unchanged(consumer);
            }
        } catch (Exception e) {
            log.error("Hypervisor checkin failed", e);
            result.failed(hypervisorId, e.getMessage());
        }
    }
    log.info("Summary of hypervisor checkin by principal \"{}\": {}", principal, result);
    return result;
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) Date(java.util.Date) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) GuestId(org.candlepin.model.GuestId) BadRequestException(org.candlepin.common.exceptions.BadRequestException) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HypervisorCheckInResult(org.candlepin.resource.dto.HypervisorCheckInResult) UpdateConsumerCheckIn(org.candlepin.auth.UpdateConsumerCheckIn) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) Transactional(com.google.inject.persist.Transactional)

Example 15 with VirtConsumerMap

use of org.candlepin.model.VirtConsumerMap in project candlepin by candlepin.

the class HypervisorUpdateJob method toExecute.

/**
 * {@inheritDoc}
 *
 * Executes {@link ConsumerResource#create(org.candlepin.model.Consumer, org.candlepin.auth.Principal,
 *  java.lang.String, java.lang.String, java.lang.String)}
 * Executes (@link ConusmerResource#performConsumerUpdates(java.utl.String, org.candlepin.model.Consumer)}
 * as a pinsetter job.
 *
 * @param context the job's execution context
 */
@Transactional
@SuppressWarnings({ "checkstyle:indentation", "checkstyle:methodlength" })
public void toExecute(JobExecutionContext context) throws JobExecutionException {
    try {
        JobDataMap map = context.getMergedJobDataMap();
        String ownerKey = map.getString(JobStatus.TARGET_ID);
        Boolean create = map.getBoolean(CREATE);
        Principal principal = (Principal) map.get(PRINCIPAL);
        String jobReporterId = map.getString(REPORTER_ID);
        HypervisorUpdateResultUuids result = new HypervisorUpdateResultUuids();
        Owner owner = ownerCurator.lookupByKey(ownerKey);
        if (owner == null) {
            context.setResult("Nothing to do. Owner does not exist");
            log.warn("Hypervisor update attempted against non-existent org id \"{0}\"", ownerKey);
            return;
        }
        if (owner.isAutobindDisabled()) {
            log.debug("Could not update host/guest mapping. Auto-Attach is disabled for owner {}", owner.getKey());
            throw new BadRequestException(i18n.tr("Could not update host/guest mapping. Auto-attach is disabled for owner {0}.", owner.getKey()));
        }
        byte[] data = (byte[]) map.get(DATA);
        String json = decompress(data);
        HypervisorList hypervisors = (HypervisorList) Util.fromJson(json, HypervisorList.class);
        log.debug("Hypervisor consumers for create/update: {}", hypervisors.getHypervisors().size());
        log.debug("Updating hypervisor consumers for org {0}", ownerKey);
        Set<String> hosts = new HashSet<>();
        Set<String> guests = new HashSet<>();
        Map<String, Consumer> incomingHosts = new HashMap<>();
        parseHypervisorList(hypervisors, hosts, guests, incomingHosts);
        // TODO Need to ensure that we retrieve existing guestIds from the DB before continuing.
        // Maps virt hypervisor ID to registered consumer for that hypervisor, should one exist:
        VirtConsumerMap hypervisorConsumersMap = consumerCurator.getHostConsumersMap(owner, hosts);
        Map<String, GuestId> guestIds = consumerCurator.getGuestIdMap(guests, owner);
        for (String hypervisorId : hosts) {
            Consumer knownHost = hypervisorConsumersMap.get(hypervisorId);
            Consumer incoming = syncGuestIds(incomingHosts.get(hypervisorId), guestIds);
            Consumer reportedOnConsumer = null;
            if (knownHost == null) {
                if (!create) {
                    result.failed(hypervisorId, "Unable to find hypervisor with id " + hypervisorId + " in org " + ownerKey);
                } else {
                    log.debug("Registering new host consumer for hypervisor ID: {}", hypervisorId);
                    Consumer newHost = createConsumerForHypervisorId(hypervisorId, jobReporterId, owner, principal, incoming);
                    // Since we just created this new consumer, we can migrate the guests immediately
                    GuestMigration guestMigration = new GuestMigration(consumerCurator).buildMigrationManifest(incoming, newHost);
                    // Now that we have the new consumer persisted, immediately migrate the guests to it
                    if (guestMigration.isMigrationPending()) {
                        guestMigration.migrate(false);
                    }
                    hypervisorConsumersMap.add(hypervisorId, newHost);
                    result.created(newHost);
                    reportedOnConsumer = newHost;
                }
            } else {
                reportedOnConsumer = knownHost;
                if (jobReporterId != null && knownHost.getHypervisorId() != null && hypervisorId.equalsIgnoreCase(knownHost.getHypervisorId().getHypervisorId()) && knownHost.getHypervisorId().getReporterId() != null && !jobReporterId.equalsIgnoreCase(knownHost.getHypervisorId().getReporterId())) {
                    log.debug("Reporter changed for Hypervisor {} of Owner {} from {} to {}", hypervisorId, ownerKey, knownHost.getHypervisorId().getReporterId(), jobReporterId);
                }
                boolean typeUpdated = false;
                if (!hypervisorType.getId().equals(knownHost.getTypeId())) {
                    typeUpdated = true;
                    knownHost.setType(hypervisorType);
                }
                GuestMigration guestMigration = new GuestMigration(consumerCurator).buildMigrationManifest(incoming, knownHost);
                boolean factsUpdated = consumerResource.checkForFactsUpdate(knownHost, incoming);
                if (factsUpdated || guestMigration.isMigrationPending() || typeUpdated) {
                    knownHost.setLastCheckin(new Date());
                    guestMigration.migrate(false);
                    result.updated(knownHost);
                } else {
                    result.unchanged(knownHost);
                }
            }
            // update reporter id if it changed
            if (jobReporterId != null && reportedOnConsumer != null && reportedOnConsumer.getHypervisorId() != null && (reportedOnConsumer.getHypervisorId().getReporterId() == null || !jobReporterId.contentEquals(reportedOnConsumer.getHypervisorId().getReporterId()))) {
                reportedOnConsumer.getHypervisorId().setReporterId(jobReporterId);
            } else if (jobReporterId == null) {
                log.debug("hypervisor checkin reported asynchronously without reporter id " + "for hypervisor:{} of owner:{}", hypervisorId, ownerKey);
            }
        }
        for (Consumer consumer : hypervisorConsumersMap.getConsumers()) {
            consumer = result.wasCreated(consumer) ? consumerCurator.create(consumer, false) : consumerCurator.update(consumer, false);
        }
        consumerCurator.flush();
        log.info("Summary for report from {} by principal {}\n {}", jobReporterId, principal, result);
        context.setResult(result);
    } catch (Exception e) {
        log.error("HypervisorUpdateJob encountered a problem.", e);
        context.setResult(e.getMessage());
        throw new JobExecutionException(e.getMessage(), e, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) Date(java.util.Date) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) IOException(java.io.IOException) HypervisorUpdateResultUuids(org.candlepin.resource.dto.HypervisorUpdateResultUuids) GuestMigration(org.candlepin.resource.util.GuestMigration) JobExecutionException(org.quartz.JobExecutionException) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) GuestId(org.candlepin.model.GuestId) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Principal(org.candlepin.auth.Principal) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Aggregations

VirtConsumerMap (org.candlepin.model.VirtConsumerMap)23 Set (java.util.Set)18 Consumer (org.candlepin.model.Consumer)18 Test (org.junit.Test)18 Owner (org.candlepin.model.Owner)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 List (java.util.List)8 HypervisorCheckInResult (org.candlepin.resource.dto.HypervisorCheckInResult)8 HashSet (java.util.HashSet)7 GuestId (org.candlepin.model.GuestId)7 JobDetail (org.quartz.JobDetail)7 JobExecutionContext (org.quartz.JobExecutionContext)7 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)6 IdentityCertificate (org.candlepin.model.IdentityCertificate)6 GuestIdDTO (org.candlepin.dto.api.v1.GuestIdDTO)4 Transactional (com.google.inject.persist.Transactional)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Date (java.util.Date)2 Consumes (javax.ws.rs.Consumes)2