Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

[FEATURE] have error code and chaining for deployment validate cmd #490

Merged
merged 5 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/commands/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@
"description": "Run a test for the configured storage account. This will write test data and delete the test data. For more information on the behavior, please check the online documentation.",
"defaultValue": false
}
]
],
"markdown": "## Description\n\nThis command validates the\n[requirements](https://github.com/CatalystCode/spk/blob/master/guides/service-introspection.md#requirements)\nand the onboard\n[prerequisites](https://github.com/CatalystCode/spk/blob/master/guides/service-introspection.md#prerequisites)\n\n## Note\n\nThe purpose of `--self-test` option is to make sure that `spk` is able to write\ndata to the provided storage account. Once the test ends, it will remove the\ntest data that was added.\n"
},
"hld append-variable-group": {
"command": "append-variable-group <variable-group-name>",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/deployment/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const handlePipeline1 = async (
) {
throw buildError(
errorStatusCode.VALIDATION_ERR,
"introspect-create-cmd-cmd-p1-missing-values"
"introspect-create-cmd-p1-missing-values"
);
}
await addSrcToACRPipeline(
Expand All @@ -104,7 +104,7 @@ export const handlePipeline2 = async (
) {
throw buildError(
errorStatusCode.VALIDATION_ERR,
"introspect-create-cmd-cmd-p2-missing-values"
"introspect-create-cmd-p2-missing-values"
);
}
await updateACRToHLDPipeline(
Expand Down
4 changes: 3 additions & 1 deletion src/commands/deployment/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ describe("test runSelfTest function", () => {

const config = deepClone(mockedValidateConfig);
config.tableName = "";
await runSelfTest(config);
await expect(runSelfTest(config)).rejects.toThrow(
"introspect-validate-cmd-valid-exception: Error was caught during validation."
);
});
it("negative test: error thrown", async () => {
jest
Expand Down
47 changes: 33 additions & 14 deletions src/commands/deployment/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
import { logger } from "../../logger";
import { ConfigYaml } from "../../types";
import decorator from "./validator.decorator.json";
import { build as buildError, log as logError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../../lib/errorStatusCode";
import decorator from "./validate.decorator.json";

const service = "spk-self-test";

Expand Down Expand Up @@ -64,10 +66,10 @@ export const isValidConfig = (config: ConfigYaml): ValidateConfig => {
}

if (missingConfig.length > 0) {
logger.error(
"Validation failed. Missing configuration: " + missingConfig.join(" ")
);
throw new Error("missing configuration in spk configuration");
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "introspect-validate-cmd-valid-err",
values: [missingConfig.join(" ")],
});
} else {
logger.info("Configuration validation: SUCCEEDED");
}
Expand All @@ -90,8 +92,10 @@ export const isValidConfig = (config: ConfigYaml): ValidateConfig => {
key: config.introspection.azure.key,
};
}
throw Error(
"You need to specify configuration for your introspection storage account and DevOps pipeline to run this dashboard. Please initialize the spk tool with the right configuration"

throw buildError(
errorStatusCode.VALIDATION_ERR,
"introspect-validate-cmd-missing-vals"
);
};

Expand Down Expand Up @@ -133,8 +137,11 @@ export const writeSelfTestData = async (

return buildId;
} catch (err) {
logger.error(err);
throw new Error("Error writing data to service introspection.");
throw buildError(
errorStatusCode.ENV_SETTING_ERR,
"introspect-validate-cmd-write-pipeline",
err
);
}
};

Expand Down Expand Up @@ -216,12 +223,18 @@ export const runSelfTest = async (config: ValidateConfig): Promise<void> => {

if (!isVerified) {
logger.error(statusMessage + "FAILED. Please try again.");
} else {
logger.info(statusMessage + "SUCCEEDED.");
throw buildError(
errorStatusCode.ENV_SETTING_ERR,
"introspect-validate-cmd-valid-failed"
);
}
logger.info(statusMessage + "SUCCEEDED.");
} catch (err) {
logger.error("Error running self-test.");
throw err;
throw buildError(
errorStatusCode.EXE_FLOW_ERR,
"introspect-validate-cmd-valid-exception",
err
);
}
};

Expand All @@ -244,7 +257,13 @@ export const execute = async (
}
await exitFn(0);
} catch (err) {
logger.error(err);
logError(
buildError(
errorStatusCode.CMD_EXE_ERR,
"introspect-validate-cmd-failed",
err
)
);
await exitFn(1);
}
};
Expand Down
110 changes: 63 additions & 47 deletions src/lib/azure/deploymenttable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,28 @@ export const addSrcToACRPipeline = async (
commitId: string,
repository?: string
): Promise<RowSrcToACRPipeline> => {
const entry: RowSrcToACRPipeline = {
PartitionKey: tableInfo.partitionKey,
RowKey: getRowKey(),
commitId,
imageTag,
p1: pipelineId,
service: serviceName,
};
if (repository) {
entry.sourceRepo = repository.toLowerCase();
try {
const entry: RowSrcToACRPipeline = {
PartitionKey: tableInfo.partitionKey,
RowKey: getRowKey(),
commitId,
imageTag,
p1: pipelineId,
service: serviceName,
};
if (repository) {
entry.sourceRepo = repository.toLowerCase();
}
await insertToTable(tableInfo, entry);
logger.info("Added first pipeline details to the database");
return entry;
} catch (err) {
throw buildError(
errorStatusCode.AZURE_STORAGE_OP_ERR,
"deployment-table-add-src-to-acr-pipeline",
err
);
}
await insertToTable(tableInfo, entry);
logger.info("Added first pipeline details to the database");
return entry;
};

/**
Expand Down Expand Up @@ -330,33 +338,48 @@ export const updateACRToHLDPipeline = async (
pr?: string,
repository?: string
): Promise<RowACRToHLDPipeline> => {
const entries = await findMatchingDeployments<EntryACRToHLDPipeline>(
tableInfo,
"imageTag",
imageTag
);

// 1. try to find the matching entry.
if (entries && entries.length > 0) {
const found = await updateMatchingArcToHLDPipelineEntry(
entries,
try {
const entries = await findMatchingDeployments<EntryACRToHLDPipeline>(
tableInfo,
pipelineId,
imageTag,
hldCommitId,
env,
pr,
repository
"imageTag",
imageTag
);

if (found) {
return found;
// 1. try to find the matching entry.
if (entries && entries.length > 0) {
const found = await updateMatchingArcToHLDPipelineEntry(
entries,
tableInfo,
pipelineId,
imageTag,
hldCommitId,
env,
pr,
repository
);

if (found) {
return found;
}

// 2. when cannot find the entry, we take the last row and INSERT it.
// TODO: rethink this logic.
return await updateLastRowOfArcToHLDPipelines(
entries,
tableInfo,
pipelineId,
imageTag,
hldCommitId,
env,
pr,
repository
);
}

// 2. when cannot find the entry, we take the last row and INSERT it.
// Fallback: Ideally we should not be getting here, because there should
// always be a p1 for any p2 being created.
// TODO: rethink this logic.
return await updateLastRowOfArcToHLDPipelines(
entries,
return await addNewRowToArcToHLDPipelines(
tableInfo,
pipelineId,
imageTag,
Expand All @@ -365,20 +388,13 @@ export const updateACRToHLDPipeline = async (
pr,
repository
);
} catch (err) {
throw buildError(
errorStatusCode.AZURE_STORAGE_OP_ERR,
"deployment-table-add-acr-to-hld-pipeline",
err
);
}

// Fallback: Ideally we should not be getting here, because there should
// always be a p1 for any p2 being created.
// TODO: rethink this logic.
return await addNewRowToArcToHLDPipelines(
tableInfo,
pipelineId,
imageTag,
hldCommitId,
env,
pr,
repository
);
};

/**
Expand Down
15 changes: 12 additions & 3 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@
"introspect-create-cmd-failed": "Deployment create command was not successfully executed.",
"introspect-create-cmd-no-ops": "No action could be performed for specified arguments.",
"introspect-create-cmd-missing-values": "Access key, storage account name, partition key and/or table name were not provided. Provide them.",
"introspect-create-cmd-cmd-p1-missing-values": "Values for image-tag, commit-id and service options were missing. They are required for updating the details of source pipeline. Provide them.",
"introspect-create-cmd-cmd-p2-missing-values": "Values for p2, hld-commit-id, image-tag and env options were missing. They are required For updating the details of image tag release pipeline. Provide them.",
"introspect-create-cmd-p1-missing-values": "Values for image-tag, commit-id and service options were missing. They are required for updating the details of source pipeline. Provide them.",
"introspect-create-cmd-p2-missing-values": "Values for p2, hld-commit-id, image-tag and env options were missing. They are required For updating the details of image tag release pipeline. Provide them.",

"introspect-validate-cmd-failed": "Deployment validate command was not successfully executed.",
"introspect-validate-cmd-valid-err": "Validation failed. Missing configuration: {0}",
"introspect-validate-cmd-missing-vals": "Configuration for introspection storage account and DevOps pipeline to execute this command were missing. Initialize the spk tool with the right configuration",
"introspect-validate-cmd-valid-failed": "Validation was unsuccessful. Try again.",
"introspect-validate-cmd-valid-exception": "Error was caught during validation.",
"introspect-validate-cmd-write-pipeline": "Error writing data to service introspection.",

"introspect-dashboard-cmd-failed": "Deployment dashboard command was not successfully executed.",
"introspect-dashboard-cmd-invalid-port": "value for port option has to be a valid port number. Enter a valid port number.",
Expand All @@ -88,6 +95,8 @@

"deployment-table-update-hld-manifest-pipeline-failed": "Could not update HLD to manifest pipeline.",
"deployment-table-update-manifest-commit-id-failed": "Could not update manifest commit Id.",
"deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}."
"deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}.",
"deployment-table-add-src-to-acr-pipeline": "Could not add source to ACR pipeline information to storage table.",
"deployment-table-add-acr-to-hld-pipeline": "Could not add ACR to HLD pipeline information to storage table."
}
}