Skip to content

Commit

Permalink
feat: add support for GKO NativeAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiiiel committed Nov 26, 2024
1 parent e30967d commit 050a54a
Show file tree
Hide file tree
Showing 35 changed files with 1,455 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,23 @@
*/
package io.gravitee.definition.model.v4;

import static io.gravitee.definition.model.v4.AbstractApi.*;
import static io.gravitee.definition.model.v4.listener.Listener.HTTP_LABEL;
import static io.gravitee.definition.model.v4.listener.Listener.SUBSCRIPTION_LABEL;
import static io.gravitee.definition.model.v4.listener.Listener.TCP_LABEL;
import static io.gravitee.definition.model.v4.AbstractApi.MESSAGE_LABEL;
import static io.gravitee.definition.model.v4.AbstractApi.NATIVE_LABEL;
import static io.gravitee.definition.model.v4.AbstractApi.PROXY_LABEL;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.gravitee.definition.model.DefinitionVersion;
import io.gravitee.definition.model.Plugin;
import io.gravitee.definition.model.ResponseTemplate;
import io.gravitee.definition.model.v4.analytics.Analytics;
import io.gravitee.definition.model.v4.endpointgroup.EndpointGroup;
import io.gravitee.definition.model.v4.failover.Failover;
import io.gravitee.definition.model.v4.flow.Flow;
import io.gravitee.definition.model.v4.flow.execution.FlowExecution;
import io.gravitee.definition.model.v4.listener.Listener;
import io.gravitee.definition.model.v4.listener.http.HttpListener;
import io.gravitee.definition.model.v4.listener.subscription.SubscriptionListener;
import io.gravitee.definition.model.v4.listener.tcp.TcpListener;
import io.gravitee.definition.model.v4.nativeapi.NativeApi;
import io.gravitee.definition.model.v4.plan.AbstractPlan;
import io.gravitee.definition.model.v4.plan.Plan;
import io.gravitee.definition.model.v4.property.Property;
import io.gravitee.definition.model.v4.resource.Resource;
import io.gravitee.definition.model.v4.service.ApiServices;
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public enum PlanType {
* A plan for a bunch of APIs.
*/
CATALOG,

/**
* A plan for native APIs.
*/
NATIVE,
}

public enum PlanMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.gravitee.apim.core.api.model.NewHttpApi;
import io.gravitee.apim.core.api.model.NewNativeApi;
import io.gravitee.apim.core.api.model.crd.ApiCRDSpec;
import io.gravitee.apim.core.api.model.crd.NativeApiCRDSpec;
import io.gravitee.apim.core.api.model.import_definition.ApiExport;
import io.gravitee.apim.core.documentation.model.Page;
import io.gravitee.definition.model.v4.ApiType;
Expand Down Expand Up @@ -231,6 +232,10 @@ io.gravitee.rest.api.management.v2.rest.model.ApiV1 mapToV1(
@Mapping(target = "plans", qualifiedByName = "mapPlanCRD")
ApiCRDSpec map(io.gravitee.rest.api.management.v2.rest.model.ApiCRDSpec crd);

@Mapping(target = "listeners", qualifiedByName = "toNativeListeners")
@Mapping(target = "metadata", ignore = true)
NativeApiCRDSpec map(io.gravitee.rest.api.management.v2.rest.model.NativeApiCRDSpec crd);

@Mapping(target = "source.configuration", qualifiedByName = "serializeConfiguration")
@Mapping(target = "source.configurationMap", source = "source.configuration", qualifiedByName = "convertToMapConfiguration")
io.gravitee.apim.core.api.model.crd.PageCRD map(PageCRD crd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.gravitee.rest.api.management.v2.rest.model.CreatePlanV2;
import io.gravitee.rest.api.management.v2.rest.model.CreatePlanV4;
import io.gravitee.rest.api.management.v2.rest.model.FlowV4;
import io.gravitee.rest.api.management.v2.rest.model.NativePlanCRD;
import io.gravitee.rest.api.management.v2.rest.model.Plan;
import io.gravitee.rest.api.management.v2.rest.model.PlanCRD;
import io.gravitee.rest.api.management.v2.rest.model.PlanFederated;
Expand Down Expand Up @@ -154,6 +155,9 @@ default io.gravitee.apim.core.plan.model.Plan map(CreatePlanV4 source, Api api)
@Mapping(target = "security", qualifiedByName = "mapToPlanSecurityV4")
io.gravitee.apim.core.api.model.crd.PlanCRD fromPlanCRD(PlanCRD plan);

@Mapping(target = "security", qualifiedByName = "mapToPlanSecurityV4")
io.gravitee.apim.core.api.model.crd.NativePlanCRD fromPlanCRD(NativePlanCRD plan);

@Mapping(target = "security.configuration", qualifiedByName = "serializeConfiguration")
UpdatePlanEntity map(UpdatePlanV4 plan);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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 io.gravitee.definition.model.DefinitionContext;
import io.gravitee.definition.model.DefinitionVersion;
import io.gravitee.definition.model.ResponseTemplate;
import io.gravitee.definition.model.v4.flow.execution.FlowExecution;
import io.gravitee.definition.model.v4.service.ApiServices;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.*;
import lombok.Data;

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

@NotNull
@NotEmpty
private String id;

@NotNull
@NotEmpty
private String crossId;

@NotNull
@Valid
private DefinitionContext definitionContext;

@NotEmpty
private String name;

@NotEmpty
private String version;

@NotNull
private ApiType type;

private String description;

private Set<String> tags = new HashSet<>();

private List<@Valid Listener> listeners;

@NotNull
@Size(min = 1)
private List<@Valid EndpointGroupV4> endpointGroups;

private Analytics analytics;

private List<@Valid Property> properties = new ArrayList<>();

private List<@Valid Resource> resources = new ArrayList<>();

private Map<String, @Valid NativePlanCRD> plans = new HashMap<>();

private FlowExecution flowExecution;

private List<FlowV4> flows;

private Map<String, Map<String, ResponseTemplate>> responseTemplates = new LinkedHashMap<>();

private ApiServices services;

private Set<String> groups;

private Visibility visibility;

private GenericApi.StateEnum state;

private PrimaryOwner primaryOwner;

private List<String> labels = new ArrayList<>();

private List<Metadata> metadata = new ArrayList<>();

private ApiLifecycleState lifecycleState;

private Set<String> categories;

private Set<MemberCRD> members;

private boolean notifyMembers;

private Map<String, PageCRD> pages;

public DefinitionVersion getDefinitionVersion() {
return DefinitionVersion.V4;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 io.gravitee.definition.model.DefinitionVersion;
import io.gravitee.definition.model.v4.nativeapi.NativeFlow;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class NativePlanCRD {

@NotNull
@NotEmpty
private String id;

private String crossId;

private String name;

private String description;

private DefinitionVersion definitionVersion;

private PlanSecurity security;

private List<String> characteristics;

private List<String> excludedGroups;

private String generalConditions;

private int order;

private String selectionRule;

@NotNull
private PlanStatus status;

private List<String> tags;

@NotNull
private PlanType type;

private PlanValidation validation;

private List<NativeFlow> flows;

@NotNull
private PlanMode mode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import io.gravitee.apim.core.api.use_case.CreateNativeApiUseCase;
import io.gravitee.apim.core.api.use_case.ImportApiCRDUseCase;
import io.gravitee.apim.core.api.use_case.ImportApiDefinitionUseCase;
import io.gravitee.apim.core.api.use_case.ImportNativeApiCRDUseCase;
import io.gravitee.apim.core.api.use_case.OAIToImportApiUseCase;
import io.gravitee.apim.core.api.use_case.ValidateApiCRDUseCase;
import io.gravitee.apim.core.api.use_case.ValidateNativeApiCRDUseCase;
import io.gravitee.apim.core.api.use_case.VerifyApiHostsUseCase;
import io.gravitee.apim.core.api.use_case.VerifyApiPathsUseCase;
import io.gravitee.apim.core.audit.model.AuditActor;
Expand All @@ -44,6 +46,7 @@
import io.gravitee.rest.api.management.v2.rest.model.CreateApiV4;
import io.gravitee.rest.api.management.v2.rest.model.ExportApiV4;
import io.gravitee.rest.api.management.v2.rest.model.ImportSwaggerDescriptor;
import io.gravitee.rest.api.management.v2.rest.model.NativeApiCRDSpec;
import io.gravitee.rest.api.management.v2.rest.model.VerifyApiHosts;
import io.gravitee.rest.api.management.v2.rest.model.VerifyApiHostsResponse;
import io.gravitee.rest.api.management.v2.rest.model.VerifyApiPaths;
Expand Down Expand Up @@ -128,9 +131,15 @@ public ApiResource getApiResource() {
@Inject
private ImportApiCRDUseCase importCRDUseCase;

@Inject
private ImportNativeApiCRDUseCase importNativeCRDUseCase;

@Inject
private ValidateApiCRDUseCase validateApiCRDUseCase;

@Inject
private ValidateNativeApiCRDUseCase validateNativeApiCRDUseCase;

@Inject
private ImportApiDefinitionUseCase importApiDefinitionUseCase;

Expand Down Expand Up @@ -232,6 +241,36 @@ public Response createApiWithCRD(@Valid ApiCRDSpec crd, @QueryParam("dryRun") bo
: Response.ok(importCRDUseCase.execute(input).status()).build();
}

@PUT
@Path("/_import/native-crd")
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_API, acls = RolePermissionAction.CREATE) })
public Response createNativeApiWithCRD(@Valid NativeApiCRDSpec crd, @QueryParam("dryRun") boolean dryRun) {
var executionContext = GraviteeContext.getExecutionContext();
var userDetails = getAuthenticatedUserDetails();

var input = new ImportNativeApiCRDUseCase.Input(
AuditInfo
.builder()
.organizationId(executionContext.getOrganizationId())
.environmentId(executionContext.getEnvironmentId())
.actor(
AuditActor
.builder()
.userId(userDetails.getUsername())
.userSource(userDetails.getSource())
.userSourceId(userDetails.getSourceId())
.build()
)
.build(),
ApiMapper.INSTANCE.map(crd)
);

return dryRun
? Response.ok(validateNativeApiCRDUseCase.execute(input).status()).build()
: Response.ok(importNativeCRDUseCase.execute(input).status()).build();
}

@POST
@Path("/_import/swagger")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4498,6 +4498,7 @@ components:
enum:
- API
- CATALOG
- NATIVE
PlanValidation:
type: string
description: Plan validation type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public enum PlanType {
* A plan for a bunch of APIs.
*/
CATALOG,

/**
* A plan for a bunch of APIs.
*/
NATIVE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.gravitee.rest.api.model.v4.nativeapi;

import io.gravitee.definition.model.v4.flow.Flow;
import io.gravitee.definition.model.v4.nativeapi.NativeFlow;
import io.gravitee.rest.api.model.DeploymentRequired;
import io.gravitee.rest.api.model.v4.plan.BasePlanEntity;
Expand Down
Loading

0 comments on commit 050a54a

Please sign in to comment.