github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 FindByNameAndStack(name, stack string) (buildpack models.Buildpack, apiErr error) 22 FindByNameWithNilStack(name string) (buildpack models.Buildpack, apiErr error) 23 ListBuildpacks(func(models.Buildpack) bool) error 24 Create(name string, position *int, enabled *bool, locked *bool) (createdBuildpack models.Buildpack, apiErr error) 25 Delete(buildpackGUID string) (apiErr error) 26 Update(buildpack models.Buildpack) (updatedBuildpack models.Buildpack, apiErr error) 27 } 28 29 type CloudControllerBuildpackRepository struct { 30 config coreconfig.Reader 31 gateway net.Gateway 32 } 33 34 func NewCloudControllerBuildpackRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerBuildpackRepository) { 35 repo.config = config 36 repo.gateway = gateway 37 return 38 } 39 40 func (repo CloudControllerBuildpackRepository) ListBuildpacks(cb func(models.Buildpack) bool) error { 41 return repo.gateway.ListPaginatedResources( 42 repo.config.APIEndpoint(), 43 buildpacksPath, 44 resources.BuildpackResource{}, 45 func(resource interface{}) bool { 46 return cb(resource.(resources.BuildpackResource).ToFields()) 47 }) 48 } 49 50 func (repo CloudControllerBuildpackRepository) FindByName(name string) (buildpack models.Buildpack, apiErr error) { 51 found := 0 52 53 apiErr = repo.gateway.ListPaginatedResources( 54 repo.config.APIEndpoint(), 55 fmt.Sprintf("%s?q=%s", buildpacksPath, url.QueryEscape("name:"+name)), 56 resources.BuildpackResource{}, 57 func(resource interface{}) bool { 58 found++ 59 buildpack = resource.(resources.BuildpackResource).ToFields() 60 return true 61 }) 62 63 if found == 0 { 64 apiErr = errors.NewModelNotFoundError("Buildpack", name) 65 } else if found > 1 { 66 apiErr = errors.NewAmbiguousModelError("Buildpack", name) 67 } 68 return 69 } 70 71 func (repo CloudControllerBuildpackRepository) FindByNameAndStack(name, stack string) (buildpack models.Buildpack, apiErr error) { 72 foundIt := false 73 apiErr = repo.gateway.ListPaginatedResources( 74 repo.config.APIEndpoint(), 75 fmt.Sprintf("%s?q=%s", buildpacksPath, url.QueryEscape("name:"+name+";stack:"+stack)), 76 resources.BuildpackResource{}, 77 func(resource interface{}) bool { 78 buildpack = resource.(resources.BuildpackResource).ToFields() 79 foundIt = true 80 return false 81 }) 82 83 if !foundIt { 84 apiErr = errors.NewModelNotFoundError("Buildpack", name) 85 } 86 return 87 } 88 89 func (repo CloudControllerBuildpackRepository) FindByNameWithNilStack(name string) (buildpack models.Buildpack, apiErr error) { 90 foundIt := false 91 apiErr = repo.gateway.ListPaginatedResources( 92 repo.config.APIEndpoint(), 93 fmt.Sprintf("%s?q=%s", buildpacksPath, url.QueryEscape("name:"+name)), 94 resources.BuildpackResource{}, 95 func(resource interface{}) bool { 96 buildpack = resource.(resources.BuildpackResource).ToFields() 97 if buildpack.Stack == "" { 98 foundIt = true 99 return false 100 } 101 return true 102 }) 103 104 if !foundIt { 105 apiErr = errors.NewModelNotFoundError("Buildpack", name) 106 } 107 return 108 } 109 110 func (repo CloudControllerBuildpackRepository) Create(name string, position *int, enabled *bool, locked *bool) (createdBuildpack models.Buildpack, apiErr error) { 111 entity := resources.BuildpackEntity{Name: name, Position: position, Enabled: enabled, Locked: locked} 112 body, err := json.Marshal(entity) 113 if err != nil { 114 apiErr = fmt.Errorf("%s: %s", T("Could not serialize information"), err.Error()) 115 return 116 } 117 118 resource := new(resources.BuildpackResource) 119 apiErr = repo.gateway.CreateResource(repo.config.APIEndpoint(), buildpacksPath, bytes.NewReader(body), resource) 120 if apiErr != nil { 121 return 122 } 123 124 createdBuildpack = resource.ToFields() 125 return 126 } 127 128 func (repo CloudControllerBuildpackRepository) Delete(buildpackGUID string) (apiErr error) { 129 path := fmt.Sprintf("%s/%s", buildpacksPath, buildpackGUID) 130 apiErr = repo.gateway.DeleteResource(repo.config.APIEndpoint(), path) 131 return 132 } 133 134 func (repo CloudControllerBuildpackRepository) Update(buildpack models.Buildpack) (updatedBuildpack models.Buildpack, apiErr error) { 135 path := fmt.Sprintf("%s/%s", buildpacksPath, buildpack.GUID) 136 137 entity := resources.BuildpackEntity{ 138 Name: buildpack.Name, 139 Position: buildpack.Position, 140 Enabled: buildpack.Enabled, 141 Key: "", 142 Filename: "", 143 Locked: buildpack.Locked, 144 } 145 146 body, err := json.Marshal(entity) 147 if err != nil { 148 apiErr = fmt.Errorf("%s: %s", T("Could not serialize updates."), err.Error()) 149 return 150 } 151 152 resource := new(resources.BuildpackResource) 153 apiErr = repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(body), resource) 154 if apiErr != nil { 155 return 156 } 157 158 updatedBuildpack = resource.ToFields() 159 return 160 } 161 162 const buildpacksPath = "/v2/buildpacks"