All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.pivotal.services.plugin.CfPropertiesMapper Maven / Gradle / Ivy

There is a newer version: 2.3.0-rc.6
Show newest version
/*
 * Copyright 2013-2016 the original author or authors.
 *
 * 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.pivotal.services.plugin;

import org.gradle.api.Project;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Responsible for mapping plugin extension provided via a block
 * which looks like this:
 * 
 *     cfConfig {
 *       ccHost = "api.local.pcfdev.io"
 *       ccUser = "admin"
 *       ccPassword = "admin"
 *       org = "pcfdev-org"
 *       space = "pcfdev-space"
 *       ....
 *       }
 * 
* OR overridden via command line flags say: *
 *    ./gradlew cf-push -Pcf.name=newname
 * 
*

* and providing a structure with the final values * * @author Biju Kunjummen */ public class CfPropertiesMapper { private final Project project; private final Map systemEnv; public CfPropertiesMapper(Project project) { this.project = project; this.systemEnv = System.getenv(); } //For Tests CfPropertiesMapper(Project project, Map systemEnv) { this.project = project; this.systemEnv = systemEnv; } public CfProperties getProperties() { return ImmutableCfProperties.builder() .name(getCfApplicationName()) .ccHost(getCcHost()) .ccUser(getCcUser()) .ccPassword(getCcPassword()) .ccToken(getCcToken()) .org(getOrg()) .space(getSpace()) .filePath(getFilePath()) .host(getAppHostName()) .domain(getAppDomain()) .path(getCfPath()) .state(this.getExtension().getState()) .buildpack(this.getBuildpack()) .command(this.getCommand()) .console(this.getExtension().getConsole()) .debug(this.getExtension().getDebug()) .detectedStartCommand(this.getExtension().getDetectedStartCommand()) .diskQuota(this.getDiskQuota()) .enableSsh(this.getExtension().getEnableSsh()) .environment(getEnvironment()) .timeout(this.getTimeout()) .healthCheckType(this.getExtension().getHealthCheckType()) .instances(this.getInstances()) .memory(this.getMemory()) .ports(this.getExtension().getPorts()) .services(getServices()) .stagingTimeout(this.getStagingTimeout()) .startupTimeout(this.getStartupTimeout()) .cfServices(this.getCfServices()) .cfUserProvidedServices(this.getCfUserProvidedServices()) .cfProxySettings(this.getCfProxySettings()) .build(); } private List getCfServices() { return mapCfServices(this.getExtension().getCfServices()); } List mapCfServices(List cfServices) { List serviceDetails = new ArrayList<>(); if (cfServices != null) { for (CfService cfService : cfServices) { CfServiceDetail cfServiceDetail = ImmutableCfServiceDetail.builder() .instanceName(cfService.getInstanceName()) .name(cfService.getName()) .plan(cfService.getPlan()) .tags(cfService.getTags()) .completionTimeout((cfService.getCompletionTimeout() != null) ? cfService.getCompletionTimeout() : DefaultProperties.SERVICE_CREATION_COMPLETION_TIMEOUT) .build(); serviceDetails.add(cfServiceDetail); } } return serviceDetails; } private List getCfUserProvidedServices() { return mapCfUserProvidedServices(this.getExtension().getCfUserProvidedServices()); } List mapCfUserProvidedServices(List cfUserProvidedServices) { List serviceDetails = new ArrayList<>(); if (cfUserProvidedServices != null) { for (CfUserProvidedService service : cfUserProvidedServices) { CfUserProvidedServiceDetail cfServiceDetail = ImmutableCfUserProvidedServiceDetail.builder() .instanceName(service.getInstanceName()) .credentials(service.getCredentials()) .completionTimeout((service.getCompletionTimeout() != null) ? service.getCompletionTimeout() : DefaultProperties.SERVICE_CREATION_COMPLETION_TIMEOUT) .build(); serviceDetails.add(cfServiceDetail); } } return serviceDetails; } private CfProxySettingsDetail getCfProxySettings() { final CfProxySettings proxySettings = this.getExtension().getCfProxySettings(); if (proxySettings == null) { return null; } return ImmutableCfProxySettingsDetail.builder() .proxyHost(getStringPropertyFromProject(PropertyNameConstants.CF_PROXY_HOST) .orElse(proxySettings.getProxyHost())) .proxyPort(getIntegerPropertyFromProject(PropertyNameConstants.CF_PROXY_PORT) .orElse(proxySettings.getProxyPort())) .proxyUser(getStringPropertyFromProject(PropertyNameConstants.CF_PROXY_USER) .orElse(proxySettings.getProxyUser())) .proxyPassword(getStringPropertyFromProject(PropertyNameConstants.CF_PROXY_PASSWORD) .orElse(proxySettings.getProxyPassword())) .build(); } CfPluginExtension getExtension() { return this.project.getExtensions().findByType(CfPluginExtension.class); } public String getCfApplicationName() { return getStringPropertyFromProject(PropertyNameConstants.CF_APPLICATION_NAME) .orElse(this.getExtension().getName()); } public String getNewName() { return getStringPropertyFromProject(PropertyNameConstants.CF_APPLICATION_NEW_NAME) .orElse(null); } public String getCommand() { return this.getExtension().getCommand(); } public String getAppHostName() { return getStringPropertyFromProject(PropertyNameConstants.CF_APPLICATION_HOST_NAME) .orElse(this.getExtension().getHost()); } public String getAppDomain() { return getStringPropertyFromProject(PropertyNameConstants.CF_APPLICATION_DOMAIN) .orElse(this.getExtension().getDomain()); } public String getFilePath() { return getStringPropertyFromProject(PropertyNameConstants.CF_FILE_PATH) .orElse(this.getExtension().getFilePath()); } public String getCcHost() { return getStringPropertyFromProject(PropertyNameConstants.CC_HOST) .orElse(this.getExtension().getCcHost()); } public String getCcUser() { return getPropertyFromEnvironment(PropertyNameConstants.CC_USER_ENV) .orElse( this.getStringPropertyFromProject(PropertyNameConstants.CC_USER) .orElse(this.getExtension().getCcUser())); } public String getCcPassword() { return getPropertyFromEnvironment(PropertyNameConstants.CC_PASSWORD_ENV) .orElse(getStringPropertyFromProject(PropertyNameConstants.CC_PASSWORD) .orElse(this.getExtension().getCcPassword())); } public List getServices() { return getListPropertyFromProject(PropertyNameConstants.CF_SERVICES) .orElse(this.getExtension().getServices()); } public String getCcToken() { return getStringPropertyFromProject(PropertyNameConstants.CC_TOKEN) .orElse(this.getExtension().getCcToken()); } public String getBuildpack() { return getStringPropertyFromProject(PropertyNameConstants.CF_BUILDPACK) .orElse(this.getExtension().getBuildpack()); } public String getOrg() { return getStringPropertyFromProject(PropertyNameConstants.CF_ORG) .orElse(this.getExtension().getOrg()); } public String getSpace() { return getStringPropertyFromProject(PropertyNameConstants.CF_SPACE) .orElse(this.getExtension().getSpace()); } public String getCfPath() { return getStringPropertyFromProject(PropertyNameConstants.CF_PATH) .orElse(this.getExtension().getPath()); } public Integer getInstances() { return getIntegerPropertyFromProject(PropertyNameConstants.CF_INSTANCES) .orElse(this.getExtension().getInstances()); } public Integer getMemory() { return getIntegerPropertyFromProject(PropertyNameConstants.CF_MEMORY) .orElse(this.getExtension().getMemory()); } public Integer getTimeout() { return getIntegerPropertyFromProject(PropertyNameConstants.CF_HEALTH_CHECK_TIMEOUT) .orElse(this.getExtension().getTimeout()); } public Integer getDiskQuota() { return getIntegerPropertyFromProject(PropertyNameConstants.CF_DISK_QUOTA) .orElse(this.getExtension().getDiskQuota()); } public Integer getStagingTimeout() { Integer stagingTimeout = getIntegerPropertyFromProject(PropertyNameConstants.CF_STAGING_TIMEOUT) .orElse(this.getExtension().getStagingTimeout()); return stagingTimeout != null ? stagingTimeout : DefaultProperties.STAGING_TIMEOUT; } public Integer getStartupTimeout() { Integer startupTimeout = getIntegerPropertyFromProject(PropertyNameConstants.CF_STARTUP_TIMEOUT) .orElse(this.getExtension().getStartupTimeout()); return startupTimeout != null ? startupTimeout : DefaultProperties.STARTUP_TIMEOUT; } private Map getEnvironment() { Map baseEnvironment = this.getExtension().getEnvironment(); Map withProperties = baseEnvironment != null ? new HashMap<>(baseEnvironment) : new HashMap<>(); Map allProperties = this.project.getProperties(); for (String propName : allProperties.keySet()) { String prefix = PropertyNameConstants.CF_ENVIRONMENT + "."; if (propName.startsWith(prefix)) { String newKey = propName.substring(prefix.length()); withProperties.put(newKey, getStringPropertyFromProject(propName).orElse("")); } } return withProperties; } public Optional getPropertyFromEnvironment(String propertyName) { if (this.systemEnv.containsKey(propertyName)) { return Optional.of((String) this.systemEnv.get(propertyName)); } return Optional.empty(); } public Optional getStringPropertyFromProject(String propertyName) { if (this.project.hasProperty(propertyName)) { return Optional.of((String) this.project.property(propertyName)); } return Optional.empty(); } public Optional> getListPropertyFromProject(String propertyName) { if (this.project.hasProperty(propertyName)) { String rawProperty = (String) this.project.property(propertyName); return Optional.of((Arrays.asList(rawProperty.split(",")))); } return Optional.empty(); } /** * Get a property value from the Project properties. * * @param propertyName name of property * @return value of property, @{link java.util.Optional#empty} if not available. */ public Optional getIntegerPropertyFromProject(String propertyName) { if (this.project.hasProperty(propertyName)) { return Optional.of((Integer) this.project.property(propertyName)); } return Optional.empty(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy