Search in sources :

Example 1 with InstanceNetworkInterface

use of com.amazonaws.services.ec2.model.InstanceNetworkInterface in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method alreadyBound.

public boolean alreadyBound() throws MalformedURLException {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2Service = getEC2Service();
    List<InstanceNetworkInterface> instanceNetworkInterfaces = instanceData(myInstanceId, ec2Service).getNetworkInterfaces();
    List<String> candidateIPs = getCandidateIps();
    for (String ip : candidateIPs) {
        for (InstanceNetworkInterface ini : instanceNetworkInterfaces) {
            if (ip.equals(ini.getPrivateIpAddress())) {
                logger.info("My instance {} seems to be already associated with the ip {}", myInstanceId, ip);
                return true;
            }
        }
    }
    return false;
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 2 with InstanceNetworkInterface

use of com.amazonaws.services.ec2.model.InstanceNetworkInterface in project photon-model by vmware.

the class TestAWSSetupUtils method detachNICDirectlyWithEC2Client.

/**
 * 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;
    });
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) DetachNetworkInterfaceRequest(com.amazonaws.services.ec2.model.DetachNetworkInterfaceRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface)

Example 3 with InstanceNetworkInterface

use of com.amazonaws.services.ec2.model.InstanceNetworkInterface in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method unbind.

/**
     * Unbind the IP that this instance is associated with.
     */
public void unbind() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2 = getEC2Service();
    List<InstanceNetworkInterface> result = instanceData(myInstanceId, ec2).getNetworkInterfaces();
    List<String> ips = getCandidateIps();
    for (InstanceNetworkInterface networkInterface : result) {
        if (ips.contains(networkInterface.getPrivateIpAddress())) {
            String attachmentId = networkInterface.getAttachment().getAttachmentId();
            ec2.detachNetworkInterface(new DetachNetworkInterfaceRequest().withAttachmentId(attachmentId));
            break;
        }
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 4 with InstanceNetworkInterface

use of com.amazonaws.services.ec2.model.InstanceNetworkInterface in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method populateCreateOperations.

/**
 * 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)) {
                // 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);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) Instance(com.amazonaws.services.ec2.model.Instance) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) HashSet(java.util.HashSet)

Example 5 with InstanceNetworkInterface

use of com.amazonaws.services.ec2.model.InstanceNetworkInterface in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method updateNICState.

/**
 * 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;
    }
    // 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;
}
Also used : NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Aggregations

InstanceNetworkInterface (com.amazonaws.services.ec2.model.InstanceNetworkInterface)4 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)3 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 Instance (com.amazonaws.services.ec2.model.Instance)2 AmazonInfo (com.netflix.appinfo.AmazonInfo)2 InstanceInfo (com.netflix.appinfo.InstanceInfo)2 AdapterUtils.createDeleteOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation)2 AdapterUtils.createPatchOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation)2 AdapterUtils.createPostOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation)2 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)2 Operation (com.vmware.xenon.common.Operation)2 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DetachNetworkInterfaceRequest (com.amazonaws.services.ec2.model.DetachNetworkInterfaceRequest)1 Filter (com.amazonaws.services.ec2.model.Filter)1 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)1 AWSUtils.getAWSNonTerminatedInstancesFilter (com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter)1 InstanceDescKey (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey)1 ZoneData (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData)1 AWSEnumerationUtils.mapInstanceToComputeState (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState)1