func (cmd CreateBuildpack) createBuildpack(buildpackName string, c *cli.Context) (buildpack models.Buildpack, apiErr errors.Error) { position, err := strconv.Atoi(c.Args()[2]) if err != nil { apiErr = errors.NewErrorWithMessage("Invalid position. %s", err.Error()) return } enabled := c.Bool("enable") disabled := c.Bool("disable") if enabled && disabled { apiErr = errors.NewErrorWithMessage("Cannot specify both enabled and disabled.") return } var enableOption *bool = nil if enabled { enableOption = &enabled } if disabled { disabled = false enableOption = &disabled } buildpack, apiErr = cmd.buildpackRepo.Create(buildpackName, &position, enableOption, nil) return }
func (repo CloudControllerApplicationBitsRepository) UploadApp(appGuid string, appDir string, cb func(path string, zipSize, fileCount uint64)) (apiErr errors.Error) { fileutils.TempDir("apps", func(uploadDir string, err error) { if err != nil { apiErr = errors.NewErrorWithMessage(err.Error()) return } var presentResourcesJson []byte repo.sourceDir(appDir, func(sourceDir string, sourceErr error) { if sourceErr != nil { err = sourceErr return } presentResourcesJson, err = repo.copyUploadableFiles(sourceDir, uploadDir) }) if err != nil { apiErr = errors.NewErrorWithMessage("%s", err) return } fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if err != nil { apiErr = errors.NewErrorWithMessage("%s", err.Error()) return } err = repo.zipper.Zip(uploadDir, zipFile) if err != nil { apiErr = errors.NewErrorWithError("Error zipping application", err) return } stat, err := zipFile.Stat() if err != nil { apiErr = errors.NewErrorWithError("Error zipping application", err) return } cb(appDir, uint64(stat.Size()), app_files.CountFiles(uploadDir)) apiErr = repo.uploadBits(appGuid, zipFile, presentResourcesJson) if apiErr != nil { return } }) }) return }
func (repo CloudControllerUserRepository) getAuthEndpoint() (string, errors.Error) { uaaEndpoint := repo.config.UaaEndpoint() if uaaEndpoint == "" { return "", errors.NewErrorWithMessage("UAA endpoint missing from config file") } return uaaEndpoint, nil }
func (gateway Gateway) waitForJob(jobUrl, accessToken string, timeout time.Duration) (apiErr errors.Error) { startTime := time.Now() for true { if time.Since(startTime) > timeout { apiErr = errors.NewErrorWithMessage("Error: timed out waiting for async job '%s' to finish", jobUrl) return } var request *Request request, apiErr = gateway.NewRequest("GET", jobUrl, accessToken, nil) response := &JobResponse{} _, apiErr = gateway.PerformRequestForJSONResponse(request, response) if apiErr != nil { return } switch response.Entity.Status { case JOB_FINISHED: return case JOB_FAILED: apiErr = errors.NewError("Job failed", JOB_FAILED) return } accessToken = request.HttpReq.Header.Get("Authorization") time.Sleep(gateway.PollingThrottle) } return }
func (repo CloudControllerServiceRepository) DeleteService(instance models.ServiceInstance) (apiErr errors.Error) { if len(instance.ServiceBindings) > 0 { return errors.NewErrorWithMessage("Cannot delete service instance, apps are still bound to it") } path := fmt.Sprintf("%s/v2/service_instances/%s", repo.config.ApiEndpoint(), instance.Guid) return repo.gateway.DeleteResource(path, repo.config.AccessToken()) }
func (repo *FakeBuildpackBitsRepository) UploadBuildpack(buildpack models.Buildpack, dir string) errors.Error { if repo.UploadBuildpackErr { return errors.NewErrorWithMessage("Invalid buildpack") } repo.UploadBuildpackPath = dir return repo.UploadBuildpackApiResponse }
func (repo RemoteEndpointRepository) GetCloudControllerEndpoint() (endpoint string, apiErr errors.Error) { if repo.config.ApiEndpoint() == "" { apiErr = errors.NewErrorWithMessage("Target endpoint missing from config file") return } endpoint = repo.config.ApiEndpoint() return }
func (repo *FakeApplicationRepository) Update(appGuid string, params models.AppParams) (updatedApp models.Application, apiErr errors.Error) { repo.UpdateAppGuid = appGuid repo.UpdateParams = params updatedApp = repo.UpdateAppResult if repo.UpdateErr { apiErr = errors.NewErrorWithMessage("Error updating app.") } return }
func (repo RemoteEndpointRepository) GetUAAEndpoint() (endpoint string, apiErr errors.Error) { if repo.config.AuthorizationEndpoint() == "" { apiErr = errors.NewErrorWithMessage("UAA endpoint missing from config file") return } endpoint = strings.Replace(repo.config.AuthorizationEndpoint(), "login", "uaa", 1) return }
func (repo *FakeRouteRepository) FindByHost(host string) (route models.Route, apiErr errors.Error) { repo.FindByHostHost = host if repo.FindByHostErr { apiErr = errors.NewErrorWithMessage("Route not found") } route = repo.FindByHostRoute return }
func (repo CloudControllerUserRepository) checkSpaceRole(userGuid, spaceGuid, role string) (fullPath string, apiErr errors.Error) { rolePath, found := spaceRoleToPathMap[role] if !found { apiErr = errors.NewErrorWithMessage("Invalid Role %s", role) } fullPath = fmt.Sprintf("%s/v2/spaces/%s/%s/%s", repo.config.ApiEndpoint(), spaceGuid, rolePath, userGuid) return }
func (repo CloudControllerPasswordRepository) UpdatePassword(old string, new string) errors.Error { uaaEndpoint := repo.config.UaaEndpoint() if uaaEndpoint == "" { return errors.NewErrorWithMessage("UAA endpoint missing from config file") } url := fmt.Sprintf("%s/Users/%s/password", uaaEndpoint, repo.config.UserGuid()) body := fmt.Sprintf(`{"password":"******","oldPassword":"******"}`, new, old) return repo.gateway.UpdateResource(url, repo.config.AccessToken(), strings.NewReader(body)) }
func (repo *FakeRouteRepository) ListRoutes(cb func(models.Route) bool) (apiErr errors.Error) { if repo.ListErr { return errors.NewErrorWithMessage("WHOOPSIE") } for _, route := range repo.Routes { if !cb(route) { break } } return }
func (repo *FakeQuotaRepository) FindByName(name string) (quota models.QuotaFields, apiErr errors.Error) { repo.FindByNameName = name quota = repo.FindByNameQuota if repo.FindByNameNotFound { apiErr = errors.NewModelNotFoundError("Org", name) } if repo.FindByNameErr { apiErr = errors.NewErrorWithMessage("Error finding quota") } return }
func (cmd Target) setOrganization(orgName string) error { // setting an org necessarily invalidates any space you had previously targeted cmd.config.SetOrganizationFields(models.OrganizationFields{}) cmd.config.SetSpaceFields(models.SpaceFields{}) org, apiErr := cmd.orgRepo.FindByName(orgName) if apiErr != nil { return errors.NewErrorWithMessage("Could not target org.\n%s", apiErr.Error()) } cmd.config.SetOrganizationFields(org.OrganizationFields) return nil }
func (repo *FakeRouteRepository) CreateInSpace(host, domainGuid, spaceGuid string) (createdRoute models.Route, apiErr errors.Error) { repo.CreateInSpaceHost = host repo.CreateInSpaceDomainGuid = domainGuid repo.CreateInSpaceSpaceGuid = spaceGuid if repo.CreateInSpaceErr { apiErr = errors.NewErrorWithMessage("Error") } else { createdRoute = repo.CreateInSpaceCreatedRoute } return }
func (repo *FakeDomainRepository) FindByName(name string) (domain models.DomainFields, apiErr errors.Error) { repo.FindByNameName = name domain = repo.FindByNameDomain if repo.FindByNameNotFound { apiErr = errors.NewModelNotFoundError("Domain", name) } if repo.FindByNameErr { apiErr = errors.NewErrorWithMessage("Error finding domain") } return }
func (repo *FakeServiceBrokerRepo) ListServiceBrokers(callback func(broker models.ServiceBroker) bool) errors.Error { for _, broker := range repo.ServiceBrokers { if !callback(broker) { break } } if repo.ListErr { return errors.NewErrorWithMessage("Error finding service brokers") } else { return nil } }
func (repo RemoteEndpointRepository) GetLoggregatorEndpoint() (endpoint string, apiErr errors.Error) { if repo.config.LoggregatorEndpoint() == "" { if repo.config.ApiEndpoint() == "" { apiErr = errors.NewErrorWithMessage("Loggregator endpoint missing from config file") } else { endpoint = defaultLoggregatorEndpoint(repo.config.ApiEndpoint()) } } else { endpoint = repo.config.LoggregatorEndpoint() } return }
func (repo *FakeApplicationBitsRepository) UploadApp(appGuid, dir string, cb func(path string, zipSize, fileCount uint64)) (apiErr errors.Error) { repo.UploadedDir = dir repo.UploadedAppGuid = appGuid if repo.UploadAppErr { apiErr = errors.NewErrorWithMessage("Error uploading app") return } cb(repo.CallbackPath, repo.CallbackZipSize, repo.CallbackFileCount) return }
func (cmd Target) setSpace(spaceName string) error { cmd.config.SetSpaceFields(models.SpaceFields{}) if !cmd.config.HasOrganization() { return errors.New("An org must be targeted before targeting a space") } space, apiErr := cmd.spaceRepo.FindByName(spaceName) if apiErr != nil { return errors.NewErrorWithMessage("Unable to access space %s.\n%s", spaceName, apiErr.Error()) } cmd.config.SetSpaceFields(space.SpaceFields) return nil }
func (repo CloudControllerStackRepository) FindByName(name string) (stack models.Stack, apiErr errors.Error) { path := fmt.Sprintf("%s/v2/stacks?q=%s", repo.config.ApiEndpoint(), url.QueryEscape("name:"+name)) stacks, apiErr := repo.findAllWithPath(path) if apiErr != nil { return } if len(stacks) == 0 { apiErr = errors.NewErrorWithMessage("Stack '%s' not found", name) return } stack = stacks[0] return }
func (repo *FakeRouteRepository) FindByHostAndDomain(host, domain string) (route models.Route, apiErr errors.Error) { repo.FindByHostAndDomainHost = host repo.FindByHostAndDomainDomain = domain if repo.FindByHostAndDomainErr { apiErr = errors.NewErrorWithMessage("Error finding Route") } if repo.FindByHostAndDomainNotFound { apiErr = errors.NewModelNotFoundError("Org", host+"."+domain) } route = repo.FindByHostAndDomainRoute return }
func (repo *FakeApplicationRepository) Read(name string) (app models.Application, apiErr errors.Error) { repo.ReadName = name app = repo.ReadApp if repo.ReadErr { apiErr = errors.NewErrorWithMessage("Error finding app by name.") } if repo.ReadAuthErr { apiErr = errors.NewHttpError(401, "", "", "1000", "Authentication failed.") } if repo.ReadNotFound { apiErr = errors.NewModelNotFoundError("App", name) } return }
func (auth *FakeAuthenticationRepository) Authenticate(credentials map[string]string) (apiErr errors.Error) { auth.AuthenticateArgs.Credentials = credentials if auth.AuthError { apiErr = errors.NewErrorWithMessage("Error authenticating.") return } if auth.AccessToken == "" { auth.AccessToken = "BEARER some_access_token" } auth.Config.SetAccessToken(auth.AccessToken) auth.Config.SetRefreshToken(auth.RefreshToken) return }
func (repo *FakeServiceRepo) FindInstanceByName(name string) (instance models.ServiceInstance, apiErr errors.Error) { repo.FindInstanceByNameName = name if repo.FindInstanceByNameMap != nil && repo.FindInstanceByNameMap.Has(name) { instance = repo.FindInstanceByNameMap.Get(name).(models.ServiceInstance) } else { instance = repo.FindInstanceByNameServiceInstance } if repo.FindInstanceByNameErr { apiErr = errors.NewErrorWithMessage("Error finding instance") } if repo.FindInstanceByNameNotFound { apiErr = errors.NewModelNotFoundError("Service instance", name) } return }
func (uaa UAAAuthenticationRepository) Authenticate(credentials map[string]string) (apiErr errors.Error) { data := url.Values{ "grant_type": {"password"}, "scope": {""}, } for key, val := range credentials { data[key] = []string{val} } apiErr = uaa.getAuthToken(data) switch response := apiErr.(type) { case errors.HttpError: if response.StatusCode() == 401 { apiErr = errors.NewErrorWithMessage("Password is incorrect, please try again.") } } return }
func (repo CloudControllerUserRepository) setOrUnsetOrgRole(verb, userGuid, orgGuid, role string) (apiErr errors.Error) { rolePath, found := orgRoleToPathMap[role] if !found { apiErr = errors.NewErrorWithMessage("Invalid Role %s", role) return } path := fmt.Sprintf("%s/v2/organizations/%s/%s/%s", repo.config.ApiEndpoint(), orgGuid, rolePath, userGuid) request, apiErr := repo.ccGateway.NewRequest(verb, path, repo.config.AccessToken(), nil) if apiErr != nil { return } apiErr = repo.ccGateway.PerformRequest(request) if apiErr != nil { return } return }
func (repo *FakeSpaceRepository) FindByName(name string) (space models.Space, apiErr errors.Error) { repo.FindByNameName = name var foundSpace bool = false for _, someSpace := range repo.Spaces { if name == someSpace.Name { foundSpace = true space = someSpace break } } if repo.FindByNameErr || !foundSpace { apiErr = errors.NewErrorWithMessage("Error finding space by name.") } if repo.FindByNameNotFound { apiErr = errors.NewModelNotFoundError("Space", name) } return }
func (repo *FakeOrgRepository) FindByName(name string) (org models.Organization, apiErr errors.Error) { repo.FindByNameName = name var foundOrg bool = false for _, anOrg := range repo.Organizations { if name == anOrg.Name { foundOrg = true org = anOrg break } } if repo.FindByNameErr || !foundOrg { apiErr = errors.NewErrorWithMessage("Error finding organization by name.") } if repo.FindByNameNotFound { apiErr = errors.NewModelNotFoundError("Org", name) } return }