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

com.sap.cloud.mt.subscription.ServiceBinding Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
/*******************************************************************************
 *   © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved.
 ******************************************************************************/
package com.sap.cloud.mt.subscription;

import com.sap.cloud.mt.subscription.ServiceOperation.Status;
import com.sap.cloud.mt.subscription.ServiceOperation.Type;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service binding parameters
 */
public class ServiceBinding extends HashMap {
    public static final String CREDENTIALS = "credentials";
    public static final String SERVICE_INSTANCE_ID = "service_instance_id";
    public static final String ID = "id";
    public static final String NAME = "name";
    private final ServiceOperation lastOperation;

    public ServiceBinding(Map m) {
        super(m);
        lastOperation = new ServiceOperation(m != null ? m : new HashMap());
    }

    public String getId() {
        return containsKey(ID) ? (String) get(ID) : "";
    }

    public boolean isReady() {
        return containsKey("ready") && (boolean) get("ready");
    }

    public String getName() {
        return containsKey(NAME) ? (String) get(NAME) : "";
    }

    public String getServiceInstanceId() {
        return containsKey(SERVICE_INSTANCE_ID) ? (String) get(SERVICE_INSTANCE_ID) : "";
    }

    public Instant getCreatedAt() {
        return Instant.parse((String) get("created_at"));
    }

    public Instant getUpdatedAt() {
        return Instant.parse((String) get("updated_at"));
    }

    public Map getCredentials() {
        return containsKey(CREDENTIALS) ? (Map) get(CREDENTIALS) : new HashMap<>();
    }

    public ServiceOperation getLastOperation() {
        return lastOperation;
    }

    public List getTenants() {
        var labels = (Map) get("labels");
        if (labels == null) {
            return new ArrayList<>();
        }
        var tenants = (List) labels.get("tenant_id");
        return tenants != null ? tenants.stream().filter(t -> !StringUtils.isBlank(t)).toList() : new ArrayList<>();
    }

    public boolean hasTenant() {
        return !getTenants().isEmpty();
    }

    boolean isUsable() {
        var status = lastOperation.getStatus();
        return isReady() &&
                !getCredentials().isEmpty() &&
                !(lastOperation.getType() == Type.CREATE && (status == Status.FAILED || status == Status.IN_PROGRESS));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy