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