-
Notifications
You must be signed in to change notification settings - Fork 5
/
configure.js
153 lines (131 loc) · 5.04 KB
/
configure.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'babel-polyfill';
import EdgeGrid from 'akamai-edgegrid';
import dotenv from 'dotenv';
import inquirer from 'inquirer';
import formatJson from 'format-json';
import fs from 'fs';
(async function() {
// load .env vars
dotenv.config();
let papiResponses = new Map();
const edgegrid = new EdgeGrid({
path: process.env.AKA_EDGERC,
section: 'default'
});
let contractId = await papiChoice(
'Select Akamai contract:',
'/papi/v1/contracts',
'contracts', 'contractId', 'contractTypeName'
);
let groupId = await papiChoice(
'Select Akamai property group:',
'/papi/v1/groups/?contractId=' + contractId,
'groups', 'groupId', 'groupName'
);
let propertyId = await papiChoice(
'Select Akamai property:',
'/papi/v1/properties/?contractId=' + contractId + '&groupId=' + groupId,
'properties', 'propertyId', 'propertyName'
);
let latestVersion = papiResponses.get('properties').properties.items.filter((property) => {
return property.propertyId === propertyId;
})[0].latestVersion;
// request property version
let version = await inquirer.prompt([
{
type: 'input',
name: 'version',
message: 'The latest property verions is ' + latestVersion + ', which would you like?',
default: latestVersion,
validate: (version) => {
if (parseInt(version) > 0 && parseInt(version) <= latestVersion) {
return true;
} else {
return 'Please enter a valid version number.';
}
}
}
]).then(function (answers) {
console.log('selected version = ' + answers.version);
return answers.version;
});
let propertyJson = await callPapi('property', '/papi/v1/properties/' +
propertyId + '/versions/' + version +
'/rules?contractId=' + contractId + '&groupId=' + groupId).then((data) => {
return data;
});
let propertyName = papiResponses.get('properties').properties.items.filter((property) => {
return property.propertyId === propertyId;
})[0].propertyName;
await inquirer.prompt([
{
type: 'confirm',
name: 'outputToFile',
message: 'Output property ' + propertyName + ' v' + version + ' json to file now?',
default: true,
}
]).then(function (answers) {
console.log('selected outputToFile = ' + answers.outputToFile);
if (answers.outputToFile) {
let outputDir = __dirname + '/../papiJson';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
fs.writeFileSync(
outputDir + '/' + propertyName + '-v' + version + '.papi.json',
formatJson.plain(propertyJson), 'utf8'
);
console.log('\npapi json written to: ./papiJson/' + propertyName + '-v' + version + '.papi.json');
}
});
console.log(
'\n# ---------------------------------------------------------\n' +
'# place the following in .env or set as shell/node env vars\n' +
'# if you would like to use these parameters to configure nginx directly\n' +
'# from api calls - otherwise point at the generated papi json.\n' +
'# refer to start.js and start-local.js\n' +
'AKA_CONTRACT_ID=' + contractId + '\n' +
'AKA_GROUP_ID=' + groupId + '\n' +
'AKA_PROPERTY_ID=' + propertyId + '\n' +
'AKA_PROPERTY_VERSION=' + version + '\n'
);
async function papiChoice(message, papiUrl, containerField, valueField, nameField) {
let choices = await callPapi(containerField, papiUrl).then((data) => {
return data[containerField].items.map((item) => {
let choice = {};
choice.name = item[valueField] + ' ' + item[nameField];
choice.value = item[valueField];
return choice;
});
});
return await inquirer.prompt([
{
type: 'list',
name: valueField,
message: message,
paginated: true,
choices: choices
}
]).then(function (answers) {
console.log('selected ' + valueField + ' = ' + answers[valueField]);
return answers[valueField];
});
}
async function callPapi(type, papiUrl) {
return new Promise(
(resolve, reject) => {
console.log('calling papi url: ' + papiUrl + '\n');
edgegrid.auth({
path: papiUrl,
method: 'GET'
}).send((error, response, body) => {
if (error) {
return reject(error);
}
let jsonResult = JSON.parse(body);
papiResponses.set(type, jsonResult);
return resolve(jsonResult);
});
});
}
})();