github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v7action/environment_variable.go (about) 1 package v7action 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 6 "code.cloudfoundry.org/cli/resources" 7 ) 8 9 // EnvironmentVariableGroups represents all environment variables for application 10 type EnvironmentVariableGroups ccv3.Environment 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.GetApplicationEnvironment(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.UpdateApplicationEnvironmentVariables( 42 app.GUID, 43 resources.EnvironmentVariables{ 44 envPair.Key: {Value: envPair.Value, IsSet: true}, 45 }) 46 warnings = append(warnings, v3Warnings...) 47 return warnings, apiErr 48 } 49 50 // UnsetEnvironmentVariableByApplicationNameAndSpace removes an environment 51 // variable from an application. It must be restarted for changes to take 52 // effect. 53 func (actor *Actor) UnsetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, environmentVariableName string) (Warnings, error) { 54 app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 55 if appErr != nil { 56 return warnings, appErr 57 } 58 envGroups, getWarnings, getErr := actor.CloudControllerClient.GetApplicationEnvironment(app.GUID) 59 warnings = append(warnings, getWarnings...) 60 if getErr != nil { 61 return warnings, getErr 62 } 63 64 if _, ok := envGroups.EnvironmentVariables[environmentVariableName]; !ok { 65 return warnings, actionerror.EnvironmentVariableNotSetError{EnvironmentVariableName: environmentVariableName} 66 } 67 68 _, patchWarnings, patchErr := actor.CloudControllerClient.UpdateApplicationEnvironmentVariables( 69 app.GUID, 70 resources.EnvironmentVariables{ 71 environmentVariableName: {Value: "", IsSet: false}, 72 }) 73 warnings = append(warnings, patchWarnings...) 74 return warnings, patchErr 75 }