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