Merge pull request #3120 from vivekkr12/BAEL-1173
BEAL-1173 - Add EC2 metadata access class and Integration Test
This commit is contained in:
commit
036fed69f9
@ -2,8 +2,10 @@ package com.baeldung.spring.cloud.aws;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ImportResource;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@ImportResource("classpath:aws-config.xml")
|
||||||
public class SpringCloudAwsApplication {
|
public class SpringCloudAwsApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.spring.cloud.aws.ec2;
|
||||||
|
|
||||||
|
import org.springframework.cloud.aws.context.config.annotation.EnableContextInstanceData;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableContextInstanceData
|
||||||
|
public class EC2EnableMetadata {
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.baeldung.spring.cloud.aws.ec2;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Lazy
|
||||||
|
@Component
|
||||||
|
public class EC2Metadata {
|
||||||
|
|
||||||
|
@Value("${ami-id:N/A}")
|
||||||
|
private String amiId;
|
||||||
|
|
||||||
|
@Value("${hostname:N/A}")
|
||||||
|
private String hostname;
|
||||||
|
|
||||||
|
@Value("${instance-type:N/A}")
|
||||||
|
private String instanceType;
|
||||||
|
|
||||||
|
@Value("${services/domain:N/A}")
|
||||||
|
private String serviceDomain;
|
||||||
|
|
||||||
|
@Value("#{instanceData['Name'] ?: 'N/A'}")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getAmiId() {
|
||||||
|
return amiId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHostname() {
|
||||||
|
return hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInstanceType() {
|
||||||
|
return instanceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getServiceDomain() {
|
||||||
|
return serviceDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("EC2Metadata [amiId=");
|
||||||
|
builder.append(amiId);
|
||||||
|
builder.append(", hostname=");
|
||||||
|
builder.append(hostname);
|
||||||
|
builder.append(", instanceType=");
|
||||||
|
builder.append(instanceType);
|
||||||
|
builder.append(", serviceDomain=");
|
||||||
|
builder.append(serviceDomain);
|
||||||
|
builder.append(", name=");
|
||||||
|
builder.append(name);
|
||||||
|
builder.append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/cloud/aws/context
|
||||||
|
http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd">
|
||||||
|
|
||||||
|
<aws-context:context-instance-data user-tags-map="instanceData" />
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.spring.cloud.aws.ec2;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Assume;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.amazonaws.regions.Regions;
|
||||||
|
import com.amazonaws.services.ec2.AmazonEC2;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@TestPropertySource("classpath:application-test.properties")
|
||||||
|
public class EC2MetadataIntegrationTest {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(EC2MetadataIntegrationTest.class);
|
||||||
|
|
||||||
|
private boolean serverEc2;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
serverEc2 = Regions.getCurrentRegion() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EC2Metadata eC2Metadata;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AmazonEC2 amazonEC2;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEC2ClinentNotNull_thenSuccess() {
|
||||||
|
assertThat(amazonEC2).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEC2MetadataNotNull_thenSuccess() {
|
||||||
|
assertThat(eC2Metadata).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMetdataValuesNotNull_thenSuccess() {
|
||||||
|
Assume.assumeTrue(serverEc2);
|
||||||
|
assertThat(eC2Metadata.getAmiId()).isNotEqualTo("N/A");
|
||||||
|
assertThat(eC2Metadata.getInstanceType()).isNotEqualTo("N/A");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMetadataLogged_thenSuccess() {
|
||||||
|
logger.info("Environment is EC2: {}", serverEc2);
|
||||||
|
logger.info(eC2Metadata.toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user