com.amazonaws.services.ec2.model.DescribeInstanceAttributeResult Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.DescribeInstanceAttributeResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Ec2InstanceStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
@Override
public InstanceAttribute getInstanceAttribute(String instanceId, String attributeName) {
  OperationStats ops = new OperationStats("es2InstanceStore", "getInstanceAttribute");
  try {
    awsRateLimiter.acquire();
    DescribeInstanceAttributeRequest request = new DescribeInstanceAttributeRequest()
        .withInstanceId(instanceId)
        .withAttribute(InstanceAttributeName.fromValue(attributeName))
        .withSdkRequestTimeout(300 * 1000).withSdkClientExecutionTimeout(600 * 1000);

    DescribeInstanceAttributeResult result = defaultClient.describeInstanceAttribute(request);
    while (result != null) {
      ops.succeed();
      return result.getInstanceAttribute();
    }
  } catch (Exception ex) {
    ops.failed();
    throw ex;
  }
  return null;
}
 
Example #2
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithTaupageAmiButInvalidYaml() throws Exception {
    // a yaml list is not a valid taupage format. Map is required.
    final String yamlData = "- a\n- b\n- c\n";

    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString(yamlData.getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isEmpty();


    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #3
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithVersionSimilarToNumber1() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: '6478e18'".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    assertThat(result.get().getApplicationId()).isEqualTo("fdsa");
    assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18");

    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #4
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithVersionSimilarToNumber() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("application_id: fdsa\napplication_version: 6478e18".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();

    assertThat(result.get().getApplicationId()).isEqualTo("fdsa");
    assertThat(result.get().getApplicationVersion()).isEqualTo("6478e18");

    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #5
Source File: TaupageYamlProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyWithTaupageAmi() throws Exception {
    when(ec2InstanceContextMock.isTaupageAmi()).thenReturn(Optional.of(true));

    when(ec2InstanceContextMock.getInstanceId()).thenReturn(INSTANCE_ID);
    when(ec2InstanceContextMock.getClient(eq(AmazonEC2Client.class))).thenReturn(amazonEC2ClientMock);
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("blub: fdsa".getBytes()))));

    final Optional<TaupageYaml> result = taupageYamlProvider.apply(ec2InstanceContextMock);

    assertThat(result).isPresent();


    verify(ec2InstanceContextMock).isTaupageAmi();
    verify(ec2InstanceContextMock).getInstanceId();
    verify(ec2InstanceContextMock).getClient(eq(AmazonEC2Client.class));
    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #6
Source File: FetchTaupageYamlImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testBrokenYaml() throws Exception{
    // a yaml list is not a valid taupage format. Map is required.
    final String yamlData = "- a\n- b\n- c\n";

    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString(yamlData.getBytes()))));

    final FetchTaupageYaml fetchTaupageYaml = new FetchTaupageYamlImpl(clientProviderMock);

    final Optional<TaupageYaml> result = fetchTaupageYaml.getTaupageYaml(INSTANCE_ID, ACCOUNT, REGION);

    assertThat(result).isEmpty();

    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #7
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public DescribeInstanceAttributeResult describeAttribute(String attribute,
        ResultCapture<DescribeInstanceAttributeResult> extractor) {

    DescribeInstanceAttributeRequest request = new
            DescribeInstanceAttributeRequest()

        .withAttribute(attribute);
    return describeAttribute(request, extractor);
}
 
Example #8
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public DescribeInstanceAttributeResult describeAttribute(
        DescribeInstanceAttributeRequest request,
        ResultCapture<DescribeInstanceAttributeResult> extractor) {

    ActionResult result = resource.performAction("DescribeAttribute",
            request, extractor);

    if (result == null) return null;
    return (DescribeInstanceAttributeResult) result.getData();
}
 
Example #9
Source File: TaupageYamlProviderImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Optional<TaupageYaml> getTaupageYaml(@Nonnull final EC2InstanceContext context) {

        if (context.isTaupageAmi().orElse(false)) {

            final String instanceId = context.getInstanceId();

            try {
                return Optional.of(context.getClient(AmazonEC2Client.class))
                        .map(client -> client.describeInstanceAttribute(new DescribeInstanceAttributeRequest()
                                .withInstanceId(instanceId)
                                .withAttribute(USER_DATA)))
                        .map(DescribeInstanceAttributeResult::getInstanceAttribute)
                        .map(InstanceAttribute::getUserData)
                        .map(Base64::decode)
                        .map(String::new)
                        .map(TaupageYamlUtil::parseTaupageYaml);

            } catch (final AmazonClientException e) {
                log.warn("Could not get Taupage YAML for instance: " + instanceId, e);
                return empty();
            } catch (YAMLException | IllegalArgumentException s)   {
                log.warn("Taupage YAML is not valid for instance: " + instanceId, s);
                return empty();
            }

        } else {
            return empty();
        }

    }
 
Example #10
Source File: FetchTaupageYamlImplTest.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTaupageYaml() throws Exception {
    when(amazonEC2ClientMock.describeInstanceAttribute(any())).thenReturn(new DescribeInstanceAttributeResult().
            withInstanceAttribute(new InstanceAttribute()
                    .withUserData(Base64.encodeAsString("blub: fdsa".getBytes()))));
    final FetchTaupageYaml fetchTaupageYaml = new FetchTaupageYamlImpl(clientProviderMock);

    final Optional<TaupageYaml> result = fetchTaupageYaml.getTaupageYaml(INSTANCE_ID, ACCOUNT, REGION);

    assertThat(result).isPresent();

    verify(amazonEC2ClientMock).describeInstanceAttribute(any());
}
 
Example #11
Source File: FetchTaupageYamlImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<TaupageYaml> getTaupageYaml(final String instanceId, final String account, final String region) {
    final AmazonEC2Client client = clientProvider.getClient(AmazonEC2Client.class,
            account,
            Region.getRegion(Regions.fromName(region)));

    try {
        final DescribeInstanceAttributeResult response = client.describeInstanceAttribute(
                new DescribeInstanceAttributeRequest()
                        .withInstanceId(instanceId)
                        .withAttribute(USER_DATA));


        return ofNullable(response)
                .map(DescribeInstanceAttributeResult::getInstanceAttribute)
                .map(InstanceAttribute::getUserData)
                .map(Base64::decode)
                .map(String::new)
                .map(TaupageYamlUtil::parseTaupageYaml);

    } catch (final AmazonClientException e) {
        log.warn("Could not get Taupage YAML for instance: " + instanceId, e);
        return empty();
    } catch (YAMLException | IllegalArgumentException s) {
        log.warn("Taupage YAML is not valid for instance: " + instanceId, s);
        return empty();
    }
}
 
Example #12
Source File: MockEc2OperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public DescribeInstanceAttributeResult describeInstanceAttribute(AmazonEC2Client ec2Client,
    DescribeInstanceAttributeRequest describeInstanceAttributeRequest)
{
    InstanceAttribute instanceAttribute = new InstanceAttribute();
    instanceAttribute.withGroups(new GroupIdentifier().withGroupId("A_TEST_SECURITY_GROUP"));
    return new DescribeInstanceAttributeResult().withInstanceAttribute(instanceAttribute);
}
 
Example #13
Source File: Ec2OperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Describe the EC2 instance attribute
 */
@Override
public DescribeInstanceAttributeResult describeInstanceAttribute(AmazonEC2Client ec2Client,
    DescribeInstanceAttributeRequest describeInstanceAttributeRequest)
{
    return ec2Client.describeInstanceAttribute(describeInstanceAttributeRequest);
}
 
Example #14
Source File: Ec2DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the security groups to an EC2 instance.
 *
 * @param ec2InstanceId the ec2 instance id.
 * @param securityGroups security groups to be added.
 * @param awsParams awsParamsDto object
 *
 * @return updated security groups.
 */
@Override
public List<String> addSecurityGroupsToEc2Instance(String ec2InstanceId, List<String> securityGroups, AwsParamsDto awsParams)
{
    Set<String> updatedSecurityGroups = new HashSet<>();
    for (String securityGroup : securityGroups)
    {
        updatedSecurityGroups.add(securityGroup);
    }

    // Get existing security groups
    DescribeInstanceAttributeRequest describeInstanceAttributeRequest =
        new DescribeInstanceAttributeRequest().withInstanceId(ec2InstanceId).withAttribute(InstanceAttributeName.GroupSet);

    DescribeInstanceAttributeResult describeInstanceAttributeResult =
        ec2Operations.describeInstanceAttribute(getEc2Client(awsParams), describeInstanceAttributeRequest);

    List<GroupIdentifier> groups = describeInstanceAttributeResult.getInstanceAttribute().getGroups();
    for (GroupIdentifier groupIdentifier : groups)
    {
        updatedSecurityGroups.add(groupIdentifier.getGroupId());
    }

    // Add security group on master EC2 instance
    ModifyInstanceAttributeRequest modifyInstanceAttributeRequest =
        new ModifyInstanceAttributeRequest().withInstanceId(ec2InstanceId).withGroups(updatedSecurityGroups);

    ec2Operations.modifyInstanceAttribute(getEc2Client(awsParams), modifyInstanceAttributeRequest);

    return new ArrayList<>(updatedSecurityGroups);
}
 
Example #15
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public DescribeInstanceAttributeResult describeAttribute(
        DescribeInstanceAttributeRequest request) {

    return describeAttribute(request, null);
}
 
Example #16
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public DescribeInstanceAttributeResult describeAttribute(String attribute) {
    return describeAttribute(attribute,
            (ResultCapture<DescribeInstanceAttributeResult>)null);
}
 
Example #17
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>DescribeAttribute</code> action.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Instance</code> resource, and any conflicting parameter value set
 * in the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>InstanceId</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return The response of the low-level client operation associated with
 *         this resource action.
 * @see DescribeInstanceAttributeRequest
 */
DescribeInstanceAttributeResult describeAttribute(
        DescribeInstanceAttributeRequest request);
 
Example #18
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>DescribeAttribute</code> action and use a
 * ResultCapture to retrieve the low-level client response.
 *
 * <p>
 * The following request parameters will be populated from the data of this
 * <code>Instance</code> resource, and any conflicting parameter value set
 * in the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>InstanceId</code></b>
 *         - mapped from the <code>Id</code> identifier.
 *   </li>
 * </ul>
 *
 * <p>
 *
 * @return The response of the low-level client operation associated with
 *         this resource action.
 * @see DescribeInstanceAttributeRequest
 */
DescribeInstanceAttributeResult describeAttribute(
        DescribeInstanceAttributeRequest request,
        ResultCapture<DescribeInstanceAttributeResult> extractor);
 
Example #19
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * The convenient method form for the <code>DescribeAttribute</code> action.
 *
 * @see #describeAttribute(DescribeInstanceAttributeRequest)
 */
DescribeInstanceAttributeResult describeAttribute(String attribute);
 
Example #20
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * The convenient method form for the <code>DescribeAttribute</code> action.
 *
 * @see #describeAttribute(DescribeInstanceAttributeRequest, ResultCapture)
 */
DescribeInstanceAttributeResult describeAttribute(String attribute,
        ResultCapture<DescribeInstanceAttributeResult> extractor);
 
Example #21
Source File: Ec2Operations.java    From herd with Apache License 2.0 2 votes vote down vote up
/**
 * {@link AmazonEC2#describeInstanceAttribute(DescribeInstanceAttributeRequest)}
 * 
 * @param ec2Client {@link AmazonEC2} to use.
 * @param describeInstanceAttributeRequest The request object.
 * @return {@link DescribeInstanceAttributeResult}
 */
public DescribeInstanceAttributeResult describeInstanceAttribute(AmazonEC2Client ec2Client,
    DescribeInstanceAttributeRequest describeInstanceAttributeRequest);