Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (81 loc) · 2.89 KB
/
Copy pathindex.js
File metadata and controls
89 lines (81 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const core = require("@actions/core");
const axios = require("axios");
async function main() {
try {
const variableSet = core.getInput("variableSetName");
const organizationName = core.getInput("organizationName");
const token = core.getInput("terraformToken");
const terraformHost = "app.terraform.io";
const variableValue = core.getInput("variableValue");
const variableName = core.getInput("variableName");
let variableHCL = core.getInput("variableHCL");
if (variableHCL !== "true") {
variableHCL = "false";
}
const options = {
headers: {
"Content-Type": "application/vnd.api+json",
Authorization: "Bearer " + token,
},
};
// Fetching WorkSpaceId
// TODO: Throw exception if workspace don't exist.
const terraformWorkSpaceEndpoint =
"https://" +
terraformHost +
"/api/v2/organizations/" +
organizationName +
"/varsets";
const response = await axios.get(terraformWorkSpaceEndpoint, options);
var variableSetId = response.data.data.find(
(item) => item.attributes.name === variableSet
)?.id;
console.log("variable set id:" + variableSetId);
if (variableSetId == null) {
core.setFailed("Variable Set Could Not Be Found");
}
const terraformVariableSetEndpoint =
"https://" + terraformHost + "/api/v2/varsets/" + variableSetId + "/relationships/vars";
const response1 = await axios.get(terraformVariableSetEndpoint, options);
var variableId = response1.data.data.find(
(item) => item.attributes.key === variableName
)?.id;
console.log("variable id:" + variableId);
if (variableId == null) { // if variable not found, create it
console.log("variable not found, creating it");
const terraformVariableSetEndpoint =
"https://" + terraformHost + "/api/v2/varsets/" + variableSetId + "/relationships/vars";
await axios.post(terraformVariableSetEndpoint, {
data: {
type: "vars",
attributes: {
key: variableName,
value: variableValue,
hcl: variableHCL,
category: "terraform",
},
},
}, options);
console.log("variable created");
}else{
console.log("variable found, updating it");
const terraformVariableSetEndpoint =
"https://" + terraformHost + "/api/v2/varsets/" + variableSetId + "/relationships/vars/" + variableId;
await axios.patch(terraformVariableSetEndpoint, {
data: {
type: "vars",
attributes: {
key: variableName,
value: variableValue,
hcl: variableHCL,
category: "terraform",
},
},
}, options);
console.log("variable updated");
}
} catch (error) {
core.setFailed(error.message);
}
}
main();