-
Notifications
You must be signed in to change notification settings - Fork 928
/
feature_flag.go
101 lines (80 loc) · 2.39 KB
/
feature_flag.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
94
95
96
97
98
99
100
101
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// FeatureFlag represents a Cloud Controller V3 Feature Flag.
type FeatureFlag struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
}
func (f FeatureFlag) MarshalJSON() ([]byte, error) {
var ccBodyFlag struct {
Enabled bool `json:"enabled"`
}
ccBodyFlag.Enabled = f.Enabled
return json.Marshal(ccBodyFlag)
}
func (client *Client) GetFeatureFlag(flagName string) (FeatureFlag, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetFeatureFlagRequest,
URIParams: map[string]string{"name": flagName},
})
if err != nil {
return FeatureFlag{}, nil, err
}
var ccFlag FeatureFlag
response := cloudcontroller.Response{
DecodeJSONResponseInto: &ccFlag,
}
err = client.connection.Make(request, &response)
if err != nil {
return FeatureFlag{}, response.Warnings, err
}
return ccFlag, response.Warnings, nil
}
// GetFeatureFlags lists feature flags.
func (client *Client) GetFeatureFlags() ([]FeatureFlag, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetFeatureFlagsRequest,
})
if err != nil {
return nil, nil, err
}
var fullFeatureFlagList []FeatureFlag
warnings, err := client.paginate(request, FeatureFlag{}, func(item interface{}) error {
if featureFlag, ok := item.(FeatureFlag); ok {
fullFeatureFlagList = append(fullFeatureFlagList, featureFlag)
} else {
return ccerror.UnknownObjectInListError{
Expected: FeatureFlag{},
Unexpected: item,
}
}
return nil
})
return fullFeatureFlagList, warnings, err
}
func (client *Client) UpdateFeatureFlag(flag FeatureFlag) (FeatureFlag, Warnings, error) {
bodyBytes, err := json.Marshal(flag)
if err != nil {
return FeatureFlag{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchFeatureFlagRequest,
URIParams: map[string]string{"name": flag.Name},
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return FeatureFlag{}, nil, err
}
var ccFlag FeatureFlag
response := cloudcontroller.Response{
DecodeJSONResponseInto: &ccFlag,
}
err = client.connection.Make(request, &response)
return ccFlag, response.Warnings, err
}