@@ -32,6 +32,90 @@ function checkAuthorization(context, event, callback) {
32
32
return true ;
33
33
}
34
34
35
+ async function getCurrentEnvironment ( context ) {
36
+ if ( context . DOMAIN_NAME && context . DOMAIN_NAME . startsWith ( 'localhost' ) ) {
37
+ return ;
38
+ }
39
+ const client = context . getTwilioClient ( ) ;
40
+ const services = await client . serverless . services . list ( ) ;
41
+ for ( const service of services ) {
42
+ const environments = await client . serverless
43
+ . services ( service . sid )
44
+ . environments . list ( ) ;
45
+ const environment = environments . find (
46
+ ( env ) => env . domainName === context . DOMAIN_NAME
47
+ ) ;
48
+ if ( environment ) {
49
+ // Exit the function
50
+ // eslint-disable-next-line consistent-return
51
+ return environment ;
52
+ }
53
+ }
54
+ }
55
+
56
+ async function getEnvironmentVariables ( context , environment ) {
57
+ const client = context . getTwilioClient ( ) ;
58
+ return client . serverless
59
+ . services ( environment . serviceSid )
60
+ . environments ( environment . sid )
61
+ . variables . list ( ) ;
62
+ }
63
+
64
+ async function getEnvironmentVariable ( context , environment , key ) {
65
+ // The list filter method isn't implemented yet.
66
+ const envVars = await getEnvironmentVariables ( context , environment ) ;
67
+ return envVars . find ( ( variable ) => variable . key === key ) ;
68
+ }
69
+
70
+ async function setEnvironmentVariable (
71
+ context ,
72
+ environment ,
73
+ key ,
74
+ value ,
75
+ override = true
76
+ ) {
77
+ const client = context . getTwilioClient ( ) ;
78
+ try {
79
+ const currentVariable = await getEnvironmentVariable (
80
+ context ,
81
+ environment ,
82
+ key
83
+ ) ;
84
+ if ( currentVariable ) {
85
+ if ( currentVariable . value !== value ) {
86
+ if ( override ) {
87
+ if ( value === undefined ) {
88
+ console . log ( `Removing ${ key } ...` ) ;
89
+ await currentVariable . remove ( ) ;
90
+ } else {
91
+ console . log ( `Updating ${ key } ...` ) ;
92
+ await currentVariable . update ( { value } ) ;
93
+ }
94
+ return true ;
95
+ }
96
+ console . log (
97
+ `Not overriding existing variable '${ key } ' which is set to '${ currentVariable . value } '`
98
+ ) ;
99
+ return false ;
100
+ }
101
+ console . warn ( `Variable '${ key } ' was already set to '${ value } '` ) ;
102
+ return false ;
103
+ }
104
+ console . log ( `Creating variable ${ key } ` ) ;
105
+ await client . serverless
106
+ . services ( environment . serviceSid )
107
+ . environments ( environment . sid )
108
+ . variables . create ( {
109
+ key,
110
+ value,
111
+ } ) ;
112
+ } catch ( err ) {
113
+ console . error ( `Error creating '${ key } ' with '${ value } ': ${ err } ` ) ;
114
+ return false ;
115
+ }
116
+ return true ;
117
+ }
118
+
35
119
function urlForSiblingPage ( newPage , ...paths ) {
36
120
const url = path . resolve ( ...paths ) ;
37
121
const parts = url . split ( '/' ) ;
@@ -43,5 +127,9 @@ function urlForSiblingPage(newPage, ...paths) {
43
127
module . exports = {
44
128
checkAuthorization,
45
129
createToken,
130
+ getCurrentEnvironment,
131
+ getEnvironmentVariables,
132
+ getEnvironmentVariable,
133
+ setEnvironmentVariable,
46
134
urlForSiblingPage,
47
135
} ;
0 commit comments