Search in sources :

Example 41 with Namespace

use of com.ctrip.framework.apollo.biz.entity.Namespace in project apollo by ctripcorp.

the class NamespaceService method findChildNamespace.

public Namespace findChildNamespace(String appId, String parentClusterName, String namespaceName) {
    List<Namespace> namespaces = findByAppIdAndNamespaceName(appId, namespaceName);
    if (CollectionUtils.isEmpty(namespaces) || namespaces.size() == 1) {
        return null;
    }
    List<Cluster> childClusters = clusterService.findChildClusters(appId, parentClusterName);
    if (CollectionUtils.isEmpty(childClusters)) {
        return null;
    }
    Set<String> childClusterNames = childClusters.stream().map(Cluster::getName).collect(Collectors.toSet());
    // the child namespace is the intersection of the child clusters and child namespaces
    for (Namespace namespace : namespaces) {
        if (childClusterNames.contains(namespace.getClusterName())) {
            return namespace;
        }
    }
    return null;
}
Also used : Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Example 42 with Namespace

use of com.ctrip.framework.apollo.biz.entity.Namespace in project apollo by ctripcorp.

the class NamespaceService method deleteNamespace.

@Transactional
public Namespace deleteNamespace(Namespace namespace, String operator) {
    String appId = namespace.getAppId();
    String clusterName = namespace.getClusterName();
    String namespaceName = namespace.getNamespaceName();
    itemService.batchDelete(namespace.getId(), operator);
    commitService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);
    // histories need them
    if (!isChildNamespace(namespace)) {
        releaseService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);
    }
    // delete child namespace
    Namespace childNamespace = findChildNamespace(namespace);
    if (childNamespace != null) {
        namespaceBranchService.deleteBranch(appId, clusterName, namespaceName, childNamespace.getClusterName(), NamespaceBranchStatus.DELETED, operator);
        // delete child namespace's releases. Notice: delete child namespace will not delete child namespace's releases
        releaseService.batchDelete(appId, childNamespace.getClusterName(), namespaceName, operator);
    }
    releaseHistoryService.batchDelete(appId, clusterName, namespaceName, operator);
    instanceService.batchDeleteInstanceConfig(appId, clusterName, namespaceName);
    namespaceLockService.unlock(namespace.getId());
    namespace.setDeleted(true);
    namespace.setDataChangeLastModifiedBy(operator);
    auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.DELETE, operator);
    Namespace deleted = namespaceRepository.save(namespace);
    // Publish release message to do some clean up in config service, such as updating the cache
    messageSender.sendMessage(ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName), Topics.APOLLO_RELEASE_TOPIC);
    return deleted;
}
Also used : Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 43 with Namespace

use of com.ctrip.framework.apollo.biz.entity.Namespace in project apollo by ctripcorp.

the class NamespaceService method save.

@Transactional
public Namespace save(Namespace entity) {
    if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {
        throw new ServiceException("namespace not unique");
    }
    // protection
    entity.setId(0);
    Namespace namespace = namespaceRepository.save(entity);
    auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT, namespace.getDataChangeCreatedBy());
    return namespace;
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 44 with Namespace

use of com.ctrip.framework.apollo.biz.entity.Namespace in project apollo by ctripcorp.

the class NamespaceService method instanceOfAppNamespaces.

@Transactional
public void instanceOfAppNamespaces(String appId, String clusterName, String createBy) {
    List<AppNamespace> appNamespaces = appNamespaceService.findByAppId(appId);
    for (AppNamespace appNamespace : appNamespaces) {
        Namespace ns = new Namespace();
        ns.setAppId(appId);
        ns.setClusterName(clusterName);
        ns.setNamespaceName(appNamespace.getName());
        ns.setDataChangeCreatedBy(createBy);
        ns.setDataChangeLastModifiedBy(createBy);
        namespaceRepository.save(ns);
        auditService.audit(Namespace.class.getSimpleName(), ns.getId(), Audit.OP.INSERT, createBy);
    }
}
Also used : AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 45 with Namespace

use of com.ctrip.framework.apollo.biz.entity.Namespace in project apollo by ctripcorp.

the class ConfigFileControllerIntegrationTest method testConfigChanged.

@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testConfigChanged() throws Exception {
    ResponseEntity<String> response = restTemplate.getForEntity("http://{baseurl}/configfiles/{appId}/{clusterName}/{namespace}", String.class, getHostUrl(), someAppId, someCluster, someNamespace);
    String result = response.getBody();
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertTrue(result.contains("k2=v2"));
    String someReleaseName = "someReleaseName";
    String someReleaseComment = "someReleaseComment";
    Namespace namespace = new Namespace();
    namespace.setAppId(someAppId);
    namespace.setClusterName(someCluster);
    namespace.setNamespaceName(someNamespace);
    String someOwner = "someOwner";
    Map<String, String> newConfigurations = ImmutableMap.of("k1", "v1-changed", "k2", "v2-changed");
    buildRelease(someReleaseName, someReleaseComment, namespace, newConfigurations, someOwner);
    ResponseEntity<String> anotherResponse = restTemplate.getForEntity("http://{baseurl}/configfiles/{appId}/{clusterName}/{namespace}", String.class, getHostUrl(), someAppId, someCluster, someNamespace);
    assertEquals(response.getBody(), anotherResponse.getBody());
    List<String> keys = Lists.newArrayList(someAppId, someCluster, someNamespace);
    String message = Strings.join(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR, keys.iterator());
    sendReleaseMessage(message);
    TimeUnit.MILLISECONDS.sleep(500);
    ResponseEntity<String> newResponse = restTemplate.getForEntity("http://{baseurl}/configfiles/{appId}/{clusterName}/{namespace}", String.class, getHostUrl(), someAppId, someCluster, someNamespace);
    result = newResponse.getBody();
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertTrue(result.contains("k1=v1-changed"));
    assertTrue(result.contains("k2=v2-changed"));
}
Also used : Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Aggregations

Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)60 Test (org.junit.Test)24 Release (com.ctrip.framework.apollo.biz.entity.Release)22 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)14 Item (com.ctrip.framework.apollo.biz.entity.Item)13 Transactional (org.springframework.transaction.annotation.Transactional)11 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)10 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)10 Sql (org.springframework.test.context.jdbc.Sql)10 ReleaseHistory (com.ctrip.framework.apollo.biz.entity.ReleaseHistory)8 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)8 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)8 GrayReleaseRule (com.ctrip.framework.apollo.biz.entity.GrayReleaseRule)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 AbstractUnitTest (com.ctrip.framework.apollo.biz.AbstractUnitTest)4 Commit (com.ctrip.framework.apollo.biz.entity.Commit)3 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)2 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)2 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2