@Override public CloudResource build( OpenStackContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception { try { OSClient osClient = createOSClient(auth); NeutronNetworkView networkView = new NeutronNetworkView(network); Router router = Builders.router() .name(resource.getName()) .adminStateUp(true) .tenantId(context.getStringParameter(OpenStackConstants.TENANT_ID)) .externalGateway(networkView.getPublicNetId()) .build(); router = osClient.networking().router().create(router); osClient .networking() .router() .attachInterface( router.getId(), AttachInterfaceType.SUBNET, context.getStringParameter(OpenStackConstants.SUBNET_ID)); return createPersistedResource(resource, router.getId()); } catch (OS4JException ex) { throw new OpenStackResourceException( "Router creation failed", resourceType(), resource.getName(), ex); } }
@Override @SuppressWarnings("unchecked") public CloudResource delete( OpenStackContext context, AuthenticatedContext auth, CloudResource resource) throws Exception { context .getParameter(OpenStackConstants.FLOATING_IP_IDS, List.class) .add(resource.getReference()); return null; }
@Override protected boolean checkStatus( OpenStackContext context, AuthenticatedContext auth, CloudResource resource) { CloudContext cloudContext = auth.getCloudContext(); OSClient osClient = createOSClient(auth); Router osRouter = osClient.networking().router().get(resource.getReference()); if (osRouter != null && context.isBuild()) { State routerStatus = osRouter.getStatus(); if (State.ERROR == routerStatus) { throw new OpenStackResourceException( "Router in failed state", resource.getType(), cloudContext.getStackName(), cloudContext.getStackId(), resource.getName()); } return routerStatus == State.ACTIVE; } else if (osRouter == null && !context.isBuild()) { return true; } return false; }
@Override public CloudResource delete( OpenStackContext context, AuthenticatedContext auth, CloudResource resource) throws Exception { try { OSClient osClient = createOSClient(auth); String subnetId = context.getStringParameter(OpenStackConstants.SUBNET_ID); osClient.networking().router().detachInterface(resource.getReference(), subnetId, null); ActionResponse response = osClient.networking().router().delete(resource.getReference()); return checkDeleteResponse( response, resourceType(), auth, resource, "Router deletion failed"); } catch (OS4JException ex) { throw new OpenStackResourceException( "Router deletion failed", resourceType(), resource.getName(), ex); } }
@Override public List<CloudResource> build( OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource) throws Exception { CloudResource resource = buildableResource.get(0); try { OSClient osClient = createOSClient(auth); List<CloudResource> computeResources = context.getComputeResources(privateId); CloudResource instance = getInstance(computeResources); String pool = osClient.compute().floatingIps().getPoolNames().get(0); FloatingIP unusedIp = osClient.compute().floatingIps().allocateIP(pool); ActionResponse response = osClient .compute() .floatingIps() .addFloatingIP( instance.getParameter(OpenStackConstants.SERVER, Server.class), unusedIp.getFloatingIpAddress()); if (!response.isSuccess()) { throw new OpenStackResourceException( "Add floating-ip to server failed", resourceType(), resource.getName(), auth.getCloudContext().getId(), response.getFault()); } return Collections.singletonList(createPersistedResource(resource, unusedIp.getId())); } catch (OS4JException ex) { throw new OpenStackResourceException( "Add floating-ip to server failed", resourceType(), resource.getName(), ex); } }
@Override public CloudResource create( OpenStackContext context, AuthenticatedContext auth, Network network) { String resourceName = resourceNameService.resourceName(resourceType(), context.getName()); return createNamedResource(resourceType(), resourceName); }