-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle subscriptions and apikeys in local sync
- Loading branch information
Showing
6 changed files
with
352 additions
and
122 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...nc/src/main/java/io/gravitee/gateway/services/sync/process/local/mapper/ApiKeyMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.gateway.services.sync.process.local.mapper; | ||
|
||
import io.gravitee.gateway.api.service.ApiKey; | ||
import io.gravitee.gateway.api.service.Subscription; | ||
import java.util.Set; | ||
|
||
public class ApiKeyMapper { | ||
|
||
public ApiKey to(io.gravitee.repository.management.model.ApiKey apiKeyModel, Set<Subscription> subscriptions) { | ||
ApiKey.ApiKeyBuilder apiKeyBuilder = ApiKey | ||
.builder() | ||
.id(apiKeyModel.getId()) | ||
.key(apiKeyModel.getKey()) | ||
.application(apiKeyModel.getApplication()) | ||
.expireAt(apiKeyModel.getExpireAt()) | ||
.revoked(apiKeyModel.isRevoked()) | ||
.paused(apiKeyModel.isPaused()) | ||
.active(false); | ||
|
||
if (subscriptions != null) { | ||
subscriptions | ||
.stream() | ||
.filter(subscription -> apiKeyModel.getSubscriptions().contains(subscription.getId())) | ||
.findFirst() | ||
.ifPresent(apiKeySubscription -> | ||
apiKeyBuilder | ||
.api(apiKeySubscription.getApi()) | ||
.plan(apiKeySubscription.getPlan()) | ||
.subscription(apiKeySubscription.getId()) | ||
.active( | ||
!apiKeyModel.isPaused() && | ||
!apiKeyModel.isRevoked() && | ||
io.gravitee.repository.management.model.Subscription.Status.ACCEPTED | ||
.name() | ||
.equals(apiKeySubscription.getStatus()) | ||
) | ||
); | ||
} | ||
return apiKeyBuilder.build(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...-sync/src/main/java/io/gravitee/gateway/services/sync/process/local/mapper/ApiMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.gateway.services.sync.process.local.mapper; | ||
|
||
import static io.gravitee.repository.management.model.Event.EventProperties.API_ID; | ||
import static io.gravitee.repository.management.model.Event.EventProperties.DEPLOYMENT_NUMBER; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.gravitee.definition.model.DefinitionVersion; | ||
import io.gravitee.definition.model.v4.ApiType; | ||
import io.gravitee.definition.model.v4.nativeapi.NativeApi; | ||
import io.gravitee.gateway.reactor.ReactableApi; | ||
import io.gravitee.gateway.services.sync.process.repository.service.EnvironmentService; | ||
import io.gravitee.repository.management.model.Event; | ||
import io.gravitee.repository.management.model.LifecycleState; | ||
import io.reactivex.rxjava3.core.Maybe; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ApiMapper { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final EnvironmentService environmentService; | ||
|
||
public ReactableApi<?> to(Event apiEvent) { | ||
try { | ||
// Read API definition from event | ||
var api = objectMapper.readValue(apiEvent.getPayload(), io.gravitee.repository.management.model.Api.class); | ||
|
||
ReactableApi<?> reactableApi; | ||
|
||
// Check the version of the API definition to read the right model entity | ||
if (DefinitionVersion.V4 != api.getDefinitionVersion()) { | ||
var eventApiDefinition = objectMapper.readValue(api.getDefinition(), io.gravitee.definition.model.Api.class); | ||
|
||
// Update definition with required information for deployment phase | ||
reactableApi = new io.gravitee.gateway.handlers.api.definition.Api(eventApiDefinition); | ||
} else { | ||
if (api.getType() == ApiType.NATIVE) { | ||
var eventApiDefinition = objectMapper.readValue(api.getDefinition(), NativeApi.class); | ||
|
||
// Update definition with required information for deployment phase | ||
reactableApi = new io.gravitee.gateway.reactive.handlers.api.v4.NativeApi(eventApiDefinition); | ||
} else if (api.getType() == ApiType.PROXY || api.getType() == ApiType.MESSAGE) { | ||
var eventApiDefinition = objectMapper.readValue(api.getDefinition(), io.gravitee.definition.model.v4.Api.class); | ||
|
||
// Update definition with required information for deployment phase | ||
reactableApi = new io.gravitee.gateway.reactive.handlers.api.v4.Api(eventApiDefinition); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported ApiType [" + api.getType() + "] for api: " + api.getId()); | ||
} | ||
} | ||
|
||
reactableApi.setEnabled(api.getLifecycleState() == LifecycleState.STARTED); | ||
reactableApi.setDeployedAt(apiEvent.getCreatedAt()); | ||
reactableApi.setRevision( | ||
Optional.ofNullable(apiEvent.getProperties()).map(props -> props.get(DEPLOYMENT_NUMBER.getValue())).orElse(null) | ||
); | ||
|
||
environmentService.fill(api.getEnvironmentId(), reactableApi); | ||
|
||
return reactableApi; | ||
} catch (Exception e) { | ||
// Log the error and ignore this event. | ||
log.error("Unable to extract api definition from event [{}].", apiEvent.getId(), e); | ||
return null; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
.../main/java/io/gravitee/gateway/services/sync/process/local/mapper/SubscriptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.gateway.services.sync.process.local.mapper; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.gravitee.gateway.api.service.Subscription; | ||
import io.gravitee.gateway.api.service.SubscriptionConfiguration; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SubscriptionMapper { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public Subscription to(io.gravitee.repository.management.model.Subscription subscriptionModel) { | ||
try { | ||
Subscription subscription = new Subscription(); | ||
subscription.setApi(subscriptionModel.getApi()); | ||
subscription.setApplication(subscriptionModel.getApplication()); | ||
subscription.setClientId(subscriptionModel.getClientId()); | ||
subscription.setClientCertificate(subscriptionModel.getClientCertificate()); | ||
subscription.setStartingAt(subscriptionModel.getStartingAt()); | ||
subscription.setEndingAt(subscriptionModel.getEndingAt()); | ||
subscription.setId(subscriptionModel.getId()); | ||
subscription.setPlan(subscriptionModel.getPlan()); | ||
if (subscriptionModel.getStatus() != null) { | ||
subscription.setStatus(subscriptionModel.getStatus().name()); | ||
} | ||
if (subscriptionModel.getConsumerStatus() != null) { | ||
subscription.setConsumerStatus(Subscription.ConsumerStatus.valueOf(subscriptionModel.getConsumerStatus().name())); | ||
} | ||
if (subscriptionModel.getType() != null) { | ||
subscription.setType(Subscription.Type.valueOf(subscriptionModel.getType().name().toUpperCase())); | ||
} | ||
if (subscriptionModel.getConfiguration() != null) { | ||
subscription.setConfiguration( | ||
objectMapper.readValue(subscriptionModel.getConfiguration(), SubscriptionConfiguration.class) | ||
); | ||
} | ||
subscription.setMetadata(subscriptionModel.getMetadata()); | ||
subscription.setEnvironmentId(subscriptionModel.getEnvironmentId()); | ||
return subscription; | ||
} catch (Exception e) { | ||
log.error("Unable to map subscription from model [{}].", subscriptionModel.getId(), e); | ||
return null; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...n/java/io/gravitee/gateway/services/sync/process/local/model/LocalSyncFileDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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.gateway.services.sync.process.local.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.gravitee.repository.management.model.ApiKey; | ||
import io.gravitee.repository.management.model.Event; | ||
import io.gravitee.repository.management.model.Subscription; | ||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class LocalSyncFileDefinition { | ||
|
||
@JsonProperty("apiEvent") | ||
Event repositoryApiEvent; | ||
|
||
@JsonProperty("subscriptions") | ||
List<Subscription> repositorySubscriptionList; | ||
|
||
@JsonProperty("apiKeys") | ||
List<ApiKey> repositoryApiKeyList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.