-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0070c6
commit 215f70f
Showing
5 changed files
with
106 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// tfvars.ts | ||
'use server'; | ||
|
||
import { EnvVar } from '@/types/userTypes'; | ||
import { | ||
deleteEnvVar, | ||
getAllEnvVars, | ||
storeEncryptedEnvVar, | ||
} from '../admin/encryption'; | ||
|
||
export async function tfvarsOnUpdate( | ||
oldName: string, | ||
newName: string, | ||
value: string, | ||
isSecret: boolean, | ||
projectId: string, | ||
): Promise<EnvVar[]> { | ||
if (oldName !== newName) { | ||
await deleteEnvVar(projectId, oldName); | ||
} | ||
await storeEncryptedEnvVar(projectId, newName, value, isSecret); | ||
const vars = await getAllEnvVars(projectId); | ||
return vars.map((v) => ({ ...v, updated_at: new Date().toISOString() })); | ||
} | ||
|
||
export async function tfvarsOnDelete( | ||
name: string, | ||
projectId: string, | ||
): Promise<EnvVar[]> { | ||
await deleteEnvVar(projectId, name); | ||
const vars = await getAllEnvVars(projectId); | ||
return vars.map((v) => ({ ...v, updated_at: new Date().toISOString() })); | ||
} | ||
|
||
export async function tfvarsOnBulkUpdate( | ||
vars: EnvVar[], | ||
projectId: string, | ||
): Promise<EnvVar[]> { | ||
const currentVars = await getAllEnvVars(projectId); | ||
const currentVarsMap = Object.fromEntries( | ||
currentVars.map((v) => [v.name, v]), | ||
); | ||
|
||
for (const newVar of vars) { | ||
const currentVar = currentVarsMap[newVar.name]; | ||
if (currentVar) { | ||
if ( | ||
!currentVar.is_secret && | ||
(currentVar.value !== newVar.value || currentVar.name !== newVar.name) | ||
) { | ||
await tfvarsOnUpdate( | ||
currentVar.name, | ||
newVar.name, | ||
newVar.value, | ||
currentVar.is_secret, | ||
projectId, | ||
); | ||
} | ||
} else { | ||
await tfvarsOnUpdate( | ||
newVar.name, | ||
newVar.name, | ||
newVar.value, | ||
false, | ||
projectId, | ||
); | ||
} | ||
} | ||
|
||
for (const currentVar of currentVars) { | ||
if ( | ||
!vars.some((v) => v.name === currentVar.name) && | ||
!currentVar.is_secret | ||
) { | ||
await tfvarsOnDelete(currentVar.name, projectId); | ||
} | ||
} | ||
|
||
const updatedVars = await getAllEnvVars(projectId); | ||
return updatedVars.map((v) => ({ | ||
...v, | ||
updated_at: new Date().toISOString(), | ||
})); | ||
} |