Search in sources :

Example 26 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional 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 27 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class ServiceManagerResourceUIServiceImpl method getResources.

@Override
@Transactional(readOnly = true)
public String getResources() {
    final TemplateCall call = templater.template("resource_templates");
    call.set("entities", templateDao.getAll());
    return call.process();
}
Also used : TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 28 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class ServiceManagerResourceUIServiceImpl method doDiscard.

@Override
@Transactional
public Response doDiscard(final int id, final String nonce) {
    nonceStore.validate(nonce);
    final ResourceInstanceEntity instance = resourceProvisionService.discardInstance(id);
    return Response.seeOther(URI.create("/resources/template/" + instance.getTemplate().getId())).build();
}
Also used : ResourceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ResourceInstanceEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 29 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class LargeTableQueryTest method testSearchWorks.

/**
 * Test that searching works with a simple primary key
 */
@Test
@Transactional
public void testSearchWorks() {
    dao.save(new LargeTableSimplePKEntity("Alice"));
    dao.save(new LargeTableSimplePKEntity("Bob"));
    dao.save(new LargeTableSimplePKEntity("Carol"));
    dao.save(new LargeTableSimplePKEntity("Dave"));
    dao.save(new LargeTableSimplePKEntity("Eve"));
    // Try a regular query
    assertEquals(2, dao.countWithAInName());
    // Now try a web query
    assertEquals(2, dao.findByUriQuery(new WebQuery().contains("name", "a")).getList().size());
}
Also used : WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Test(org.junit.Test) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 30 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class ServiceManagerRegistryRestServiceImpl method register.

@Override
@Transactional
public void register(final String instanceId, final String endpoint, final String managementToken, final String codeRevision) {
    ServiceInstanceEntity entity = dao.getById(instanceId);
    final boolean update = (entity != null);
    // Update an existing entity
    if (entity == null) {
        entity = new ServiceInstanceEntity();
        entity.setId(instanceId);
    }
    entity.setEndpoint(endpoint);
    entity.setManagementToken(managementToken);
    entity.setCodeRevision(codeRevision);
    // Proactively update the cache used by the logging service so that it won't have to make a db query
    cache.put(entity);
    if (update)
        dao.update(entity);
    else
        dao.save(entity);
}
Also used : ServiceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Aggregations

Transactional (com.peterphi.std.guice.database.annotation.Transactional)46 UserEntity (com.peterphi.usermanager.db.entity.UserEntity)13 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)11 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)9 RoleEntity (com.peterphi.usermanager.db.entity.RoleEntity)9 AuthConstraint (com.peterphi.std.guice.common.auth.annotations.AuthConstraint)8 OAuthServiceEntity (com.peterphi.usermanager.db.entity.OAuthServiceEntity)5 AuthenticationFailureException (com.peterphi.usermanager.guice.authentication.AuthenticationFailureException)5 OAuthSessionEntity (com.peterphi.usermanager.db.entity.OAuthSessionEntity)4 Test (org.junit.Test)4 ResourceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ResourceInstanceEntity)3 ResourceTemplateEntity (com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity)3 Criteria (org.hibernate.Criteria)3 List (java.util.List)2 DateTime (org.joda.time.DateTime)2 Timer (com.codahale.metrics.Timer)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 ServiceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity)1 ResourceNetworkConfig (com.peterphi.servicemanager.service.guice.ResourceNetworkConfig)1