-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_flags.go
61 lines (48 loc) · 1.59 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
package feature_flags
import (
"fmt"
"strings"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type FeatureFlagRepository interface {
List() ([]models.FeatureFlag, error)
FindByName(string) (models.FeatureFlag, error)
Update(string, bool) error
}
type CloudControllerFeatureFlagRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerFeatureFlagRepository(config core_config.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))
}