forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_flags.go
63 lines (49 loc) · 1.63 KB
/
feature_flags.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
package featureflags
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
)
//go:generate counterfeiter . FeatureFlagRepository
type FeatureFlagRepository interface {
List() ([]models.FeatureFlag, error)
FindByName(string) (models.FeatureFlag, error)
Update(string, bool) error
}
type CloudControllerFeatureFlagRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerFeatureFlagRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerFeatureFlagRepository {
return CloudControllerFeatureFlagRepository{
config: config,
gateway: gateway,
}
}
func (repo CloudControllerFeatureFlagRepository) List() ([]models.FeatureFlag, error) {
flags := []models.FeatureFlag{}
apiError := repo.gateway.GetResource(
fmt.Sprintf("%s/v2/config/feature_flags", repo.config.APIEndpoint()),
&flags)
if apiError != nil {
return nil, apiError
}
return flags, nil
}
func (repo CloudControllerFeatureFlagRepository) FindByName(name string) (models.FeatureFlag, error) {
flag := models.FeatureFlag{}
apiError := repo.gateway.GetResource(
fmt.Sprintf("%s/v2/config/feature_flags/%s", repo.config.APIEndpoint(), name),
&flag)
if apiError != nil {
return models.FeatureFlag{}, apiError
}
return flag, nil
}
func (repo CloudControllerFeatureFlagRepository) Update(flag string, set bool) error {
url := fmt.Sprintf("/v2/config/feature_flags/%s", flag)
body := fmt.Sprintf(`{"enabled": %v}`, set)
return repo.gateway.UpdateResource(repo.config.APIEndpoint(), url, strings.NewReader(body))
}