github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/featureflags/feature_flags.go (about)

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