Skip to content

Commit

Permalink
Merge pull request #8 from thde/client
Browse files Browse the repository at this point in the history
Improve client configuration
  • Loading branch information
simonfuhrer authored Oct 3, 2020
2 parents 0f5117e + 9c378fc commit 4675320
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 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 All @@ -37,7 +35,7 @@ type Client struct {
credentials *Credentials
httpClient *http.Client
userAgent string
insecure bool
skipVerify bool

Image ImageClient
Cluster ClusterClient
Expand Down Expand Up @@ -68,19 +66,20 @@ 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)
}
}

// WithInsecure returns a ClientOption that configure the client connection to be insecure
func WithInsecure() ClientOption {
// WithSkipVerify returns a ClientOption that configure the client connection to not verify https connectins
func WithSkipVerify() ClientOption {
return func(client *Client) {
client.insecure = true
client.skipVerify = true
}
}

Expand All @@ -97,7 +96,7 @@ func NewClient(options ...ClientOption) *Client {
client.userAgent = userAgent
transCfg := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: client.insecure},
TLSClientConfig: &tls.Config{InsecureSkipVerify: client.skipVerify},
}
client.httpClient.Transport = transCfg

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 4675320

Please sign in to comment.