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

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/url"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/resources"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/errors"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/models"
    14  	"code.cloudfoundry.org/cli/cf/net"
    15  )
    16  
    17  //go:generate counterfeiter . BuildpackRepository
    18  
    19  type BuildpackRepository interface {
    20  	FindByName(name string) (buildpack models.Buildpack, apiErr error)
    21  	ListBuildpacks(func(models.Buildpack) bool) error
    22  	Create(name string, position *int, enabled *bool, locked *bool) (createdBuildpack models.Buildpack, apiErr error)
    23  	Delete(buildpackGUID string) (apiErr error)
    24  	Update(buildpack models.Buildpack) (updatedBuildpack models.Buildpack, apiErr error)
    25  }
    26  
    27  type CloudControllerBuildpackRepository struct {
    28  	config  coreconfig.Reader
    29  	gateway net.Gateway
    30  }
    31  
    32  func NewCloudControllerBuildpackRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerBuildpackRepository) {
    33  	repo.config = config
    34  	repo.gateway = gateway
    35  	return
    36  }
    37  
    38  func (repo CloudControllerBuildpackRepository) ListBuildpacks(cb func(models.Buildpack) bool) error {
    39  	return repo.gateway.ListPaginatedResources(
    40  		repo.config.APIEndpoint(),
    41  		buildpacksPath,
    42  		resources.BuildpackResource{},
    43  		func(resource interface{}) bool {
    44  			return cb(resource.(resources.BuildpackResource).ToFields())
    45  		})
    46  }
    47  
    48  func (repo CloudControllerBuildpackRepository) FindByName(name string) (buildpack models.Buildpack, apiErr error) {
    49  	foundIt := false
    50  	apiErr = repo.gateway.ListPaginatedResources(
    51  		repo.config.APIEndpoint(),
    52  		fmt.Sprintf("%s?q=%s", buildpacksPath, url.QueryEscape("name:"+name)),
    53  		resources.BuildpackResource{},
    54  		func(resource interface{}) bool {
    55  			buildpack = resource.(resources.BuildpackResource).ToFields()
    56  			foundIt = true
    57  			return false
    58  		})
    59  
    60  	if !foundIt {
    61  		apiErr = errors.NewModelNotFoundError("Buildpack", name)
    62  	}
    63  	return
    64  }
    65  
    66  func (repo CloudControllerBuildpackRepository) Create(name string, position *int, enabled *bool, locked *bool) (createdBuildpack models.Buildpack, apiErr error) {
    67  	entity := resources.BuildpackEntity{Name: name, Position: position, Enabled: enabled, Locked: locked}
    68  	body, err := json.Marshal(entity)
    69  	if err != nil {
    70  		apiErr = fmt.Errorf("%s: %s", T("Could not serialize information"), err.Error())
    71  		return
    72  	}
    73  
    74  	resource := new(resources.BuildpackResource)
    75  	apiErr = repo.gateway.CreateResource(repo.config.APIEndpoint(), buildpacksPath, bytes.NewReader(body), resource)
    76  	if apiErr != nil {
    77  		return
    78  	}
    79  
    80  	createdBuildpack = resource.ToFields()
    81  	return
    82  }
    83  
    84  func (repo CloudControllerBuildpackRepository) Delete(buildpackGUID string) (apiErr error) {
    85  	path := fmt.Sprintf("%s/%s", buildpacksPath, buildpackGUID)
    86  	apiErr = repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
    87  	return
    88  }
    89  
    90  func (repo CloudControllerBuildpackRepository) Update(buildpack models.Buildpack) (updatedBuildpack models.Buildpack, apiErr error) {
    91  	path := fmt.Sprintf("%s/%s", buildpacksPath, buildpack.GUID)
    92  
    93  	entity := resources.BuildpackEntity{
    94  		Name:     buildpack.Name,
    95  		Position: buildpack.Position,
    96  		Enabled:  buildpack.Enabled,
    97  		Key:      "",
    98  		Filename: "",
    99  		Locked:   buildpack.Locked,
   100  	}
   101  
   102  	body, err := json.Marshal(entity)
   103  	if err != nil {
   104  		apiErr = fmt.Errorf("%s: %s", T("Could not serialize updates."), err.Error())
   105  		return
   106  	}
   107  
   108  	resource := new(resources.BuildpackResource)
   109  	apiErr = repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(body), resource)
   110  	if apiErr != nil {
   111  		return
   112  	}
   113  
   114  	updatedBuildpack = resource.ToFields()
   115  	return
   116  }
   117  
   118  const buildpacksPath = "/v2/buildpacks"