@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 public CloudInstanceMetaData extractMetadata(OSClient client, Server server, String instanceId) { String hypervisor = hypervisorExtractor.getHypervisor(server); LOGGER.debug("Address map was empty, trying to extract ips"); List<? extends Port> ports = client.networking().port().list(getPortListOptions(instanceId)); String portId = ports.get(0).getId(); List<? extends NetFloatingIP> floatingIps = client.networking().floatingip().list(getFloatingIpListOptions(portId)); NetFloatingIP ips = floatingIps.get(0); LOGGER.info("PrivateIp of instance: {} is {}", server.getName(), ips.getFixedIpAddress()); LOGGER.info("FloatingIp of instance: {} is {}", server.getName(), ips.getFloatingIpAddress()); return new CloudInstanceMetaData( ips.getFixedIpAddress(), ips.getFloatingIpAddress(), hypervisor); }
@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 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; }