Search in sources :

Example 1 with SecurityGroupList

use of org.openstack.model.compute.SecurityGroupList in project platformlayer by platformlayer.

the class OpenstackCloudHelpers method getMachineSecurityGroup.

public SecurityGroup getMachineSecurityGroup(OpenstackComputeClient openstackComputeClient, Server server) throws OpsException {
    // SecurityGroupList securityGroups;
    // try {
    // securityGroups = openstackComputeClient.root().servers().server(server.getId()).listSecurityGroups();
    // } catch (OpenstackException e) {
    // throw new OpsException("Error getting security groups for server", e);
    // }
    SecurityGroupList securityGroups;
    try {
        log.info("Listing security groups");
        securityGroups = openstackComputeClient.root().securityGroups().list();
    } catch (OpenstackException e) {
        throw new OpsException("Error getting security groups for server", e);
    }
    SecurityGroup securityGroup = null;
    if (securityGroups != null && securityGroups.getList() != null) {
        for (SecurityGroup candidate : securityGroups.getList()) {
            if (candidate.getName() == null) {
                continue;
            }
            if (candidate.getName().equals(OpenstackCloudContext.SECURITY_GROUP_PREFIX + server.getName())) {
                securityGroup = candidate;
                break;
            }
        // if (candidate.getName().startsWith(OpenstackCloudContext.SECURITY_GROUP_PREFIX)) {
        // securityGroup = candidate;
        // break;
        // }
        }
    }
    if (securityGroup == null) {
        throw new OpsException("Could not find platform layer security group for server: " + server);
    }
    return securityGroup;
}
Also used : SecurityGroupList(org.openstack.model.compute.SecurityGroupList) OpsException(org.platformlayer.ops.OpsException) SecurityGroup(org.openstack.model.compute.SecurityGroup) OpenstackException(org.openstack.client.OpenstackException)

Aggregations

OpenstackException (org.openstack.client.OpenstackException)1 SecurityGroup (org.openstack.model.compute.SecurityGroup)1 SecurityGroupList (org.openstack.model.compute.SecurityGroupList)1 OpsException (org.platformlayer.ops.OpsException)1