Search in sources :

Example 46 with AppNamespace

use of com.ctrip.framework.apollo.common.entity.AppNamespace 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 47 with AppNamespace

use of com.ctrip.framework.apollo.common.entity.AppNamespace in project apollo by ctripcorp.

the class AppNamespaceServiceWithCache method handleUpdatedAppNamespaces.

// for those updated app namespaces
private Set<Long> handleUpdatedAppNamespaces(Iterable<AppNamespace> appNamespaces) {
    Set<Long> foundIds = Sets.newHashSet();
    for (AppNamespace appNamespace : appNamespaces) {
        foundIds.add(appNamespace.getId());
        AppNamespace thatInCache = appNamespaceIdCache.get(appNamespace.getId());
        if (thatInCache != null && appNamespace.getDataChangeLastModifiedTime().after(thatInCache.getDataChangeLastModifiedTime())) {
            appNamespaceIdCache.put(appNamespace.getId(), appNamespace);
            String oldKey = assembleAppNamespaceKey(thatInCache);
            String newKey = assembleAppNamespaceKey(appNamespace);
            appNamespaceCache.put(newKey, appNamespace);
            // in case appId or namespaceName changes
            if (!newKey.equals(oldKey)) {
                appNamespaceCache.remove(oldKey);
            }
            if (appNamespace.isPublic()) {
                publicAppNamespaceCache.put(appNamespace.getName(), appNamespace);
                // in case namespaceName changes
                if (!appNamespace.getName().equals(thatInCache.getName()) && thatInCache.isPublic()) {
                    publicAppNamespaceCache.remove(thatInCache.getName());
                }
            } else if (thatInCache.isPublic()) {
                // just in case isPublic changes
                publicAppNamespaceCache.remove(thatInCache.getName());
            }
            logger.info("Found AppNamespace changes, old: {}, new: {}", thatInCache, appNamespace);
        }
    }
    return foundIds;
}
Also used : AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Example 48 with AppNamespace

use of com.ctrip.framework.apollo.common.entity.AppNamespace in project apollo by ctripcorp.

the class AppNamespaceServiceWithCache method findByAppIdAndNamespaces.

public List<AppNamespace> findByAppIdAndNamespaces(String appId, Set<String> namespaceNames) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(appId), "appId must not be null");
    if (namespaceNames == null || namespaceNames.isEmpty()) {
        return Collections.emptyList();
    }
    List<AppNamespace> result = Lists.newArrayList();
    for (String namespaceName : namespaceNames) {
        AppNamespace appNamespace = appNamespaceCache.get(STRING_JOINER.join(appId, namespaceName));
        if (appNamespace != null) {
            result.add(appNamespace);
        }
    }
    return result;
}
Also used : AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Example 49 with AppNamespace

use of com.ctrip.framework.apollo.common.entity.AppNamespace in project apollo by ctripcorp.

the class ConfigControllerTest method testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace.

@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace() throws Exception {
    String someClientSideReleaseKey = "1";
    String someServerSideReleaseKey = "2";
    HttpServletResponse someResponse = mock(HttpServletResponse.class);
    String somePublicAppId = "somePublicAppId";
    AppNamespace somePublicAppNamespace = assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName);
    String appId = ConfigConsts.NO_APPID_PLACEHOLDER;
    when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName)).thenReturn(somePublicAppNamespace);
    when(configService.loadConfig(appId, someClientIp, someClientLabel, somePublicAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(somePublicRelease);
    when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);
    when(namespaceUtil.normalizeNamespace(appId, somePublicNamespaceName)).thenReturn(somePublicNamespaceName);
    ApolloConfig result = configController.queryConfig(appId, someClusterName, somePublicNamespaceName, someDataCenter, someClientSideReleaseKey, someClientIp, someClientLabel, someMessagesAsString, someRequest, someResponse);
    verify(configService, never()).loadConfig(appId, someClientIp, someClientLabel, appId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages);
    assertEquals(someServerSideReleaseKey, result.getReleaseKey());
    assertEquals(appId, result.getAppId());
    assertEquals(someClusterName, result.getCluster());
    assertEquals(somePublicNamespaceName, result.getNamespaceName());
    assertEquals("foo", result.getConfigurations().get("apollo.public.bar"));
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Test(org.junit.Test)

Example 50 with AppNamespace

use of com.ctrip.framework.apollo.common.entity.AppNamespace in project apollo by ctripcorp.

the class ConfigControllerTest method testQueryConfigWithPublicNamespaceAndAppOverride.

@Test
public void testQueryConfigWithPublicNamespaceAndAppOverride() throws Exception {
    String someAppSideReleaseKey = "1";
    String somePublicAppSideReleaseKey = "2";
    HttpServletResponse someResponse = mock(HttpServletResponse.class);
    String somePublicAppId = "somePublicAppId";
    AppNamespace somePublicAppNamespace = assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName);
    when(someRelease.getConfigurations()).thenReturn("{\"apollo.public.foo\": \"foo-override\"}");
    when(somePublicRelease.getConfigurations()).thenReturn("{\"apollo.public.foo\": \"foo\", \"apollo.public.bar\": \"bar\"}");
    when(configService.loadConfig(someAppId, someClientIp, someClientLabel, someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(someRelease);
    when(someRelease.getReleaseKey()).thenReturn(someAppSideReleaseKey);
    when(someRelease.getNamespaceName()).thenReturn(somePublicNamespaceName);
    when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName)).thenReturn(somePublicAppNamespace);
    when(configService.loadConfig(someAppId, someClientIp, someClientLabel, somePublicAppId, someClusterName, somePublicNamespaceName, someDataCenter, someNotificationMessages)).thenReturn(somePublicRelease);
    when(somePublicRelease.getReleaseKey()).thenReturn(somePublicAppSideReleaseKey);
    when(somePublicRelease.getAppId()).thenReturn(somePublicAppId);
    when(somePublicRelease.getClusterName()).thenReturn(someDataCenter);
    when(somePublicRelease.getNamespaceName()).thenReturn(somePublicNamespaceName);
    ApolloConfig result = configController.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter, someAppSideReleaseKey, someClientIp, someClientLabel, someMessagesAsString, someRequest, someResponse);
    assertEquals(Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(someAppSideReleaseKey, somePublicAppSideReleaseKey), result.getReleaseKey());
    assertEquals(someAppId, result.getAppId());
    assertEquals(someClusterName, result.getCluster());
    assertEquals(somePublicNamespaceName, result.getNamespaceName());
    assertEquals("foo-override", result.getConfigurations().get("apollo.public.foo"));
    assertEquals("bar", result.getConfigurations().get("apollo.public.bar"));
    verify(instanceConfigAuditUtil, times(1)).audit(someAppId, someClusterName, someDataCenter, someClientIp, someAppId, someClusterName, somePublicNamespaceName, someAppSideReleaseKey);
    verify(instanceConfigAuditUtil, times(1)).audit(someAppId, someClusterName, someDataCenter, someClientIp, somePublicAppId, someDataCenter, somePublicNamespaceName, somePublicAppSideReleaseKey);
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Test(org.junit.Test)

Aggregations

AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)71 Test (org.junit.Test)32 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)15 Sql (org.springframework.test.context.jdbc.Sql)13 AbstractIntegrationTest (com.ctrip.framework.apollo.portal.AbstractIntegrationTest)11 Transactional (org.springframework.transaction.annotation.Transactional)9 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)7 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)7 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)6 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AppNamespaceCreationEvent (com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)3 App (com.ctrip.framework.apollo.common.entity.App)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Release (com.ctrip.framework.apollo.biz.entity.Release)2 AppNamespaceDTO (com.ctrip.framework.apollo.common.dto.AppNamespaceDTO)2 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)2 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)2