use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsMetaDataCollectorTest method collectNewAndExistingOneGroup.
@Test
public void collectNewAndExistingOneGroup() {
List<CloudInstance> vms = new ArrayList<>();
List<Volume> volumes = new ArrayList<>();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
List<String> gatewayIds = Arrays.asList("i-1", "i-2");
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
Instance instance1 = Mockito.mock(Instance.class);
when(instance1.getInstanceId()).thenReturn("i-1");
when(instance1.getPrivateIpAddress()).thenReturn("privateIp1");
when(instance1.getPublicIpAddress()).thenReturn("publicIp1");
Instance instance2 = Mockito.mock(Instance.class);
when(instance2.getInstanceId()).thenReturn("i-2");
when(instance2.getPrivateIpAddress()).thenReturn("privateIp2");
when(instance2.getPublicIpAddress()).thenReturn("publicIp2");
List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2));
when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
AuthenticatedContext ac = authenticatedContext();
List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
verify(amazonEC2Client, times(2)).createTags(any(CreateTagsRequest.class));
Assert.assertEquals(2, statuses.size());
Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
Assert.assertEquals("privateIp1", statuses.get(0).getMetaData().getPrivateIp());
Assert.assertEquals("publicIp1", statuses.get(0).getMetaData().getPublicIp());
Assert.assertEquals("i-2", statuses.get(1).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
Assert.assertEquals("privateIp2", statuses.get(1).getMetaData().getPrivateIp());
Assert.assertEquals("publicIp2", statuses.get(1).getMetaData().getPublicIp());
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AzureInstanceConnector method start.
@Override
public List<CloudVmInstanceStatus> start(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
String stackName = armTemplateUtils.getStackName(ac.getCloudContext());
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
for (CloudInstance vm : vms) {
try {
AzureClient azureClient = ac.getParameter(AzureClient.class);
azureClient.startVirtualMachine(stackName, vm.getInstanceId());
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
} catch (RuntimeException e) {
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.FAILED, e.getMessage()));
}
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AzureInstanceConnector method check.
@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
String stackName = armTemplateUtils.getStackName(ac.getCloudContext());
for (CloudInstance vm : vms) {
try {
AzureClient azureClient = ac.getParameter(AzureClient.class);
boolean virtualMachineExists = azureClient.isVirtualMachineExists(stackName, vm.getInstanceId());
if (virtualMachineExists) {
PowerState virtualMachinePowerState = azureClient.getVirtualMachinePowerState(stackName, vm.getInstanceId());
statuses.add(new CloudVmInstanceStatus(vm, AzureInstanceStatus.get(virtualMachinePowerState)));
} else {
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
}
} catch (CloudException e) {
if (e.body() != null && "ResourceNotFound".equals(e.body().code())) {
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
} else {
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.UNKNOWN));
}
} catch (RuntimeException ignored) {
statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.UNKNOWN));
}
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AzureResourceConnector method downscale.
@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms, Map<String, Map<String, Object>> resourcesToRemove) {
AzureClient client = ac.getParameter(AzureClient.class);
String stackName = azureUtils.getStackName(ac.getCloudContext());
String resourceGroupName = azureUtils.getResourceGroupName(ac.getCloudContext());
String diskContainer = azureStorage.getDiskContainerName(ac.getCloudContext());
for (CloudInstance instance : vms) {
String instanceId = instance.getInstanceId();
Map<String, Object> instanceResources = resourcesToRemove.get(instanceId);
try {
deallocateVirtualMachine(client, stackName, instanceId);
deleteVirtualMachine(client, stackName, instanceId);
if (instanceResources != null) {
deleteNetworkInterfaces(client, stackName, (Iterable<String>) instanceResources.get(NETWORK_INTERFACES_NAMES));
deletePublicIps(client, stackName, (Iterable<String>) instanceResources.get(PUBLIC_ADDRESS_NAME));
deleteDisk((Iterable<String>) instanceResources.get(STORAGE_PROFILE_DISK_NAMES), client, resourceGroupName, (String) instanceResources.get(ATTACHED_DISK_STORAGE_NAME), diskContainer);
deleteManagedDisks((Iterable<String>) instanceResources.get(MANAGED_DISK_IDS), client);
if (azureStorage.getArmAttachedStorageOption(stack.getParameters()) == ArmAttachedStorageOption.PER_VM) {
azureStorage.deleteStorage(client, (String) instanceResources.get(ATTACHED_DISK_STORAGE_NAME), resourceGroupName);
}
}
} catch (CloudConnectorException e) {
throw e;
} catch (RuntimeException e) {
throw new CloudConnectorException(String.format("Failed to cleanup resources after downscale: %s", stackName), e);
}
}
return check(ac, resources);
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class CloudMetaDataStatuses method createCloudVmMetaDataStatuses.
private List<CloudVmMetaDataStatus> createCloudVmMetaDataStatuses(Collection<CloudInstance> cloudInstances) {
List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
for (Entry<String, CloudVmMetaDataStatus> stringCloudVmMetaDataStatusEntry : instanceMap.entrySet()) {
CloudVmMetaDataStatus oldCloudVmMetaDataStatus = stringCloudVmMetaDataStatusEntry.getValue();
InstanceTemplate oldTemplate = oldCloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance().getTemplate();
Optional<CloudInstance> cloudInstance = cloudInstances.stream().filter(instance -> Objects.equals(instance.getTemplate().getPrivateId(), oldTemplate.getPrivateId())).findFirst();
if (cloudInstance.isPresent()) {
CloudInstance newCloudInstance = new CloudInstance(stringCloudVmMetaDataStatusEntry.getKey(), cloudInstance.get().getTemplate(), cloudInstance.get().getAuthentication(), cloudInstance.get().getParameters());
CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(newCloudInstance, oldCloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus());
CloudVmMetaDataStatus newCloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, oldCloudVmMetaDataStatus.getMetaData());
cloudVmMetaDataStatuses.add(newCloudVmMetaDataStatus);
}
}
return cloudVmMetaDataStatuses;
}
Aggregations