Search in sources :

Example 6 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class NamespaceProvisionerTest method nameIsSanitized.

@Test
void nameIsSanitized() {
    // 
    // Given that no resources associated to the provided deployment exist
    // 
    final ConnectorNamespace namespace = new ConnectorNamespace();
    namespace.id(uid());
    namespace.name("--eval");
    ConnectorNamespaceTenant tenant = new ConnectorNamespaceTenant().id(uid()).kind(ConnectorNamespaceTenantKind.ORGANISATION);
    namespace.setStatus(new ConnectorNamespaceStatus1().state(ConnectorNamespaceState.READY).connectorsDeployed(0));
    namespace.setTenant(tenant);
    namespace.setExpiration(new Date().toString());
    final List<ManagedConnector> connectors = List.of();
    final List<Secret> secrets = List.of();
    final FleetShardClient fleetShard = ConnectorTestSupport.fleetShard(CLUSTER_ID, connectors, secrets);
    final FleetManagerClient fleetManager = ConnectorTestSupport.fleetManagerClient();
    final FleetShardSyncConfig config = ConnectorTestSupport.config();
    final MeterRegistry registry = Mockito.mock(MeterRegistry.class);
    final ConnectorNamespaceProvisioner provisioner = new ConnectorNamespaceProvisioner(config, fleetShard, fleetManager, registry);
    final ArgumentCaptor<Namespace> nc = ArgumentCaptor.forClass(Namespace.class);
    // 
    // When deployment is applied
    // 
    provisioner.provision(namespace);
    verify(fleetShard).createNamespace(nc.capture());
    // 
    // Then resources must be created according to the deployment
    // 
    assertThat(nc.getValue()).satisfies(val -> {
        assertThat(val.getMetadata().getLabels()).containsEntry(LABEL_KUBERNETES_NAME, "a--eval");
    });
}
Also used : FleetShardClient(org.bf2.cos.fleetshard.sync.client.FleetShardClient) ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) ConnectorNamespaceStatus1(org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1) ManagedConnector(org.bf2.cos.fleetshard.api.ManagedConnector) ConnectorNamespaceProvisioner(org.bf2.cos.fleetshard.sync.resources.ConnectorNamespaceProvisioner) Date(java.util.Date) ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) Namespace(io.fabric8.kubernetes.api.model.Namespace) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Secret(io.fabric8.kubernetes.api.model.Secret) FleetManagerClient(org.bf2.cos.fleetshard.sync.client.FleetManagerClient) FleetShardSyncConfig(org.bf2.cos.fleetshard.sync.FleetShardSyncConfig) ConnectorNamespaceTenant(org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant) Test(org.junit.jupiter.api.Test)

Example 7 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class SyncTestSupport method namespace.

public static ConnectorNamespace namespace(String id, String name) {
    ConnectorNamespace answer = new ConnectorNamespace().id(id).name(name);
    ConnectorNamespaceTenant tenant = new ConnectorNamespaceTenant().id(uid()).kind(ConnectorNamespaceTenantKind.ORGANISATION);
    answer.setStatus(new ConnectorNamespaceStatus1().state(ConnectorNamespaceState.READY).connectorsDeployed(0));
    answer.setTenant(tenant);
    answer.setExpiration(new Date().toString());
    return answer;
}
Also used : ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) ConnectorNamespaceStatus1(org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1) ConnectorNamespaceTenant(org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant) Date(java.util.Date)

Example 8 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class MigrationService method runMigration.

public void runMigration() throws TenantManagerServiceException {
    if (deleteAll.isPresent() && deleteAll.get()) {
        log.warn("Removing all data first");
        TenantManagerConfig tm = TenantManagerConfig.builder().tenantManagerUrl("http://tenant-manager:8585").registryDeploymentUrl("https://service-registry-stage.apps.app-sre-stage-0.k3s7.p1.openshiftapps.com").build();
        for (Tenant t : tenantManagerClient.getAllTenants(tm)) {
            log.warn("Deleting tenant '{}'", t.getId());
            try {
                tenantManagerClient.deleteTenant(tm, t.getId());
            } catch (TenantNotFoundServiceException ex) {
                log.warn("Could not delete tenant '{}'. Tenant does not exist and may have been already deleted.", t.getId());
            }
        }
        CleanResult cleanResult = flyway.clean();
        log.info("Database clean result: " + "flywayVersion = '{}', " + "database = '{}', " + "schemasCleaned = '{}', " + "schemasDropped = '{}', " + "warnings = '{}'.", cleanResult.flywayVersion, cleanResult.database, cleanResult.schemasCleaned, cleanResult.schemasDropped, cleanResult.warnings);
    }
    MigrateResult migrateResult = flyway.migrate();
    log.info("Database migrate result: " + "flywayVersion = '{}', " + "database = '{}', " + "initialSchemaVersion = '{}', " + "targetSchemaVersion = '{}', " + "migrations = '{}', " + "warnings = '{}'.", migrateResult.flywayVersion, migrateResult.database, migrateResult.initialSchemaVersion, migrateResult.targetSchemaVersion, migrateResult.migrations, migrateResult.warnings);
}
Also used : TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) Tenant(org.bf2.srs.fleetmanager.spi.tenants.model.Tenant) MigrateResult(org.flywaydb.core.api.output.MigrateResult) TenantManagerConfig(org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig) CleanResult(org.flywaydb.core.api.output.CleanResult)

Example 9 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class TestInfraManager method runTenantManager.

// TODO replace tenant manager with mock?
private void runTenantManager(boolean authEnabled) throws IOException {
    Map<String, String> appEnv = new HashMap<>();
    if (authEnabled) {
        appEnv.put("AUTH_ENABLED", "true");
        appEnv.put("KEYCLOAK_URL", getMandatoryEnvVar(MAS_SSO_URL));
        appEnv.put("KEYCLOAK_REALM", getMandatoryEnvVar(MAS_SSO_REALM));
        appEnv.put("KEYCLOAK_API_CLIENT_ID", getMandatoryEnvVar(MAS_SSO_CLIENT_ID));
    }
    String datasourceUrl = deployPostgresql("tenant-manager");
    appEnv.put("DATASOURCE_URL", datasourceUrl);
    appEnv.put("DATASOURCE_USERNAME", "postgres");
    appEnv.put("DATASOURCE_PASSWORD", "postgres");
    // registry is not deployed in purpose, it may still work
    appEnv.put("REGISTRY_ROUTE_URL", "http://localhost:3888");
    appEnv.put("LOG_LEVEL", "DEBUG");
    String path = getTenantManagerJarPath();
    LOGGER.info("Starting Tenant Manager app from: {}", path);
    Exec executor = new Exec();
    CompletableFuture.supplyAsync(() -> {
        try {
            List<String> cmd = new ArrayList<>();
            cmd.add("java");
            cmd.addAll(Arrays.asList("-jar", path));
            int timeout = executor.execute(cmd, appEnv);
            return timeout == 0;
        } catch (Exception e) {
            LOGGER.error("Failed to start tenant manager (could not find runner JAR).", e);
            System.exit(1);
            return false;
        }
    }, runnable -> new Thread(runnable).start());
    processes.add(new TestInfraProcess() {

        @Override
        public String getName() {
            return "tenant-manager";
        }

        @Override
        public void close() throws Exception {
            executor.stop();
        }

        @Override
        public String getStdOut() {
            return executor.stdOut();
        }

        @Override
        public String getStdErr() {
            return executor.stdErr();
        }

        @Override
        public boolean isContainer() {
            return false;
        }
    });
    Awaitility.await("Tenant Manager is reachable").atMost(45, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> HttpUtils.isReachable("localhost", 8585, "Tenant Manager"));
    Awaitility.await("Tenant Manager is ready").atMost(45, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> HttpUtils.isReady(this.tenantManagerUrl, "/q/health/ready", false, "Tenant Manager"));
}
Also used : Exec(org.bf2.srs.fleetmanager.it.executor.Exec) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) UncheckedIOException(java.io.UncheckedIOException)

Example 10 with Tenant

use of org.bf2.srs.fleetmanager.spi.tenants.model.Tenant in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class RestClientTenantManagerServiceImpl method createTenant.

@Timed(value = Constants.TENANT_MANAGER_CREATE_TENANT_TIMER, description = Constants.TENANT_MANAGER_DESCRIPTION)
@Audited
// 3 retries, 200ms jitter
@Retry(retryOn = { TenantManagerServiceException.class })
@Override
public Tenant createTenant(TenantManagerConfig tm, CreateTenantRequest tenantRequest) throws TenantManagerServiceException {
    try {
        var client = getClient(tm);
        NewRegistryTenantRequest req = new NewRegistryTenantRequest();
        req.setOrganizationId(tenantRequest.getOrganizationId());
        req.setTenantId(tenantRequest.getTenantId());
        req.setCreatedBy(tenantRequest.getCreatedBy());
        req.setResources(Optional.ofNullable(tenantRequest.getResources()).stream().flatMap(Collection::stream).map(r -> {
            TenantResource tr = new TenantResource();
            tr.setType(ResourceType.fromValue(r.getType()));
            tr.setLimit(r.getLimit());
            return tr;
        }).collect(Collectors.toList()));
        RegistryTenant tenant = client.createTenant(req);
        return convert(tenant);
    } catch (TenantManagerClientException ex) {
        throw ExceptionConvert.convert(ex);
    }
}
Also used : TenantResource(io.apicurio.multitenant.api.datamodel.TenantResource) RegistryTenant(io.apicurio.multitenant.api.datamodel.RegistryTenant) TenantManagerClientException(io.apicurio.multitenant.client.exception.TenantManagerClientException) NewRegistryTenantRequest(io.apicurio.multitenant.api.datamodel.NewRegistryTenantRequest) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited) Timed(io.micrometer.core.annotation.Timed) Retry(org.eclipse.microprofile.faulttolerance.Retry)

Aggregations

TenantNotFoundServiceException (org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException)4 Transactional (javax.transaction.Transactional)3 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)3 Timed (io.micrometer.core.annotation.Timed)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ConnectorNamespace (org.bf2.cos.fleet.manager.model.ConnectorNamespace)2 ConnectorNamespaceStatus1 (org.bf2.cos.fleet.manager.model.ConnectorNamespaceStatus1)2 ConnectorNamespaceTenant (org.bf2.cos.fleet.manager.model.ConnectorNamespaceTenant)2 Audited (org.bf2.srs.fleetmanager.common.operation.auditing.Audited)2 DeprovisionRegistryTask (org.bf2.srs.fleetmanager.execution.impl.tasks.deprovision.DeprovisionRegistryTask)2 Tenant (org.bf2.srs.fleetmanager.spi.tenants.model.Tenant)2 TenantManagerConfig (org.bf2.srs.fleetmanager.spi.tenants.model.TenantManagerConfig)2 RegistryDeploymentData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryDeploymentData)2 Test (org.junit.jupiter.api.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 NewRegistryTenantRequest (io.apicurio.multitenant.api.datamodel.NewRegistryTenantRequest)1 RegistryTenant (io.apicurio.multitenant.api.datamodel.RegistryTenant)1 TenantResource (io.apicurio.multitenant.api.datamodel.TenantResource)1 UpdateRegistryTenantRequest (io.apicurio.multitenant.api.datamodel.UpdateRegistryTenantRequest)1