github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/environment_variables.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     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.FilteredString `json:"var"`
    29  	}
    30  
    31  	err := cloudcontroller.DecodeJSON(data, &ccEnvVars)
    32  	*variables = EnvironmentVariables(ccEnvVars.Var)
    33  
    34  	return err
    35  }
    36  
    37  // UpdateApplicationEnvironmentVariables adds/updates the user provided
    38  // environment variables on an application. A restart is required for changes
    39  // to take effect.
    40  func (client *Client) UpdateApplicationEnvironmentVariables(appGUID string, envVars EnvironmentVariables) (EnvironmentVariables, Warnings, error) {
    41  	bodyBytes, err := json.Marshal(envVars)
    42  	if err != nil {
    43  		return EnvironmentVariables{}, nil, err
    44  	}
    45  
    46  	request, err := client.newHTTPRequest(requestOptions{
    47  		URIParams:   internal.Params{"app_guid": appGUID},
    48  		RequestName: internal.PatchApplicationEnvironmentVariablesRequest,
    49  		Body:        bytes.NewReader(bodyBytes),
    50  	})
    51  	if err != nil {
    52  		return EnvironmentVariables{}, nil, err
    53  	}
    54  
    55  	var responseEnvVars EnvironmentVariables
    56  	response := cloudcontroller.Response{
    57  		DecodeJSONResponseInto: &responseEnvVars,
    58  	}
    59  	err = client.connection.Make(request, &response)
    60  	return responseEnvVars, response.Warnings, err
    61  }