Skip to content

Commit

Permalink
allow custom port / insecure scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
thde committed Sep 21, 2020
1 parent c4b9a57 commit 9c378fc
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (

const (
libraryVersion = "v3"
defaulthttps = "https://%s"
defaultBaseURL = "%s:9440/"
absolutePath = "api/nutanix/" + libraryVersion
defaultV2BaseURL = "PrismGateway/services/rest/v2.0"
userAgent = "nutanix/" + "cmd.Version"
Expand Down Expand Up @@ -68,12 +66,13 @@ func WithCredentials(cred *Credentials) ClientOption {
func WithEndpoint(endpoint string) ClientOption {
return func(client *Client) {
passedURL := endpoint

// Required because url.Parse returns an empty string for the hostname if there was no schema
if !strings.HasPrefix(passedURL, "https://") {
if !strings.HasPrefix(passedURL, "https://") || !strings.HasPrefix(passedURL, "http://") {
passedURL = "https://" + passedURL
}
client.baseURL, _ = url.Parse(fmt.Sprintf(defaultBaseURL, passedURL))

client.baseURL, _ = url.Parse(passedURL)
}
}

Expand Down Expand Up @@ -233,8 +232,8 @@ func (c *Client) NewV3PERequest(ctx context.Context, method string, clusterUUID
if err != nil {
return nil, err
}
apiEndpoint := fmt.Sprintf(defaulthttps, cluster.Spec.Resources.Network.ExternalIP)
urlEndpoint, _ := url.Parse(fmt.Sprintf(defaultBaseURL, apiEndpoint))

urlEndpoint, _ := url.Parse(fmt.Sprintf("%s://%s:%s", c.baseURL.Scheme, cluster.Spec.Resources.Network.ExternalIP, c.baseURL.Port()))

url := urlEndpoint.ResolveReference(rel)
return c.newV3Request(ctx, method, url, body)
Expand Down Expand Up @@ -282,8 +281,8 @@ func (c *Client) NewV2PERequest(ctx context.Context, method string, clusterUUID
if err != nil {
return nil, err
}
apiEndpoint := fmt.Sprintf(defaulthttps, cluster.Spec.Resources.Network.ExternalIP)
urlEndpoint, _ := url.Parse(fmt.Sprintf(defaultBaseURL, apiEndpoint))

urlEndpoint, _ := url.Parse(fmt.Sprintf("%s://%s:%s", c.baseURL.Scheme, cluster.Spec.Resources.Network.ExternalIP, c.baseURL.Port()))

url := urlEndpoint.ResolveReference(rel)
return c.newV2Request(ctx, method, url, body)
Expand Down

0 comments on commit 9c378fc

Please sign in to comment.