github.com/lukasheimann/cloudfoundrycli@v7.1.0+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/api/cloudcontroller/ccv3/constant"
     7  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  // EnvironmentVariableGroups represents all environment variables for application
    11  type EnvironmentVariableGroups ccv3.Environment
    12  
    13  // EnvironmentVariableGroup represents a CC environment variable group (e.g. staging or running)
    14  type EnvironmentVariableGroup ccv3.EnvironmentVariables
    15  
    16  // EnvironmentVariablePair represents an environment variable and its value
    17  // on an application
    18  type EnvironmentVariablePair struct {
    19  	Key   string
    20  	Value string
    21  }
    22  
    23  // GetEnvironmentVariableGroup returns the values of an environment variable group.
    24  func (actor *Actor) GetEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName) (EnvironmentVariableGroup, Warnings, error) {
    25  	ccEnvGroup, warnings, err := actor.CloudControllerClient.GetEnvironmentVariableGroup(group)
    26  	return EnvironmentVariableGroup(ccEnvGroup), Warnings(warnings), err
    27  }
    28  
    29  // GetEnvironmentVariablesByApplicationNameAndSpace returns the environment
    30  // variables for an application.
    31  func (actor *Actor) GetEnvironmentVariablesByApplicationNameAndSpace(appName string, spaceGUID string) (EnvironmentVariableGroups, Warnings, error) {
    32  	app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    33  	if appErr != nil {
    34  		return EnvironmentVariableGroups{}, warnings, appErr
    35  	}
    36  
    37  	ccEnvGroups, v3Warnings, apiErr := actor.CloudControllerClient.GetApplicationEnvironment(app.GUID)
    38  	warnings = append(warnings, v3Warnings...)
    39  	return EnvironmentVariableGroups(ccEnvGroups), warnings, apiErr
    40  }
    41  
    42  // SetEnvironmentVariableByApplicationNameAndSpace adds an
    43  // EnvironmentVariablePair to an application. It must be restarted for changes
    44  // to take effect.
    45  func (actor *Actor) SetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, envPair EnvironmentVariablePair) (Warnings, error) {
    46  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    47  	if err != nil {
    48  		return warnings, err
    49  	}
    50  
    51  	_, v3Warnings, apiErr := actor.CloudControllerClient.UpdateApplicationEnvironmentVariables(
    52  		app.GUID,
    53  		ccv3.EnvironmentVariables{
    54  			envPair.Key: {Value: envPair.Value, IsSet: true},
    55  		})
    56  	warnings = append(warnings, v3Warnings...)
    57  	return warnings, apiErr
    58  }
    59  
    60  // SetEnvironmentVariableGroup sets a given environment variable group according to the given
    61  // keys and values. Any existing variables that are not present in the given set of variables
    62  // will be unset.
    63  func (actor *Actor) SetEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName, newEnvVars ccv3.EnvironmentVariables) (Warnings, error) {
    64  	var allWarnings Warnings
    65  
    66  	existingEnvVars, warnings, err := actor.CloudControllerClient.GetEnvironmentVariableGroup(group)
    67  	allWarnings = append(allWarnings, warnings...)
    68  	if err != nil {
    69  		return allWarnings, err
    70  	}
    71  
    72  	for k := range existingEnvVars {
    73  		if _, ok := newEnvVars[k]; !ok {
    74  			newEnvVars[k] = types.FilteredString{IsSet: false}
    75  		}
    76  	}
    77  
    78  	_, warnings, err = actor.CloudControllerClient.UpdateEnvironmentVariableGroup(group, newEnvVars)
    79  	allWarnings = append(allWarnings, warnings...)
    80  
    81  	return allWarnings, err
    82  }
    83  
    84  // UnsetEnvironmentVariableByApplicationNameAndSpace removes an environment
    85  // variable from an application. It must be restarted for changes to take
    86  // effect.
    87  func (actor *Actor) UnsetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, environmentVariableName string) (Warnings, error) {
    88  	app, warnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    89  	if appErr != nil {
    90  		return warnings, appErr
    91  	}
    92  	envGroups, getWarnings, getErr := actor.CloudControllerClient.GetApplicationEnvironment(app.GUID)
    93  	warnings = append(warnings, getWarnings...)
    94  	if getErr != nil {
    95  		return warnings, getErr
    96  	}
    97  
    98  	if _, ok := envGroups.EnvironmentVariables[environmentVariableName]; !ok {
    99  		return warnings, actionerror.EnvironmentVariableNotSetError{EnvironmentVariableName: environmentVariableName}
   100  	}
   101  
   102  	_, patchWarnings, patchErr := actor.CloudControllerClient.UpdateApplicationEnvironmentVariables(
   103  		app.GUID,
   104  		ccv3.EnvironmentVariables{
   105  			environmentVariableName: {Value: "", IsSet: false},
   106  		})
   107  	warnings = append(warnings, patchWarnings...)
   108  	return warnings, patchErr
   109  }