Search in sources :

Example 1 with ResourceTemplateEntity

use of com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity in project stdlib by petergeneric.

the class ResourceProvisionService method newInstance.

public ResourceInstanceEntity newInstance(String template, Map<String, String> metadata) {
    final ResourceTemplateDefinition def = getTemplateDefinition(template);
    final ResourceTemplateEntity dbTemplate = getOrCreateTemplate(template);
    return newInstance(dbTemplate, def, metadata);
}
Also used : ResourceTemplateDefinition(com.peterphi.servicemanager.service.rest.resource.jaxb.ResourceTemplateDefinition) ResourceTemplateEntity(com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity)

Example 2 with ResourceTemplateEntity

use of com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity in project stdlib by petergeneric.

the class ServiceManagerResourceUIServiceImpl method getResource.

/**
 * N.B. transaction may be read-write because this read operation may result in reading a new template (or discovering an
 * update was made to the template)
 *
 * @param id
 *
 * @return
 */
@Override
@Transactional
public String getResource(String id) {
    final TemplateCall call = templater.template("resource_template");
    final ResourceTemplateEntity entity = resourceProvisionService.getOrCreateTemplate(id);
    if (entity == null)
        throw new RestException(404, "No such resource with id: " + id);
    call.set("entity", entity);
    call.set("nonce", nonceStore.getValue());
    return call.process();
}
Also used : RestException(com.peterphi.std.guice.restclient.exception.RestException) ResourceTemplateEntity(com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 3 with ResourceTemplateEntity

use of com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity in project stdlib by petergeneric.

the class ResourceProvisionService method getOrCreateTemplate.

@Transactional
public ResourceTemplateEntity getOrCreateTemplate(final String name) {
    final String revision = templateConfig.config.getLastRevision();
    ResourceTemplateEntity entity = templateDao.getById(name);
    if (entity != null) {
        // Check if the template has been updated
        if (!StringUtils.equals(entity.getLatestRevision(), revision)) {
            entity.setLatestRevision(revision);
            entity.setRevisions(entity.getRevisions() + 1);
            templateDao.update(entity);
        }
        return entity;
    } else {
        entity = new ResourceTemplateEntity();
        entity.setId(name);
        entity.setLatestRevision(revision);
        entity.setRevisions(1);
        templateDao.save(entity);
        return templateDao.getById(name);
    }
}
Also used : ResourceTemplateEntity(com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 4 with ResourceTemplateEntity

use of com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity in project stdlib by petergeneric.

the class ResourceProvisionService method newInstance.

@Transactional
public ResourceInstanceEntity newInstance(final ResourceTemplateEntity dbTemplate, final ResourceTemplateDefinition definition, Map<String, String> metadata) {
    log.info("Provision new instance of template:" + dbTemplate.getId());
    final AzureExistingVM azureVM = definition.azureExistingVM;
    // TODO when adding a second provider make this generic so providers can be easily plugged in
    List<ResourceInstanceEntity> running = getAllRunning(dbTemplate);
    if (running.isEmpty()) {
        log.info("Provision new instance of template: ");
        ResourceInstanceEntity instance = new ResourceInstanceEntity();
        instance.setTemplate(dbTemplate);
        instance.setTemplateRevision(dbTemplate.getLatestRevision());
        instance.setTemplateRevisionCounter(dbTemplate.getRevisions());
        instance.setMetadata(metadata);
        instance.setProvider(AzureExistingVMProvider.PROVIDER);
        instance.setProviderInstanceId(azureVM.id);
        // TODO in the future when making generic this should be TO_PROVISION and we should make the provision call asynchronously
        instance.setState(ResourceInstanceState.PROVISIONING);
        instance.setId(instanceDao.save(instance));
        // Create a new instance
        azureExistingVMProvider.start(azureVM, metadata);
        return instance;
    } else {
        throw new IllegalArgumentException("Cannot create a new instance: provider is " + AzureExistingVMProvider.PROVIDER + " but an instance already appears to be running: " + running.stream().map(e -> e.getProviderInstanceId()).collect(Collectors.toList()));
    }
}
Also used : ResourceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ResourceInstanceEntity) ResourceInstanceState(com.peterphi.servicemanager.service.rest.resource.type.ResourceInstanceState) StringUtils(org.apache.commons.lang.StringUtils) HibernateDao(com.peterphi.std.guice.hibernate.dao.HibernateDao) Inject(com.google.inject.Inject) Transactional(com.peterphi.std.guice.database.annotation.Transactional) ResourceNetworkConfig(com.peterphi.servicemanager.service.guice.ResourceNetworkConfig) ResourceTemplateDefinition(com.peterphi.servicemanager.service.rest.resource.jaxb.ResourceTemplateDefinition) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) List(java.util.List) JAXBSerialiserFactory(com.peterphi.std.util.jaxb.JAXBSerialiserFactory) Map(java.util.Map) AzureExistingVM(com.peterphi.servicemanager.service.rest.resource.jaxb.AzureExistingVM) ResourceTemplateEntity(com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity) Singleton(com.google.inject.Singleton) AzureExistingVM(com.peterphi.servicemanager.service.rest.resource.jaxb.AzureExistingVM) ResourceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ResourceInstanceEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 5 with ResourceTemplateEntity

use of com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity in project stdlib by petergeneric.

the class ServiceManagerResourceUIServiceImpl method doProvision.

@Override
public Response doProvision(final String id, final String nonce) {
    nonceStore.validate(nonce);
    final ResourceTemplateEntity entity = resourceProvisionService.getOrCreateTemplate(id);
    if (entity == null)
        throw new RestException(404, "No such resource with id: " + id);
    resourceProvisionService.newInstance(id, Collections.emptyMap());
    // Redirect to template page
    return Response.seeOther(URI.create("/resources/template/" + id)).build();
}
Also used : RestException(com.peterphi.std.guice.restclient.exception.RestException) ResourceTemplateEntity(com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity)

Aggregations

ResourceTemplateEntity (com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity)5 Transactional (com.peterphi.std.guice.database.annotation.Transactional)3 ResourceTemplateDefinition (com.peterphi.servicemanager.service.rest.resource.jaxb.ResourceTemplateDefinition)2 RestException (com.peterphi.std.guice.restclient.exception.RestException)2 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 ResourceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ResourceInstanceEntity)1 ResourceNetworkConfig (com.peterphi.servicemanager.service.guice.ResourceNetworkConfig)1 AzureExistingVM (com.peterphi.servicemanager.service.rest.resource.jaxb.AzureExistingVM)1 ResourceInstanceState (com.peterphi.servicemanager.service.rest.resource.type.ResourceInstanceState)1 HibernateDao (com.peterphi.std.guice.hibernate.dao.HibernateDao)1 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)1 JAXBSerialiserFactory (com.peterphi.std.util.jaxb.JAXBSerialiserFactory)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang.StringUtils)1 Logger (org.apache.log4j.Logger)1