use of org.openstack4j.model.heat.Stack 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;
}
use of org.openstack4j.model.heat.Stack in project cloudbreak by hortonworks.
the class OpenStackResourceConnector method checkByResourceType.
private CloudResourceStatus checkByResourceType(AuthenticatedContext authenticatedContext, OSClient<?> client, String stackName, Collection<CloudResource> list, CloudResource resource) {
CloudResourceStatus result = null;
switch(resource.getType()) {
case HEAT_STACK:
String heatStackId = resource.getName();
LOGGER.info("Checking OpenStack Heat stack status of: {}", stackName);
Stack heatStack = client.heat().stacks().getDetails(stackName, heatStackId);
result = utils.heatStatus(resource, heatStack);
break;
case OPENSTACK_NETWORK:
result = checkResourceStatus(authenticatedContext, stackName, list, resource, ResourceType.OPENSTACK_NETWORK);
break;
case OPENSTACK_SUBNET:
result = checkResourceStatus(authenticatedContext, stackName, list, resource, ResourceType.OPENSTACK_SUBNET);
break;
case OPENSTACK_ROUTER:
case OPENSTACK_INSTANCE:
case OPENSTACK_PORT:
case OPENSTACK_ATTACHED_DISK:
case OPENSTACK_SECURITY_GROUP:
case OPENSTACK_FLOATING_IP:
break;
default:
throw new CloudConnectorException(String.format("Invalid resource type: %s", resource.getType()));
}
return result;
}
use of org.openstack4j.model.heat.Stack in project cloudbreak by hortonworks.
the class OpenStackMetadataCollector method collect.
@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
CloudResource resource = utils.getHeatResource(resources);
String stackName = utils.getStackName(authenticatedContext);
String heatStackId = resource.getName();
List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> utils.getPrivateInstanceId(from.getGroupName(), Long.toString(from.getPrivateId())));
OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
Stack heatStack = client.heat().stacks().getDetails(stackName, heatStackId);
List<CloudVmMetaDataStatus> results = new ArrayList<>();
List<Map<String, Object>> outputs = heatStack.getOutputs();
for (Map<String, Object> map : outputs) {
String instanceUUID = (String) map.get("output_value");
if (!StringUtils.isEmpty(instanceUUID)) {
Server server = client.compute().servers().get(instanceUUID);
Map<String, String> metadata = server.getMetadata();
String privateInstanceId = utils.getPrivateInstanceId(metadata);
InstanceTemplate template = templateMap.get(privateInstanceId);
if (template != null) {
CloudInstanceMetaData md = cloudInstanceMetaDataExtractor.extractMetadata(client, server, instanceUUID);
// TODO use here sshkey
CloudInstance cloudInstance = new CloudInstance(instanceUUID, template, null);
CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
results.add(new CloudVmMetaDataStatus(status, md));
}
}
}
return results;
}
use of org.openstack4j.model.heat.Stack in project openstack4j by ContainX.
the class StackServiceTests method testAdoptStack.
public void testAdoptStack() throws Exception {
respondWith(JSON_ADOPT);
AdoptStackData adoptStackData = new ObjectMapper().readValue(getResource(JSON_ABANDON), HeatAdoptStackData.class);
Stack adoptedStack = osv3().heat().stacks().adopt(adoptStackData, new HashMap<String, String>(), false, 30L, null);
takeRequest();
assertEquals(adoptedStack.getId(), "79370050-6038-4ea2-baaa-3e4706d59e0e");
}
Aggregations