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

com.atlassian.bamboo.specs.api.builders.pbc.PerBuildContainerForJob Maven / Gradle / Ivy

The newest version!
package com.atlassian.bamboo.specs.api.builders.pbc;

import com.atlassian.bamboo.specs.api.builders.plan.configuration.PluginConfiguration;
import com.atlassian.bamboo.specs.api.model.pbc.ExtraContainerProperties;
import com.atlassian.bamboo.specs.api.model.pbc.PerBuildContainerForJobProperties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

/**
 * Per Build Container definition for Bamboo Jobs.
 */
public class PerBuildContainerForJob extends PluginConfiguration {

    private String image;
    private boolean enabled = true;
    private String size = ContainerSize.REGULAR.name();
    private String awsRole;
    private String architecture;
    private List extraContainers = new ArrayList<>();
    private HashSet featureFlags = new HashSet<>();

    public PerBuildContainerForJob() {}

    /**
     * Marks PBC as enabled for given job. Default value is true.
     */
    public PerBuildContainerForJob enabled(boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    public PerBuildContainerForJob image(String image) {
        this.image = image;
        return this;
    }

    public PerBuildContainerForJob withFeatureFlag(String featureFlag) {
        this.featureFlags.add(featureFlag);
        return this;
    }

    public PerBuildContainerForJob withFeatureFlags(HashSet featureFlags) {
        this.featureFlags.addAll(featureFlags);
        return this;
    }

    /**
     * Symbolic name for size of the container.
     * See ContainerSize enum for allowed values.
     * The default value is 'REGULAR'.
     */
    public PerBuildContainerForJob size(String size) {
        this.size = size;
        return this;
    }

    /**
     * Symbolic name for size of the container.
     * The default value is 'REGULAR'.
     */
    public PerBuildContainerForJob size(ContainerSize size) {
        this.size = size != null ? size.name() : null;
        return this;
    }

    /**
     * AWS Role of the current build.
     * Optional. Needs to be supported by the runtime environment.
     */
    public PerBuildContainerForJob awsRole(String awsRole) {
        this.awsRole = awsRole;
        return this;
    }

    /**
     * Architecture the build should run on
     * Optional. If specified, the Bamboo server must have this configured as one of the available architectures.
     */
    public PerBuildContainerForJob architecture(Architecture architecture) {
        this.architecture = architecture.toString();
        return this;
    }

    /**
     * This method will not be removed, but it should not be used unless you have a need for a specific architecture
     * not specified in the Architecture enum. You should use {@code .architecture(Architecture a)} instead.
     * @param architecture A string which is the name of the architecture
     */
    @Deprecated
    public PerBuildContainerForJob architecture(String architecture) {
        this.architecture = architecture;
        return this;
    }

    public PerBuildContainerForJob extraContainers(ExtraContainer... extraContainers) {
        extraContainers(Arrays.asList(extraContainers));
        return this;
    }

    public PerBuildContainerForJob extraContainers(List extraContainers) {
        extraContainers.stream().map(ExtraContainer::build).forEach(this.extraContainers::add);
        return this;
    }

    @Override
    protected PerBuildContainerForJobProperties build() {
        return new PerBuildContainerForJobProperties(
                enabled, image, size, extraContainers, awsRole, architecture, featureFlags);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PerBuildContainerForJob that = (PerBuildContainerForJob) o;
        return enabled == that.enabled
                && Objects.equals(image, that.image)
                && Objects.equals(size, that.size)
                && Objects.equals(awsRole, that.awsRole)
                && Objects.equals(architecture, that.architecture)
                && extraContainersEqual(extraContainers, that.extraContainers)
                && Objects.equals(featureFlags, that.featureFlags);
    }

    private boolean extraContainersEqual(
            List thisContainers, List thatContainers) {
        if (thisContainers == thatContainers) {
            return true;
        }
        if (thisContainers.size() != thatContainers.size()) {
            return false;
        }
        for (ExtraContainerProperties thisContainer : thisContainers) {
            if (isExtraContainerNotIn(thisContainer, thatContainers)) {
                return false;
            }
        }
        for (ExtraContainerProperties container : thatContainers) {
            if (isExtraContainerNotIn(container, thisContainers)) {
                return false;
            }
        }
        return true;
    }

    private boolean isExtraContainerNotIn(
            ExtraContainerProperties container, List otherContainers) {
        for (ExtraContainerProperties other : otherContainers) {
            if (Objects.equals(container, other)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(image, enabled, size, awsRole, architecture, extraContainers, featureFlags);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy