Search in sources :

Example 1 with ApiDeploymentEntity

use of io.gravitee.rest.api.model.api.ApiDeploymentEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiResourceTest method shouldDeployApi.

@Test
public void shouldDeployApi() {
    ApiDeploymentEntity deployEntity = new ApiDeploymentEntity();
    deployEntity.setDeploymentLabel("label");
    mockApi.setState(Lifecycle.State.STARTED);
    mockApi.setUpdatedAt(new Date());
    doReturn(mockApi).when(apiService).deploy(any(), any(), eq(EventType.PUBLISH_API), any());
    final Response response = envTarget(API + "/deploy").request().post(Entity.json(deployEntity));
    assertEquals(OK_200, response.getStatus());
    verify(apiService, times(1)).deploy(any(), any(), eq(EventType.PUBLISH_API), any());
}
Also used : Response(javax.ws.rs.core.Response) ApiDeploymentEntity(io.gravitee.rest.api.model.api.ApiDeploymentEntity) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ApiDeploymentEntity

use of io.gravitee.rest.api.model.api.ApiDeploymentEntity in project gravitee-management-rest-api by gravitee-io.

the class DynamicPropertyUpdater method update.

private void update(Collection<DynamicProperty> dynamicProperties) {
    // Get latest changes
    ApiEntity latestApi = apiService.findById(api.getId());
    List<Property> properties = (latestApi.getProperties() != null) ? latestApi.getProperties().getProperties() : Collections.emptyList();
    List<Property> userDefinedProperties = properties.stream().filter(property -> !property.isDynamic()).collect(Collectors.toList());
    Map<String, Property> propertyMap = properties.stream().collect(Collectors.toMap(Property::getKey, property -> property));
    List<Property> updatedProperties = new ArrayList<>();
    boolean needToBeSaved = false;
    for (DynamicProperty dynamicProperty : dynamicProperties) {
        Property property = propertyMap.get(dynamicProperty.getKey());
        if (property == null || property.isDynamic()) {
            updatedProperties.add(dynamicProperty);
        }
        // save properties only if there's something new
        if (property == null || (property.isDynamic() && !property.getValue().equals(dynamicProperty.getValue()))) {
            needToBeSaved = true;
        }
    }
    if (needToBeSaved) {
        // Add previous user-defined properties
        updatedProperties.addAll(userDefinedProperties);
        // Sort properties alphabetically to avoid redeploy if just the order has changed.
        List<Property> sortedUpdatedProperties = updatedProperties.stream().sorted(Comparator.comparing(Property::getKey)).collect(Collectors.toList());
        // Create properties container
        Properties apiProperties = new Properties();
        try {
            apiProperties.setProperties(sortedUpdatedProperties);
        } catch (RuntimeException e) {
            logger.error(e.getMessage(), e);
        }
        latestApi.setProperties(apiProperties);
        boolean isSync = apiService.isSynchronized(api.getId());
        // Update API
        apiService.update(latestApi.getId(), ApiService.convert(latestApi));
        // Do not deploy if there are manual changes to push
        if (isSync) {
            // Publish API only in case of changes
            if (!updatedProperties.containsAll(properties) || !properties.containsAll(updatedProperties)) {
                ApiDeploymentEntity deployEntity = new ApiDeploymentEntity();
                deployEntity.setDeploymentLabel("Dynamic properties sync");
                apiService.deploy(latestApi.getId(), "dynamic-property-updater", EventType.PUBLISH_API, deployEntity);
            }
        }
    }
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) Logger(org.slf4j.Logger) Properties(io.gravitee.definition.model.Properties) Property(io.gravitee.definition.model.Property) Provider(io.gravitee.rest.api.services.dynamicproperties.provider.Provider) LoggerFactory(org.slf4j.LoggerFactory) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) UserDetails(io.gravitee.rest.api.idp.api.authentication.UserDetails) Collectors(java.util.stream.Collectors) ApiService(io.gravitee.rest.api.service.ApiService) GrantedAuthority(org.springframework.security.core.GrantedAuthority) EventType(io.gravitee.rest.api.model.EventType) SecurityContext(org.springframework.security.core.context.SecurityContext) Handler(io.vertx.core.Handler) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) ApiDeploymentEntity(io.gravitee.rest.api.model.api.ApiDeploymentEntity) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) SystemRole(io.gravitee.rest.api.model.permissions.SystemRole) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiDeploymentEntity(io.gravitee.rest.api.model.api.ApiDeploymentEntity) Properties(io.gravitee.definition.model.Properties) Property(io.gravitee.definition.model.Property) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty)

Example 3 with ApiDeploymentEntity

use of io.gravitee.rest.api.model.api.ApiDeploymentEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiResourceTest method shouldNotDeployApi.

@Test
public void shouldNotDeployApi() {
    ApiDeploymentEntity deployEntity = new ApiDeploymentEntity();
    deployEntity.setDeploymentLabel("label_too_long_because_more_than_32_chars");
    mockApi.setState(Lifecycle.State.STARTED);
    mockApi.setUpdatedAt(new Date());
    doReturn(mockApi).when(apiService).deploy(any(), any(), eq(EventType.PUBLISH_API), any());
    final Response response = envTarget(API + "/deploy").request().post(Entity.json(deployEntity));
    assertEquals(BAD_REQUEST_400, response.getStatus());
    verify(apiService, times(0)).deploy(any(), any(), eq(EventType.PUBLISH_API), any());
}
Also used : Response(javax.ws.rs.core.Response) ApiDeploymentEntity(io.gravitee.rest.api.model.api.ApiDeploymentEntity) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ApiDeploymentEntity (io.gravitee.rest.api.model.api.ApiDeploymentEntity)3 Date (java.util.Date)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 Properties (io.gravitee.definition.model.Properties)1 Property (io.gravitee.definition.model.Property)1 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)1 EventType (io.gravitee.rest.api.model.EventType)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 RoleScope (io.gravitee.rest.api.model.permissions.RoleScope)1 SystemRole (io.gravitee.rest.api.model.permissions.SystemRole)1 ApiService (io.gravitee.rest.api.service.ApiService)1 DynamicProperty (io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty)1 Provider (io.gravitee.rest.api.services.dynamicproperties.provider.Provider)1 Handler (io.vertx.core.Handler)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Authentication (org.springframework.security.core.Authentication)1