use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method setApiResourceBuilderProperties.
/**
* Extract properties in Operation entry and assign them to api resource builder properties.
*
* @param operationEntry Map entry to be extracted properties
* @param uriTemplateBuilder Uri template builder to assign related properties
* @param resourcePath resource path
* @return APIResource.Builder object
*/
private APIResource.Builder setApiResourceBuilderProperties(Map.Entry<HttpMethod, Operation> operationEntry, UriTemplate.UriTemplateBuilder uriTemplateBuilder, String resourcePath) {
Operation operation = operationEntry.getValue();
APIResource.Builder apiResourceBuilder = new APIResource.Builder();
List<String> producesList = operation.getProduces();
if (producesList != null) {
String produceSeparatedString = "\"";
produceSeparatedString += String.join("\",\"", producesList) + "\"";
apiResourceBuilder.produces(produceSeparatedString);
}
List<String> consumesList = operation.getConsumes();
if (consumesList != null) {
String consumesSeparatedString = "\"";
consumesSeparatedString += String.join("\",\"", consumesList) + "\"";
apiResourceBuilder.consumes(consumesSeparatedString);
}
if (operation.getOperationId() != null) {
uriTemplateBuilder.templateId(operation.getOperationId());
} else {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(resourcePath, operationEntry.getKey().name()));
}
uriTemplateBuilder.httpVerb(operationEntry.getKey().name());
apiResourceBuilder.uriTemplate(uriTemplateBuilder.build());
return apiResourceBuilder;
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class APIExecutor method execute.
/**
* This method will be called when the invoke() method of the default lifecycle implementation is called.
* Execution logic should reside in this method since the default lifecycle implementation will determine
* the execution output by looking at the output of this method.
*
* @param resource The resource in which the lifecycle state is changed.
* @param currentState Current lifecycle state.
* @param targetState The target lifecycle state.
* @throws LifecycleException If exception occurs while running the executor.
*/
@Override
public void execute(Object resource, String currentState, String targetState) throws LifecycleException {
API api = (API) resource;
if (!currentState.equals(targetState)) {
// todo:This place need to write how to handle Gateway publishing
try {
ApiDAO apiDAO = DAOFactory.getApiDAO();
apiDAO.changeLifeCycleStatus(api.getId(), targetState);
} catch (APIMgtDAOException e) {
throw new LifecycleException("Couldn't create APIPublisher from user", e);
}
}
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class KubernetesGatewayImpl method createContainerGateway.
/**
* @see ContainerBasedGatewayGenerator#createContainerGateway(String, API)
*/
@Override
public void createContainerGateway(String label, API api) throws ContainerBasedGatewayException {
Map<String, String> templateValues = new HashMap<>();
String serviceName = label + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
String deploymentName = label + ContainerBasedGatewayConstants.CMS_DEPLOYMENT_SUFFIX;
String ingressName = label + ContainerBasedGatewayConstants.CMS_INGRESS_SUFFIX;
templateValues.put(ContainerBasedGatewayConstants.NAMESPACE, namespace);
templateValues.put(ContainerBasedGatewayConstants.GATEWAY_LABEL, label);
templateValues.put(ContainerBasedGatewayConstants.SERVICE_NAME, serviceName);
templateValues.put(ContainerBasedGatewayConstants.DEPLOYMENT_NAME, deploymentName);
templateValues.put(ContainerBasedGatewayConstants.INGRESS_NAME, ingressName);
templateValues.put(ContainerBasedGatewayConstants.CONTAINER_NAME, label + ContainerBasedGatewayConstants.CMS_CONTAINER_SUFFIX);
templateValues.put(ContainerBasedGatewayConstants.API_CORE_URL, apiCoreUrl);
templateValues.put(ContainerBasedGatewayConstants.BROKER_HOST, brokerHost);
templateValues.put(ContainerBasedGatewayConstants.GATEWAY_HOSTNAME, generateSubDomain(api) + "." + gatewayHostname);
ContainerBasedGatewayTemplateBuilder builder = new ContainerBasedGatewayTemplateBuilder();
// Create gateway service resource
createServiceResource(builder.generateTemplate(templateValues, ContainerBasedGatewayConstants.GATEWAY_SERVICE_TEMPLATE), serviceName);
// Create gateway deployment resource
createDeploymentResource(builder.generateTemplate(templateValues, ContainerBasedGatewayConstants.GATEWAY_DEPLOYMENT_TEMPLATE), deploymentName);
// Create gateway ingress resource
createIngressResource(builder.generateTemplate(templateValues, ContainerBasedGatewayConstants.GATEWAY_INGRESS_TEMPLATE), ingressName);
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class KubernetesGatewayImpl method createDeploymentResource.
/**
* Create a deployment in cms
*
* @param deploymentTemplate Deployment template as a String
* @param deploymentName Name of the deployment
* @throws ContainerBasedGatewayException if failed to create a deployment
*/
private void createDeploymentResource(String deploymentTemplate, String deploymentName) throws ContainerBasedGatewayException {
HasMetadata resource = getResourcesFromTemplate(deploymentTemplate);
try {
if (resource instanceof Deployment) {
// check whether there are existing service already
if (client.extensions().deployments().inNamespace(namespace).withName(deploymentName).get() == null) {
log.debug("Deploying in CMS type: {} and the Deployment resource definition: {} ", cmsType, deploymentTemplate);
Deployment deployment = (Deployment) resource;
Deployment result = client.extensions().deployments().inNamespace(namespace).create(deployment);
log.info("Created Deployment : " + result.getMetadata().getName() + " in Namespace : " + result.getMetadata().getNamespace() + " in " + cmsType);
} else {
log.info("There exist a deployment with the same name in " + cmsType + ". Deployment name : " + deploymentName);
}
} else {
throw new ContainerBasedGatewayException("Loaded Resource is not a Deployment in " + cmsType + "! " + resource, ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
}
} catch (KubernetesClientException e) {
throw new ContainerBasedGatewayException("Error while creating container based gateway deployment in " + cmsType + "!", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
}
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class KubernetesGatewayImpl method createIngressResource.
/**
* Create an Ingress resource in cms
*
* @param ingressTemplate Ingress template as a String
* @param ingressName Name of the ingress
* @throws ContainerBasedGatewayException if failed to create a service
*/
private void createIngressResource(String ingressTemplate, String ingressName) throws ContainerBasedGatewayException {
HasMetadata resource = getResourcesFromTemplate(ingressTemplate);
try {
if (resource instanceof Ingress) {
// check whether there are existing service already
if (client.extensions().ingresses().inNamespace(namespace).withName(ingressName).get() == null) {
log.debug("Deploying in CMS type: {} and the Ingress resource definition: {} ", cmsType, ingressTemplate);
Ingress ingress = (Ingress) resource;
Ingress result = client.extensions().ingresses().inNamespace(namespace).create(ingress);
log.info("Created Ingress : " + result.getMetadata().getName() + " in Namespace : " + result.getMetadata().getNamespace() + " in " + cmsType);
} else {
log.info("There exist an ingress with the same name in " + cmsType + ". Ingress name : " + ingressName);
}
} else {
throw new ContainerBasedGatewayException("Loaded Resource is not a Service in " + cmsType + "! " + resource, ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
}
} catch (KubernetesClientException e) {
throw new ContainerBasedGatewayException("Error while creating container based gateway ingress in " + cmsType + "!", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
}
}
Aggregations