-
Notifications
You must be signed in to change notification settings - Fork 8
/
cloud_builder_client.go
73 lines (59 loc) · 2.16 KB
/
cloud_builder_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2023 Broadcom. All Rights Reserved.
// SPDX-License-Identifier: MPL-2.0
package api_client
import (
"crypto/tls"
openapiclient "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
vcfclient "github.com/vmware/vcf-sdk-go/client"
"net/http"
)
// CloudBuilderClient is an API client that can execute the APIs of the CloudBuilder appliance.
// Supports only HTTP Basic Authentication.
type CloudBuilderClient struct {
username string
password string
cloudBuilderUrl string
ApiClient *vcfclient.VcfClient
allowUnverifiedTls bool
}
func NewCloudBuilderClient(username, password, url string, allowUnverifiedTls bool) *CloudBuilderClient {
result := &CloudBuilderClient{
username: username,
password: password,
cloudBuilderUrl: url,
allowUnverifiedTls: allowUnverifiedTls,
}
result.init()
return result
}
func (cloudBuilderClient *CloudBuilderClient) init() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: cloudBuilderClient.allowUnverifiedTls}
cfg := vcfclient.DefaultTransportConfig()
openApiClient := openapiclient.New(cloudBuilderClient.cloudBuilderUrl, cfg.BasePath, cfg.Schemes)
openApiClient.Transport = cloudBuilderClient.newTransport()
// create the API client, with the transport
cloudBuilderOpenApiClient := vcfclient.New(openApiClient, strfmt.Default)
// save the client for later use
cloudBuilderClient.ApiClient = cloudBuilderOpenApiClient
}
func (cloudBuilderClient *CloudBuilderClient) newTransport() *cloudBuilderCustomHttpTransport {
return &cloudBuilderCustomHttpTransport{
originalTransport: http.DefaultTransport,
cloudBuilderClient: cloudBuilderClient,
}
}
type cloudBuilderCustomHttpTransport struct {
originalTransport http.RoundTripper
cloudBuilderClient *CloudBuilderClient
}
func (c *cloudBuilderCustomHttpTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.SetBasicAuth(c.cloudBuilderClient.username, c.cloudBuilderClient.password)
r.Header.Add("Content-Type", "application/json")
resp, err := c.originalTransport.RoundTrip(r)
if err != nil {
return nil, err
}
return resp, nil
}