use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSResetServiceTest method setVMSecurityGroupsToBeDeleted.
private void setVMSecurityGroupsToBeDeleted(Instance instance, ComputeState vm) {
// This assert is only suitable for real (non-mocking env).
if (this.isMock) {
return;
}
// 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);
for (SecurityGroupState currentSGState : currentSGNamesToStates.values()) {
// Ensure that the requested SecurityGroup was actually provisioned
if (currentSGState.name.contains(TestAWSSetupUtils.AWS_NEW_GROUP_PREFIX)) {
this.sgToCleanUp = currentSGState.id;
}
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method collectEbsDiskLinks.
/**
* Collect EBS selfLinks by querying for all EBS disk IDs. Map these selfLinks to
* their respective instances. These will be used for reconciliation with local state while
* creating/updating compute state's diskLinks.
*/
private void collectEbsDiskLinks(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
List<String> diskIds = new ArrayList<>();
Map<String, Instance> instancesByDiskIds = new HashMap<>();
context.request.instancesToBeCreated.stream().forEach(instance -> {
instance.getBlockDeviceMappings().stream().forEach(instanceBlockDeviceMapping -> {
String id = instanceBlockDeviceMapping.getEbs().getVolumeId();
instancesByDiskIds.put(id, instance);
diskIds.add(id);
});
});
context.request.instancesToBeUpdated.values().stream().forEach(instance -> {
instance.getBlockDeviceMappings().stream().forEach(instanceBlockDeviceMapping -> {
String id = instanceBlockDeviceMapping.getEbs().getVolumeId();
instancesByDiskIds.put(id, instance);
diskIds.add(id);
});
});
// No disks found for current page of instances.
if (diskIds.isEmpty()) {
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
return;
}
Query ebsQuery = Query.Builder.create().addKindFieldClause(DiskState.class).addCollectionItemClause(ResourceState.FIELD_NAME_ENDPOINT_LINKS, context.request.endpointLink).addInClause(DiskState.FIELD_NAME_ID, diskIds).build();
QueryTask ebsQueryTask = QueryTask.Builder.createDirectTask().setQuery(ebsQuery).addOption(QueryOption.EXPAND_CONTENT).build();
ebsQueryTask.tenantLinks = context.request.tenantLinks;
QueryUtils.startInventoryQueryTask(this, ebsQueryTask).whenComplete((qrt, e) -> {
if (e != null) {
logWarning("Error querying diskLinks for endpoint %s", context.request.endpointLink);
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
return;
}
if (qrt.results != null && qrt.results.documentCount > 0) {
qrt.results.documents.entrySet().forEach(entry -> {
DiskState state = Utils.fromJson(entry.getValue(), DiskState.class);
Instance instance = instancesByDiskIds.get(state.id);
if (instance != null) {
if (context.diskLinksByInstances.containsKey(instance)) {
context.diskLinksByInstances.get(instance).add(entry.getKey());
} else {
context.diskLinksByInstances.put(instance, new ArrayList<>(Arrays.asList(entry.getKey())));
}
}
});
}
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
});
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method addUpdateOrRemoveNICStates.
/**
* From the previously calculated NICs delta (based on whether the local state correspond to
* existing AWS object, the AWS object was deleted, or a new AWS object was added): 1) Create,
* update or delete NICState objects 2) Update the CS's references to the added or removed
* NetworkInterfaceStates
*/
private Map<String, Map<String, Collection<Object>>> addUpdateOrRemoveNICStates(AWSComputeStateCreationContext context, Instance instance, Map<String, List<Integer>> nicsDeviceIndexDeltaMap, String existingEndpointLink, Set<String> endpointLinks) {
List<NetworkInterfaceState> existingNicStates = context.request.nicStatesToBeUpdated.get(instance.getInstanceId());
// Generate operation for adding NIC state and description, and retain its link to add to
// the CS
List<Integer> deviceIndexesToAdd = nicsDeviceIndexDeltaMap.get(ADD_NIC_STATES);
Collection<Object> networkInterfaceLinksToBeAdded = instance.getNetworkInterfaces().stream().filter(awsNic -> deviceIndexesToAdd.contains(awsNic.getAttachment().getDeviceIndex()) && context.request.enumeratedNetworks != null && context.request.enumeratedNetworks.subnets != null && context.request.enumeratedNetworks.subnets.containsKey(awsNic.getSubnetId())).map(awsNic -> createNICStateAndDescription(context, awsNic, existingEndpointLink, endpointLinks)).map(addedNicState -> UriUtils.buildUriPath(NetworkInterfaceService.FACTORY_LINK, addedNicState.documentSelfLink)).collect(Collectors.toList());
// Generate operation for removing NIC states, and retain its link to remove from the CS {{
List<Integer> deviceIndexesToRemove = nicsDeviceIndexDeltaMap.get(REMOVE_NIC_STATES);
Collection<Object> networkInterfaceLinksToBeRemoved = deviceIndexesToRemove.stream().map(deviceIndexToRemove -> {
NetworkInterfaceState stateToDelete = existingNicStates.stream().filter(existNicState -> existNicState.deviceIndex == deviceIndexToRemove).findFirst().orElse(null);
return stateToDelete;
}).filter(existingNicState -> existingNicState != null).map(existingNicState -> deleteNICState(context, existingNicState)).map(existingNicState -> existingNicState.documentSelfLink).collect(Collectors.toList());
// Generate operation for updating NIC states, no links should be updated on CS in this case
List<Integer> deviceIndexesToUpdate = nicsDeviceIndexDeltaMap.get(UPDATE_NIC_STATES);
deviceIndexesToUpdate.stream().map(deviceIndexToUpdate -> existingNicStates.stream().filter(existNicState -> existNicState.deviceIndex == deviceIndexToUpdate).findFirst().orElse(null)).filter(existingNicState -> existingNicState != null).forEach(existingNicState -> updateNICState(context, instance, existingNicState));
if (context.request.nicStatesToBeDeleted.size() > 0 && context.request.nicStatesToBeDeleted.get(instance.getInstanceId()) != null) {
this.logInfo(() -> String.format("Compute %s failed to discover %d nics", instance.getInstanceId(), context.request.nicStatesToBeDeleted.get(instance.getInstanceId()).size()));
networkInterfaceLinksToBeRemoved.addAll(context.request.nicStatesToBeDeleted.get(instance.getInstanceId()));
}
Map<String, Map<String, Collection<Object>>> nicsDeltaMap = new HashMap<>();
// only add the collections to the delta map in case there is something to add/remove
if (!networkInterfaceLinksToBeRemoved.isEmpty()) {
Map<String, Collection<Object>> collectionsToRemoveMap = new HashMap<>();
collectionsToRemoveMap.put(ComputeState.FIELD_NAME_NETWORK_INTERFACE_LINKS, networkInterfaceLinksToBeRemoved);
nicsDeltaMap.put(REMOVE_NIC_STATES, collectionsToRemoveMap);
}
if (!networkInterfaceLinksToBeAdded.isEmpty()) {
Map<String, Collection<Object>> collectionsToAddMap = new HashMap<>();
collectionsToAddMap.put(ComputeState.FIELD_NAME_NETWORK_INTERFACE_LINKS, networkInterfaceLinksToBeAdded);
nicsDeltaMap.put(ADD_NIC_STATES, collectionsToAddMap);
}
return nicsDeltaMap;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSEnumerationAndDeletionAdapterService method retireComputeStates.
/**
* Creates operations to retire all the compute states in the local system for which the AWS
* instance has been terminated/missing from the remote instance.
*/
private void retireComputeStates(EnumerationDeletionContext context) {
List<Operation> operations = new ArrayList<>();
// Disks.
for (ComputeState cs : context.instancesToBeDeleted) {
ComputeState cps = new ComputeState();
cps.powerState = PowerState.OFF;
cps.lifecycleState = LifecycleState.RETIRED;
Operation operation = Operation.createPatch(this.getHost(), cs.documentSelfLink).setBody(cps).setReferer(getHost().getUri());
operations.add(operation);
}
// Kick off patch operations with a join handler.
if (operations == null || operations.size() == 0) {
logFine(() -> "No local compute states to be deleted.");
deleteResourcesInLocalSystem(context);
return;
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Failure retiring local compute states: %s ", Utils.toString(exc)));
deleteResourcesInLocalSystem(context);
return;
}
logFine(() -> "Successfully retired local compute states.");
deleteResourcesInLocalSystem(context);
return;
};
OperationJoin joinOp = OperationJoin.create(operations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(getHost());
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class TestAWSProvisionTask method assertVMSercurityGroupsConfiguration.
private void assertVMSercurityGroupsConfiguration(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