Search in sources :

Example 1 with NeutronNetworkView

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

the class OpenStackRouterResourceBuilder method build.

@Override
public CloudResource build(OpenStackContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) {
    try {
        OSClient<?> osClient = createOSClient(auth);
        NeutronNetworkView networkView = new NeutronNetworkView(network);
        String routerId = networkView.getCustomRouterId();
        if (!networkView.isExistingNetwork()) {
            Router router = Builders.router().name(resource.getName()).adminStateUp(true).tenantId(context.getStringParameter(OpenStackConstants.TENANT_ID)).externalGateway(networkView.getPublicNetId()).build();
            Router newRouter = osClient.networking().router().create(router);
            if (newRouter == null) {
                throw new OpenStackResourceException("Router creation failed, maybe network does not exists", resourceType(), resource.getName());
            }
            routerId = newRouter.getId();
        }
        if (!networkView.isExistingSubnet()) {
            osClient.networking().router().attachInterface(routerId, AttachInterfaceType.SUBNET, context.getStringParameter(OpenStackConstants.SUBNET_ID));
        }
        return createPersistedResource(resource, routerId);
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Router creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) Router(org.openstack4j.model.network.Router) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Example 2 with NeutronNetworkView

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

the class OpenStackRouterResourceBuilder method delete.

@Override
public CloudResource delete(OpenStackContext context, AuthenticatedContext auth, CloudResource resource, Network network) {
    try {
        OSClient<?> osClient = createOSClient(auth);
        NeutronNetworkView networkView = new NeutronNetworkView(network);
        if (!networkView.isExistingSubnet()) {
            String subnetId = context.getStringParameter(OpenStackConstants.SUBNET_ID);
            osClient.networking().router().detachInterface(resource.getReference(), subnetId, null);
        }
        if (!networkView.isExistingNetwork()) {
            ActionResponse response = osClient.networking().router().delete(resource.getReference());
            return checkDeleteResponse(response, resourceType(), auth, resource, "Router deletion failed");
        }
        return null;
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Router 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)

Example 3 with NeutronNetworkView

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

the class OpenStackResourceConnector method launch.

@SuppressWarnings("unchecked")
@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier notifier, AdjustmentType adjustmentType, Long threshold) {
    String stackName = utils.getStackName(authenticatedContext);
    NeutronNetworkView neutronNetworkView = new NeutronNetworkView(stack.getNetwork());
    boolean existingNetwork = neutronNetworkView.isExistingNetwork();
    String existingSubnetCidr = getExistingSubnetCidr(authenticatedContext, stack);
    ModelContext modelContext = new ModelContext();
    modelContext.withExistingNetwork(existingNetwork);
    modelContext.withExistingSubnet(existingSubnetCidr != null);
    modelContext.withGroups(stack.getGroups());
    modelContext.withInstanceUserData(stack.getImage());
    modelContext.withLocation(authenticatedContext.getCloudContext().getLocation());
    modelContext.withStackName(stackName);
    modelContext.withNeutronNetworkView(neutronNetworkView);
    modelContext.withTemplateString(stack.getTemplate());
    modelContext.withTags(stack.getTags());
    String heatTemplate = heatTemplateBuilder.build(modelContext);
    Map<String, String> parameters = heatTemplateBuilder.buildParameters(authenticatedContext, stack, existingNetwork, existingSubnetCidr);
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    List<CloudResourceStatus> resources;
    Stack existingStack = client.heat().stacks().getStackByName(stackName);
    if (existingStack == null) {
        if (stack.getInstanceAuthentication().getPublicKeyId() == null) {
            createKeyPair(authenticatedContext, stack, client);
        }
        Stack heatStack = client.heat().stacks().create(Builders.stack().name(stackName).template(heatTemplate).disableRollback(false).parameters(parameters).timeoutMins(OPERATION_TIMEOUT).build());
        List<CloudResource> cloudResources = collectResources(authenticatedContext, notifier, heatStack, stack, neutronNetworkView);
        resources = check(authenticatedContext, cloudResources);
    } else {
        LOGGER.info("Heat stack already exists: {}", existingStack.getName());
        CloudResource cloudResource = new Builder().type(ResourceType.HEAT_STACK).name(existingStack.getId()).build();
        resources = Collections.singletonList(new CloudResourceStatus(cloudResource, ResourceStatus.CREATED));
    }
    LOGGER.debug("Launched resources: {}", resources);
    return resources;
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) Stack(org.openstack4j.model.heat.Stack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack)

Example 4 with NeutronNetworkView

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

the class HeatTemplateBuilderTest method buildTestWithExistingSubnetWithoutExistingNetworkAndAssignFloatingIp.

@Test
public void buildTestWithExistingSubnetWithoutExistingNetworkAndAssignFloatingIp() throws Exception {
    // GIVEN
    boolean existingNetwork = false;
    boolean existingSubnet = true;
    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, not(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 5 with NeutronNetworkView

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

the class HeatTemplateBuilderTest method buildTestWithExistingNetworkAndExistingSubnetAndAssignFloatingIp.

@Test
public void buildTestWithExistingNetworkAndExistingSubnetAndAssignFloatingIp() throws Exception {
    // GIVEN
    boolean existingNetwork = true;
    boolean existingSubnet = true;
    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("cb-sec-group_" + 't'));
    assertThat(templateString, containsString("app_net_id"));
    assertThat(templateString, not(containsString("app_network")));
    assertThat(templateString, containsString("subnet_id"));
    assertThat(templateString, not(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)

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