Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create integration and list integrations #6991

Merged
merged 1 commit into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,29 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.management.api;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.management.model.Integration;

/**
* @author Remi Baptiste (remi.baptiste at graviteesource.com)
* @author GraviteeSource Team
*/
public interface IntegrationRepository extends CrudRepository<Integration, String> {
Page<Integration> findAllByEnvironment(String environmentId, Pageable pageable) throws TechnicalException;
}
@@ -0,0 +1,47 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.management.model;

import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author Remi Baptiste (remi.baptiste at graviteesource.com)
* @author GraviteeSource Team
*/
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Integration {

private String id;

private String name;

private String description;

private String provider;

private String environmentId;

private Date createdAt;

private Date updatedAt;
}
@@ -0,0 +1,113 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.IntegrationRepository;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.management.model.Integration;
import io.gravitee.repository.mongodb.management.internal.integration.IntegrationMongoRepository;
import io.gravitee.repository.mongodb.management.internal.model.IntegrationMongo;
import io.gravitee.repository.mongodb.management.mapper.GraviteeMapper;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* @author Remi Baptiste (remi.baptiste at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
public class MongoIntegrationRepository implements IntegrationRepository {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private IntegrationMongoRepository internalRepository;

@Autowired
private GraviteeMapper mapper;

@Override
public Optional<Integration> findById(String s) throws TechnicalException {
logger.debug("Find integration by id [{}]", s);
Optional<Integration> result = internalRepository.findById(s).map(this::map);
logger.debug("Find integration by id [{}] - DONE", s);
return result;
}

@Override
public Integration create(Integration integration) throws TechnicalException {
logger.debug("Create integration [{}]", integration.getId());
Integration createdIntegration = map(internalRepository.insert(map(integration)));
logger.debug("Create integration [{}] - Done", createdIntegration.getId());
return createdIntegration;
}

@Override
public Integration update(Integration integration) throws TechnicalException {
if (integration == null) {
throw new IllegalStateException("Integration must not be null");
}

return internalRepository
.findById(integration.getId())
.map(found -> {
logger.debug("Update integration [{}]", integration.getId());
Integration updatedIntegration = map(internalRepository.save(map(integration)));
logger.debug("Update integration [{}] - Done", updatedIntegration.getId());
return updatedIntegration;
})
.orElseThrow(() -> new IllegalStateException(String.format("No integration found with id [%s]", integration.getId())));
}

@Override
public void delete(String id) throws TechnicalException {
logger.debug("Delete integration [{}]", id);
internalRepository.deleteById(id);
logger.debug("Delete integration [{}] - Done", id);
}

@Override
public Set<Integration> findAll() throws TechnicalException {
return internalRepository.findAll().stream().map(this::map).collect(Collectors.toSet());
}

@Override
public Page<Integration> findAllByEnvironment(String environmentId, Pageable pageable) throws TechnicalException {
logger.debug("Search by environment ID [{}]", environmentId);

Page<IntegrationMongo> integrations = internalRepository.findAllByEnvironmentId(environmentId, pageable);
List<Integration> content = mapper.mapIntegrationsList(integrations.getContent());

logger.debug("Search by environment ID [{}] - Done", environmentId);
return new Page<>(content, integrations.getPageNumber(), (int) integrations.getPageElements(), integrations.getTotalElements());
}

private Integration map(IntegrationMongo integrationMongo) {
return mapper.map(integrationMongo);
}

private IntegrationMongo map(Integration integration) {
return mapper.map(integration);
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.integration;

import io.gravitee.repository.mongodb.management.internal.model.IntegrationMongo;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

/**
* @author Remi Baptiste (remi.baptiste at graviteesource.com)
* @author GraviteeSource Team
*/
@Repository
public interface IntegrationMongoRepository extends MongoRepository<IntegrationMongo, String>, IntegrationMongoRepositoryCustom {}
@@ -0,0 +1,24 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.integration;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.mongodb.management.internal.model.IntegrationMongo;

public interface IntegrationMongoRepositoryCustom {
Page<IntegrationMongo> findAllByEnvironmentId(String environmentId, Pageable pageable);
}
@@ -0,0 +1,52 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.integration;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.mongodb.management.internal.model.IntegrationMongo;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class IntegrationMongoRepositoryImpl implements IntegrationMongoRepositoryCustom {

private final MongoTemplate mongoTemplate;

@Override
public Page<IntegrationMongo> findAllByEnvironmentId(String environmentId, Pageable pageable) {
Query query = new Query();
query.addCriteria(Criteria.where("environmentId").is(environmentId));
query.with(Sort.by(Sort.Direction.DESC, "updatedAt"));

long total = mongoTemplate.count(query, IntegrationMongo.class);

if (pageable != null) {
query.with(PageRequest.of(pageable.pageNumber(), pageable.pageSize()));
}

List<IntegrationMongo> integrations = mongoTemplate.find(query, IntegrationMongo.class);

return new Page<>(integrations, (pageable != null) ? pageable.pageNumber() : 0, integrations.size(), total);
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 漏 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.model;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* @author Remi Baptiste (remi.baptiste at graviteesource.com)
* @author GraviteeSource Team
*/
@Getter
@Setter
@Document(collection = "#{@environment.getProperty('management.mongodb.prefix')}integration")
public class IntegrationMongo extends Auditable {

@Id
private String id;

private String name;

private String description;

private String provider;

private String environmentId;

public IntegrationMongo() {}
}
Expand Up @@ -168,6 +168,11 @@ public interface GraviteeMapper {

InstallationMongo map(Installation toMap);

// Integration mapping
Integration map(IntegrationMongo integrationMongo);

IntegrationMongo map(Integration toMap);

// Invitation mapping
Invitation map(InvitationMongo toMap);

Expand Down Expand Up @@ -297,4 +302,6 @@ public interface GraviteeMapper {
Workflow map(WorkflowMongo toMap);

WorkflowMongo map(Workflow toMap);

List<Integration> mapIntegrationsList(Collection<IntegrationMongo> toMap);
}