forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment_variables.go
93 lines (73 loc) · 2.85 KB
/
environment_variables.go
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
package ccv3
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
"code.cloudfoundry.org/cli/types"
)
// EnvironmentVariables represents the environment variables that can be set on
// an application by the user.
type EnvironmentVariables map[string]types.FilteredString
func (variables EnvironmentVariables) MarshalJSON() ([]byte, error) {
ccEnvVars := struct {
Var map[string]types.FilteredString `json:"var"`
}{
Var: variables,
}
return json.Marshal(ccEnvVars)
}
func (variables *EnvironmentVariables) UnmarshalJSON(data []byte) error {
var ccEnvVars struct {
Var map[string]types.FilteredInterface `json:"var"`
}
err := cloudcontroller.DecodeJSON(data, &ccEnvVars)
*variables = EnvironmentVariables{}
for envVarName, envVarValue := range ccEnvVars.Var {
var valueAsString string
if str, ok := envVarValue.Value.(string); ok {
valueAsString = str
} else {
bytes, err := json.Marshal(envVarValue.Value)
if err != nil {
return err
}
valueAsString = string(bytes)
}
(*variables)[envVarName] = types.FilteredString{Value: valueAsString, IsSet: true}
}
return err
}
// GetEnvironmentVariableGroup gets the values of a particular environment variable group.
func (client *Client) GetEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName) (EnvironmentVariables, Warnings, error) {
var responseBody EnvironmentVariables
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.GetEnvironmentVariableGroupRequest,
URIParams: internal.Params{"group_name": string(group)},
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}
// UpdateApplicationEnvironmentVariables adds/updates the user provided
// environment variables on an application. A restart is required for changes
// to take effect.
func (client *Client) UpdateApplicationEnvironmentVariables(appGUID string, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) {
var responseBody EnvironmentVariables
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.PatchApplicationEnvironmentVariablesRequest,
URIParams: internal.Params{"app_guid": appGUID},
RequestBody: envVars,
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}
func (client *Client) UpdateEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) {
var responseBody EnvironmentVariables
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.PatchEnvironmentVariableGroupRequest,
URIParams: internal.Params{"group_name": string(group)},
RequestBody: envVars,
ResponseBody: &responseBody,
})
return responseBody, warnings, err
}