Search in sources :

Example 86 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsMetadataCollector method collectCloudVmMetaDataStatuses.

private List<CloudVmMetaDataStatus> collectCloudVmMetaDataStatuses(AuthenticatedContext ac, List<CloudInstance> vms, List<CloudResource> resources, List<String> knownInstanceIdList) {
    LOGGER.debug("Collect Cloud VM metadata statuses");
    List<CloudVmMetaDataStatus> collectedCloudVmMetaDataStatuses = new ArrayList<>();
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient amazonCFClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), region);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), region);
    AmazonEc2Client amazonEC2Client = new AuthenticatedContextView(ac).getAmazonEC2Client();
    Multimap<String, CloudInstance> instanceGroupMap = getInstanceGroupMap(vms);
    Set<String> subnetIds = new HashSet<>();
    Multimap<String, Instance> instancesOnAWSForGroup = ArrayListMultimap.create();
    for (String group : instanceGroupMap.keySet()) {
        List<Instance> instancesForGroup = collectInstancesForGroup(ac, amazonASClient, amazonEC2Client, amazonCFClient, group);
        LOGGER.info("Collected instances for group: {}", instancesForGroup.stream().map(Instance::getInstanceId).collect(Collectors.joining(",")));
        instancesOnAWSForGroup.putAll(group, instancesForGroup);
        subnetIds.addAll(getSubnetIdsForInstances(instancesForGroup));
    }
    LOGGER.info("Collected subnet IDs: {}", subnetIds);
    Map<String, String> subnetIdToAvailabilityZoneMap = buildSubnetIdToAvailabilityZoneMap(subnetIds, amazonEC2Client);
    LOGGER.info("Subnet id to availability zone map: {}", subnetIdToAvailabilityZoneMap);
    Multimap<String, Instance> unknownInstancesForGroup = getUnknownInstancesForGroup(knownInstanceIdList, instancesOnAWSForGroup);
    for (CloudInstance vm : vms) {
        if (vm.getInstanceId() == null) {
            addFromUnknownMap(vm, resources, unknownInstancesForGroup, collectedCloudVmMetaDataStatuses, subnetIdToAvailabilityZoneMap);
        } else {
            addKnownInstance(vm, instancesOnAWSForGroup, collectedCloudVmMetaDataStatuses, subnetIdToAvailabilityZoneMap);
        }
    }
    LOGGER.info("Collected cloud VM metadata and statuses: {}", collectedCloudVmMetaDataStatuses);
    return collectedCloudVmMetaDataStatuses;
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) HashSet(java.util.HashSet)

Example 87 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilderTest method testIsFinishedWhenTerminationAndOneTerminatedAndOneOther.

@Test
public void testIsFinishedWhenTerminationAndOneTerminatedAndOneOther() {
    CloudResource cloudResource = CloudResource.builder().name("name").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).params(emptyMap()).build();
    Instance instance1 = new Instance().withInstanceId("instanceId1").withState(new InstanceState().withCode(AwsNativeInstanceResourceBuilder.AWS_INSTANCE_TERMINATED_CODE));
    Instance instance2 = new Instance().withInstanceId("instanceId2").withState(new InstanceState().withCode(0));
    when(awsContext.isBuild()).thenReturn(false);
    when(awsContext.getAmazonEc2Client()).thenReturn(amazonEc2Client);
    DescribeInstancesResult describeInstanceResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(instance1, instance2));
    when(amazonEc2Client.describeInstances(any())).thenReturn(describeInstanceResult);
    boolean actual = underTest.isFinished(awsContext, ac, cloudResource);
    Assertions.assertFalse(actual);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) InstanceState(com.amazonaws.services.ec2.model.InstanceState) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 88 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilderTest method testIsFinishedWhenCreationOneRunningAndOneOther.

@Test
public void testIsFinishedWhenCreationOneRunningAndOneOther() {
    CloudResource cloudResource = CloudResource.builder().name("name").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).params(emptyMap()).build();
    Instance instance1 = new Instance().withInstanceId("instanceId1").withState(new InstanceState().withCode(AwsNativeInstanceResourceBuilder.AWS_INSTANCE_RUNNING_CODE));
    Instance instance2 = new Instance().withInstanceId("instanceId2").withState(new InstanceState().withCode(0));
    when(awsContext.isBuild()).thenReturn(true);
    when(awsContext.getAmazonEc2Client()).thenReturn(amazonEc2Client);
    DescribeInstancesResult describeInstanceResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(instance1, instance2));
    when(amazonEc2Client.describeInstances(any())).thenReturn(describeInstanceResult);
    boolean actual = underTest.isFinished(awsContext, ac, cloudResource);
    Assertions.assertFalse(actual);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) InstanceState(com.amazonaws.services.ec2.model.InstanceState) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 89 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilderTest method testIsFinishedWhenCreationAllMatchRunning.

@Test
public void testIsFinishedWhenCreationAllMatchRunning() {
    CloudResource cloudResource = CloudResource.builder().name("name").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).params(emptyMap()).build();
    Instance instance1 = new Instance().withInstanceId("instanceId1").withState(new InstanceState().withCode(AwsNativeInstanceResourceBuilder.AWS_INSTANCE_RUNNING_CODE));
    Instance instance2 = new Instance().withInstanceId("instanceId2").withState(new InstanceState().withCode(AwsNativeInstanceResourceBuilder.AWS_INSTANCE_RUNNING_CODE));
    when(awsContext.isBuild()).thenReturn(true);
    when(awsContext.getAmazonEc2Client()).thenReturn(amazonEc2Client);
    DescribeInstancesResult describeInstanceResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(instance1, instance2));
    when(amazonEc2Client.describeInstances(any())).thenReturn(describeInstanceResult);
    boolean actual = underTest.isFinished(awsContext, ac, cloudResource);
    Assertions.assertTrue(actual);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) InstanceState(com.amazonaws.services.ec2.model.InstanceState) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 90 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilderTest method testBuildWhenInstanceNoExist.

@Test
public void testBuildWhenInstanceNoExist() throws Exception {
    RunInstancesResult runInstancesResult = mock(RunInstancesResult.class);
    InstanceAuthentication authentication = mock(InstanceAuthentication.class);
    Instance instance = new Instance().withInstanceId("instanceId");
    CloudResource cloudResource = CloudResource.builder().name("name").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).group("groupName").params(emptyMap()).build();
    CloudResource securityGroupCloudResource = CloudResource.builder().name("name").type(ResourceType.AWS_SECURITY_GROUP).status(CommonStatus.CREATED).reference("sg-id").group("groupName").params(emptyMap()).build();
    Image image = mock(Image.class);
    long privateId = 0;
    when(awsMethodExecutor.execute(any(), eq(Optional.empty()))).thenReturn(Optional.empty());
    when(awsTaggingService.prepareEc2TagSpecification(cloudStack.getTags(), com.amazonaws.services.ec2.model.ResourceType.Instance)).thenReturn(new TagSpecification());
    when(amazonEc2Client.createInstance(any())).thenReturn(runInstancesResult);
    when(runInstancesResult.getReservation()).thenReturn(new Reservation().withInstances(instance));
    when(group.getReferenceInstanceTemplate()).thenReturn(instanceTemplate);
    when(group.getName()).thenReturn("groupName");
    when(cloudStack.getImage()).thenReturn(image);
    when(image.getImageName()).thenReturn("img-name");
    when(cloudStack.getInstanceAuthentication()).thenReturn(authentication);
    when(awsContext.getAmazonEc2Client()).thenReturn(amazonEc2Client);
    when(awsContext.getGroupResources("groupName")).thenReturn(List.of(securityGroupCloudResource));
    ArgumentCaptor<RunInstancesRequest> runInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(RunInstancesRequest.class);
    List<CloudResource> actual = underTest.build(awsContext, cloudInstance, privateId, ac, group, Collections.singletonList(cloudResource), cloudStack);
    verify(amazonEc2Client).createInstance(runInstancesRequestArgumentCaptor.capture());
    RunInstancesRequest runInstancesRequest = runInstancesRequestArgumentCaptor.getValue();
    Assertions.assertEquals(actual.get(0).getInstanceId(), "instanceId");
    Assertions.assertEquals("sg-id", runInstancesRequest.getSecurityGroupIds().get(0));
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) Image(com.sequenceiq.cloudbreak.cloud.model.Image) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) Test(org.junit.jupiter.api.Test)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18