use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIUtil method getEnvironmentsOfAPI.
/**
* This method used to get the currently published gateway environments of an API .
*
* @param api API object with the attributes value
*/
public static List<Environment> getEnvironmentsOfAPI(API api) throws APIManagementException {
String organization = api.getOrganization();
Map<String, Environment> gatewayEnvironments = getEnvironments(organization);
Set<String> apiEnvironments = api.getEnvironments();
List<Environment> returnEnvironments = new ArrayList<Environment>();
for (Environment environment : gatewayEnvironments.values()) {
for (String apiEnvironment : apiEnvironments) {
if (environment.getName().equals(apiEnvironment)) {
returnEnvironments.add(environment);
break;
}
}
}
return returnEnvironments;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIUtil method getGatewayEndpoint.
/**
* Read the GateWay Endpoint from the APIConfiguration. If multiple Gateway
* environments defined, get the gateway endpoint according to the environment type
*
* @param transports transports allowed for gateway endpoint
* @param environmentName gateway environment name
* @param environmentType gateway environment type
* @return Gateway URL
*/
public static String getGatewayEndpoint(String transports, String environmentName, String environmentType, String organization) throws APIManagementException {
String gatewayURLs;
String gatewayEndpoint = "";
Map<String, Environment> gatewayEnvironments = getEnvironments(organization);
Environment environment = gatewayEnvironments.get(environmentName);
if (environment.getType().equals(environmentType)) {
gatewayURLs = environment.getApiGatewayEndpoint();
gatewayEndpoint = extractHTTPSEndpoint(gatewayURLs, transports);
if (log.isDebugEnabled()) {
log.debug("Gateway urls are: " + gatewayURLs + " and the url with the correct transport is: " + gatewayEndpoint);
}
} else {
handleException("Environment type mismatch for environment: " + environmentName + " for the environment types: " + environment.getType() + " and " + environmentType);
}
return gatewayEndpoint;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIUtil method getGatewayendpoint.
/**
* Read the GateWay Endpoint from the APIConfiguration. If multiple Gateway
* environments defined,
* take only the production node's Endpoint.
* Else, pick what is available as the gateway node.
*
* @return {@link String} - Gateway URL
*/
public static String getGatewayendpoint(String transports, String organization) throws APIManagementException {
String gatewayURLs;
Map<String, Environment> gatewayEnvironments = getEnvironments(organization);
if (gatewayEnvironments.size() > 1) {
for (Environment environment : gatewayEnvironments.values()) {
if (APIConstants.GATEWAY_ENV_TYPE_HYBRID.equals(environment.getType())) {
// This might have http,https
gatewayURLs = environment.getApiGatewayEndpoint();
// pick correct endpoint
return APIUtil.extractHTTPSEndpoint(gatewayURLs, transports);
}
}
for (Environment environment : gatewayEnvironments.values()) {
if (APIConstants.GATEWAY_ENV_TYPE_PRODUCTION.equals(environment.getType())) {
// This might have http,https
gatewayURLs = environment.getApiGatewayEndpoint();
// pick correct endpoint
return APIUtil.extractHTTPSEndpoint(gatewayURLs, transports);
}
}
for (Environment environment : gatewayEnvironments.values()) {
if (APIConstants.GATEWAY_ENV_TYPE_SANDBOX.equals(environment.getType())) {
// This might have http,https
gatewayURLs = environment.getApiGatewayEndpoint();
// pick correct endpoint
return APIUtil.extractHTTPSEndpoint(gatewayURLs, transports);
}
}
} else {
gatewayURLs = ((Environment) gatewayEnvironments.values().toArray()[0]).getApiGatewayEndpoint();
return extractHTTPSEndpoint(gatewayURLs, transports);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class MicroGatewayArtifactGenerator method generateGatewayArtifact.
@Override
public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList) throws APIManagementException {
try {
DeploymentDescriptorDto descriptorDto = new DeploymentDescriptorDto();
Map<String, ApiProjectDto> deploymentsMap = new HashMap<>();
// "tempDirectory" is the root artifact directory
File tempDirectory = CommonUtil.createTempDirectory(null);
for (APIRuntimeArtifactDto apiRuntimeArtifactDto : apiRuntimeArtifactDtoList) {
if (apiRuntimeArtifactDto.isFile()) {
InputStream artifact = (InputStream) apiRuntimeArtifactDto.getArtifact();
String fileName = apiRuntimeArtifactDto.getApiId().concat("-").concat(apiRuntimeArtifactDto.getRevision()).concat(APIConstants.ZIP_FILE_EXTENSION);
Path path = Paths.get(tempDirectory.getAbsolutePath(), fileName);
FileUtils.copyInputStreamToFile(artifact, path.toFile());
ApiProjectDto apiProjectDto = deploymentsMap.get(fileName);
if (apiProjectDto == null) {
apiProjectDto = new ApiProjectDto();
deploymentsMap.put(fileName, apiProjectDto);
apiProjectDto.setApiFile(fileName);
apiProjectDto.setEnvironments(new HashSet<>());
apiProjectDto.setOrganizationId(apiRuntimeArtifactDto.getOrganization());
}
// environment is unique for a revision in a deployment
// create new environment
EnvironmentDto environment = new EnvironmentDto();
environment.setName(apiRuntimeArtifactDto.getLabel());
environment.setVhost(apiRuntimeArtifactDto.getVhost());
// ignored if the name of the environment is same
apiProjectDto.getEnvironments().add(environment);
}
}
descriptorDto.setDeployments(new HashSet<>(deploymentsMap.values()));
String descriptorFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE).toString();
CommonUtil.writeDtoToFile(descriptorFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE_TYPE, descriptorDto);
// adding env_properties.json
Map<String, Map<String, Environment>> environmentSpecificAPIProperties = getEnvironmentSpecificAPIProperties(apiRuntimeArtifactDtoList);
String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE, APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME, environmentSpecificAPIProperties);
CommonUtil.archiveDirectory(tempDirectory.getAbsolutePath());
FileUtils.deleteQuietly(tempDirectory);
RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
runtimeArtifactDto.setArtifact(new File(tempDirectory.getAbsolutePath() + APIConstants.ZIP_FILE_EXTENSION));
runtimeArtifactDto.setFile(true);
return runtimeArtifactDto;
} catch (APIImportExportException | IOException e) {
throw new APIManagementException("Error while Generating API artifact", e);
}
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class APIUtilTest method testGetTokenEndpointsByType.
@Test
public void testGetTokenEndpointsByType() throws Exception {
System.setProperty("carbon.home", APIUtilTest.class.getResource("/").getFile());
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Environment environment = new Environment();
environment.setType("production");
environment.setName("Production");
environment.setDefault(true);
environment.setApiGatewayEndpoint("http://localhost:8280,https://localhost:8243");
Map<String, Environment> environmentMap = new HashMap<String, Environment>();
environmentMap.put("Production", environment);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getApiGatewayEnvironments()).thenReturn(environmentMap);
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
PowerMockito.mockStatic(ApiMgtDAO.class);
Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
Mockito.when(apiMgtDAO.getAllEnvironments(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(new ArrayList<org.wso2.carbon.apimgt.api.model.Environment>());
String tokenEndpointType = APIUtil.getTokenEndpointsByType("production", "61416403c40f086ad2dc5eef");
Assert.assertEquals("https://localhost:8243", tokenEndpointType);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
Aggregations