Search in sources :

Example 1 with KeystoneCredentialView

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

the class OpenStackInstanceBuilder method build.

@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) {
    CloudResource resource = buildableResource.get(0);
    try {
        OSClient<?> osClient = createOSClient(auth);
        InstanceTemplate template = getInstanceTemplate(group, privateId);
        CloudResource port = getPort(context.getComputeResources(privateId));
        KeystoneCredentialView osCredential = new KeystoneCredentialView(auth);
        NovaInstanceView novaInstanceView = new NovaInstanceView(context.getName(), template, group.getType(), group.getLoginUserName());
        String imageId = osClient.imagesV2().list(Collections.singletonMap("name", image.getImageName())).get(0).getId();
        LOGGER.info("Selected image id: {}", imageId);
        Map<String, String> metadata = mergeMetadata(novaInstanceView.getMetadataMap(), tags);
        ServerCreateBuilder serverCreateBuilder = Builders.server().name(resource.getName()).image(imageId).flavor(getFlavorId(osClient, novaInstanceView.getFlavor())).keypairName(osCredential.getKeyPairName()).addMetadata(metadata).addNetworkPort(port.getStringParameter(OpenStackConstants.PORT_ID)).userData(new String(Base64.encodeBase64(image.getUserDataByType(group.getType()).getBytes())));
        BlockDeviceMappingBuilder blockDeviceMappingBuilder = Builders.blockDeviceMapping().uuid(imageId).sourceType(BDMSourceType.IMAGE).deviceName("/dev/vda").bootIndex(0).deleteOnTermination(true).destinationType(BDMDestType.LOCAL);
        serverCreateBuilder = serverCreateBuilder.blockDevice(blockDeviceMappingBuilder.build());
        for (CloudResource computeResource : context.getComputeResources(privateId)) {
            if (computeResource.getType() == ResourceType.OPENSTACK_ATTACHED_DISK) {
                BlockDeviceMappingCreate blockDeviceMappingCreate = Builders.blockDeviceMapping().uuid(computeResource.getReference()).deviceName(computeResource.getStringParameter(OpenStackConstants.VOLUME_MOUNT_POINT)).sourceType(BDMSourceType.VOLUME).destinationType(BDMDestType.VOLUME).build();
                serverCreateBuilder.blockDevice(blockDeviceMappingCreate);
            }
        }
        ServerCreate serverCreate = serverCreateBuilder.build();
        Server server = osClient.compute().servers().boot(serverCreate);
        return Collections.singletonList(createPersistedResource(resource, group.getName(), server.getId(), Collections.singletonMap(OpenStackConstants.SERVER, server)));
    } catch (OS4JException ex) {
        LOGGER.error("Failed to create OpenStack instance with privateId: {}", privateId, ex);
        throw new OpenStackResourceException("Instance creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) ServerCreate(org.openstack4j.model.compute.ServerCreate) BlockDeviceMappingCreate(org.openstack4j.model.compute.BlockDeviceMappingCreate) BlockDeviceMappingBuilder(org.openstack4j.model.compute.builder.BlockDeviceMappingBuilder) OS4JException(org.openstack4j.api.exceptions.OS4JException) NovaInstanceView(com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView) ServerCreateBuilder(org.openstack4j.model.compute.builder.ServerCreateBuilder) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 2 with KeystoneCredentialView

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

the class HeatTemplateBuilder method buildParameters.

public Map<String, String> buildParameters(AuthenticatedContext auth, CloudStack cloudStack, boolean existingNetwork, String existingSubnetCidr) {
    KeystoneCredentialView osCredential = new KeystoneCredentialView(auth);
    NeutronNetworkView neutronView = new NeutronNetworkView(cloudStack.getNetwork());
    Map<String, String> parameters = new HashMap<>();
    if (neutronView.isAssignFloatingIp()) {
        parameters.put("public_net_id", neutronView.getPublicNetId());
    }
    parameters.put("image_id", cloudStack.getImage().getImageName());
    if (cloudStack.getInstanceAuthentication().getPublicKeyId() != null) {
        parameters.put("key_name", cloudStack.getInstanceAuthentication().getPublicKeyId());
    } else {
        parameters.put("key_name", osCredential.getKeyPairName());
    }
    if (existingNetwork) {
        parameters.put("app_net_id", neutronView.getCustomNetworkId());
        if (isNoneEmpty(existingSubnetCidr)) {
            parameters.put("subnet_id", neutronView.getCustomSubnetId());
        } else {
            parameters.put("router_id", neutronView.getCustomRouterId());
        }
    }
    parameters.put("app_net_cidr", isBlank(existingSubnetCidr) ? neutronView.getSubnetCIDR() : existingSubnetCidr);
    return parameters;
}
Also used : HashMap(java.util.HashMap) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) FreeMarkerTemplateUtils.processTemplateIntoString(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils.processTemplateIntoString) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView)

Example 3 with KeystoneCredentialView

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

the class OpenStackResourceConnector method deleteKeyPair.

private void deleteKeyPair(AuthenticatedContext authenticatedContext, OSClient<?> client) {
    KeystoneCredentialView keystoneCredential = openStackClient.createKeystoneCredential(authenticatedContext);
    String keyPairName = keystoneCredential.getKeyPairName();
    client.compute().keypairs().delete(keyPairName);
    LOGGER.info("Keypair has been deleted: {}", keyPairName);
}
Also used : KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)

Example 4 with KeystoneCredentialView

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

the class OpenStackResourceConnector method createKeyPair.

private void createKeyPair(AuthenticatedContext authenticatedContext, CloudStack stack, OSClient<?> client) {
    KeystoneCredentialView keystoneCredential = openStackClient.createKeystoneCredential(authenticatedContext);
    String keyPairName = keystoneCredential.getKeyPairName();
    if (client.compute().keypairs().get(keyPairName) == null) {
        try {
            Keypair keyPair = client.compute().keypairs().create(keyPairName, stack.getInstanceAuthentication().getPublicKey());
            LOGGER.info("Keypair has been created: {}", keyPair);
        } catch (Exception e) {
            LOGGER.error("Failed to create keypair", e);
            throw new CloudConnectorException(e.getMessage(), e);
        }
    } else {
        LOGGER.info("Keypair already exists: {}", keyPairName);
    }
}
Also used : Keypair(org.openstack4j.model.compute.Keypair) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)

Example 5 with KeystoneCredentialView

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

the class OpenStackPlatformResources method networks.

@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    OSClient<?> osClient = openStackClient.createOSClient(cloudCredential);
    KeystoneCredentialView osCredential = openStackClient.createKeystoneCredential(cloudCredential);
    Set<CloudNetwork> cloudNetworks = new HashSet<>();
    List<? extends Network> networks = getNetworks(osClient);
    for (Network network : networks) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("networkType", network.getNetworkType());
        properties.put("providerPhyNet", network.getProviderPhyNet());
        properties.put("providerSegID", network.getProviderSegID());
        properties.put("tenantId", network.getTenantId());
        Map<String, String> subnets = new HashMap<>();
        List<? extends Subnet> neutronSubnets = network.getNeutronSubnets();
        LOGGER.info("neutron subnets for {}: {}", network.getName(), neutronSubnets);
        if (neutronSubnets != null) {
            for (Subnet neutronSubnet : neutronSubnets) {
                if (neutronSubnet != null) {
                    subnets.put(neutronSubnet.getId(), neutronSubnet.getName());
                }
            }
        }
        CloudNetwork cloudNetwork = new CloudNetwork(network.getName(), network.getId(), subnets, properties);
        cloudNetworks.add(cloudNetwork);
    }
    Map<String, Set<CloudNetwork>> result = new HashMap<>(1);
    result.put(region.value() == null ? osCredential.getTenantName() : region.value(), cloudNetworks);
    LOGGER.info("openstack cloud networks result: {}", result);
    return new CloudNetworks(result);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) CloudNetworks(com.sequenceiq.cloudbreak.cloud.model.CloudNetworks) Network(org.openstack4j.model.network.Network) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) Subnet(org.openstack4j.model.network.Subnet) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) HashSet(java.util.HashSet)

Aggregations

KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)8 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Keypair (org.openstack4j.model.compute.Keypair)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudNetwork (com.sequenceiq.cloudbreak.cloud.model.CloudNetwork)1 CloudNetworks (com.sequenceiq.cloudbreak.cloud.model.CloudNetworks)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 CloudSecurityGroup (com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup)1 CloudSecurityGroups (com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups)1 CloudSshKey (com.sequenceiq.cloudbreak.cloud.model.CloudSshKey)1 CloudSshKeys (com.sequenceiq.cloudbreak.cloud.model.CloudSshKeys)1 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)1 OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)1 NeutronNetworkView (com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView)1 NovaInstanceView (com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView)1 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)1 FreeMarkerTemplateUtils.processTemplateIntoString (com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils.processTemplateIntoString)1 OS4JException (org.openstack4j.api.exceptions.OS4JException)1