Skip to content

Commit

Permalink
Add functionality to open ports in OpenStack IA (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
wederbn authored Nov 18, 2023
1 parent b084b75 commit 7c8a9b3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.openstack4j.model.compute.Address;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.compute.FloatingIP;
import org.openstack4j.model.compute.IPProtocol;
import org.openstack4j.model.compute.Image;
import org.openstack4j.model.compute.SecGroupExtension;
import org.openstack4j.model.compute.Server;
import org.openstack4j.model.compute.Server.Status;
import org.openstack4j.model.compute.ServerCreate;
Expand Down Expand Up @@ -102,15 +104,6 @@ public void createVM(@RequestPayload CreateVMRequest request, MessageContext mes
}
}

String securityGroup = "default";
if (request.getVMSecurityGroup() != null && !request.getVMSecurityGroup().isEmpty()) {
securityGroup = request.getVMSecurityGroup();
if (!securityGroup.contains("default")) {
securityGroup = "default," + securityGroup;
}
}
logger.info("Received security groups {}", securityGroup);

// Create OpenStack client
OSClient<?> osClient = authenticate(request);

Expand Down Expand Up @@ -160,6 +153,34 @@ public void createVM(@RequestPayload CreateVMRequest request, MessageContext mes
return;
}

// add defined security group or create new security group with defined open ports
String securityGroup;
if (request.getVMSecurityGroup() != null && !request.getVMSecurityGroup().isEmpty()) {
logger.info("Adding configured security group: {}", request.getVMSecurityGroup());
securityGroup = request.getVMSecurityGroup();
} else{
logger.info("Creating new security group to open ports: {}", request.getVMOpenPorts());

// create security group
SecGroupExtension group = osClient.compute().securityGroups().create("OpenTOSCA-" + System.currentTimeMillis(), "OpenTOSCA security group");
securityGroup = group.getName();
logger.info("Created new security group with name: {}", securityGroup);

// open ports within security group
String[] ports = request.getVMOpenPorts().split(",");
logger.info("Opening {} ports...", ports.length);
for (String port :ports){
logger.info("Opening port: {}", port);
osClient.compute().securityGroups()
.createRule(Builders.secGroupRule()
.parentGroupId(group.getId())
.protocol(IPProtocol.TCP)
.cidr("0.0.0.0/0")
.range(Integer.parseInt(port), Integer.parseInt(port)).build());
}
}
logger.info("Resulting security group: {}", securityGroup);

// Get Networks based on Type String
List<? extends Network> availableNetworks = osClient.networking().network().list();
logger.info("Found "+ availableNetworks.size() + " Networks");
Expand Down Expand Up @@ -231,16 +252,9 @@ public void createVM(@RequestPayload CreateVMRequest request, MessageContext mes
.flavor(flavor)
.image(image)
.networks(availableNetworksIds)
.addSecurityGroup(securityGroup)
.keypairName(request.getVMKeyPairName());

for (String secGroup : securityGroup.split(",")) {
String trim = secGroup.trim();
if (!trim.isEmpty()) {
serverCreateBuilder.addSecurityGroup(trim);
logger.info("Added security group {}", trim);
}
}

ServerCreate sc = serverCreateBuilder.build();

// Start Server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<xs:element form="qualified" minOccurs="0" name="VMPublicKey" type="xs:string"/>
<xs:element form="qualified" minOccurs="0" name="VMSecurityGroup" type="xs:string"/>
<xs:element form="qualified" minOccurs="0" name="VMNetworks" type="xs:string"/>
<xs:element form="qualified" minOccurs="0" name="VMOpenPorts" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
Expand All @@ -63,5 +64,4 @@
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

0 comments on commit 7c8a9b3

Please sign in to comment.