Skip to content

Commit 1478551

Browse files
authored
Merge pull request #243 from nutanix/v1.2.0-beta
V1.2.0 beta
2 parents cf84d3f + c7356de commit 1478551

654 files changed

Lines changed: 122296 additions & 59830 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ linters:
3434
- errcheck
3535
# errocheck disabled to silience errors here: https://travis-ci.com/nutanix/terraform-provider-nutanix/jobs/131154435
3636
# Example error:
37-
# nutanix/data_source_nutanix_image.go:61:7: Error return value of `d.Set` is not checked (errcheck)
38-
# d.Set("name", utils.StringValue(resp.Status.Name))
37+
# nutanix/data_source_nutanix_image.go:61:7: Error return value of `d.Set` is not checked (errcheck)
38+
# d.Set("name", utils.StringValue(resp.Status.Name))
3939
# waiting on terraform/hashi to let us know how they want us to changle those errors
40-
# see Error return value of `d.Set` is not checked (errcheck)
40+
# see Error return value of `d.Set` is not checked (errcheck)
4141
- typecheck
4242
- gosec
4343
- gochecknoinits
@@ -56,7 +56,7 @@ issues:
5656
exclude-rules:
5757
- path: _test\.go
5858
linters:
59-
- testpackage
59+
- testpackage
6060
# part of the golangci govet package is picking up things that go vet doesn't. Seems flaky, shutting that specific error off
6161

6262
run:

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test: fmtcheck
1212
go test $(TEST) -timeout=30s -parallel=4
1313

1414
testacc: fmtcheck
15-
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m -coverprofile c.out
15+
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 200m -coverprofile c.out
1616
go tool cover -html=c.out
1717

1818
fmt:

client/client.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import (
1717
)
1818

1919
const (
20-
libraryVersion = "v3"
20+
// libraryVersion = "v3"
2121
defaultBaseURL = "https://%s/"
22-
absolutePath = "api/nutanix/" + libraryVersion
23-
userAgent = "nutanix/" + libraryVersion
24-
mediaType = "application/json"
22+
// absolutePath = "api/nutanix/" + libraryVersion
23+
// userAgent = "nutanix/" + libraryVersion
24+
mediaType = "application/json"
2525
)
2626

2727
// Client Config Configuration of the client
@@ -41,6 +41,9 @@ type Client struct {
4141

4242
// Optional function called after every successful request made.
4343
onRequestCompleted RequestCompletionCallback
44+
45+
// absolutePath: for example api/nutanix/v3
46+
AbsolutePath string
4447
}
4548

4649
// RequestCompletionCallback defines the type of the request callback function
@@ -59,7 +62,14 @@ type Credentials struct {
5962
}
6063

6164
// NewClient returns a new Nutanix API client.
62-
func NewClient(credentials *Credentials) (*Client, error) {
65+
func NewClient(credentials *Credentials, userAgent string, absolutePath string) (*Client, error) {
66+
if userAgent == "" {
67+
return nil, fmt.Errorf("userAgent argument must be passed")
68+
}
69+
if absolutePath == "" {
70+
return nil, fmt.Errorf("absolutePath argument must be passed")
71+
}
72+
6373
transCfg := &http.Transport{
6474
// nolint:gas
6575
TLSClientConfig: &tls.Config{InsecureSkipVerify: credentials.Insecure}, // ignore expired SSL certificates
@@ -85,7 +95,7 @@ func NewClient(credentials *Credentials) (*Client, error) {
8595
return nil, err
8696
}
8797

88-
c := &Client{credentials, httpClient, baseURL, userAgent, nil, nil}
98+
c := &Client{credentials, httpClient, baseURL, userAgent, nil, nil, absolutePath}
8999

90100
if credentials.SessionAuth {
91101
log.Printf("[DEBUG] Using session_auth\n")
@@ -117,7 +127,7 @@ func NewClient(credentials *Credentials) (*Client, error) {
117127

118128
// NewRequest creates a request
119129
func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body interface{}) (*http.Request, error) {
120-
rel, errp := url.Parse(absolutePath + urlStr)
130+
rel, errp := url.Parse(c.AbsolutePath + urlStr)
121131
if errp != nil {
122132
return nil, errp
123133
}
@@ -156,7 +166,7 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int
156166

157167
// NewUploadRequest Handles image uploads for image service
158168
func (c *Client) NewUploadRequest(ctx context.Context, method, urlStr string, body []byte) (*http.Request, error) {
159-
rel, errp := url.Parse(absolutePath + urlStr)
169+
rel, errp := url.Parse(c.AbsolutePath + urlStr)
160170
if errp != nil {
161171
return nil, errp
162172
}
@@ -211,6 +221,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error
211221
_, err = io.Copy(w, resp.Body)
212222
if err != nil {
213223
fmt.Printf("Error io.Copy %s", err)
224+
214225
return err
215226
}
216227
} else {
@@ -251,7 +262,6 @@ func CheckResponse(r *http.Response) error {
251262
rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf))
252263

253264
r.Body = rdr2
254-
255265
// if has entities -> return nil
256266
// if has message_list -> check_error["state"]
257267
// if has status -> check_error["status.state"]
@@ -264,9 +274,9 @@ func CheckResponse(r *http.Response) error {
264274
if err != nil {
265275
return fmt.Errorf("unmarshalling error response %s", err)
266276
}
277+
log.Print("[DEBUG] after json.Unmarshal")
267278

268279
errRes := &ErrorResponse{}
269-
270280
if status, ok := res["status"]; ok {
271281
_, sok := status.(string)
272282
if sok {
@@ -280,14 +290,25 @@ func CheckResponse(r *http.Response) error {
280290
return nil
281291
}
282292

293+
log.Print("[DEBUG] after bunch of switch cases")
283294
if err != nil {
284295
return err
285296
}
297+
log.Print("[DEBUG] first nil check")
286298

299+
// karbon error check
300+
if messageInfo, ok := res["message_info"]; ok {
301+
return fmt.Errorf("error: %s", messageInfo)
302+
}
303+
if message, ok := res["message"]; ok {
304+
log.Print(message)
305+
return fmt.Errorf("error: %s", message)
306+
}
287307
if errRes.State != "ERROR" {
288308
return nil
289309
}
290310

311+
log.Print("[DEBUG] after errRes.State")
291312
pretty, _ := json.MarshalIndent(errRes, "", " ")
292313
return fmt.Errorf("error: %s", string(pretty))
293314
}
@@ -319,6 +340,7 @@ func (r *ErrorResponse) Error() string {
319340
for key, value := range r.MessageList {
320341
err = fmt.Sprintf("%d: {message:%s, reason:%s }", key, value.Message, value.Reason)
321342
}
343+
322344
return err
323345
}
324346

@@ -327,5 +349,6 @@ func fillStruct(data map[string]interface{}, result interface{}) error {
327349
if err != nil {
328350
return err
329351
}
352+
330353
return json.Unmarshal(j, result)
331354
}

client/client_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ import (
1212
"testing"
1313
)
1414

15+
const (
16+
testLibraryVersion = "v3"
17+
testAbsolutePath = "api/nutanix/" + testLibraryVersion
18+
testUserAgent = "nutanix/" + testLibraryVersion
19+
)
20+
1521
func setup() (*http.ServeMux, *Client, *httptest.Server) {
1622
mux := http.NewServeMux()
1723
server := httptest.NewServer(mux)
1824

19-
client, _ := NewClient(&Credentials{"", "username", "password", "", "", true, false, ""})
25+
client, _ := NewClient(&Credentials{"", "username", "password", "", "", true, false, ""}, testUserAgent, testAbsolutePath)
2026
client.BaseURL, _ = url.Parse(server.URL)
2127

2228
return mux, client, server
2329
}
2430

2531
func TestNewClient(t *testing.T) {
26-
c, err := NewClient(&Credentials{"foo.com", "username", "password", "", "", true, false, ""})
32+
c, err := NewClient(&Credentials{"foo.com", "username", "password", "", "", true, false, ""}, testUserAgent, testAbsolutePath)
2733

2834
if err != nil {
2935
t.Errorf("Unexpected Error: %v", err)
@@ -35,19 +41,19 @@ func TestNewClient(t *testing.T) {
3541
t.Errorf("NewClient BaseURL = %v, expected %v", c.BaseURL, expectedURL)
3642
}
3743

38-
if c.UserAgent != userAgent {
39-
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, userAgent)
44+
if c.UserAgent != testUserAgent {
45+
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, testUserAgent)
4046
}
4147
}
4248

4349
func TestNewRequest(t *testing.T) {
44-
c, err := NewClient(&Credentials{"foo.com", "username", "password", "", "", true, false, ""})
50+
c, err := NewClient(&Credentials{"foo.com", "username", "password", "", "", true, false, ""}, testUserAgent, testAbsolutePath)
4551

4652
if err != nil {
4753
t.Errorf("Unexpected Error: %v", err)
4854
}
4955

50-
inURL, outURL := "/foo", fmt.Sprintf(defaultBaseURL+absolutePath+"/foo", "foo.com")
56+
inURL, outURL := "/foo", fmt.Sprintf(defaultBaseURL+testAbsolutePath+"/foo", "foo.com")
5157
inBody, outBody := map[string]interface{}{"name": "bar"}, `{"name":"bar"}`+"\n"
5258

5359
req, _ := c.NewRequest(context.TODO(), http.MethodPost, inURL, inBody)
@@ -320,6 +326,7 @@ func TestClient_NewUploadRequest(t *testing.T) {
320326
got, err := c.NewUploadRequest(tt.args.ctx, tt.args.method, tt.args.urlStr, tt.args.body)
321327
if (err != nil) != tt.wantErr {
322328
t.Errorf("Client.NewUploadRequest() error = %v, wantErr %v", err, tt.wantErr)
329+
323330
return
324331
}
325332
if !reflect.DeepEqual(got, tt.want) {

client/karbon/karbon_api.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package karbon
2+
3+
import (
4+
"github.com/terraform-providers/terraform-provider-nutanix/client"
5+
)
6+
7+
const (
8+
absolutePath = "karbon"
9+
userAgent = "nutanix"
10+
)
11+
12+
// Client manages the V3 API
13+
type Client struct {
14+
client *client.Client
15+
Cluster ClusterService
16+
PrivateRegistry PrivateRegistryService
17+
}
18+
19+
// NewKarbonAPIClient return a client to operate Karbon resources
20+
func NewKarbonAPIClient(credentials client.Credentials) (*Client, error) {
21+
c, err := client.NewClient(&credentials, userAgent, absolutePath)
22+
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
f := &Client{
28+
client: c,
29+
Cluster: ClusterOperations{
30+
client: c,
31+
},
32+
PrivateRegistry: PrivateRegistryOperations{
33+
client: c,
34+
},
35+
}
36+
37+
return f, nil
38+
}

0 commit comments

Comments
 (0)