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