private boolean notEqualInstancess(List<Instance> l1, List<Instance> l2) { for (Instance i1 : l1) { Collections.sort(i1.getProductCodes(), new ProductCodeComparator()); Collections.sort(i1.getBlockDeviceMappings(), new InstanceBlockDeviceMappingComparator()); Collections.sort(i1.getTags(), new TagComparator()); Collections.sort(i1.getSecurityGroups(), new GroupIdentifierComparator()); Collections.sort(i1.getNetworkInterfaces(), new InstanceNetworkInterfaceComparator()); for (InstanceNetworkInterface ini1 : i1.getNetworkInterfaces()) { Collections.sort(ini1.getGroups(), new GroupIdentifierComparator()); Collections.sort(ini1.getPrivateIpAddresses(), new InstancePrivateIpAddressComparator()); } } for (Instance i2 : l2) { Collections.sort(i2.getProductCodes(), new ProductCodeComparator()); Collections.sort(i2.getBlockDeviceMappings(), new InstanceBlockDeviceMappingComparator()); Collections.sort(i2.getTags(), new TagComparator()); Collections.sort(i2.getSecurityGroups(), new GroupIdentifierComparator()); Collections.sort(i2.getNetworkInterfaces(), new InstanceNetworkInterfaceComparator()); for (InstanceNetworkInterface ini2 : i2.getNetworkInterfaces()) { Collections.sort(ini2.getGroups(), new GroupIdentifierComparator()); Collections.sort(ini2.getPrivateIpAddresses(), new InstancePrivateIpAddressComparator()); } } return notEqualCollection(l1, l2); }
/** * Removes a specified AWS NIC from the VM it is currently attached to */ public static void detachNICDirectlyWithEC2Client(String instanceId, String nicAttachmentId, String nicId, AmazonEC2Client client, VerificationHost host) { // detach the new AWS NIC to the AWS VM DetachNetworkInterfaceRequest detachNic = new DetachNetworkInterfaceRequest(); detachNic.withAttachmentId(nicAttachmentId); host.log("Detaching NIC with id: %s and attachment id: %s", nicId, nicAttachmentId); client.detachNetworkInterface(detachNic); host.waitFor("Timeout waiting for AWS to detach a NIC from " + instanceId + " with attachment id: " + nicAttachmentId, () -> { DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() .withFilters(new Filter("instance-id", Collections.singletonList(instanceId))); DescribeInstancesResult result = client.describeInstances(describeInstancesRequest); Instance currentInstance = result.getReservations().get(0).getInstances().get(0); for (InstanceNetworkInterface awsNic : currentInstance.getNetworkInterfaces()) { if (awsNic.getNetworkInterfaceId().equals(nicId)) { //Requested NIC was not detached from the instance return false; } } host.log("Detached NIC with attachment id: %s", nicAttachmentId); return true; }); }
private List<HostAndPort> listHosts(AmazonEC2 client) throws Exception { List<HostAndPort> hosts = newArrayList(); DescribeInstancesRequest request = new DescribeInstancesRequest().withFilters(_filters); String nextToken = null; LOG.log(FINER, "Discovering instances: filters={0}", new Object[]{_filters}); do { DescribeInstancesResult result = client.describeInstances(request.withNextToken(nextToken)); for (Reservation reservation : result.getReservations()) { for (Instance instance : reservation.getInstances()) { LOG.log(FINER, "Instance state: {0} {1}", new Object[]{instance.getInstanceId(), instance.getState().getName()}); if ("running".equalsIgnoreCase(instance.getState().getName())) { List<InstanceNetworkInterface> networkInterfaces = instance.getNetworkInterfaces(); if (networkInterfaces.size() < 1) { LOG.log(WARNING, "No network interface found on instance {0}", instance.getInstanceId()); } else { InstanceNetworkInterface nic = networkInterfaces.get(0); String ip; if (_usePublicIP) { ip = nic.getAssociation().getPublicIp(); } else { ip = nic.getPrivateIpAddress(); } hosts.add(HostAndPort.fromString(ip)); } } } } nextToken = result.getNextToken(); } while (nextToken != null); return hosts; }
public boolean notEqualNetworkInterfaces(List<InstanceNetworkInterface> l1, List<InstanceNetworkInterface> l2) { for (InstanceNetworkInterface ini1 : l1) { sortInstanceNetworkInterface(ini1); } for (InstanceNetworkInterface ini2 : l2) { sortInstanceNetworkInterface(ini2); } return notEqualCollection(l1, l2); }
private void generateEvents4Attachment(Collection<Event> result, InstanceNetworkInterface ini, Ec2Instance oldResource, Ec2Instance newResource) { String status = ini.getAttachment().getStatus(); if (status.equals("attaching")) { result.add(createEvent(oldResource, newResource, EventType.Ec2_Instance_Network_Interface_Attaching, ini.getNetworkInterfaceId())); } else if (status.equals("attached")) { result.add(createEvent(oldResource, newResource, EventType.Ec2_Instance_Network_Interface_Attached, ini.getNetworkInterfaceId())); } else if (status.equals("detaching")) { result.add(createEvent(oldResource, newResource, EventType.Ec2_Instance_Network_Interface_Detaching, ini.getNetworkInterfaceId())); } else { log.error("not handled attachment status:{}", status); result.add(createEvent(oldResource, newResource, EventType.Unknown)); } }
public Map<String, InstanceNetworkInterface> generateNetworkInterfaceId(List<InstanceNetworkInterface> networkInterfaces) { Map<String, InstanceNetworkInterface> result = new HashMap<>(); for (InstanceNetworkInterface networkInterface : networkInterfaces) { result.put(networkInterface.getNetworkInterfaceId(), networkInterface); } return result; }
public CompareResult<InstanceNetworkInterface> compareNetworkInterfaces(List<InstanceNetworkInterface> oldNetworkInterfaces, List<InstanceNetworkInterface> newNetworkInterfaces) { Validate.noNullElements(new Object[]{oldNetworkInterfaces, newNetworkInterfaces}); CompareResult<InstanceNetworkInterface> result = new CompareResult<>(); Map<String, InstanceNetworkInterface> oldNetworkInterfaceIdMap = generateNetworkInterfaceId(oldNetworkInterfaces); Map<String, InstanceNetworkInterface> newNetworkInterfaceIdMap = generateNetworkInterfaceId(newNetworkInterfaces); for (String newKey : newNetworkInterfaceIdMap.keySet()) { InstanceNetworkInterface newINI = newNetworkInterfaceIdMap.get(newKey); InstanceNetworkInterface oldINI = oldNetworkInterfaceIdMap.get(newKey); if (oldINI != null) { Ec2Instance.sortInstanceNetworkInterface(oldINI); Ec2Instance.sortInstanceNetworkInterface(newINI); if (!oldINI.equals(newINI)) { result.getUpdate().add(Pair.of(oldINI, newINI)); } } else { result.getAdd().add(newINI); } oldNetworkInterfaceIdMap.remove(newKey); } for (String oldKey : oldNetworkInterfaceIdMap.keySet()) { InstanceNetworkInterface oldInstanceNetworkInterface = oldNetworkInterfaceIdMap.get(oldKey); result.getDelete().add(oldInstanceNetworkInterface); } return result; }
/** * Method to create Compute States associated with the instances received from the AWS host. */ private void populateCreateOperations(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) { if (context.request.instancesToBeCreated == null || context.request.instancesToBeCreated.size() == 0) { logFine(() -> "No local compute states to be created"); context.creationStage = next; handleComputeStateCreateOrUpdate(context); } else { logFine(() -> String.format("Need to create %d local compute states", context.request.instancesToBeCreated.size())); for (int i = 0; i < context.request.instancesToBeCreated.size(); i++) { Instance instance = context.request.instancesToBeCreated.get(i); String zoneId = instance.getPlacement().getAvailabilityZone(); ZoneData zoneData = context.request.zones.get(zoneId); String regionId = zoneData.regionId; InstanceDescKey descKey = InstanceDescKey.build(regionId, zoneId, instance.getInstanceType()); Set<String> endpointLinks = new HashSet<>(); endpointLinks.add(context.request.endpointLink); ComputeState computeStateToBeCreated = mapInstanceToComputeState( this.getHost(), instance, context.request.parentComputeLink, zoneData.computeLink, context.request.resourcePoolLink, null, endpointLinks, context.computeDescriptionMap.get(descKey), context.request.parentCDStatsAdapterReferences, context.internalTagLinksMap.get(ec2_instance.toString()), regionId, zoneId, context.request.tenantLinks, context.createdExternalTags, true, context.diskLinksByInstances.get(instance)); computeStateToBeCreated.networkInterfaceLinks = new ArrayList<>(); if (!AWSEnumerationUtils.instanceIsInStoppedState(instance)) { // for each NIC create Description and State create operations. Link the // ComputeState to be created to the NIC State for (InstanceNetworkInterface awsNic : instance.getNetworkInterfaces()) { if (context.request.enumeratedNetworks != null && context.request.enumeratedNetworks.subnets != null && context.request.enumeratedNetworks.subnets .containsKey(awsNic.getSubnetId())) { NetworkInterfaceState nicState = createNICStateAndDescription( context, awsNic, null, endpointLinks); computeStateToBeCreated.networkInterfaceLinks.add(UriUtils.buildUriPath( NetworkInterfaceService.FACTORY_LINK, nicState.documentSelfLink)); } } } Operation postComputeState = createPostOperation(this, computeStateToBeCreated, ComputeService.FACTORY_LINK); context.enumerationOperations.add(postComputeState); } context.creationStage = next; handleComputeStateCreateOrUpdate(context); } }
private NetworkInterfaceState createNICStateAndDescription( AWSComputeStateCreationContext context, InstanceNetworkInterface awsNic, String existingEndpointLink, Set<String> endpointLinks) { final NetworkInterfaceState nicState; { nicState = new NetworkInterfaceState(); nicState.id = awsNic.getNetworkInterfaceId(); nicState.name = nicState.id; nicState.address = awsNic.getPrivateIpAddress(); if (context.request.enumeratedNetworks != null && context.request.enumeratedNetworks.subnets != null) { nicState.subnetLink = context.request.enumeratedNetworks.subnets .get(awsNic.getSubnetId()); } nicState.tenantLinks = context.request.tenantLinks; // retain the existing link, if it is not null nicState.endpointLink = existingEndpointLink; if (nicState.endpointLink == null ) { nicState.endpointLink = context.request.endpointLink; } if (nicState.endpointLinks == null) { nicState.endpointLinks = new HashSet<String>(); } nicState.endpointLinks.addAll(endpointLinks); nicState.regionId = context.request.regionId; nicState.computeHostLink = context.request.parentComputeLink; Set<String> internalTagLinks = context.internalTagLinksMap .get(ec2_net_interface.toString()); // append internal tagLinks to any existing ones if (internalTagLinks != null && !internalTagLinks.isEmpty()) { if (nicState.tagLinks != null && !nicState.tagLinks.isEmpty()) { nicState.tagLinks.addAll(internalTagLinks); } else { nicState.tagLinks = new HashSet<>(); nicState.tagLinks.addAll(internalTagLinks); } } if (context.request.enumeratedSecurityGroups != null) { for (GroupIdentifier awsSG : awsNic.getGroups()) { // we should have updated the list of SG Ids before this step and // should have ensured that all the SGs exist locally String securityGroupLink = context.request.enumeratedSecurityGroups.securityGroupStates .get(awsSG.getGroupId()); if (securityGroupLink == null || securityGroupLink.isEmpty()) { continue; } if (nicState.securityGroupLinks == null) { nicState.securityGroupLinks = new ArrayList<>(); } nicState.securityGroupLinks.add(securityGroupLink); } } nicState.deviceIndex = awsNic.getAttachment().getDeviceIndex(); // Link is set, because it's referenced by CS before post nicState.documentSelfLink = UUID.randomUUID().toString(); Operation postNetworkInterfaceState = createPostOperation(this, nicState, NetworkInterfaceService.FACTORY_LINK); context.enumerationOperations .add(postNetworkInterfaceState); } return nicState; }
/** * For each NetworkInterfaceState, obtain the corresponding AWS NIC, and generate POST operation * to update its private address */ private NetworkInterfaceState updateNICState(AWSComputeStateCreationContext context, Instance instance, NetworkInterfaceState existingNicState) { InstanceNetworkInterface awsNic = instance .getNetworkInterfaces() .stream() .filter(currentAwsNic -> currentAwsNic.getAttachment() .getDeviceIndex() == existingNicState.deviceIndex) .findFirst().orElse(null); // create a new NetworkInterfaceState for updating the address NetworkInterfaceState updateNicState = new NetworkInterfaceState(); if (StringUtils.isEmpty(updateNicState.endpointLink)) { updateNicState.endpointLink = context.request.endpointLink; } // Use the endpointLinks from the existing state if present else initialize the collection and add the // endpoint link. if (existingNicState.endpointLinks == null) { updateNicState.endpointLinks = new HashSet<>(); } else { updateNicState.endpointLinks = existingNicState.endpointLinks; } updateNicState.endpointLinks.add(context.request.endpointLink); updateNicState.address = awsNic.getPrivateIpAddress(); if (context.request.enumeratedSecurityGroups != null) { for (GroupIdentifier awsSG : awsNic.getGroups()) { // we should have updated the list of SG Ids before this step and should have // ensured that all the SGs exist locally String securityGroupLink = context.request.enumeratedSecurityGroups.securityGroupStates .get(awsSG.getGroupId()); if (securityGroupLink == null || securityGroupLink.isEmpty()) { continue; } if (updateNicState.securityGroupLinks == null) { updateNicState.securityGroupLinks = new ArrayList<>(); } updateNicState.securityGroupLinks.add(securityGroupLink); } } // create update operation and add it for batch execution on the next stage Operation updateNicOperation = createPatchOperation(this, updateNicState, existingNicState.documentSelfLink); context.enumerationOperations.add(updateNicOperation); // If existing network state does not have an internal tag then create dedicated // patch to update the internal tag link. String networkInterfaceInternalTagLink = context.internalTagLinksMap .get(ec2_net_interface.toString()).iterator().next(); if (existingNicState.tagLinks == null || (existingNicState.tagLinks != null && !existingNicState.tagLinks.contains(networkInterfaceInternalTagLink))) { Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap( NetworkInterfaceState.FIELD_NAME_TAG_LINKS, Collections.singletonList(networkInterfaceInternalTagLink)); Map<String, Collection<Object>> collectionsToRemoveMap = Collections .singletonMap(NetworkInterfaceState.FIELD_NAME_TAG_LINKS, Collections.emptyList()); ServiceStateCollectionUpdateRequest updateTagLinksRequest = ServiceStateCollectionUpdateRequest .create(collectionsToAddMap, collectionsToRemoveMap); context.enumerationOperations.add(Operation.createPatch(this.getHost(), existingNicState.documentSelfLink) .setReferer(this.getUri()) .setBody(updateTagLinksRequest)); } return updateNicState; }
private void assertVmNetworksConfiguration(Instance awsInstance) throws Throwable { // This assert is only suitable for real (non-mocking env). if (this.isMock) { return; } this.host.log(Level.INFO, "%s: Assert network configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name); ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink)); assertNotNull( "ComputeState.address should be set to public IP.", vm.address); assertEquals("ComputeState.address should be set to AWS Instance public IP.", awsInstance.getPublicIpAddress(), vm.address); for (String nicLink : vm.networkInterfaceLinks) { NetworkInterfaceState nicState = this.host.getServiceState(null, NetworkInterfaceState.class, UriUtils.buildUri(this.host, nicLink)); // for now validate only the 0 NIC as we are creating single NIC VM if (nicState.deviceIndex != 0) { continue; } InstanceNetworkInterface awsNic = null; for (InstanceNetworkInterface nic : awsInstance.getNetworkInterfaces()) { if (nic.getAttachment().getDeviceIndex() == nicState.deviceIndex) { awsNic = nic; break; } } assertNotNull("Unable to find AWS NIC with device index " + nicState.deviceIndex, awsNic); assertEquals( "NetworkInterfaceState[" + nicState.deviceIndex + "].address should be set to AWS NIC private IP.", awsNic.getPrivateIpAddress(), nicState.address); } assertVMSercurityGroupsConfiguration(awsInstance, vm); }
@Override public List<InstanceNetworkInterface> getNetworkInterfaces() { return (List<InstanceNetworkInterface>) resource.getAttribute("NetworkInterfaces"); }
public static void sortInstanceNetworkInterface(InstanceNetworkInterface ini) { Collections.sort(ini.getGroups(), new GroupIdentifierComparator()); Collections.sort(ini.getPrivateIpAddresses(), new InstancePrivateIpAddressComparator()); }
@Override public int compare(InstanceNetworkInterface ini1, InstanceNetworkInterface ini2) { return new CompareToBuilder().append(ini1.getNetworkInterfaceId(), ini2.getNetworkInterfaceId()).toComparison(); }
/** * Gets the value of the NetworkInterfaces attribute. If this resource is * not yet loaded, a call to {@code load()} is made to retrieve the value of * the attribute. */ List<InstanceNetworkInterface> getNetworkInterfaces();