github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/environment_variables.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     9  	"code.cloudfoundry.org/cli/types"
    10  )
    11  
    12  // EnvironmentVariables represents the environment variables that can be set on
    13  // an application by the user.
    14  type EnvironmentVariables map[string]types.FilteredString
    15  
    16  func (variables EnvironmentVariables) MarshalJSON() ([]byte, error) {
    17  	ccEnvVars := struct {
    18  		Var map[string]types.FilteredString `json:"var"`
    19  	}{
    20  		Var: variables,
    21  	}
    22  
    23  	return json.Marshal(ccEnvVars)
    24  }
    25  
    26  func (variables *EnvironmentVariables) UnmarshalJSON(data []byte) error {
    27  	var ccEnvVars struct {
    28  		Var map[string]types.FilteredInterface `json:"var"`
    29  	}
    30  
    31  	err := cloudcontroller.DecodeJSON(data, &ccEnvVars)
    32  
    33  	*variables = EnvironmentVariables{}
    34  
    35  	for envVarName, envVarValue := range ccEnvVars.Var {
    36  		var valueAsString string
    37  		if str, ok := envVarValue.Value.(string); ok {
    38  			valueAsString = str
    39  		} else {
    40  			bytes, err := json.Marshal(envVarValue.Value)
    41  			if err != nil {
    42  				return err
    43  			}
    44  			valueAsString = string(bytes)
    45  		}
    46  
    47  		(*variables)[envVarName] = types.FilteredString{Value: valueAsString, IsSet: true}
    48  	}
    49  
    50  	return err
    51  }
    52  
    53  // GetEnvironmentVariableGroup gets the values of a particular environment variable group.
    54  func (client *Client) GetEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName) (EnvironmentVariables, Warnings, error) {
    55  	var responseBody EnvironmentVariables
    56  
    57  	_, warnings, err := client.MakeRequest(RequestParams{
    58  		RequestName:  internal.GetEnvironmentVariableGroupRequest,
    59  		URIParams:    internal.Params{"group_name": string(group)},
    60  		ResponseBody: &responseBody,
    61  	})
    62  
    63  	return responseBody, warnings, err
    64  }
    65  
    66  // UpdateApplicationEnvironmentVariables adds/updates the user provided
    67  // environment variables on an application. A restart is required for changes
    68  // to take effect.
    69  func (client *Client) UpdateApplicationEnvironmentVariables(appGUID string, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) {
    70  	var responseBody EnvironmentVariables
    71  
    72  	_, warnings, err := client.MakeRequest(RequestParams{
    73  		RequestName:  internal.PatchApplicationEnvironmentVariablesRequest,
    74  		URIParams:    internal.Params{"app_guid": appGUID},
    75  		RequestBody:  envVars,
    76  		ResponseBody: &responseBody,
    77  	})
    78  
    79  	return responseBody, warnings, err
    80  }
    81  
    82  func (client *Client) UpdateEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) {
    83  	var responseBody EnvironmentVariables
    84  
    85  	_, warnings, err := client.MakeRequest(RequestParams{
    86  		RequestName:  internal.PatchEnvironmentVariableGroupRequest,
    87  		URIParams:    internal.Params{"group_name": string(group)},
    88  		RequestBody:  envVars,
    89  		ResponseBody: &responseBody,
    90  	})
    91  
    92  	return responseBody, warnings, err
    93  }