Search in sources :

Example 21 with NeutronNetworkView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView in project cloudbreak by hortonworks.

the class HeatTemplateBuilderTest method buildTestWithoutExistingNetworkAndExistingSubnetAndAssignFloatingIp.

@Test
public void buildTestWithoutExistingNetworkAndExistingSubnetAndAssignFloatingIp() throws Exception {
    // GIVEN
    boolean existingNetwork = false;
    boolean existingSubnet = false;
    NeutronNetworkView neutronNetworkView = createNeutronNetworkView(null);
    // WHEN
    when(openStackUtil.adjustStackNameLength(Mockito.anyString())).thenReturn("t");
    ModelContext modelContext = new ModelContext();
    modelContext.withExistingNetwork(existingNetwork);
    modelContext.withExistingSubnet(existingSubnet);
    modelContext.withGroups(groups);
    modelContext.withInstanceUserData(image);
    modelContext.withLocation(location());
    modelContext.withStackName(stackName);
    modelContext.withNeutronNetworkView(neutronNetworkView);
    modelContext.withTemplateString(heatTemplateBuilder.getTemplate());
    String templateString = heatTemplateBuilder.build(modelContext);
    // THEN
    assertThat(templateString, containsString("name: cb-sec-group_" + 't'));
    assertThat(templateString, not(containsString("app_net_id")));
    assertThat(templateString, containsString("app_network"));
    assertThat(templateString, containsString("subnet_id"));
    assertThat(templateString, containsString("app_subnet"));
    assertThat(templateString, containsString("network_id"));
    assertThat(templateString, not(containsString("public_net_id")));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) Test(org.junit.Test)

Example 22 with NeutronNetworkView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView in project cloudbreak by hortonworks.

the class HeatTemplateBuilderTest method buildTestWithAssignFloatingIpWithoutExistingNetworkAndExistingSubnet.

@Test
public void buildTestWithAssignFloatingIpWithoutExistingNetworkAndExistingSubnet() throws Exception {
    // GIVEN
    boolean existingNetwork = false;
    boolean existingSubnet = false;
    NeutronNetworkView neutronNetworkView = createNeutronNetworkView("floating_pool_id");
    // WHEN
    when(openStackUtil.adjustStackNameLength(Mockito.anyString())).thenReturn("t");
    ModelContext modelContext = new ModelContext();
    modelContext.withExistingNetwork(existingNetwork);
    modelContext.withExistingSubnet(existingSubnet);
    modelContext.withGroups(groups);
    modelContext.withInstanceUserData(image);
    modelContext.withLocation(location());
    modelContext.withStackName(stackName);
    modelContext.withNeutronNetworkView(neutronNetworkView);
    modelContext.withTemplateString(heatTemplateBuilder.getTemplate());
    String templateString = heatTemplateBuilder.build(modelContext);
    // THEN
    assertThat(templateString, containsString("name: cb-sec-group_" + 't'));
    assertThat(templateString, not(containsString("app_net_id")));
    assertThat(templateString, containsString("app_network"));
    assertThat(templateString, containsString("subnet_id"));
    assertThat(templateString, containsString("app_subnet"));
    assertThat(templateString, containsString("network_id"));
    assertThat(templateString, containsString("public_net_id"));
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext) Matchers.containsString(org.hamcrest.Matchers.containsString) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) Test(org.junit.Test)

Example 23 with NeutronNetworkView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView 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)

Example 24 with NeutronNetworkView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView in project cloudbreak by hortonworks.

the class OpenStackNetworkResourceBuilder method build.

@Override
public CloudResource build(OpenStackContext context, AuthenticatedContext auth, Network network, Security security, CloudResource buildableResource) {
    OSClient<?> osClient = createOSClient(auth);
    try {
        NeutronNetworkView neutronView = new NeutronNetworkView(network);
        String networkId = neutronView.isExistingNetwork() ? neutronView.getCustomNetworkId() : context.getParameter(NETWORK_ID, String.class);
        if (!neutronView.isExistingNetwork()) {
            org.openstack4j.model.network.Network osNetwork = Builders.network().name(buildableResource.getName()).tenantId(context.getStringParameter(OpenStackConstants.TENANT_ID)).adminStateUp(true).build();
            networkId = osClient.networking().network().create(osNetwork).getId();
        }
        context.putParameter(NETWORK_ID, networkId);
        return createPersistedResource(buildableResource, networkId);
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Network creation failed", resourceType(), buildableResource.getName(), ex);
    }
}
Also used : OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Example 25 with NeutronNetworkView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView in project cloudbreak by hortonworks.

the class OpenStackNetworkResourceBuilder method delete.

@Override
public CloudResource delete(OpenStackContext context, AuthenticatedContext auth, CloudResource resource, Network network) {
    try {
        OSClient<?> osClient = createOSClient(auth);
        deAllocateFloatingIps(context, osClient);
        NeutronNetworkView neutronView = new NeutronNetworkView(network);
        if (!neutronView.isExistingNetwork()) {
            ActionResponse response = osClient.networking().network().delete(resource.getReference());
            return checkDeleteResponse(response, resourceType(), auth, resource, "Network deletion failed");
        }
        return null;
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Network deletion failed", resourceType(), resource.getName(), ex);
    }
}
Also used : OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) OS4JException(org.openstack4j.api.exceptions.OS4JException) ActionResponse(org.openstack4j.model.common.ActionResponse)

Aggregations

NeutronNetworkView (com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView)25 ModelContext (com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext)16 Matchers.containsString (org.hamcrest.Matchers.containsString)13 Test (org.junit.Test)12 OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)7 OS4JException (org.openstack4j.api.exceptions.OS4JException)7 Ignore (org.junit.Ignore)3 ActionResponse (org.openstack4j.model.common.ActionResponse)3 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)2 HashMap (java.util.HashMap)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)1 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)1 Group (com.sequenceiq.cloudbreak.cloud.model.Group)1 Network (com.sequenceiq.cloudbreak.cloud.model.Network)1 PortDefinition (com.sequenceiq.cloudbreak.cloud.model.PortDefinition)1 Security (com.sequenceiq.cloudbreak.cloud.model.Security)1 SecurityRule (com.sequenceiq.cloudbreak.cloud.model.SecurityRule)1 KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)1 FreeMarkerTemplateUtils.processTemplateIntoString (com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils.processTemplateIntoString)1