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

io.pivotal.services.plugin.tasks.helper.CfCreateServiceHelper Maven / Gradle / Ivy

There is a newer version: 2.3.0-rc.6
Show newest version
package io.pivotal.services.plugin.tasks.helper;

import io.pivotal.services.plugin.CfServiceDetail;
import org.cloudfoundry.operations.CloudFoundryOperations;
import org.cloudfoundry.operations.services.CreateServiceInstanceRequest;
import org.cloudfoundry.operations.services.ServiceInstance;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import reactor.core.publisher.Mono;

import java.util.Optional;

/**
 * Responsible for creating a Service Instance
 *
 * @author Biju Kunjummen
 */
public class CfCreateServiceHelper {

    private CfServicesDetailHelper servicesDetailHelper = new CfServicesDetailHelper();
    private static final Logger LOGGER = Logging.getLogger(CfCreateServiceHelper.class);

    public Mono createService(CloudFoundryOperations cfOperations,
                                               CfServiceDetail cfServiceDetail) {

        Mono> serviceInstanceMono = servicesDetailHelper
            .getServiceInstanceDetail(cfOperations, cfServiceDetail.instanceName());

        return serviceInstanceMono
            .flatMap(serviceInstanceOpt -> serviceInstanceOpt.map(serviceInstance -> {
                LOGGER.lifecycle(
                    "Existing service with name {} found. This service will not be re-created",
                    cfServiceDetail.instanceName());
                return Mono.just(serviceInstance);
            }).orElseGet(() -> {
                LOGGER
                    .lifecycle("Creating service -  instance: {}, service: {}, plan: {}",
                        cfServiceDetail.instanceName(), cfServiceDetail.name(),
                        cfServiceDetail.plan());

                return cfOperations.services().createInstance(
                    CreateServiceInstanceRequest.builder()
                        .serviceInstanceName(cfServiceDetail.instanceName())
                        .serviceName(cfServiceDetail.name())
                        .planName(cfServiceDetail.plan())
                        .tags(cfServiceDetail.tags())
                        .build())
                    .then(servicesDetailHelper.getServiceInstanceDetail(cfOperations, cfServiceDetail.instanceName()))
                    .map(instanceOpt -> instanceOpt.get());
            }));

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy