use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIAdminImpl method deleteEnvironment.
@Override
public void deleteEnvironment(String tenantDomain, String uuid) throws APIManagementException {
// check if the VHost exists in the tenant domain with given UUID, throw error if not found
Environment existingEnv = getEnvironment(tenantDomain, uuid);
if (existingEnv.isReadOnly()) {
String errorMessage = String.format("Failed to delete Environment with UUID '%s'. Environment is read only", uuid);
throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT, String.format("UUID '%s'", uuid)));
}
apiMgtDAO.deleteEnvironment(uuid);
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ExternallyDeployedApiNotifier method undeployApiWhenRetiring.
/**
* Undeploy APIs from external gateway when life cycle state changed to retire
*
* @param apiEvent APIEvent to undeploy APIs from external gateway
* @throws NotifierException if error occurs when undeploying APIs from external gateway
*/
private void undeployApiWhenRetiring(APIEvent apiEvent) throws NotifierException {
apiMgtDAO = ApiMgtDAO.getInstance();
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
boolean deleted;
String apiId = apiEvent.getUuid();
if (!APIConstants.RETIRED.equals(apiEvent.getApiStatus())) {
return;
}
try {
APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
List<APIRevisionDeployment> test = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiId);
for (APIRevisionDeployment deployment : test) {
String deploymentEnv = deployment.getDeployment();
if (gatewayEnvironments.containsKey(deploymentEnv)) {
ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(gatewayEnvironments.get(deploymentEnv).getProvider());
if (deployer != null) {
try {
deleted = deployer.undeployWhenRetire(api, gatewayEnvironments.get(deploymentEnv));
if (!deleted) {
throw new NotifierException("Error while deleting API product from Solace broker");
}
} catch (DeployerException e) {
throw new NotifierException(e.getMessage());
}
}
}
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ExternalGatewayNotifier method unDeployApi.
/**
* Undeploy APIs from external gateway
*
* @param deployAPIInGatewayEvent DeployAPIInGatewayEvent to undeploy APIs from external gateway
* @throws NotifierException if error occurs when undeploying APIs from external gateway
*/
private void unDeployApi(DeployAPIInGatewayEvent deployAPIInGatewayEvent) throws NotifierException {
boolean deleted;
Set<String> gateways = deployAPIInGatewayEvent.getGatewayLabels();
String apiId = deployAPIInGatewayEvent.getUuid();
try {
Map<String, Environment> environments = APIUtil.getEnvironments(deployAPIInGatewayEvent.getTenantDomain());
APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(deployAPIInGatewayEvent.getProvider());
API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
for (String deploymentEnv : gateways) {
if (environments.containsKey(deploymentEnv)) {
ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(environments.get(deploymentEnv).getProvider());
if (deployer != null) {
try {
deleted = deployer.undeploy(api.getId().getName(), api.getId().getVersion(), api.getContext(), environments.get(deploymentEnv));
if (!deleted) {
throw new NotifierException("Error while deleting API product from Solace broker");
}
} catch (DeployerException e) {
throw new NotifierException(e.getMessage());
}
}
}
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdEnvironmentsEnvIdKeysGet.
@Override
public Response apisApiIdEnvironmentsEnvIdKeysGet(String apiId, String envId, MessageContext messageContext) throws APIManagementException {
// validate api UUID
validateAPIExistence(apiId);
// validate environment UUID
validateEnvironment(envId);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// get properties
EnvironmentPropertiesDTO properties = apiProvider.getEnvironmentSpecificAPIProperties(apiId, envId);
// convert to string to remove null values
String jsonContent = new Gson().toJson(properties);
return Response.ok().entity(jsonContent).build();
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ImportUtils method getValidatedDeploymentsList.
/**
* This method is used to validate the Gateway environments from the deployment environments file. Gateway
* environments will be validated with a set of all the labels and environments of the tenant domain. If
* environment is not found in this set, it will be skipped with an error message in the console. This method is
* common to both APIs and API Products
*
* @param deploymentInfoArray Deployment environment array found in the import artifact
* @param tenantDomain Tenant domain
* @param apiProvider Provider of the API/ API Product
* @return a list of API/API Product revision deployments ready to be deployed.
* @throws APIManagementException If an error occurs when validating the deployments list
*/
private static List<APIRevisionDeployment> getValidatedDeploymentsList(JsonArray deploymentInfoArray, String tenantDomain, APIProvider apiProvider, String organization) throws APIManagementException {
List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
if (deploymentInfoArray != null && deploymentInfoArray.size() > 0) {
Map<String, Environment> gatewayEnvironments = APIUtil.getEnvironments(organization);
for (int i = 0; i < deploymentInfoArray.size(); i++) {
JsonObject deploymentJson = deploymentInfoArray.get(i).getAsJsonObject();
JsonElement deploymentNameElement = deploymentJson.get(ImportExportConstants.DEPLOYMENT_NAME);
if (deploymentNameElement != null) {
String deploymentName = deploymentNameElement.getAsString();
Environment gatewayEnvironment = gatewayEnvironments.get(deploymentName);
if (gatewayEnvironment != null) {
JsonElement deploymentVhostElement = deploymentJson.get(ImportExportConstants.DEPLOYMENT_VHOST);
String deploymentVhost;
if (deploymentVhostElement != null) {
deploymentVhost = deploymentVhostElement.getAsString();
} else {
// set the default vhost of the given environment
if (gatewayEnvironment.getVhosts().isEmpty()) {
throw new APIManagementException("No VHosts defined for the environment: " + deploymentName);
}
deploymentVhost = gatewayEnvironment.getVhosts().get(0).getHost();
}
// resolve vhost to null if it is the default vhost of read only environment
deploymentVhost = VHostUtils.resolveIfDefaultVhostToNull(deploymentName, deploymentVhost);
JsonElement displayOnDevportalElement = deploymentJson.get(ImportExportConstants.DISPLAY_ON_DEVPORTAL_OPTION);
boolean displayOnDevportal = displayOnDevportalElement == null || displayOnDevportalElement.getAsBoolean();
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setDeployment(deploymentName);
apiRevisionDeployment.setVhost(deploymentVhost);
apiRevisionDeployment.setDisplayOnDevportal(displayOnDevportal);
apiRevisionDeployments.add(apiRevisionDeployment);
} else {
throw new APIManagementException("Label " + deploymentName + " is not a defined gateway environment. Hence " + "skipped without deployment", ExceptionCodes.from(ExceptionCodes.GATEWAY_ENVIRONMENT_NOT_FOUND, String.format("label '%s'", deploymentName)));
}
}
}
}
return apiRevisionDeployments;
}
Aggregations