mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
Optimize Terraform Cloud sync function
This commit is contained in:
@@ -1816,7 +1816,7 @@ const syncSecretsCheckly = async ({
|
||||
};
|
||||
|
||||
/**
|
||||
* Sync/push [secrets] to Terraform Cloud projects with id [integration.appId]
|
||||
* Sync/push [secrets] to Terraform Cloud project with id [integration.appId]
|
||||
* @param {Object} obj
|
||||
* @param {IIntegration} obj.integration - integration details
|
||||
* @param {Object} obj.secrets - secrets to push to integration (object where keys are secret keys and values are secret values)
|
||||
@@ -1831,7 +1831,6 @@ const syncSecretsTerraformCloud = async ({
|
||||
secrets: any;
|
||||
accessToken: string;
|
||||
}) => {
|
||||
|
||||
// get secrets from Terraform Cloud
|
||||
const getSecretsRes = (
|
||||
await standardRequest.get(`${INTEGRATION_TERRAFORM_CLOUD_API_URL}/api/v2/workspaces/${integration.appId}/vars`,
|
||||
@@ -1841,13 +1840,19 @@ const syncSecretsTerraformCloud = async ({
|
||||
Accept: "application/json",
|
||||
},
|
||||
}
|
||||
)).data.data;
|
||||
|
||||
))
|
||||
.data
|
||||
.data
|
||||
.reduce((obj: any, secret: any) => ({
|
||||
...obj,
|
||||
[secret.attributes.key]: secret
|
||||
}), {});
|
||||
|
||||
// create or update secrets on Terraform Cloud
|
||||
for await (const key of Object.keys(secrets)) {
|
||||
const existingSecret = getSecretsRes.find((sec: any) => sec.attributes.key == key);
|
||||
|
||||
if (!existingSecret) {
|
||||
if (!(key in getSecretsRes)) {
|
||||
// case: secret does not exist in Terraform Cloud
|
||||
// -> add secret
|
||||
await standardRequest.post(
|
||||
`${INTEGRATION_TERRAFORM_CLOUD_API_URL}/api/v2/workspaces/${integration.appId}/vars`,
|
||||
{
|
||||
@@ -1867,19 +1872,20 @@ const syncSecretsTerraformCloud = async ({
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
} else {
|
||||
if (secrets[key] !== existingSecret.attributes.value) {
|
||||
|
||||
// case: secret exists in Terraform Cloud
|
||||
if (secrets[key] !== getSecretsRes[key].attributes.value) {
|
||||
// -> update secret
|
||||
await standardRequest.patch(
|
||||
`${INTEGRATION_TERRAFORM_CLOUD_API_URL}/api/v2/workspaces/${integration.appId}/vars/${existingSecret.id}`,
|
||||
`${INTEGRATION_TERRAFORM_CLOUD_API_URL}/api/v2/workspaces/${integration.appId}/vars/${getSecretsRes[key].id}`,
|
||||
{
|
||||
data: {
|
||||
type: "vars",
|
||||
id: existingSecret.id,
|
||||
id: getSecretsRes[key].id,
|
||||
attributes: {
|
||||
...existingSecret,
|
||||
value: secrets[existingSecret.attributes.key],
|
||||
...getSecretsRes[key],
|
||||
value: secrets[key]
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1890,15 +1896,15 @@ const syncSecretsTerraformCloud = async ({
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// delete secrets from Terraform Cloud
|
||||
for await (const sec of getSecretsRes) {
|
||||
if (!(sec.attributes.key in secrets)) {
|
||||
await standardRequest.delete(`${INTEGRATION_TERRAFORM_CLOUD_API_URL}/api/v2/workspaces/${integration.appId}/vars/${sec.id}`, {
|
||||
for await (const key of Object.keys(getSecretsRes)) {
|
||||
if (!(key in secrets)) {
|
||||
// case: delete secret
|
||||
await standardRequest.delete(`${INTEGRATION_TERRAFORM_CLOUD_API_URL}/api/v2/workspaces/${integration.appId}/vars/${getSecretsRes[key].id}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
|
||||
@@ -58,7 +58,7 @@ export default function TerraformCloudCreateIntegrationPage() {
|
||||
<Input placeholder="API Token" value={apiKey} onChange={(e) => setApiKey(e.target.value)} />
|
||||
</FormControl>
|
||||
<FormControl
|
||||
label="Terraform Cloud Workspace Id"
|
||||
label="Terraform Cloud Workspace ID"
|
||||
errorText={workspacesIdErrorText}
|
||||
isError={workspacesIdErrorText !== "" ?? false}
|
||||
>
|
||||
|
||||
@@ -18,6 +18,11 @@ import {
|
||||
import { useGetWorkspaceById } from "../../../hooks/api/workspace";
|
||||
import createIntegration from "../../api/integrations/createIntegration";
|
||||
|
||||
const variableTypes = [
|
||||
{ name: "env" },
|
||||
{ name: "terraform" }
|
||||
];
|
||||
|
||||
export default function TerraformCloudCreateIntegrationPage() {
|
||||
const router = useRouter();
|
||||
|
||||
@@ -39,6 +44,7 @@ export default function TerraformCloudCreateIntegrationPage() {
|
||||
useEffect(() => {
|
||||
if (workspace) {
|
||||
setSelectedSourceEnvironment(workspace.environments[0].slug);
|
||||
setVariableType(variableTypes[0].name);
|
||||
}
|
||||
}, [workspace]);
|
||||
|
||||
@@ -90,10 +96,6 @@ export default function TerraformCloudCreateIntegrationPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const variableTypes = [
|
||||
{ name: "env" },
|
||||
{ name: "terraform" }
|
||||
]
|
||||
|
||||
return integrationAuth &&
|
||||
workspace &&
|
||||
@@ -126,7 +128,7 @@ export default function TerraformCloudCreateIntegrationPage() {
|
||||
placeholder="Provide a path, default is /"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormControl label="Variable Type" className="mt-4" errorText={variableTypeErrorText}
|
||||
<FormControl label="Category" className="mt-4" errorText={variableTypeErrorText}
|
||||
isError={variableTypeErrorText !== "" ?? false}>
|
||||
<Select
|
||||
value={variableType}
|
||||
|
||||
Reference in New Issue
Block a user