github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/environment_variable_groups/environment_variable_groups.go (about)

     1  package environment_variable_groups
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	"github.com/cloudfoundry/cli/cf/net"
    11  )
    12  
    13  type EnvironmentVariableGroupsRepository interface {
    14  	ListRunning() (variables []models.EnvironmentVariable, apiErr error)
    15  	ListStaging() (variables []models.EnvironmentVariable, apiErr error)
    16  	SetStaging(string) error
    17  	SetRunning(string) error
    18  }
    19  
    20  type CloudControllerEnvironmentVariableGroupsRepository struct {
    21  	config  core_config.Reader
    22  	gateway net.Gateway
    23  }
    24  
    25  func NewCloudControllerEnvironmentVariableGroupsRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerEnvironmentVariableGroupsRepository) {
    26  	repo.config = config
    27  	repo.gateway = gateway
    28  	return
    29  }
    30  
    31  func (repo CloudControllerEnvironmentVariableGroupsRepository) ListRunning() (variables []models.EnvironmentVariable, apiErr error) {
    32  	var raw_response interface{}
    33  	url := fmt.Sprintf("%s/v2/config/environment_variable_groups/running", repo.config.ApiEndpoint())
    34  	apiErr = repo.gateway.GetResource(url, &raw_response)
    35  	if apiErr != nil {
    36  		return
    37  	}
    38  
    39  	variables, err := repo.marshalToEnvironmentVariables(raw_response)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return variables, nil
    45  }
    46  
    47  func (repo CloudControllerEnvironmentVariableGroupsRepository) ListStaging() (variables []models.EnvironmentVariable, apiErr error) {
    48  	var raw_response interface{}
    49  	url := fmt.Sprintf("%s/v2/config/environment_variable_groups/staging", repo.config.ApiEndpoint())
    50  	apiErr = repo.gateway.GetResource(url, &raw_response)
    51  	if apiErr != nil {
    52  		return
    53  	}
    54  
    55  	variables, err := repo.marshalToEnvironmentVariables(raw_response)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return variables, nil
    61  }
    62  
    63  func (repo CloudControllerEnvironmentVariableGroupsRepository) SetStaging(staging_vars string) error {
    64  	return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), "/v2/config/environment_variable_groups/staging", strings.NewReader(staging_vars))
    65  }
    66  
    67  func (repo CloudControllerEnvironmentVariableGroupsRepository) SetRunning(running_vars string) error {
    68  	return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), "/v2/config/environment_variable_groups/running", strings.NewReader(running_vars))
    69  }
    70  
    71  func (repo CloudControllerEnvironmentVariableGroupsRepository) marshalToEnvironmentVariables(raw_response interface{}) ([]models.EnvironmentVariable, error) {
    72  	var variables []models.EnvironmentVariable
    73  	for key, value := range raw_response.(map[string]interface{}) {
    74  		stringvalue, err := repo.convertValueToString(value)
    75  		if err != nil {
    76  			return nil, err
    77  		}
    78  		variable := models.EnvironmentVariable{Name: key, Value: stringvalue}
    79  		variables = append(variables, variable)
    80  	}
    81  	return variables, nil
    82  }
    83  
    84  func (repo CloudControllerEnvironmentVariableGroupsRepository) convertValueToString(value interface{}) (string, error) {
    85  	stringvalue, ok := value.(string)
    86  	if !ok {
    87  		floatvalue, ok := value.(float64)
    88  		if !ok {
    89  			return "", errors.New(fmt.Sprintf("Attempted to read environment variable value of unknown type: %#v", value))
    90  		}
    91  		stringvalue = fmt.Sprintf("%d", int(floatvalue))
    92  	}
    93  	return stringvalue, nil
    94  }