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;
}
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);
}
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);
}
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);
}
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));
}
Aggregations