Search in sources :

Example 41 with ApiEntity

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

the class SyncManager method refresh.

public void refresh() {
    logger.debug("Refreshing state...");
    try {
        // Extract all registered APIs
        Map<String, io.gravitee.repository.management.model.Api> apis = apiRepository.findAll().stream().collect(Collectors.toMap(io.gravitee.repository.management.model.Api::getId, api -> api));
        // Get last event for each API
        Map<String, Event> events = new HashMap<>();
        apis.keySet().forEach(api -> {
            Event event = getLastEvent(api);
            events.put(api, event);
        });
        // Determine API which must be stopped and stop them
        events.entrySet().stream().filter(apiEvent -> {
            Event event = apiEvent.getValue();
            return event != null && (event.getType() == EventType.STOP_API || event.getType() == EventType.UNPUBLISH_API);
        }).forEach(apiEvent -> apiManager.undeploy(apiEvent.getKey()));
        // Determine API which must be deployed
        events.entrySet().stream().filter(apiEvent -> {
            Event event = apiEvent.getValue();
            return event != null && (event.getType() == EventType.START_API || event.getType() == EventType.PUBLISH_API);
        }).forEach(apiEvent -> {
            try {
                // Read API definition from event
                io.gravitee.repository.management.model.Api payloadApi = objectMapper.readValue(apiEvent.getValue().getPayload(), io.gravitee.repository.management.model.Api.class);
                // API to deploy
                ApiEntity apiToDeploy = convert(payloadApi);
                if (apiToDeploy != null) {
                    // Get deployed API
                    ApiEntity deployedApi = apiManager.get(apiToDeploy.getId());
                    // API is not yet deployed, so let's do it !
                    if (deployedApi == null) {
                        apiManager.deploy(apiToDeploy);
                    } else {
                        if (deployedApi.getDeployedAt().before(apiToDeploy.getDeployedAt())) {
                            apiManager.update(apiToDeploy);
                        }
                    }
                }
            } catch (IOException ioe) {
                logger.error("Error while determining deployed APIs store into events payload", ioe);
            }
        });
        lastRefreshAt = System.currentTimeMillis();
    } catch (TechnicalException te) {
        logger.error("Unable to sync instance", te);
    }
}
Also used : EventRepository(io.gravitee.repository.management.api.EventRepository) Logger(org.slf4j.Logger) Event(io.gravitee.repository.management.model.Event) ApiEntity(io.gravitee.management.model.ApiEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EventCriteria(io.gravitee.repository.management.api.search.EventCriteria) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) LifecycleState(io.gravitee.repository.management.model.LifecycleState) List(java.util.List) Api(io.gravitee.repository.management.model.Api) EventType(io.gravitee.repository.management.model.EventType) ApiRepository(io.gravitee.repository.management.api.ApiRepository) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) Map(java.util.Map) Lifecycle(io.gravitee.common.component.Lifecycle) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) HashMap(java.util.HashMap) ApiEntity(io.gravitee.management.model.ApiEntity) IOException(java.io.IOException) Api(io.gravitee.repository.management.model.Api) Event(io.gravitee.repository.management.model.Event) Api(io.gravitee.repository.management.model.Api)

Example 42 with ApiEntity

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

the class DynamicPropertiesService method onEvent.

@Override
public void onEvent(Event<ApiEvent, ApiEntity> event) {
    final ApiEntity api = event.content();
    switch(event.type()) {
        case DEPLOY:
            startDynamicProperties(api);
            break;
        case UNDEPLOY:
            stopDynamicProperties(api);
            break;
        case UPDATE:
            stopDynamicProperties(api);
            startDynamicProperties(api);
            break;
    }
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity)

Example 43 with ApiEntity

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

the class ApiMembersResource method getPermissions.

@GET
@Path("/permissions")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get API members", notes = "User must have the MANAGE_MEMBERS permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "API member's permissions", response = MemberEntity.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
public Response getPermissions(@PathParam("api") String api) {
    final ApiEntity apiEntity = apiService.findById(api);
    Map<String, char[]> permissions = new HashMap<>();
    if (isAuthenticated()) {
        final String userId = getAuthenticatedUser();
        if (isAdmin()) {
            final char[] rights = new char[] { CREATE.getId(), READ.getId(), UPDATE.getId(), DELETE.getId() };
            for (ApiPermission perm : ApiPermission.values()) {
                permissions.put(perm.getName(), rights);
            }
        } else {
            permissions = membershipService.getMemberPermissions(apiEntity, userId);
        }
    }
    return Response.ok(permissions).build();
}
Also used : HashMap(java.util.HashMap) ApiPermission(io.gravitee.management.model.permissions.ApiPermission) ApiEntity(io.gravitee.management.model.ApiEntity)

Example 44 with ApiEntity

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

the class HealthCheckServiceImpl method getAvailability.

@Override
public ApiMetrics getAvailability(String api, String field) {
    logger.debug("Run health availability query for API '{}'", api);
    try {
        ApiEntity apiEntity = apiService.findById(api);
        AvailabilityResponse response = healthCheckRepository.query(QueryBuilders.availability().api(api).field(AvailabilityQuery.Field.valueOf(field)).build());
        return convert(apiEntity, response.getEndpointAvailabilities(), field);
    } catch (Exception ex) {
        logger.error("An unexpected error occurs while searching for health data.", ex);
        return null;
    }
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) AvailabilityResponse(io.gravitee.repository.healthcheck.query.availability.AvailabilityResponse) AnalyticsException(io.gravitee.repository.analytics.AnalyticsException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 45 with ApiEntity

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

the class TopApiServiceImpl method findAll.

@Override
public List<TopApiEntity> findAll() {
    LOGGER.debug("Find all top APIs");
    final List<ApiEntity> apis = parameterService.findAll(PORTAL_TOP_APIS.getKey(), apiId -> apiService.findById(apiId), apiService::exists);
    if (!apis.isEmpty()) {
        final List<TopApiEntity> topApis = new ArrayList<>(apis.size());
        for (int i = 0; i < apis.size(); i++) {
            final ApiEntity api = apis.get(i);
            final TopApiEntity topApiEntity = new TopApiEntity();
            topApiEntity.setApi(api.getId());
            topApiEntity.setName(api.getName());
            topApiEntity.setVersion(api.getVersion());
            topApiEntity.setDescription(api.getDescription());
            topApiEntity.setOrder(i);
            topApis.add(topApiEntity);
        }
        return topApis;
    }
    return emptyList();
}
Also used : TopApiEntity(io.gravitee.management.model.TopApiEntity) UpdateTopApiEntity(io.gravitee.management.model.UpdateTopApiEntity) NewTopApiEntity(io.gravitee.management.model.NewTopApiEntity) ArrayList(java.util.ArrayList) ApiEntity(io.gravitee.management.model.ApiEntity) TopApiEntity(io.gravitee.management.model.TopApiEntity) UpdateTopApiEntity(io.gravitee.management.model.UpdateTopApiEntity) NewTopApiEntity(io.gravitee.management.model.NewTopApiEntity)

Aggregations

ApiEntity (io.gravitee.management.model.ApiEntity)50 Test (org.junit.Test)34 Response (javax.ws.rs.core.Response)19 UpdateApiEntity (io.gravitee.management.model.UpdateApiEntity)13 PageEntity (io.gravitee.management.model.PageEntity)5 NewApiEntity (io.gravitee.management.model.NewApiEntity)4 NewTopApiEntity (io.gravitee.management.model.NewTopApiEntity)3 TopApiEntity (io.gravitee.management.model.TopApiEntity)3 UpdateTopApiEntity (io.gravitee.management.model.UpdateTopApiEntity)3 Permission (io.gravitee.management.rest.security.Permission)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Proxy (io.gravitee.definition.model.Proxy)2 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)2 MemberEntity (io.gravitee.management.model.MemberEntity)2 SubscriptionEntity (io.gravitee.management.model.SubscriptionEntity)2 UserEntity (io.gravitee.management.model.UserEntity)2 RolePermission (io.gravitee.management.model.permissions.RolePermission)2 RolePermissionAction (io.gravitee.management.model.permissions.RolePermissionAction)2 SubscriptionQuery (io.gravitee.management.model.subscription.SubscriptionQuery)2