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

org.cloudfoundry.client.lib.adapters.RawCloudServiceInstance Maven / Gradle / Ivy

The newest version!
package org.cloudfoundry.client.lib.adapters;

import java.util.Optional;

import org.cloudfoundry.client.lib.domain.CloudServiceInstance;
import org.cloudfoundry.client.lib.domain.ImmutableCloudServiceInstance;
import org.cloudfoundry.client.lib.domain.ServiceInstanceType;
import org.cloudfoundry.client.lib.domain.ServiceOperation;
import org.cloudfoundry.client.lib.domain.annotation.Nullable;
import org.cloudfoundry.client.v2.Resource;
import org.cloudfoundry.client.v2.serviceinstances.UnionServiceInstanceEntity;
import org.cloudfoundry.client.v2.serviceplans.ServicePlanEntity;
import org.cloudfoundry.client.v2.services.ServiceEntity;
import org.immutables.value.Value;

@Value.Immutable
public abstract class RawCloudServiceInstance extends RawCloudEntity {

    @Value.Parameter
    public abstract Resource getResource();

    @Nullable
    public abstract Resource getServicePlanResource();

    @Nullable
    public abstract Resource getServiceResource();

    @Override
    public CloudServiceInstance derive() {
        Resource resource = getResource();
        UnionServiceInstanceEntity entity = resource.getEntity();
        return ImmutableCloudServiceInstance.builder()
                                            .metadata(parseResourceMetadata(resource))
                                            .name(entity.getName())
                                            .plan(parsePlan(getServicePlanResource()))
                                            .label(parseLabel(getServiceResource()))
                                            .type(ServiceInstanceType.valueOfWithDefault(entity.getType()))
                                            .tags(entity.getTags())
                                            .credentials(entity.getCredentials())
                                            .lastOperation(ServiceOperation.fromLastOperation(entity.getLastOperation()))
                                            .build();
    }

    private static String parsePlan(Resource resource) {
        return Optional.ofNullable(resource)
                       .map(Resource::getEntity)
                       .map(ServicePlanEntity::getName)
                       .orElse(null);
    }

    private static String parseLabel(Resource serviceResource) {
        return Optional.ofNullable(serviceResource)
                       .map(Resource::getEntity)
                       .map(ServiceEntity::getLabel)
                       .orElse(null);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy