func (uaa UAAAuthenticator) getAuthToken(data url.Values) (apiErr *net.ApiError) { type AuthenticationResponse struct { AccessToken string `json:"access_token"` TokenType string `json:"token_type"` RefreshToken string `json:"refresh_token"` } path := fmt.Sprintf("%s/oauth/token", uaa.config.AuthorizationEndpoint) request, apiErr := uaa.gateway.NewRequest("POST", path, "Basic "+base64.StdEncoding.EncodeToString([]byte("cf:")), strings.NewReader(data.Encode())) if apiErr != nil { return } request.Header.Set("Content-Type", "application/x-www-form-urlencoded") response := new(AuthenticationResponse) apiErr = uaa.gateway.PerformRequestForJSONResponse(request, &response) if apiErr != nil { return } uaa.config.AccessToken = fmt.Sprintf("%s %s", response.TokenType, response.AccessToken) uaa.config.RefreshToken = response.RefreshToken err := uaa.configRepo.Save() if err != nil { apiErr = net.NewApiErrorWithError("Error setting configuration", err) } return }
func (repo CloudControllerApplicationRepository) Upload(app cf.Application, zipBuffer *bytes.Buffer) (apiErr *net.ApiError) { url := fmt.Sprintf("%s/v2/apps/%s/bits", repo.config.Target, app.Guid) body, boundary, err := createApplicationUploadBody(zipBuffer) if err != nil { apiErr = net.NewApiErrorWithError("Error creating upload", err) return } request, apiErr := repo.gateway.NewRequest("PUT", url, repo.config.AccessToken, body) contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary) request.Header.Set("Content-Type", contentType) if apiErr != nil { return } apiErr = repo.gateway.PerformRequest(request) return }
func (repo LoggregatorLogsRepository) RecentLogsFor(app cf.Application) (logs []*logmessage.LogMessage, apiErr *net.ApiError) { host := repo.loggregatorHostResolver(repo.config.Target) request, apiErr := repo.gateway.NewRequest("GET", fmt.Sprintf("%s/dump/?app=%s", host, app.Guid), repo.config.AccessToken, nil) if apiErr != nil { return } request.Header.Del("accept") bytes, apiErr := repo.gateway.PerformRequestForResponseBytes(request) if apiErr != nil { return } logs, err := logmessage.ParseDumpedLogMessages(bytes) if err != nil { apiErr = net.NewApiErrorWithError("Could not parse log messages", err) } return }
func (repo CloudControllerApplicationRepository) SetEnv(app cf.Application, envVars map[string]string) (apiErr *net.ApiError) { path := fmt.Sprintf("%s/v2/apps/%s", repo.config.Target, app.Guid) type setEnvReqBody struct { EnvJson map[string]string `json:"environment_json"` } body := setEnvReqBody{EnvJson: envVars} jsonBytes, err := json.Marshal(body) if err != nil { apiErr = net.NewApiErrorWithError("Error creating json", err) return } request, apiErr := repo.gateway.NewRequest("PUT", path, repo.config.AccessToken, bytes.NewReader(jsonBytes)) if apiErr != nil { return } apiErr = repo.gateway.PerformRequest(request) return }
func (repo CloudControllerServiceRepository) CreateUserProvidedServiceInstance(name string, params map[string]string) (apiErr *net.ApiError) { path := fmt.Sprintf("%s/v2/user_provided_service_instances", repo.config.Target) type RequestBody struct { Name string `json:"name"` Credentials map[string]string `json:"credentials"` SpaceGuid string `json:"space_guid"` } reqBody := RequestBody{name, params, repo.config.Space.Guid} jsonBytes, err := json.Marshal(reqBody) if err != nil { apiErr = net.NewApiErrorWithError("Error parsing response", err) return } request, apiErr := repo.gateway.NewRequest("POST", path, repo.config.AccessToken, bytes.NewReader(jsonBytes)) if apiErr != nil { return } apiErr = repo.gateway.PerformRequest(request) return }