-
Notifications
You must be signed in to change notification settings - Fork 9
/
context.js
194 lines (148 loc) · 6.3 KB
/
context.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
This is context object. It is the configuration hub. It stores all the variables and provides it to adapters during runtime.
*/
var yaml = require('js-yaml');
var fs = require('fs');
var path = require('path');
var mustache = require('mustache');
mustache.escape = function (value) {
return value;
};
var instance;
function getContext(config, env) {
if (!instance) {
instance = new context(config, env);
}
return instance;
}
function context(config, env) {
this.variables = {};
this.env = env;
this.config = {};
this.cmdLineVariables = {};
if (typeof config === 'string' || config instanceof String ) {
var configObj = null;
try {
var current_dir = process.cwd();
var config_file_path = path.join(current_dir, config);
configObj = yaml.safeLoad(fs.readFileSync(config_file_path, 'utf8'));
} catch(e) {
console.log('ERROR reading config file');
}
this.config = configObj;
} else {
this.config = config;
}
this.getVariable = function (variableName) {
// variables[variableName] if not found return undefined
if (!variableName) return undefined;
var vName = variableName.replace("$","").replace("\'","");
return this.variables[vName];
};
this.setVariable = function(name, value) {
this.variables[name] = value;
};
this.cleanVariables = function() {
this.variables = {};
};
this.getAllVariables = function(){
return this.variables;
};
this.loadCmdLineVariables = function (){
var cmd_variables = this.cmdLineVariables;
for (var i=0; i<Object.keys(cmd_variables).length; i++) {
this.setVariable(Object.keys(cmd_variables)[i], cmd_variables[Object.keys(cmd_variables)[i]]);
}
};
this.loadOrgDetail = function (resourceName) {
var config = this.getConfig(resourceName);
var orgDetails;
if(config)
orgDetails = config.properties.edgeOrg;
else {
console.log('ERROR retrieving config, check parameters');
return;
}
if(orgDetails && orgDetails.org)
this.setVariable('org', mustache.render(orgDetails.org, this.getAllVariables()));
if(orgDetails && orgDetails.env)
this.setVariable('env', mustache.render(orgDetails.env, this.getAllVariables()));
if(orgDetails && orgDetails.token)
this.setVariable('token', mustache.render(orgDetails.token, this.getAllVariables()));
if(orgDetails && orgDetails.username)
this.setVariable('username', mustache.render(orgDetails.username, this.getAllVariables()));
if(orgDetails && orgDetails.password)
this.setVariable('password', mustache.render(orgDetails.password, this.getAllVariables()));
};
this.loadConfiguration = function (resourceName) {
var config = this.getConfig(resourceName);
var configurations = config.properties.configurations;
for(var i=0; i<configurations.length; i++){
if(configurations[i].env == this.getEnvironment()){
var keys = Object.keys(configurations[i]);
for(var j=0; j<keys.length; j++){
this.setVariable(keys[j], mustache.render(configurations[i][keys[j]], this.getAllVariables()));
}
}
}
};
this.getConfig = function (resourceName, subResourceName) {
var config = this.config['resources'];
if (subResourceName) { // return config scope of respective subresource
for(var i=0; i<config.length; i++){
if(config[i].name == resourceName) {
for(var j=0; j<config[i].properties.subResources.length; j++){
if(config[i].properties.subResources[j].name == subResourceName) {
return config[i].properties.subResources[j];
}
}
}
}
} else if (resourceName) { // return config scope of respective resource
for(var i=0; i<config.length; i++){
if(config[i].name == resourceName) {
return config[i];
}
}
} else { // return complete config
return config;
}
};
this.getEnvironment = function() {
return this.env;
};
this.getDeploymentInfo = function () {
var deploy_info = {};
deploy_info.baseuri = this.getVariable('edge_host');
deploy_info.organization = this.getVariable('org');
var token = this.getVariable('token');
if(token){
deploy_info.token = token;
} else {
deploy_info.username = this.getVariable('username');
deploy_info.password = this.getVariable('password');
}
// making environments variable 'list' since apigeetool expects it that way
deploy_info.environments = this.getVariable('env').split(',');
return deploy_info;
};
this.getBasePath = function (resourceName) {
return this.getConfig(resourceName).properties.basePath;
};
this.setCmdLineVariables = function (args_passed){
this.cmdLineVariables = args_passed;
};
}
exports.getContext = getContext;