Search in sources :

Example 11 with GroupPromoteResult

use of org.commonjava.indy.promote.model.GroupPromoteResult in project indy by Commonjava.

the class IndyPromoteClientModule method promoteToGroup.

public GroupPromoteResult promoteToGroup(final StoreType srcType, final String srcName, final String targetGroup) throws IndyClientException {
    final GroupPromoteRequest req = new GroupPromoteRequest(new StoreKey(srcType, srcName), targetGroup);
    final GroupPromoteResult result = http.postWithResponse(GROUP_PROMOTE_PATH, req, GroupPromoteResult.class, HttpStatus.SC_OK);
    return result;
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 12 with GroupPromoteResult

use of org.commonjava.indy.promote.model.GroupPromoteResult in project indy by Commonjava.

the class PromotionManager method rollbackGroupPromote.

public GroupPromoteResult rollbackGroupPromote(GroupPromoteResult result, String user) throws PromotionException {
    GroupPromoteRequest request = result.getRequest();
    if (!storeManager.hasArtifactStore(request.getSource())) {
        String error = String.format("No such source/member store: %s", request.getSource());
        logger.warn(error);
        return new GroupPromoteResult(request, error);
    }
    Group target;
    try {
        target = (Group) storeManager.getArtifactStore(request.getTargetKey());
    } catch (IndyDataException e) {
        throw new PromotionException("Cannot retrieve target group: %s. Reason: %s", e, request.getTargetGroup(), e.getMessage());
    }
    if (target == null) {
        String error = String.format("No such target group: %s.", request.getTargetGroup());
        logger.warn(error);
        return new GroupPromoteResult(request, error);
    }
    if (target.getConstituents().contains(request.getSource())) {
        // give the preUpdate event a different object to compare vs. the original group.
        target = target.copyOf();
        target.removeConstituent(request.getSource());
        try {
            final ChangeSummary changeSummary = new ChangeSummary(user, "Removing " + request.getSource() + " from membership of group: " + target.getKey());
            storeManager.storeArtifactStore(target, changeSummary, false, true, new EventMetadata());
        } catch (IndyDataException e) {
            throw new PromotionException("Failed to store group: %s with additional member: %s. Reason: %s", e, target.getKey(), request.getSource(), e.getMessage());
        }
    } else {
        return new GroupPromoteResult(request, "Group: " + target.getKey() + " does not contain member: " + request.getSource());
    }
    return new GroupPromoteResult(request);
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 13 with GroupPromoteResult

use of org.commonjava.indy.promote.model.GroupPromoteResult in project indy by Commonjava.

the class PromotionManager method promoteToGroup.

public GroupPromoteResult promoteToGroup(GroupPromoteRequest request, String user, String baseUrl) throws PromotionException {
    if (!storeManager.hasArtifactStore(request.getSource())) {
        String error = String.format("Cannot promote from missing source: %s", request.getSource());
        logger.warn(error);
        return new GroupPromoteResult(request, error);
    }
    synchronized (getTargetKey(request.getTargetGroup())) {
        Group target;
        try {
            target = (Group) storeManager.getArtifactStore(request.getTargetKey());
        } catch (IndyDataException e) {
            throw new PromotionException("Cannot retrieve target group: %s. Reason: %s", e, request.getTargetGroup(), e.getMessage());
        }
        if (target == null) {
            String error = String.format("No such target group: %s.", request.getTargetGroup());
            logger.warn(error);
            return new GroupPromoteResult(request, error);
        }
        ValidationResult validation = new ValidationResult();
        logger.info("Running validations for promotion of: {} to group: {}", request.getSource(), request.getTargetGroup());
        validator.validate(request, validation, baseUrl);
        if (validation.isValid()) {
            if (!request.isDryRun() && !target.getConstituents().contains(request.getSource())) {
                // give the preUpdate event a different object to compare vs. the original group.
                target = target.copyOf();
                target.addConstituent(request.getSource());
                try {
                    final ChangeSummary changeSummary = new ChangeSummary(user, "Promoting " + request.getSource() + " into membership of group: " + target.getKey());
                    storeManager.storeArtifactStore(target, changeSummary, false, true, new EventMetadata());
                    if (hosted == request.getSource().getType() && config.isAutoLockHostedRepos()) {
                        HostedRepository source = (HostedRepository) storeManager.getArtifactStore(request.getSource());
                        source.setReadonly(true);
                        final ChangeSummary readOnlySummary = new ChangeSummary(user, "Promoting " + request.getSource() + " into membership of group: " + target.getKey());
                        storeManager.storeArtifactStore(source, readOnlySummary, false, true, new EventMetadata());
                    }
                } catch (IndyDataException e) {
                    throw new PromotionException("Failed to store group: %s with additional member: %s. Reason: %s", e, target.getKey(), request.getSource(), e.getMessage());
                }
            }
        }
        return new GroupPromoteResult(request, validation);
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HostedRepository(org.commonjava.indy.model.core.HostedRepository)

Example 14 with GroupPromoteResult

use of org.commonjava.indy.promote.model.GroupPromoteResult in project indy by Commonjava.

the class GroupPromoteFailsValidationTest method run.

@Test
public void run() throws Exception {
    final GroupPromoteResult result = client.module(IndyPromoteClientModule.class).promoteToGroup(new GroupPromoteRequest(source.getKey(), target.getName()));
    assertFalse(result.succeeded());
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTargetGroup(), equalTo(target.getName()));
    assertThat(result.getError(), nullValue());
    ValidationResult validations = result.getValidations();
    assertThat(validations, notNullValue());
    Map<String, String> errors = validations.getValidatorErrors();
    assertThat(errors, notNullValue());
    String error = errors.get("fail-all.groovy");
    assertThat(error, notNullValue());
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(false));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(false));
    Group g = client.stores().load(StoreType.group, target.getName(), Group.class);
    assertThat(g.getConstituents().contains(source.getKey()), equalTo(false));
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) Test(org.junit.Test)

Example 15 with GroupPromoteResult

use of org.commonjava.indy.promote.model.GroupPromoteResult in project indy by Commonjava.

the class GroupPromoteMatchesSucceedingValidationTest method run.

@Test
public void run() throws Exception {
    final GroupPromoteResult result = client.module(IndyPromoteClientModule.class).promoteToGroup(new GroupPromoteRequest(source.getKey(), target.getName()));
    assertThat(result.succeeded(), equalTo(true));
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTargetGroup(), equalTo(target.getName()));
    assertThat(result.getError(), nullValue());
    ValidationResult validations = result.getValidations();
    assertThat(validations, notNullValue());
    assertThat(validations.isValid(), equalTo(true));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(true));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(true));
    Group g = client.stores().load(StoreType.group, target.getName(), Group.class);
    assertThat(g.getConstituents().contains(source.getKey()), equalTo(true));
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) Test(org.junit.Test)

Aggregations

GroupPromoteResult (org.commonjava.indy.promote.model.GroupPromoteResult)23 GroupPromoteRequest (org.commonjava.indy.promote.model.GroupPromoteRequest)22 Test (org.junit.Test)20 ValidationResult (org.commonjava.indy.promote.model.ValidationResult)16 Category (org.junit.experimental.categories.Category)15 Group (org.commonjava.indy.model.core.Group)7 InputStream (java.io.InputStream)5 IndyPromoteClientModule (org.commonjava.indy.promote.client.IndyPromoteClientModule)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)2 IndyDataException (org.commonjava.indy.data.IndyDataException)2 AbstractContentManagementTest (org.commonjava.indy.ftest.core.AbstractContentManagementTest)2 HostedRepository (org.commonjava.indy.model.core.HostedRepository)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2 IOException (java.io.IOException)1 IndyClientException (org.commonjava.indy.client.core.IndyClientException)1 StoreKey (org.commonjava.indy.model.core.StoreKey)1