github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/feature_flags/feature_flags.go (about) 1 package feature_flags 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/cloudfoundry/cli/cf/configuration/core_config" 8 "github.com/cloudfoundry/cli/cf/models" 9 "github.com/cloudfoundry/cli/cf/net" 10 ) 11 12 type FeatureFlagRepository interface { 13 List() ([]models.FeatureFlag, error) 14 FindByName(string) (models.FeatureFlag, error) 15 Update(string, bool) error 16 } 17 18 type CloudControllerFeatureFlagRepository struct { 19 config core_config.Reader 20 gateway net.Gateway 21 } 22 23 func NewCloudControllerFeatureFlagRepository(config core_config.Reader, gateway net.Gateway) CloudControllerFeatureFlagRepository { 24 return CloudControllerFeatureFlagRepository{ 25 config: config, 26 gateway: gateway, 27 } 28 } 29 30 func (repo CloudControllerFeatureFlagRepository) List() ([]models.FeatureFlag, error) { 31 flags := []models.FeatureFlag{} 32 apiError := repo.gateway.GetResource( 33 fmt.Sprintf("%s/v2/config/feature_flags", repo.config.ApiEndpoint()), 34 &flags) 35 36 if apiError != nil { 37 return nil, apiError 38 } 39 40 return flags, nil 41 } 42 43 func (repo CloudControllerFeatureFlagRepository) FindByName(name string) (models.FeatureFlag, error) { 44 flag := models.FeatureFlag{} 45 apiError := repo.gateway.GetResource( 46 fmt.Sprintf("%s/v2/config/feature_flags/%s", repo.config.ApiEndpoint(), name), 47 &flag) 48 49 if apiError != nil { 50 return models.FeatureFlag{}, apiError 51 } 52 53 return flag, nil 54 } 55 56 func (repo CloudControllerFeatureFlagRepository) Update(flag string, set bool) error { 57 url := fmt.Sprintf("/v2/config/feature_flags/%s", flag) 58 body := fmt.Sprintf(`{"enabled": %v}`, set) 59 60 return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), url, strings.NewReader(body)) 61 }