"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeKubernetesDeployFulfill = exports.kubernetesDeployFulfiller = exports.KubernetesDeployFulfillerGoalName = void 0;
const GoalWithFulfillment_1 = require("../../../api/goal/GoalWithFulfillment");
const data_1 = require("./data");
const deploy_1 = require("./deploy");
/** Standard name for Kubernetes deployment execution goal. */
exports.KubernetesDeployFulfillerGoalName = "kubernetes-deploy-fulfill";
/**
 * Return a goal with a standard name,
 * [[KubernetesDeployFulfillerGoalName]], that will fulfill a
 * [[KubernetesDeploy]] goal by executing
 * [[executeKubernetesDeployFulfill]].
 */
function kubernetesDeployFulfiller() {
    const goalName = exports.KubernetesDeployFulfillerGoalName;
    return new GoalWithFulfillment_1.GoalWithFulfillment({ displayName: goalName, uniqueName: goalName })
        .with({
        goalExecutor: exports.executeKubernetesDeployFulfill,
        name: `${goalName}-executor`,
    });
}
exports.kubernetesDeployFulfiller = kubernetesDeployFulfiller;
/**
 * Extract [[KubernetesApplication]] from goal event data property and
 * deploy the application to the Kubernetes cluster using
 * [[deployApplication]].
 */
const executeKubernetesDeployFulfill = async (gi) => {
    const { context, goalEvent, progressLog } = gi;
    let app;
    try {
        app = data_1.getKubernetesGoalEventData(goalEvent);
    }
    catch (e) {
        e.message = `Invalid SDM goal event data: ${e.message}`;
        progressLog.write(e.message);
        throw e;
    }
    if (!app) {
        const message = "SDM goal event has no Kubernetes application data";
        progressLog.write(message);
        return { code: 0, message };
    }
    const appId = deploy_1.deployAppId(goalEvent, context, app);
    try {
        return deploy_1.deployApplication(goalEvent, context, progressLog);
    }
    catch (e) {
        const message = `Failed to deploy ${appId}: ${e.message}`;
        return { code: 1, message };
    }
};
exports.executeKubernetesDeployFulfill = executeKubernetesDeployFulfill;
//# sourceMappingURL=fulfiller.js.map