Search in sources :

Example 56 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ActivationKeyResource method addPoolToKey.

@ApiOperation(notes = "Adds a Pool to an Activation Key", value = "Add Pool to Key")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Path("{activation_key_id}/pools/{pool_id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
public ActivationKeyDTO addPoolToKey(@PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId, @PathParam("pool_id") @Verify(Pool.class) String poolId, @QueryParam("quantity") Long quantity) {
    ActivationKey key = activationKeyCurator.verifyAndLookupKey(activationKeyId);
    Pool pool = findPool(poolId);
    // Throws a BadRequestException if adding pool to key is a bad idea
    activationKeyRules.validatePoolForActKey(key, pool, quantity);
    // Make sure we don't try to register the pool twice.
    if (key.hasPool(pool)) {
        throw new BadRequestException(i18n.tr("Pool ID \"{0}\" has already been registered with this activation key", poolId));
    }
    key.addPool(pool, quantity);
    activationKeyCurator.update(key);
    return this.translator.translate(key, ActivationKeyDTO.class);
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 57 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ActivationKeyResource method deleteActivationKey.

@ApiOperation(notes = "Removes an Activation Key", value = "deleteActivationKey")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@DELETE
@Path("{activation_key_id}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteActivationKey(@PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId) {
    ActivationKey key = activationKeyCurator.verifyAndLookupKey(activationKeyId);
    log.debug("Deleting activation key: {}", activationKeyId);
    activationKeyCurator.delete(key);
}
Also used : ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 58 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ActivationKeyResource method removePoolFromKey.

@ApiOperation(notes = "Removes a Pool from an Activation Key", value = "Remove Pool From Key")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@DELETE
@Path("{activation_key_id}/pools/{pool_id}")
@Produces(MediaType.APPLICATION_JSON)
public ActivationKeyDTO removePoolFromKey(@PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId, @PathParam("pool_id") @Verify(Pool.class) String poolId) {
    ActivationKey key = activationKeyCurator.verifyAndLookupKey(activationKeyId);
    Pool pool = findPool(poolId);
    key.removePool(pool);
    activationKeyCurator.update(key);
    return this.translator.translate(key, ActivationKeyDTO.class);
}
Also used : Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 59 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ActivationKeyResourceTest method testAddingRemovingProductIDs.

@Test
public void testAddingRemovingProductIDs() {
    ActivationKey key = new ActivationKey();
    Owner owner = createOwner();
    Product product = this.createProduct(owner);
    key.setOwner(owner);
    key.setName("dd");
    key = activationKeyCurator.create(key);
    assertNotNull(key.getId());
    activationKeyResource.addProductIdToKey(key.getId(), product.getId());
    assertTrue(key.getProducts().size() == 1);
    activationKeyResource.removeProductIdFromKey(key.getId(), product.getId());
    assertEquals(0, key.getProducts().size());
}
Also used : Owner(org.candlepin.model.Owner) Product(org.candlepin.model.Product) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Test(org.junit.Test)

Example 60 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ActivationKeyResourceTest method testActivationKeyWithDiffHostReqPools.

@Test
public void testActivationKeyWithDiffHostReqPools() {
    ActivationKey ak = genActivationKey();
    ActivationKeyCurator akc = mock(ActivationKeyCurator.class);
    PoolManager poolManager = mock(PoolManager.class);
    Pool p1 = genPool();
    p1.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1");
    Pool p2 = genPool();
    p2.setAttribute(Pool.Attributes.REQUIRES_HOST, "host2");
    ak.addPool(p2, 1L);
    when(akc.verifyAndLookupKey(eq("testKey"))).thenReturn(ak);
    when(poolManager.find(eq("testPool1"))).thenReturn(p1);
    ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator);
    akr.addPoolToKey("testKey", "testPool1", 1L);
}
Also used : ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) ActivationKeyCurator(org.candlepin.model.activationkeys.ActivationKeyCurator) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) PoolManager(org.candlepin.controller.PoolManager) Test(org.junit.Test)

Aggregations

ActivationKey (org.candlepin.model.activationkeys.ActivationKey)76 Test (org.junit.Test)55 Pool (org.candlepin.model.Pool)38 Product (org.candlepin.model.Product)16 ActivationKeyPool (org.candlepin.model.activationkeys.ActivationKeyPool)15 ArrayList (java.util.ArrayList)14 ValidationResult (org.candlepin.policy.ValidationResult)14 Consumer (org.candlepin.model.Consumer)13 ActivationKeyCurator (org.candlepin.model.activationkeys.ActivationKeyCurator)13 PoolManager (org.candlepin.controller.PoolManager)11 ProductCachedSerializationModule (org.candlepin.jackson.ProductCachedSerializationModule)10 Owner (org.candlepin.model.Owner)10 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 BadRequestException (org.candlepin.common.exceptions.BadRequestException)9 Date (java.util.Date)8 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)8 HashSet (java.util.HashSet)6