code.cloudfoundry.org/cli@v7.1.0+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  )
     7  
     8  // EnvironmentVariableGroups represents all environment variables for application
     9  type EnvironmentVariableGroups ccv3.Environment
    10  
    11  // EnvironmentVariablePair represents an environment variable and its value
    12  // on an application
    13  type EnvironmentVariablePair struct {
    14  	Key   string
    15  	Value string
    16  }
    17  
    18  // GetEnvironmentVariablesByApplicationNameAndSpace returns the environment
    19  // variables for an application.
    20  func (actor *Actor) GetEnvironmentVariablesByApplicationNameAndSpace(appName string, spaceGUID string) (EnvironmentVariableGroups, Warnings, error) {
    21  	app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    22  	if appErr != nil {
    23  		return EnvironmentVariableGroups{}, warnings, appErr
    24  	}
    25  
    26  	ccEnvGroups, v3Warnings, apiErr := actor.CloudControllerClient.GetApplicationEnvironment(app.GUID)
    27  	warnings = append(warnings, v3Warnings...)
    28  	return EnvironmentVariableGroups(ccEnvGroups), warnings, apiErr
    29  }
    30  
    31  // SetEnvironmentVariableByApplicationNameAndSpace adds an
    32  // EnvironmentVariablePair to an application. It must be restarted for changes
    33  // to take effect.
    34  func (actor *Actor) SetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, envPair EnvironmentVariablePair) (Warnings, error) {
    35  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    36  	if err != nil {
    37  		return warnings, err
    38  	}
    39  
    40  	_, v3Warnings, apiErr := actor.CloudControllerClient.UpdateApplicationEnvironmentVariables(
    41  		app.GUID,
    42  		ccv3.EnvironmentVariables{
    43  			envPair.Key: {Value: envPair.Value, IsSet: true},
    44  		})
    45  	warnings = append(warnings, v3Warnings...)
    46  	return warnings, apiErr
    47  }
    48  
    49  // UnsetEnvironmentVariableByApplicationNameAndSpace removes an enviornment
    50  // variable from an application. It must be restarted for changes to take
    51  // effect.
    52  func (actor *Actor) UnsetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, environmentVariableName string) (Warnings, error) {
    53  	app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    54  	if appErr != nil {
    55  		return warnings, appErr
    56  	}
    57  	envGroups, getWarnings, getErr := actor.CloudControllerClient.GetApplicationEnvironment(app.GUID)
    58  	warnings = append(warnings, getWarnings...)
    59  	if getErr != nil {
    60  		return warnings, getErr
    61  	}
    62  
    63  	if _, ok := envGroups.EnvironmentVariables[environmentVariableName]; !ok {
    64  		return warnings, actionerror.EnvironmentVariableNotSetError{EnvironmentVariableName: environmentVariableName}
    65  	}
    66  
    67  	_, patchWarnings, patchErr := actor.CloudControllerClient.UpdateApplicationEnvironmentVariables(
    68  		app.GUID,
    69  		ccv3.EnvironmentVariables{
    70  			environmentVariableName: {Value: "", IsSet: false},
    71  		})
    72  	warnings = append(warnings, patchWarnings...)
    73  	return warnings, patchErr
    74  }