github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/environment_variables.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 10 "code.cloudfoundry.org/cli/types" 11 ) 12 13 // EnvironmentVariables represents the environment variables that can be set on 14 // an application by the user. 15 type EnvironmentVariables map[string]types.FilteredString 16 17 func (variables EnvironmentVariables) MarshalJSON() ([]byte, error) { 18 ccEnvVars := struct { 19 Var map[string]types.FilteredString `json:"var"` 20 }{ 21 Var: variables, 22 } 23 24 return json.Marshal(ccEnvVars) 25 } 26 27 func (variables *EnvironmentVariables) UnmarshalJSON(data []byte) error { 28 var ccEnvVars struct { 29 Var map[string]types.FilteredString `json:"var"` 30 } 31 32 err := cloudcontroller.DecodeJSON(data, &ccEnvVars) 33 *variables = EnvironmentVariables(ccEnvVars.Var) 34 35 return err 36 } 37 38 // GetEnvironmentVariableGroup gets the values of a particular environment variable group. 39 func (client *Client) GetEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName) (EnvironmentVariables, Warnings, error) { 40 request, err := client.newHTTPRequest(requestOptions{ 41 URIParams: internal.Params{"group_name": string(group)}, 42 RequestName: internal.GetEnvironmentVariableGroupRequest, 43 }) 44 if err != nil { 45 return EnvironmentVariables{}, nil, err 46 } 47 48 var responseEnvVars EnvironmentVariables 49 response := cloudcontroller.Response{ 50 DecodeJSONResponseInto: &responseEnvVars, 51 } 52 err = client.connection.Make(request, &response) 53 return responseEnvVars, response.Warnings, err 54 } 55 56 // UpdateApplicationEnvironmentVariables adds/updates the user provided 57 // environment variables on an application. A restart is required for changes 58 // to take effect. 59 func (client *Client) UpdateApplicationEnvironmentVariables(appGUID string, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) { 60 bodyBytes, err := json.Marshal(envVars) 61 if err != nil { 62 return EnvironmentVariables{}, nil, err 63 } 64 65 request, err := client.newHTTPRequest(requestOptions{ 66 URIParams: internal.Params{"app_guid": appGUID}, 67 RequestName: internal.PatchApplicationEnvironmentVariablesRequest, 68 Body: bytes.NewReader(bodyBytes), 69 }) 70 if err != nil { 71 return EnvironmentVariables{}, nil, err 72 } 73 74 var responseEnvVars EnvironmentVariables 75 response := cloudcontroller.Response{ 76 DecodeJSONResponseInto: &responseEnvVars, 77 } 78 err = client.connection.Make(request, &response) 79 return responseEnvVars, response.Warnings, err 80 } 81 82 func (client *Client) UpdateEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) { 83 bodyBytes, err := json.Marshal(envVars) 84 85 if err != nil { 86 return EnvironmentVariables{}, nil, err 87 } 88 89 request, err := client.newHTTPRequest(requestOptions{ 90 URIParams: internal.Params{"group_name": string(group)}, 91 RequestName: internal.PatchEnvironmentVariableGroupRequest, 92 Body: bytes.NewReader(bodyBytes), 93 }) 94 95 if err != nil { 96 return EnvironmentVariables{}, nil, err 97 } 98 99 var responseEnvVars EnvironmentVariables 100 response := cloudcontroller.Response{ 101 DecodeJSONResponseInto: &responseEnvVars, 102 } 103 104 err = client.connection.Make(request, &response) 105 return responseEnvVars, response.Warnings, err 106 }