Skip to content

Commit

Permalink
feat: add import endpoint for k8s group resource spec
Browse files Browse the repository at this point in the history
  • Loading branch information
a-cordier authored and kamiiiel committed Jan 17, 2025
1 parent f874ae0 commit 271dce5
Show file tree
Hide file tree
Showing 35 changed files with 1,222 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.rest.api.management.v2.rest.mapper;

import io.gravitee.rest.api.management.v2.rest.model.GroupCRDSpec;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* @author Antoine CORDIER (antoine.cordier at graviteesource.com)
* @author GraviteeSource Team
*/
@Mapper
public interface GroupCRDMapper {
GroupCRDMapper INSTANCE = Mappers.getMapper(GroupCRDMapper.class);

io.gravitee.apim.core.group.model.crd.GroupCRDSpec toCore(GroupCRDSpec coreSpec);

GroupCRDSpec toRest(io.gravitee.apim.core.group.model.crd.GroupCRDSpec restSpec);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.rest.api.management.v2.rest.model;

import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author Antoine CORDIER (antoine.cordier at graviteesource.com)
* @author GraviteeSource Team
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class GroupCRDSpec {

private String id;

private String name;

private Set<Member> members;

private boolean notifyMembers;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder(toBuilder = true)
public static class Member {

private String id;

private String source;

private String sourceId;

private Map<RoleScope, String> roles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
import static io.gravitee.rest.api.model.permissions.RolePermissionAction.READ;
import static io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE;

import io.gravitee.apim.core.audit.model.AuditActor;
import io.gravitee.apim.core.audit.model.AuditInfo;
import io.gravitee.apim.core.group.use_case.ImportGroupCRDUseCase;
import io.gravitee.apim.core.group.use_case.ValidateGroupCRDUseCase;
import io.gravitee.rest.api.management.v2.rest.mapper.GroupCRDMapper;
import io.gravitee.rest.api.management.v2.rest.mapper.GroupMapper;
import io.gravitee.rest.api.management.v2.rest.mapper.MemberMapper;
import io.gravitee.rest.api.management.v2.rest.model.GroupCRDSpec;
import io.gravitee.rest.api.management.v2.rest.model.GroupsResponse;
import io.gravitee.rest.api.management.v2.rest.model.Member;
import io.gravitee.rest.api.management.v2.rest.model.MembersResponse;
Expand All @@ -46,6 +52,7 @@
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
Expand All @@ -59,7 +66,13 @@ public class GroupsResource extends AbstractResource {
@Inject
private GroupService groupService;

private final GroupMapper mapper = GroupMapper.INSTANCE;
@Inject
private ValidateGroupCRDUseCase validateGroupCRD;

@Inject
private ImportGroupCRDUseCase importGroupCRD;

private static final GroupMapper MAPPER = GroupMapper.INSTANCE;

@GET
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -70,7 +83,7 @@ public GroupsResponse listGroups(@BeanParam @Valid PaginationParam paginationPar
List<GroupEntity> groupsSubset = computePaginationData(groups, paginationParam);

return new GroupsResponse()
.data(mapper.map(groupsSubset))
.data(MAPPER.map(groupsSubset))
.pagination(PaginationInfo.computePaginationInfo(groups.size(), groupsSubset.size(), paginationParam))
.links(computePaginationLinks(groups.size(), paginationParam));
}
Expand Down Expand Up @@ -133,4 +146,34 @@ public Map<String, char[]> getPermissions(@PathParam("groupId") String groupId)
return Map.of();
}
}

@PUT
@Path("/_import/crd")
@Produces(io.gravitee.common.http.MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_GROUP, acls = RolePermissionAction.CREATE) })
public Response importGroupCRD(@Valid GroupCRDSpec spec, @QueryParam("dryRun") boolean dryRun) {
var executionContext = GraviteeContext.getExecutionContext();
var userDetails = getAuthenticatedUserDetails();

var input = new ImportGroupCRDUseCase.Input(
AuditInfo
.builder()
.organizationId(executionContext.getOrganizationId())
.environmentId(executionContext.getEnvironmentId())
.actor(
AuditActor
.builder()
.userId(userDetails.getUsername())
.userSource(userDetails.getSource())
.userSourceId(userDetails.getSourceId())
.build()
)
.build(),
GroupCRDMapper.INSTANCE.toCore(spec)
);

var output = dryRun ? validateGroupCRD.execute(input) : importGroupCRD.execute(input);

return Response.ok(output.status()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import inmemory.ApplicationMetadataCrudServiceInMemory;
import inmemory.ApplicationMetadataQueryServiceInMemory;
import inmemory.CategoryQueryServiceInMemory;
import inmemory.GroupCrudServiceInMemory;
import inmemory.GroupQueryServiceInMemory;
import inmemory.ImportApplicationCRDDomainServiceInMemory;
import inmemory.InMemoryAlternative;
Expand All @@ -32,6 +33,7 @@
import inmemory.PrimaryOwnerDomainServiceInMemory;
import inmemory.RoleQueryServiceInMemory;
import inmemory.UserCrudServiceInMemory;
import inmemory.UserDomainServiceInMemory;
import io.gravitee.apim.core.api.domain_service.ApiExportDomainService;
import io.gravitee.apim.core.api.domain_service.CategoryDomainService;
import io.gravitee.apim.core.api.domain_service.VerifyApiPathDomainService;
Expand Down Expand Up @@ -180,6 +182,9 @@ public abstract class AbstractResourceTest extends JerseySpringTest {
@Autowired
protected UserCrudServiceInMemory userCrudService;

@Autowired
protected UserDomainServiceInMemory userDomainServiceInMemory;

@Autowired
protected ApiCrudServiceInMemory apiCrudService;

Expand Down Expand Up @@ -216,6 +221,9 @@ public abstract class AbstractResourceTest extends JerseySpringTest {
@Autowired
protected ApiExportDomainService apiExportDomainService;

@Autowired
protected GroupCrudServiceInMemory groupCrudServiceInMemory;

@BeforeEach
public void setUp() {
when(permissionService.hasPermission(any(), any(), any(), any())).thenReturn(true);
Expand Down
Loading

0 comments on commit 271dce5

Please sign in to comment.