Search in sources :

Example 1 with SecGroupExtension

use of org.openstack4j.model.compute.SecGroupExtension in project cloudbreak by hortonworks.

the class OpenStackPlatformResources method securityGroups.

@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    OSClient<?> osClient = openStackClient.createOSClient(cloudCredential);
    KeystoneCredentialView osCredential = openStackClient.createKeystoneCredential(cloudCredential);
    Set<CloudSecurityGroup> cloudSecurityGroups = new HashSet<>();
    List<? extends SecGroupExtension> osSecurityGroups = osClient.compute().securityGroups().list();
    LOGGER.info("security groups from openstack: {}", osSecurityGroups);
    for (SecGroupExtension secGroup : osSecurityGroups) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("tenantId", secGroup.getTenantId());
        properties.put("rules", secGroup.getRules());
        CloudSecurityGroup cloudSecurityGroup = new CloudSecurityGroup(secGroup.getName(), secGroup.getId(), properties);
        cloudSecurityGroups.add(cloudSecurityGroup);
    }
    Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
    result.put(region.value() == null ? osCredential.getTenantName() : region.value(), cloudSecurityGroups);
    LOGGER.info("openstack security groups result: {}", result);
    return new CloudSecurityGroups(result);
}
Also used : SecGroupExtension(org.openstack4j.model.compute.SecGroupExtension) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) CloudSecurityGroups(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) CloudSecurityGroup(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup) HashSet(java.util.HashSet)

Example 2 with SecGroupExtension

use of org.openstack4j.model.compute.SecGroupExtension in project cloudbreak by hortonworks.

the class OpenStackSecurityGroupResourceBuilder method build.

@Override
public CloudResource build(OpenStackContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource resource) {
    try {
        OSClient<?> osClient = createOSClient(auth);
        ComputeSecurityGroupService securityGroupService = osClient.compute().securityGroups();
        SecGroupExtension securityGroup = securityGroupService.create(resource.getName(), "");
        String securityGroupId = securityGroup.getId();
        for (SecurityRule rule : security.getRules()) {
            IPProtocol osProtocol = getProtocol(rule.getProtocol());
            String cidr = rule.getCidr();
            for (PortDefinition portStr : rule.getPorts()) {
                int from = Integer.parseInt(portStr.getFrom());
                int to = Integer.parseInt(portStr.getTo());
                securityGroupService.createRule(createRule(securityGroupId, osProtocol, cidr, from, to));
            }
        }
        NeutronNetworkView neutronView = new NeutronNetworkView(network);
        String subnetCidr = neutronView.isExistingSubnet() ? utils.getExistingSubnetCidr(auth, neutronView) : network.getSubnet().getCidr();
        securityGroupService.createRule(createRule(securityGroupId, IPProtocol.TCP, subnetCidr, MIN_PORT, MAX_PORT));
        securityGroupService.createRule(createRule(securityGroupId, IPProtocol.UDP, subnetCidr, MIN_PORT, MAX_PORT));
        securityGroupService.createRule(createRule(securityGroupId, IPProtocol.ICMP, "0.0.0.0/0"));
        return createPersistedResource(resource, group.getName(), securityGroup.getId());
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("SecurityGroup creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) SecGroupExtension(org.openstack4j.model.compute.SecGroupExtension) ComputeSecurityGroupService(org.openstack4j.api.compute.ComputeSecurityGroupService) IPProtocol(org.openstack4j.model.compute.IPProtocol) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Aggregations

SecGroupExtension (org.openstack4j.model.compute.SecGroupExtension)2 CloudSecurityGroup (com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup)1 CloudSecurityGroups (com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups)1 PortDefinition (com.sequenceiq.cloudbreak.cloud.model.PortDefinition)1 SecurityRule (com.sequenceiq.cloudbreak.cloud.model.SecurityRule)1 OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)1 KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)1 NeutronNetworkView (com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ComputeSecurityGroupService (org.openstack4j.api.compute.ComputeSecurityGroupService)1 OS4JException (org.openstack4j.api.exceptions.OS4JException)1 IPProtocol (org.openstack4j.model.compute.IPProtocol)1