github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/v3action/environment_variable.go (about) 1 package v3action 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 6 "code.cloudfoundry.org/cli/types" 7 ) 8 9 // EnvironmentVariableGroups represents all environment variables for application 10 type EnvironmentVariableGroups ccv3.EnvironmentVariableGroups 11 12 // EnvironmentVariablePair represents an environment variable and its value 13 // on an application 14 type EnvironmentVariablePair struct { 15 Key string 16 Value string 17 } 18 19 // GetEnvironmentVariablesByApplicationNameAndSpace returns the environment 20 // variables for an application. 21 func (actor *Actor) GetEnvironmentVariablesByApplicationNameAndSpace(appName string, spaceGUID string) (EnvironmentVariableGroups, Warnings, error) { 22 app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 23 if appErr != nil { 24 return EnvironmentVariableGroups{}, warnings, appErr 25 } 26 27 ccEnvGroups, v3Warnings, apiErr := actor.CloudControllerClient.GetApplicationEnvironmentVariables(app.GUID) 28 warnings = append(warnings, v3Warnings...) 29 return EnvironmentVariableGroups(ccEnvGroups), warnings, apiErr 30 } 31 32 // SetEnvironmentVariableByApplicationNameAndSpace adds an 33 // EnvironmentVariablePair to an application. It must be restarted for changes 34 // to take effect. 35 func (actor *Actor) SetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, envPair EnvironmentVariablePair) (Warnings, error) { 36 app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 37 if err != nil { 38 return warnings, err 39 } 40 41 _, v3Warnings, apiErr := actor.CloudControllerClient.PatchApplicationUserProvidedEnvironmentVariables(app.GUID, ccv3.EnvironmentVariables{Variables: map[string]types.FilteredString{envPair.Key: {Value: envPair.Value, IsSet: true}}}) 42 warnings = append(warnings, v3Warnings...) 43 return warnings, apiErr 44 } 45 46 // UnsetEnvironmentVariableByApplicationNameAndSpace removes an enviornment 47 // variable from an application. It must be restarted for changes to take 48 // effect. 49 func (actor *Actor) UnsetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, environmentVariableName string) (Warnings, error) { 50 app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 51 if appErr != nil { 52 return warnings, appErr 53 } 54 envGroups, getWarnings, getErr := actor.CloudControllerClient.GetApplicationEnvironmentVariables(app.GUID) 55 warnings = append(warnings, getWarnings...) 56 if getErr != nil { 57 return warnings, getErr 58 } 59 60 if _, ok := envGroups.UserProvided[environmentVariableName]; !ok { 61 return warnings, actionerror.EnvironmentVariableNotSetError{EnvironmentVariableName: environmentVariableName} 62 } 63 64 _, patchWarnings, patchErr := actor.CloudControllerClient.PatchApplicationUserProvidedEnvironmentVariables(app.GUID, ccv3.EnvironmentVariables{Variables: map[string]types.FilteredString{environmentVariableName: {Value: "", IsSet: false}}}) 65 warnings = append(warnings, patchWarnings...) 66 return warnings, patchErr 67 }