use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.
the class AWSComputeDiskDay2Service method getAvailableDeviceName.
private String getAvailableDeviceName(DiskContext context, String instanceId) {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceId);
DescribeInstancesResult instancesResult = context.amazonEC2Client.describeInstances(describeInstancesRequest);
List<InstanceBlockDeviceMapping> blockDeviceMappings = null;
AWSSupportedOS platform = null;
AWSSupportedVirtualizationTypes virtualizationTypes = null;
String instanceType = null;
for (Reservation reservation : instancesResult.getReservations()) {
for (Instance instance : reservation.getInstances()) {
if (instance.getInstanceId().equals(instanceId)) {
blockDeviceMappings = instance.getBlockDeviceMappings();
platform = AWSSupportedOS.get(instance.getPlatform());
virtualizationTypes = AWSSupportedVirtualizationTypes.get(instance.getVirtualizationType());
instanceType = instance.getInstanceType();
break;
}
}
}
String deviceName = null;
if (blockDeviceMappings != null) {
List<String> usedDeviceNames = getUsedDeviceNames(blockDeviceMappings);
List<String> availableDiskNames = AWSBlockDeviceNameMapper.getAvailableNames(platform, virtualizationTypes, AWSStorageType.EBS, instanceType, usedDeviceNames);
deviceName = availableDiskNames.get(0);
}
return deviceName;
}
use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.
the class AWSComputeDiskDay2Service method startInstance.
/**
* start the instance and on success updates the disk and compute state to reflect the detach information.
*/
private void startInstance(AmazonEC2AsyncClient client, DiskContext c, DeferredResult<DiskContext> dr, OperationContext opCtx) {
StartInstancesRequest startRequest = new StartInstancesRequest();
startRequest.withInstanceIds(c.baseAdapterContext.child.id);
client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
protected void handleError(Exception e) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start the instance %s. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
}
@Override
protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
if (e != null) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Instance %s failed to reach " + "running state. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
return;
}
logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully started the " + "instance %s", result.getStartingInstances().get(0).getInstanceId()));
updateComputeAndDiskState(dr, c, opCtx);
});
}
});
}
use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.
the class AWSComputeDiskDay2Service method performDetachOperation.
private DeferredResult<DiskContext> performDetachOperation(DiskContext context) {
DeferredResult<DiskContext> dr = new DeferredResult<>();
try {
validateDetachInfo(context.diskState);
if (context.request.isMockRequest) {
updateComputeAndDiskState(dr, context, null);
return dr;
}
String instanceId = context.computeState.id;
if (instanceId == null || !instanceId.startsWith(AWS_INSTANCE_ID_PREFIX)) {
return logAndGetFailedDr(context, "compute id cannot be empty");
}
String diskId = context.diskState.id;
if (diskId == null || !diskId.startsWith(AWS_VOLUME_ID_PREFIX)) {
return logAndGetFailedDr(context, "disk id cannot be empty");
}
// stop the instance, detach the disk and then start the instance.
if (context.baseAdapterContext.child.powerState.equals(ComputeService.PowerState.ON)) {
StopInstancesRequest stopRequest = new StopInstancesRequest();
stopRequest.withInstanceIds(context.baseAdapterContext.child.id);
context.amazonEC2Client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
protected void handleError(Exception e) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start compute. %s", Utils.toString(e)));
OperationContext.restoreOperationContext(this.opContext);
context.error = e;
dr.complete(context);
}
@Override
protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
OperationContext.restoreOperationContext(this.opContext);
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", context.amazonEC2Client, (is, e) -> {
if (e != null) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to stop " + "the compute. %s", Utils.toString(e)));
context.error = e;
dr.complete(context);
return;
}
logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully stopped " + "the instance %s", instanceId));
// detach disk from the instance.
detachVolume(context, dr, instanceId, diskId, true);
});
}
});
} else {
detachVolume(context, dr, instanceId, diskId, false);
}
} catch (Exception e) {
context.error = e;
return DeferredResult.completed(context);
}
return dr;
}
use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.
the class AWSComputeDiskDay2ServiceTest method assertAndSetVMSecurityGroupsToBeDeleted.
private void assertAndSetVMSecurityGroupsToBeDeleted(Instance instance, ComputeState vm) {
// This assert is only suitable for real (non-mocking env).
if (this.isMock) {
return;
}
this.host.log(Level.INFO, "%s: Assert security groups configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name);
// Get the SecurityGroupStates that were provided in the request ComputeState
Collector<SecurityGroupService.SecurityGroupState, ?, Map<String, SecurityGroupService.SecurityGroupState>> convertToMap = Collectors.<SecurityGroupService.SecurityGroupState, String, SecurityGroupService.SecurityGroupState>toMap(sg -> sg.name, sg -> sg);
Map<String, SecurityGroupService.SecurityGroupState> currentSGNamesToStates = vm.networkInterfaceLinks.stream().map(nicLink -> this.host.getServiceState(null, NetworkInterfaceService.NetworkInterfaceState.class, UriUtils.buildUri(this.host, nicLink))).<// collect all SecurityGroup States from all NIC states
SecurityGroupService.SecurityGroupState>flatMap(nicState -> nicState.securityGroupLinks.stream().map(sgLink -> {
SecurityGroupService.SecurityGroupState sgState = this.host.getServiceState(null, SecurityGroupService.SecurityGroupState.class, UriUtils.buildUri(this.host, sgLink));
return sgState;
})).collect(convertToMap);
// Compare ComputeState after provisioning to the ComputeState in the request
assertNotNull("Instance should have security groups attached.", instance.getSecurityGroups());
// Provisioned Instance should have the same number of SecurityGroups as requested
assertEquals(instance.getSecurityGroups().size(), currentSGNamesToStates.size());
for (SecurityGroupService.SecurityGroupState currentSGState : currentSGNamesToStates.values()) {
// Get corresponding requested state
GroupIdentifier provisionedGroupIdentifier = null;
for (GroupIdentifier awsGroupIdentifier : instance.getSecurityGroups()) {
if (awsGroupIdentifier.getGroupId().equals(currentSGState.id)) {
provisionedGroupIdentifier = awsGroupIdentifier;
break;
}
}
// Ensure that the requested SecurityGroup was actually provisioned
assertNotNull(provisionedGroupIdentifier);
if (currentSGState.name.contains(TestAWSSetupUtils.AWS_NEW_GROUP_PREFIX)) {
this.sgToCleanUp = currentSGState.id;
SecurityGroup awsSecurityGroup = getSecurityGroupsIdUsingEC2Client(this.client, provisionedGroupIdentifier.getGroupId());
assertNotNull(awsSecurityGroup);
// Validate rules are correctly created as requested
IpPermission awsIngressRule = awsSecurityGroup.getIpPermissions().get(0);
IpPermission awsEgressRule = awsSecurityGroup.getIpPermissionsEgress().get(1);
assertNotNull(awsIngressRule);
assertNotNull(awsEgressRule);
assertEquals("Error in created ingress rule", awsIngressRule.getIpProtocol(), currentSGState.ingress.get(0).protocol);
assertEquals("Error in created ingress rule", awsIngressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.ingress.get(0).ipRangeCidr);
assertEquals("Error in created egress rule", awsEgressRule.getIpProtocol(), currentSGState.egress.get(0).protocol);
assertEquals("Error in created egress rule", awsEgressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.egress.get(0).ipRangeCidr);
}
}
}
use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.
the class AWSRebootServiceTest method assertAndSetVMSecurityGroupsToBeDeleted.
private void assertAndSetVMSecurityGroupsToBeDeleted(Instance instance, ComputeState vm) {
// This assert is only suitable for real (non-mocking env).
if (this.isMock) {
return;
}
this.host.log(Level.INFO, "%s: Assert security groups configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name);
// Get the SecurityGroupStates that were provided in the request ComputeState
Collector<SecurityGroupState, ?, Map<String, SecurityGroupState>> convertToMap = Collectors.<SecurityGroupState, String, SecurityGroupState>toMap(sg -> sg.name, sg -> sg);
Map<String, SecurityGroupState> currentSGNamesToStates = vm.networkInterfaceLinks.stream().map(nicLink -> this.host.getServiceState(null, NetworkInterfaceState.class, UriUtils.buildUri(this.host, nicLink))).<// collect all SecurityGroup States from all NIC states
SecurityGroupState>flatMap(nicState -> nicState.securityGroupLinks.stream().map(sgLink -> {
SecurityGroupState sgState = this.host.getServiceState(null, SecurityGroupState.class, UriUtils.buildUri(this.host, sgLink));
return sgState;
})).collect(convertToMap);
// Compare ComputeState after provisioning to the ComputeState in the request
assertNotNull("Instance should have security groups attached.", instance.getSecurityGroups());
// Provisioned Instance should have the same number of SecurityGroups as requested
assertEquals(instance.getSecurityGroups().size(), currentSGNamesToStates.size());
for (SecurityGroupState currentSGState : currentSGNamesToStates.values()) {
// Get corresponding requested state
GroupIdentifier provisionedGroupIdentifier = null;
for (GroupIdentifier awsGroupIdentifier : instance.getSecurityGroups()) {
if (awsGroupIdentifier.getGroupId().equals(currentSGState.id)) {
provisionedGroupIdentifier = awsGroupIdentifier;
break;
}
}
// Ensure that the requested SecurityGroup was actually provisioned
assertNotNull(provisionedGroupIdentifier);
if (currentSGState.name.contains(TestAWSSetupUtils.AWS_NEW_GROUP_PREFIX)) {
this.sgToCleanUp = currentSGState.id;
SecurityGroup awsSecurityGroup = getSecurityGroupsIdUsingEC2Client(this.client, provisionedGroupIdentifier.getGroupId());
assertNotNull(awsSecurityGroup);
// Validate rules are correctly created as requested
IpPermission awsIngressRule = awsSecurityGroup.getIpPermissions().get(0);
IpPermission awsEgressRule = awsSecurityGroup.getIpPermissionsEgress().get(1);
assertNotNull(awsIngressRule);
assertNotNull(awsEgressRule);
assertEquals("Error in created ingress rule", awsIngressRule.getIpProtocol(), currentSGState.ingress.get(0).protocol);
assertEquals("Error in created ingress rule", awsIngressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.ingress.get(0).ipRangeCidr);
assertEquals("Error in created egress rule", awsEgressRule.getIpProtocol(), currentSGState.egress.get(0).protocol);
assertEquals("Error in created egress rule", awsEgressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.egress.get(0).ipRangeCidr);
}
}
}
Aggregations