use of com.amazonaws.services.ec2.model.CreateSecurityGroupRequest in project Synapse-Stack-Builder by Sage-Bionetworks.
the class EC2SecuritySetup method createSecurityGroup.
/**
* Create a security group. If the group already exists
* @param ec2Client
* @param request
*/
void createSecurityGroup(CreateSecurityGroupRequest request) {
try {
// First create the EC2 group
log.info("Creating Security Group: " + request.getGroupName() + "...");
CreateSecurityGroupResult result = ec2Client.createSecurityGroup(request);
} catch (AmazonServiceException e) {
if (ERROR_CODE_INVALID_GROUP_DUPLICATE.equals(e.getErrorCode())) {
// This group already exists
log.info("Security Group: " + request.getGroupName() + " already exits");
} else {
throw e;
}
}
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupRequest in project Synapse-Stack-Builder by Sage-Bionetworks.
the class EC2SecuritySetupTest method testSetupElasticBeanstalkEC2SecutiryGroup.
@Test
public void testSetupElasticBeanstalkEC2SecutiryGroup() {
String expectedDescription = config.getElasticSecurityGroupDescription();
String expectedGroupName = config.getElasticSecurityGroupName();
DescribeSecurityGroupsResult result = new DescribeSecurityGroupsResult();
SecurityGroup expectedGroup = new SecurityGroup().withGroupName(expectedGroupName).withOwnerId("123");
result.withSecurityGroups(expectedGroup);
when(mockEC2Client.describeSecurityGroups(any(DescribeSecurityGroupsRequest.class))).thenReturn(result);
DescribeKeyPairsResult kpr = new DescribeKeyPairsResult().withKeyPairs(new KeyPairInfo().withKeyName("123"));
when(mockEC2Client.describeKeyPairs(any(DescribeKeyPairsRequest.class))).thenReturn(kpr);
// Create the security group.
ec2SecuritySetup.setupResources();
SecurityGroup group = resources.getElasticBeanstalkEC2SecurityGroup();
assertEquals(expectedGroup, group);
String groupName = group.getGroupName();
assertNotNull(groupName);
assertEquals(expectedGroupName, groupName);
CreateSecurityGroupRequest groupRequest = new CreateSecurityGroupRequest(expectedGroupName, expectedDescription);
// The create group should be called
verify(mockEC2Client).createSecurityGroup(groupRequest);
// Three permission should be set
// http
List<IpPermission> list = new LinkedList<IpPermission>();
list.add(new IpPermission().withIpProtocol(IP_PROTOCOL_TCP).withFromPort(PORT_HTTP).withToPort(PORT_HTTP).withIpRanges(CIDR_ALL_IP));
AuthorizeSecurityGroupIngressRequest request = new AuthorizeSecurityGroupIngressRequest(groupName, list);
verify(mockEC2Client).authorizeSecurityGroupIngress(request);
// https
list = new LinkedList<IpPermission>();
list.add(new IpPermission().withIpProtocol(IP_PROTOCOL_TCP).withFromPort(PORT_HTTPS).withToPort(PORT_HTTPS).withIpRanges(CIDR_ALL_IP));
request = new AuthorizeSecurityGroupIngressRequest(groupName, list);
verify(mockEC2Client).authorizeSecurityGroupIngress(request);
// ssh
list = new LinkedList<IpPermission>();
list.add(new IpPermission().withIpProtocol(IP_PROTOCOL_TCP).withFromPort(PORT_SSH).withToPort(PORT_SSH).withIpRanges(config.getCIDRForSSH()));
request = new AuthorizeSecurityGroupIngressRequest(groupName, list);
verify(mockEC2Client).authorizeSecurityGroupIngress(request);
// Make sure this is set
assertNotNull(resources.getElasticBeanstalkEC2SecurityGroup());
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupRequest in project Synapse-Stack-Builder by Sage-Bionetworks.
the class EC2SecuritySetupTest method testCreateGroupDuplicate.
@Test
public void testCreateGroupDuplicate() {
// For this case we are simulating a duplicate group exception.
// When the group already exists an exception should not be thrown.
AmazonServiceException exception = new AmazonServiceException("Some error");
exception.setErrorCode(Constants.ERROR_CODE_INVALID_GROUP_DUPLICATE);
CreateSecurityGroupRequest request = new CreateSecurityGroupRequest();
when(mockEC2Client.createSecurityGroup(request)).thenThrow(exception);
ec2SecuritySetup.createSecurityGroup(request);
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupRequest in project photon-model by vmware.
the class AWSSecurityGroupClient method createSecurityGroupAsync.
public DeferredResult<String> createSecurityGroupAsync(String name, String description, String vpcId) {
CreateSecurityGroupRequest req = new CreateSecurityGroupRequest().withDescription(description).withGroupName(name);
// set vpc for the security group if provided
if (vpcId != null) {
req = req.withVpcId(vpcId);
}
String message = "Create AWS Security Group with name [" + name + "] on VPC [" + vpcId + "].";
AWSDeferredResultAsyncHandler<CreateSecurityGroupRequest, CreateSecurityGroupResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createSecurityGroupAsync(req, handler);
return handler.toDeferredResult().thenApply(CreateSecurityGroupResult::getGroupId);
}
use of com.amazonaws.services.ec2.model.CreateSecurityGroupRequest in project photon-model by vmware.
the class AWSSecurityGroupClient method createSecurityGroup.
public String createSecurityGroup(String name, String description, String vpcId) {
CreateSecurityGroupRequest req = new CreateSecurityGroupRequest().withDescription(description).withGroupName(name);
// set vpc for the security group if provided
if (vpcId != null) {
req = req.withVpcId(vpcId);
}
CreateSecurityGroupResult result = this.client.createSecurityGroup(req);
return result.getGroupId();
}
Aggregations