Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#512 from zhengxiexie/cluster_http
Browse files Browse the repository at this point in the history
Add HttpGet and HttpDelete for cluster
  • Loading branch information
zhengxiexie authored Feb 2, 2024
2 parents a17fca7 + ac5c4dd commit 3a8dfd3
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/nsx/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,58 @@ func (cluster *Cluster) GetVersion() (*NsxVersion, error) {
return nsxVersion, err
}

// HttpGet sends a http GET request to the cluster, exported for use
func (cluster *Cluster) HttpGet(url string) (map[string]interface{}, error) {
ep := cluster.endpoints[0]
serverUrl := cluster.CreateServerUrl(cluster.endpoints[0].Host(), cluster.endpoints[0].Scheme())
url = fmt.Sprintf("%s/%s", serverUrl, url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error(err, "failed to create http request")
return nil, err
}
log.V(1).Info("Get url", "url", req.URL)
err = ep.UpdateHttpRequestAuth(req)
if err != nil {
log.Error(err, "http get update auth error")
return nil, err
}
ep.UpdateCAforEnvoy(req)
resp, err := ep.client.Do(req)
if err != nil {
log.Error(err, "failed to do http GET operation")
return nil, err
}
var respJson map[string]interface{}
err, _ = util.HandleHTTPResponse(resp, respJson, true)
return respJson, err
}

// HttpDelete sends a http DELETE request to the cluster, exported for use
func (cluster *Cluster) HttpDelete(url string) error {
ep := cluster.endpoints[0]
serverUrl := cluster.CreateServerUrl(cluster.endpoints[0].Host(), cluster.endpoints[0].Scheme())
url = fmt.Sprintf("%s/%s", serverUrl, url)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
log.Error(err, "failed to create http request")
return err
}
log.V(1).Info("Delete url", "url", req.URL)
err = ep.UpdateHttpRequestAuth(req)
if err != nil {
log.Error(err, "http get update auth error")
return err
}
ep.UpdateCAforEnvoy(req)
_, err = ep.client.Do(req)
if err != nil {
log.Error(err, "failed to do http DELETE operation")
return err
}
return nil
}

func (nsxVersion *NsxVersion) Validate() error {
re, _ := regexp.Compile(`^([\d]+).([\d]+).([\d]+)`)
result := re.Find([]byte(nsxVersion.NodeVersion))
Expand Down

0 comments on commit 3a8dfd3

Please sign in to comment.