Java tutorial
// Minecraft Elastic Servers // Copyright (c) 2016. // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation version 2.1 // of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA package com.indemnity83.ephemeral.api; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest; import com.amazonaws.services.ec2.model.CreateSecurityGroupRequest; import com.amazonaws.services.ec2.model.IpPermission; import com.indemnity83.ephemeral.Ephemeral; import java.util.ArrayList; public class SecurityGroup { private String groupName; private String groupDescription; ArrayList<IpPermission> ipPermissions = new ArrayList<IpPermission>(); public SecurityGroup(String groupName) { this(groupName, "Automatically Generated by Ephemeral"); } public SecurityGroup(String groupName, String groupDescription) { this.groupName = groupName; this.groupDescription = groupDescription; } public SecurityGroup create() { CreateSecurityGroupRequest securityGroupRequest = new CreateSecurityGroupRequest(groupName, groupDescription); AuthorizeSecurityGroupIngressRequest ingressRequest = new AuthorizeSecurityGroupIngressRequest(groupName, ipPermissions); try { Ephemeral.ec2.createSecurityGroup(securityGroupRequest); Ephemeral.ec2.authorizeSecurityGroupIngress(ingressRequest); } catch (AmazonServiceException ase) { // Likely this means the security group already exists System.out.println(ase.getMessage()); } System.out.println("Created security group: " + this); return this; } @Override public String toString() { return groupName; } public SecurityGroup allowTcp(int port) { return this.allow(port, "tcp"); } public SecurityGroup allowUdp(int port) { return this.allow(port, "udp"); } public SecurityGroup allow(int port, String protocol) { return this.allow(port, protocol, "0.0.0.0/0"); } public SecurityGroup allow(int port, String protocol, String source) { ArrayList<String> ranges = new ArrayList<String>(); ranges.add(source); IpPermission rule = new IpPermission(); rule.setIpProtocol(protocol); rule.setFromPort(port); rule.setToPort(port); rule.setIpRanges(ranges); ipPermissions.add(rule); return this; } }