Skip to content

Commit

Permalink
Use HTTP client instead of cf curl to fetch Process stats
Browse files Browse the repository at this point in the history
- Since the `/v3/processes/:guid/stats endpoint now may set an
`X-Cf-Warnings` header (see
cloudfoundry/cloud_controller_ng#2227), the
CLI may return the contents of this header along with the endpoint's
regular JSON response which causes parsing errors in the BARAs
- Logged cloudfoundry/cli#2164 with the CLI to
make it so that header isn't printed to `stdout` for this command, but
until then we can just switch to use a regular http client to fetch this
endpoint

[#177518468](https://www.pivotaltracker.com/story/show/177518468)

Authored-by: Tim Downey <[email protected]>
  • Loading branch information
tcdowney committed May 3, 2021
1 parent 0c4a863 commit 0058ef1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion helpers/v3_helpers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func GetAuthToken() string {
session := cf.Cf("oauth-token")
session := cf.CfRedact("bearer", "oauth-token")
bytes := session.Wait().Out.Contents()
return strings.TrimSpace(string(bytes))
}
34 changes: 30 additions & 4 deletions helpers/v3_helpers/deployment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package v3_helpers

import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/cloudfoundry-incubator/cf-test-helpers/cf"

Expand Down Expand Up @@ -89,17 +92,27 @@ func WaitUntilDeploymentReachesStatus(deploymentGUID, statusValue, statusReason
}

func GetRunningInstancesStats(processGUID string) int {
processPath := fmt.Sprintf("/v3/processes/%s/stats", processGUID)
session := cf.Cf("curl", "-f", processPath).Wait()
processStatsURL := fmt.Sprintf("%s%s/v3/processes/%s/stats", Config.Protocol(), Config.GetApiEndpoint(), processGUID)

client := buildHTTPClient()
req, err := http.NewRequest("GET", processStatsURL, nil)
req.Header.Add("Authorization", GetAuthToken())
resp, err := client.Do(req)
Expect(err).NotTo(HaveOccurred())

instancesJSON := struct {
Resources []struct {
Type string `json:"type"`
State string `json:"state"`
} `json:"resources"`
}{}

bytes := session.Wait().Out.Contents()
err := json.Unmarshal(bytes, &instancesJSON)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))

err = json.Unmarshal(body, &instancesJSON)
Expect(err).NotTo(HaveOccurred())
numRunning := 0

Expand All @@ -110,3 +123,16 @@ func GetRunningInstancesStats(processGUID string) int {
}
return numRunning
}

func buildHTTPClient() *http.Client {
var client *http.Client
if Config.GetSkipSSLValidation() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
} else {
client = &http.Client{}
}
return client
}

0 comments on commit 0058ef1

Please sign in to comment.