Example usage for com.amazonaws.services.ec2.model Instance getIamInstanceProfile

List of usage examples for com.amazonaws.services.ec2.model Instance getIamInstanceProfile

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model Instance getIamInstanceProfile.

Prototype


public IamInstanceProfile getIamInstanceProfile() 

Source Link

Document

The IAM instance profile associated with the instance, if applicable.

Usage

From source file:com.clouck.model.aws.ec2.Ec2Instance.java

@Override
@SuppressWarnings("rawtypes")
protected boolean isEqual(AbstractResource newResource) {
    Instance oldInstance = this.getResource();
    Ec2Instance newEc2Instance = (Ec2Instance) newResource;
    Instance newInstance = newEc2Instance.getResource();

    if (notEqual(oldInstance.getInstanceId(), newInstance.getInstanceId()))
        return false;
    if (notEqual(oldInstance.getImageId(), newInstance.getImageId()))
        return false;
    if (notEqual(oldInstance.getState(), newInstance.getState()))
        return false;
    if (notEqual(oldInstance.getPrivateDnsName(), newInstance.getPrivateDnsName()))
        return false;
    if (notEqual(oldInstance.getPublicDnsName(), newInstance.getPublicDnsName()))
        return false;
    if (notEqual(oldInstance.getStateTransitionReason(), newInstance.getStateTransitionReason()))
        return false;
    if (notEqual(oldInstance.getKeyName(), newInstance.getKeyName()))
        return false;
    if (notEqual(oldInstance.getAmiLaunchIndex(), newInstance.getAmiLaunchIndex()))
        return false;
    if (notEqualCollection(oldInstance.getProductCodes(), newInstance.getProductCodes()))
        return false;
    if (notEqual(oldInstance.getInstanceType(), newInstance.getInstanceType()))
        return false;
    if (notEqual(oldInstance.getLaunchTime(), newInstance.getLaunchTime()))
        return false;
    if (notEqual(oldInstance.getPlacement(), newInstance.getPlacement()))
        return false;
    if (notEqual(oldInstance.getKernelId(), newInstance.getKernelId()))
        return false;
    if (notEqual(oldInstance.getRamdiskId(), newInstance.getRamdiskId()))
        return false;
    if (notEqual(oldInstance.getPlatform(), newInstance.getPlatform()))
        return false;
    if (notEqual(oldInstance.getMonitoring(), newInstance.getMonitoring()))
        return false;
    if (notEqual(oldInstance.getSubnetId(), newInstance.getSubnetId()))
        return false;
    if (notEqual(oldInstance.getVpcId(), newInstance.getVpcId()))
        return false;
    if (notEqual(oldInstance.getPrivateIpAddress(), newInstance.getPrivateIpAddress()))
        return false;
    if (notEqual(oldInstance.getPublicIpAddress(), newInstance.getPublicIpAddress()))
        return false;
    if (notEqual(oldInstance.getStateReason(), newInstance.getStateReason()))
        return false;
    if (notEqual(oldInstance.getArchitecture(), newInstance.getArchitecture()))
        return false;
    if (notEqual(oldInstance.getRootDeviceType(), newInstance.getRootDeviceType()))
        return false;
    if (notEqual(oldInstance.getRootDeviceName(), newInstance.getRootDeviceName()))
        return false;
    if (notEqualCollection(oldInstance.getBlockDeviceMappings(), newInstance.getBlockDeviceMappings()))
        return false;
    if (notEqual(oldInstance.getVirtualizationType(), newInstance.getVirtualizationType()))
        return false;
    if (notEqual(oldInstance.getInstanceLifecycle(), newInstance.getInstanceLifecycle()))
        return false;
    if (notEqual(oldInstance.getSpotInstanceRequestId(), newInstance.getSpotInstanceRequestId()))
        return false;
    if (notEqual(oldInstance.getLicense(), newInstance.getLicense()))
        return false;
    if (notEqual(oldInstance.getClientToken(), newInstance.getClientToken()))
        return false;
    if (notEqualCollection(oldInstance.getTags(), newInstance.getTags()))
        return false;
    if (notEqualCollection(oldInstance.getSecurityGroups(), newInstance.getSecurityGroups()))
        return false;
    if (notEqual(oldInstance.getSourceDestCheck(), newInstance.getSourceDestCheck()))
        return false;
    if (notEqual(oldInstance.getHypervisor(), newInstance.getHypervisor()))
        return false;
    if (notEqualNetworkInterfaces(oldInstance.getNetworkInterfaces(), newInstance.getNetworkInterfaces()))
        return false;
    if (notEqual(oldInstance.getIamInstanceProfile(), newInstance.getIamInstanceProfile()))
        return false;
    if (notEqual(oldInstance.getEbsOptimized(), newInstance.getEbsOptimized()))
        return false;
    if (notEqual(this.getTerminationProtection(), newEc2Instance.getTerminationProtection()))
        return false;
    if (notEqual(this.getShutdownBehavior(), newEc2Instance.getShutdownBehavior()))
        return false;
    if (notEqual(this.getUserData(), newEc2Instance.getUserData()))
        return false;

    return true;//  w  w  w. ja va  2s .c om
}

From source file:com.haskins.cloudtrailviewer.dialog.resourcedetail.detailpanels.EC2InstanceDetail.java

License:Open Source License

private void buildUI(DescribeInstancesResult detail) {

    JTabbedPane tabs = new JTabbedPane();
    tabs.add("Instance", primaryScrollPane);
    tabs.add("Tags", tagsScrollPane);

    this.add(tabs, BorderLayout.CENTER);

    List<Reservation> reservations = detail.getReservations();
    if (!reservations.isEmpty()) {

        Reservation reservation = reservations.get(0);
        List<Instance> instances = reservation.getInstances();
        if (!instances.isEmpty()) {

            Instance instance = instances.get(0);

            if (instance.getAmiLaunchIndex() != null) {
                primaryTableModel.addRow(new Object[] { "AMI Launch Index", instance.getAmiLaunchIndex() });
            }/* w ww  .  ja v  a 2  s  . c  o m*/
            if (instance.getArchitecture() != null) {
                primaryTableModel.addRow(new Object[] { "Architecture", instance.getArchitecture() });
            }
            if (instance.getAmiLaunchIndex() != null) {
                primaryTableModel.addRow(new Object[] { "Block Mapping Device", "" });
            }
            if (instance.getClientToken() != null) {
                primaryTableModel.addRow(new Object[] { "Client Token", instance.getClientToken() });
            }
            primaryTableModel.addRow(new Object[] { "EBS Optimised", instance.isEbsOptimized() });
            if (instance.getHypervisor() != null) {
                primaryTableModel.addRow(new Object[] { "Hypervisor", instance.getHypervisor() });
            }
            if (instance.getIamInstanceProfile() != null) {
                primaryTableModel.addRow(
                        new Object[] { "Instance Profile", instance.getIamInstanceProfile().toString() });
            }
            if (instance.getImageId() != null) {
                primaryTableModel.addRow(new Object[] { "Image Id", instance.getImageId() });
            }
            if (instance.getInstanceId() != null) {
                primaryTableModel.addRow(new Object[] { "Instance Id", instance.getInstanceId() });
            }
            if (instance.getInstanceLifecycle() != null) {
                primaryTableModel.addRow(new Object[] { "Instance Lifecyle", instance.getInstanceLifecycle() });
            }
            if (instance.getInstanceType() != null) {
                primaryTableModel.addRow(new Object[] { "Instance Type", instance.getInstanceType() });
            }
            if (instance.getKernelId() != null) {
                primaryTableModel.addRow(new Object[] { "Kernel Id", instance.getKernelId() });
            }
            if (instance.getKeyName() != null) {
                primaryTableModel.addRow(new Object[] { "Key Name", instance.getKeyName() });
            }
            if (instance.getLaunchTime() != null) {
                primaryTableModel.addRow(new Object[] { "Launch Time", instance.getLaunchTime() });
            }
            if (instance.getMonitoring() != null) {
                primaryTableModel.addRow(new Object[] { "Monitoring", instance.getMonitoring().toString() });
            }
            if (instance.getPlacement() != null) {
                primaryTableModel
                        .addRow(new Object[] { "Placement", instance.getPlacement().getAvailabilityZone() });
            }
            if (instance.getPlatform() != null) {
                primaryTableModel.addRow(new Object[] { "Platform", instance.getPlatform() });
            }
            if (instance.getPrivateDnsName() != null) {
                primaryTableModel.addRow(new Object[] { "Private DNS Name", instance.getPrivateDnsName() });
            }
            if (instance.getPrivateIpAddress() != null) {
                primaryTableModel.addRow(new Object[] { "Private IP Address", instance.getPrivateIpAddress() });
            }
            if (instance.getAmiLaunchIndex() != null) {
                primaryTableModel.addRow(new Object[] { "Product Codes", "" });
            }
            if (instance.getPublicDnsName() != null) {
                primaryTableModel.addRow(new Object[] { "Public DNS Name", instance.getPublicDnsName() });
            }
            if (instance.getPublicIpAddress() != null) {
                primaryTableModel.addRow(new Object[] { "Public IP Address", instance.getPublicIpAddress() });
            }
            if (instance.getRamdiskId() != null) {
                primaryTableModel.addRow(new Object[] { "Ram Disk Id", instance.getRamdiskId() });
            }
            if (instance.getRootDeviceName() != null) {
                primaryTableModel.addRow(new Object[] { "Root Device Name", instance.getRootDeviceName() });
            }
            if (instance.getRootDeviceType() != null) {
                primaryTableModel.addRow(new Object[] { "Root Device Type", instance.getRootDeviceType() });
            }
            if (instance.getAmiLaunchIndex() != null) {
                primaryTableModel.addRow(new Object[] { "Security Groups", "" });
            }
            if (instance.getSourceDestCheck() != null) {
                primaryTableModel
                        .addRow(new Object[] { "Source Destination Check", instance.getSourceDestCheck() });
            }
            if (instance.getSpotInstanceRequestId() != null) {
                primaryTableModel.addRow(
                        new Object[] { "Spot Instance Request Id", instance.getSpotInstanceRequestId() });
            }
            if (instance.getSriovNetSupport() != null) {
                primaryTableModel
                        .addRow(new Object[] { "Sriov network Support", instance.getSriovNetSupport() });
            }
            if (instance.getState() != null) {
                primaryTableModel.addRow(new Object[] { "State", instance.getState().getName() });
            }
            if (instance.getStateReason() != null) {
                primaryTableModel
                        .addRow(new Object[] { "State Reason", instance.getStateReason().getMessage() });
            }
            if (instance.getSubnetId() != null) {
                primaryTableModel.addRow(new Object[] { "Subnet Id", instance.getSubnetId() });
            }
            if (instance.getVirtualizationType() != null) {
                primaryTableModel
                        .addRow(new Object[] { "Virtualisation Type", instance.getVirtualizationType() });
            }
            if (instance.getVpcId() != null) {
                primaryTableModel.addRow(new Object[] { "Vpc Id", instance.getVpcId() });
            }

            List<Tag> tags = instance.getTags();
            for (Tag tag : tags) {
                tagsTableModel.addRow(new Object[] { tag.getKey(), tag.getValue() });
            }
        }
    }
}

From source file:hudson.plugins.ec2.SlaveTemplate.java

License:Open Source License

/**
 * Provisions an On-demand EC2 slave by launching a new instance or 
 * starting a previously-stopped instance.
 */// ww  w. ja v  a2  s  . c om
private EC2AbstractSlave provisionOndemand(TaskListener listener) throws AmazonClientException, IOException {
    PrintStream logger = listener.getLogger();
    AmazonEC2 ec2 = getParent().connect();

    try {
        String msg = "Launching " + ami + " for template " + description;
        logger.println(msg);
        LOGGER.info(msg);

        KeyPair keyPair = getKeyPair(ec2);

        RunInstancesRequest riRequest = new RunInstancesRequest(ami, 1, 1);
        InstanceNetworkInterfaceSpecification net = new InstanceNetworkInterfaceSpecification();

        if (useEphemeralDevices) {
            setupEphemeralDeviceMapping(riRequest);
        } else {
            setupCustomDeviceMapping(riRequest);
        }

        List<Filter> diFilters = new ArrayList<Filter>();
        diFilters.add(new Filter("image-id").withValues(ami));

        if (StringUtils.isNotBlank(getZone())) {
            Placement placement = new Placement(getZone());
            if (getUseDedicatedTenancy()) {
                placement.setTenancy("dedicated");
            }
            riRequest.setPlacement(placement);
            diFilters.add(new Filter("availability-zone").withValues(getZone()));
        }

        if (StringUtils.isNotBlank(getSubnetId())) {
            if (getAssociatePublicIp()) {
                net.setSubnetId(getSubnetId());
            } else {
                riRequest.setSubnetId(getSubnetId());
            }

            diFilters.add(new Filter("subnet-id").withValues(getSubnetId()));

            /* If we have a subnet ID then we can only use VPC security groups */
            if (!securityGroupSet.isEmpty()) {
                List<String> group_ids = getEc2SecurityGroups(ec2);

                if (!group_ids.isEmpty()) {
                    if (getAssociatePublicIp()) {
                        net.setGroups(group_ids);
                    } else {
                        riRequest.setSecurityGroupIds(group_ids);
                    }

                    diFilters.add(new Filter("instance.group-id").withValues(group_ids));
                }
            }
        } else {
            /* No subnet: we can use standard security groups by name */
            riRequest.setSecurityGroups(securityGroupSet);
            if (securityGroupSet.size() > 0)
                diFilters.add(new Filter("instance.group-name").withValues(securityGroupSet));
        }

        String userDataString = Base64.encodeBase64String(userData.getBytes());
        riRequest.setUserData(userDataString);
        riRequest.setKeyName(keyPair.getKeyName());
        diFilters.add(new Filter("key-name").withValues(keyPair.getKeyName()));
        riRequest.setInstanceType(type.toString());
        diFilters.add(new Filter("instance-type").withValues(type.toString()));

        if (getAssociatePublicIp()) {
            net.setAssociatePublicIpAddress(true);
            net.setDeviceIndex(0);
            riRequest.withNetworkInterfaces(net);
        }

        boolean hasCustomTypeTag = false;
        HashSet<Tag> inst_tags = null;
        if (tags != null && !tags.isEmpty()) {
            inst_tags = new HashSet<Tag>();
            for (EC2Tag t : tags) {
                inst_tags.add(new Tag(t.getName(), t.getValue()));
                diFilters.add(new Filter("tag:" + t.getName()).withValues(t.getValue()));
                if (StringUtils.equals(t.getName(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
                    hasCustomTypeTag = true;
                }
            }
        }
        if (!hasCustomTypeTag) {
            if (inst_tags == null) {
                inst_tags = new HashSet<Tag>();
            }
            inst_tags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "demand"));
        }

        DescribeInstancesRequest diRequest = new DescribeInstancesRequest();
        diFilters.add(new Filter("instance-state-name").withValues(InstanceStateName.Stopped.toString(),
                InstanceStateName.Stopping.toString()));
        diRequest.setFilters(diFilters);

        msg = "Looking for existing instances with describe-instance: " + diRequest;
        logger.println(msg);
        LOGGER.fine(msg);

        DescribeInstancesResult diResult = ec2.describeInstances(diRequest);

        Instance existingInstance = null;
        if (StringUtils.isNotBlank(getIamInstanceProfile())) {
            riRequest.setIamInstanceProfile(
                    new IamInstanceProfileSpecification().withArn(getIamInstanceProfile()));
            // cannot filter on IAM Instance Profile, so search in result
            reservationLoop: for (Reservation reservation : diResult.getReservations()) {
                for (Instance instance : reservation.getInstances()) {
                    if (instance.getIamInstanceProfile() != null
                            && instance.getIamInstanceProfile().getArn().equals(getIamInstanceProfile())) {
                        existingInstance = instance;
                        break reservationLoop;
                    }
                }
            }
        } else if (diResult.getReservations().size() > 0) {
            existingInstance = diResult.getReservations().get(0).getInstances().get(0);
        }

        if (existingInstance == null) {
            // Have to create a new instance
            Instance inst = ec2.runInstances(riRequest).getReservation().getInstances().get(0);

            /* Now that we have our instance, we can set tags on it */
            if (inst_tags != null) {
                for (int i = 0; i < 5; i++) {
                    try {
                        updateRemoteTags(ec2, inst_tags, inst.getInstanceId());
                        break;
                    } catch (AmazonServiceException e) {
                        if (e.getErrorCode().equals("InvalidInstanceRequestID.NotFound")) {
                            Thread.sleep(5000);
                            continue;
                        }
                        throw e;
                    }
                }

                // That was a remote request - we should also update our local instance data.
                inst.setTags(inst_tags);
            }
            msg = "No existing instance found - created: " + inst;
            logger.println(msg);
            LOGGER.info(msg);
            return newOndemandSlave(inst);
        }

        msg = "Found existing stopped instance: " + existingInstance;
        logger.println(msg);
        LOGGER.info(msg);

        List<String> instances = new ArrayList<String>();
        instances.add(existingInstance.getInstanceId());
        StartInstancesRequest siRequest = new StartInstancesRequest(instances);
        StartInstancesResult siResult = ec2.startInstances(siRequest);

        msg = "Starting existing instance: " + existingInstance + " result:" + siResult;
        logger.println(msg);
        LOGGER.fine(msg);

        for (EC2AbstractSlave ec2Node : NodeIterator.nodes(EC2AbstractSlave.class)) {
            if (ec2Node.getInstanceId().equals(existingInstance.getInstanceId())) {
                msg = "Found existing corresponding Jenkins slave: " + ec2Node;
                logger.println(msg);
                LOGGER.finer(msg);
                return ec2Node;
            }
        }

        // Existing slave not found
        msg = "Creating new Jenkins slave for existing instance: " + existingInstance;
        logger.println(msg);
        LOGGER.info(msg);
        return newOndemandSlave(existingInstance);

    } catch (FormException e) {
        throw new AssertionError(); // we should have discovered all configuration issues upfront
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.occiware.clouddriver.util.InstanceDataFactory.java

License:Apache License

public static InstanceDO buildInstanceDataFromModel(Instance instance) {
    InstanceDO instanceDO = new InstanceDO();
    buildBasicInstanceData(instance, instanceDO);

    Placement placement = instance.getPlacement();
    if (placement != null) {
        PlacementDO placementDO = buildPlacementDO(instanceDO, placement);
        instanceDO.setPlacement(placementDO);
    }//from  w ww  . j a  v a  2  s.c om

    // Ebs volumes attached on instance.
    if (instance.getBlockDeviceMappings() != null && !instance.getBlockDeviceMappings().isEmpty()) {
        List<InstanceVolumeDO> instanceVolumeDOs = BuildInstanceVolumeDOs(instance);
        instanceDO.setVolumes(instanceVolumeDOs);
    }

    if (instance.getIamInstanceProfile() != null) {
        IamInstanceProfileDO profileDO = buildIamInstanceProfileDO(instance);
        instanceDO.setIamInstanceProfile(profileDO);
    }

    if (instance.getMonitoring() != null) {
        Monitoring monitoring = instance.getMonitoring();
        instanceDO.setMonitoringState(monitoring.getState());
    }

    // Network part.
    if (instance.getNetworkInterfaces() != null && !instance.getNetworkInterfaces().isEmpty()) {
        List<NetworkInterfaceDO> networkInterfaceDOs = buildNetworkInterfacesDatas(instance);
        instanceDO.setNetworkAdapters(networkInterfaceDOs);
    }

    List<ProductCode> productCodes = instance.getProductCodes();
    if (productCodes != null && !productCodes.isEmpty()) {
        List<ProductCodeDO> productCodeDOs = buildProductCodesDatas(productCodes);
        instanceDO.setProductCodes(productCodeDOs);
    }

    List<GroupIdentifier> groups = instance.getSecurityGroups();
    if (groups != null && !groups.isEmpty()) {
        List<GroupIdentifierDO> groupIdentifierDOs = buildSecurityGroupsDatas(groups);
        instanceDO.setSecurityGroups(groupIdentifierDOs);
    }

    InstanceState state = instance.getState();
    if (state != null) {
        instanceDO.setInstanceState(state.getName());
        instanceDO.setInstanceStateCode(state.getCode());
        StateReason stateReason = instance.getStateReason();
        if (stateReason != null) {
            instanceDO.setInstanceStateReasonMessage(stateReason.getMessage());
            instanceDO.setInstanceStateReasonCode(stateReason.getCode());
        }
    }

    List<Tag> tags = instance.getTags();
    if (tags != null && !tags.isEmpty()) {
        List<TagDO> tagDOs = buildTagsDatas(tags);
        instanceDO.setTags(tagDOs);
    }
    return instanceDO;
}

From source file:org.occiware.clouddriver.util.InstanceDataFactory.java

License:Apache License

/**
 *
 * @param instance//  w w w . j a v a  2  s . c o m
 * @return
 */
private static IamInstanceProfileDO buildIamInstanceProfileDO(Instance instance) {
    IamInstanceProfile profile = instance.getIamInstanceProfile();
    IamInstanceProfileDO profileDO = new IamInstanceProfileDO();
    profileDO.setArn(profile.getArn());
    profileDO.setProfileId(profile.getId());
    return profileDO;
}