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