Search in sources :

Example 26 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class RemoveInstanceHandler method accept.

@Override
public void accept(Event<RemoveInstanceRequest> removeInstanceRequestEvent) {
    RemoveInstanceRequest request = removeInstanceRequestEvent.getData();
    RemoveInstanceResult result;
    try {
        CloudContext cloudContext = request.getCloudContext();
        CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
        AuthenticatedContext ac = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
        List<CloudResourceStatus> resourceStatus = connector.resources().downscale(ac, request.getCloudStack(), request.getCloudResources(), request.getInstances(), Collections.emptyMap());
        List<CloudResource> resources = ResourceLists.transform(resourceStatus);
        PollTask<ResourcesStatePollerResult> task = statusCheckFactory.newPollResourcesStateTask(ac, resources, true);
        ResourcesStatePollerResult statePollerResult = ResourcesStatePollerResults.build(cloudContext, resourceStatus);
        if (!task.completed(statePollerResult)) {
            statePollerResult = syncPollingScheduler.schedule(task);
        }
        LOGGER.info("Instance remove successfully finished for {}", cloudContext);
        result = new RemoveInstanceResult(new DownscaleStackResult(request, ResourceLists.transform(statePollerResult.getResults())), request);
    } catch (Exception e) {
        LOGGER.error("Failed to handle RemoveInstanceRequest.", e);
        result = new RemoveInstanceResult(e.getMessage(), e, request);
    }
    eventBus.notify(result.selector(), new Event<>(removeInstanceRequestEvent.getHeaders(), result));
}
Also used : CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) RemoveInstanceResult(com.sequenceiq.cloudbreak.cloud.event.resource.RemoveInstanceResult) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DownscaleStackResult(com.sequenceiq.cloudbreak.cloud.event.resource.DownscaleStackResult) RemoveInstanceRequest(com.sequenceiq.cloudbreak.cloud.event.resource.RemoveInstanceRequest) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ResourcesStatePollerResult(com.sequenceiq.cloudbreak.cloud.task.ResourcesStatePollerResult)

Example 27 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class InstanceStateQuery method getCloudVmInstanceStatuses.

public List<CloudVmInstanceStatus> getCloudVmInstanceStatuses(CloudCredential cloudCredential, CloudContext cloudContext, List<CloudInstance> instances) {
    CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
    AuthenticatedContext auth = connector.authentication().authenticate(cloudContext, cloudCredential);
    return connector.instances().check(auth, instances);
}
Also used : CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)

Example 28 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class GcpCredentialConnectorTest method testForFailedStatusBecauseMissingServiceAccId.

/**
 * Testing the GcpContext checking mechanism. If the inner created
 * GcpContext does not contains a valid service account id then a FAILED
 * status should come back.
 */
@Test
public void testForFailedStatusBecauseMissingServiceAccId() {
    final AuthenticatedContext authContext = createAuthContext();
    when(contextBuilder.contextInit(authContext.getCloudContext(), authContext, null, null, false)).thenReturn(context);
    when(context.getProjectId()).thenReturn("some id");
    when(context.getServiceAccountId()).thenReturn(null);
    when(context.getCompute()).thenReturn(createDummyCompute());
    final CloudCredentialStatus status = underTest.verify(authContext);
    Assert.assertNotNull("The returned CloudCredentialStatus instance is null!", status);
    Assert.assertEquals("Invalid credential status has specified!", CredentialStatus.FAILED, status.getStatus());
}
Also used : AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Test(org.junit.Test)

Example 29 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class GcpCredentialConnectorTest method testForFailedStatusBecauseMissingCompute.

/**
 * Testing the GcpContext checking mechanism. If the inner created
 * GcpContext does not contains a valid Compute instance then a FAILED
 * status should come back.
 */
@Test
public void testForFailedStatusBecauseMissingCompute() {
    final AuthenticatedContext authContext = createAuthContext();
    when(contextBuilder.contextInit(authContext.getCloudContext(), authContext, null, null, false)).thenReturn(context);
    when(context.getProjectId()).thenReturn("some id");
    when(context.getServiceAccountId()).thenReturn("some service id");
    when(context.getCompute()).thenReturn(null);
    final CloudCredentialStatus status = underTest.verify(authContext);
    Assert.assertNotNull("The returned CloudCredentialStatus instance is null!", status);
    Assert.assertEquals("Invalid credential status has specified!", CredentialStatus.FAILED, status.getStatus());
}
Also used : AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Test(org.junit.Test)

Example 30 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class GcpCredentialConnectorTest method testForNullExceptionOnVerifyPermissionCheck.

/**
 * Test that if some exception terminates the credential check operation,
 * or the checked credential is not valid. If that so, than the expected
 * CredentialStatus should be FAILED.
 *
 * @throws IOException this exception could thrown by the credential
 *                     checker method.
 */
@Test
public void testForNullExceptionOnVerifyPermissionCheck() throws IOException {
    final AuthenticatedContext authContext = createAuthContext();
    final String expectionReasonMessage = "exception message";
    when(contextBuilder.contextInit(authContext.getCloudContext(), authContext, null, null, false)).thenReturn(context);
    when(context.getProjectId()).thenReturn("some id");
    when(context.getServiceAccountId()).thenReturn("some service id");
    when(context.getCompute().regions().list(anyString()).executeUsingHead()).thenThrow(new BadRequestException(expectionReasonMessage));
    final CloudCredentialStatus status = underTest.verify(authContext);
    Assert.assertNotNull("The returned CloudCredentialStatus instance is null!", status);
    Assert.assertEquals("Invalid credential status has specified!", CredentialStatus.FAILED, status.getStatus());
    Assert.assertTrue("Not the specified exception has come with the status", status.getException() instanceof BadRequestException);
    Assert.assertEquals("Not the expected exception message has come with the status", expectionReasonMessage, status.getException().getMessage());
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Matchers.anyString(org.mockito.Matchers.anyString) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Test(org.junit.Test)

Aggregations

AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)52 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)34 Test (org.junit.Test)26 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)20 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)19 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)18 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)17 Location (com.sequenceiq.cloudbreak.cloud.model.Location)17 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)15 Vpc (com.amazonaws.services.ec2.model.Vpc)15 Group (com.sequenceiq.cloudbreak.cloud.model.Group)15 Network (com.sequenceiq.cloudbreak.cloud.model.Network)15 HashMap (java.util.HashMap)15 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)14 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)14 ArrayList (java.util.ArrayList)11 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)10 Event (reactor.bus.Event)10 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)8 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)8