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);
}
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations