Skip to content

Commit

Permalink
Merge pull request nutanix#243 from nutanix/v1.2.0-beta
Browse files Browse the repository at this point in the history
V1.2.0 beta
  • Loading branch information
marinsalinas authored Feb 1, 2021
2 parents 9cbc389 + 9858332 commit 27c6973
Show file tree
Hide file tree
Showing 654 changed files with 122,296 additions and 59,830 deletions.
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ linters:
- errcheck
# errocheck disabled to silience errors here: https://travis-ci.com/nutanix/terraform-provider-nutanix/jobs/131154435
# Example error:
# nutanix/data_source_nutanix_image.go:61:7: Error return value of `d.Set` is not checked (errcheck)
# d.Set("name", utils.StringValue(resp.Status.Name))
# nutanix/data_source_nutanix_image.go:61:7: Error return value of `d.Set` is not checked (errcheck)
# d.Set("name", utils.StringValue(resp.Status.Name))
# waiting on terraform/hashi to let us know how they want us to changle those errors
# see Error return value of `d.Set` is not checked (errcheck)
# see Error return value of `d.Set` is not checked (errcheck)
- typecheck
- gosec
- gochecknoinits
Expand All @@ -56,7 +56,7 @@ issues:
exclude-rules:
- path: _test\.go
linters:
- testpackage
- testpackage
# part of the golangci govet package is picking up things that go vet doesn't. Seems flaky, shutting that specific error off

run:
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test: fmtcheck
go test $(TEST) -timeout=30s -parallel=4

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

fmt:
Expand Down
43 changes: 33 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
)

const (
libraryVersion = "v3"
// libraryVersion = "v3"
defaultBaseURL = "https://%s/"
absolutePath = "api/nutanix/" + libraryVersion
userAgent = "nutanix/" + libraryVersion
mediaType = "application/json"
// absolutePath = "api/nutanix/" + libraryVersion
// userAgent = "nutanix/" + libraryVersion
mediaType = "application/json"
)

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

// Optional function called after every successful request made.
onRequestCompleted RequestCompletionCallback

// absolutePath: for example api/nutanix/v3
AbsolutePath string
}

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

// NewClient returns a new Nutanix API client.
func NewClient(credentials *Credentials) (*Client, error) {
func NewClient(credentials *Credentials, userAgent string, absolutePath string) (*Client, error) {
if userAgent == "" {
return nil, fmt.Errorf("userAgent argument must be passed")
}
if absolutePath == "" {
return nil, fmt.Errorf("absolutePath argument must be passed")
}

transCfg := &http.Transport{
// nolint:gas
TLSClientConfig: &tls.Config{InsecureSkipVerify: credentials.Insecure}, // ignore expired SSL certificates
Expand All @@ -85,7 +95,7 @@ func NewClient(credentials *Credentials) (*Client, error) {
return nil, err
}

c := &Client{credentials, httpClient, baseURL, userAgent, nil, nil}
c := &Client{credentials, httpClient, baseURL, userAgent, nil, nil, absolutePath}

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

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

// NewUploadRequest Handles image uploads for image service
func (c *Client) NewUploadRequest(ctx context.Context, method, urlStr string, body []byte) (*http.Request, error) {
rel, errp := url.Parse(absolutePath + urlStr)
rel, errp := url.Parse(c.AbsolutePath + urlStr)
if errp != nil {
return nil, errp
}
Expand Down Expand Up @@ -211,6 +221,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error
_, err = io.Copy(w, resp.Body)
if err != nil {
fmt.Printf("Error io.Copy %s", err)

return err
}
} else {
Expand Down Expand Up @@ -251,7 +262,6 @@ func CheckResponse(r *http.Response) error {
rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf))

r.Body = rdr2

// if has entities -> return nil
// if has message_list -> check_error["state"]
// if has status -> check_error["status.state"]
Expand All @@ -264,9 +274,9 @@ func CheckResponse(r *http.Response) error {
if err != nil {
return fmt.Errorf("unmarshalling error response %s", err)
}
log.Print("[DEBUG] after json.Unmarshal")

errRes := &ErrorResponse{}

if status, ok := res["status"]; ok {
_, sok := status.(string)
if sok {
Expand All @@ -280,14 +290,25 @@ func CheckResponse(r *http.Response) error {
return nil
}

log.Print("[DEBUG] after bunch of switch cases")
if err != nil {
return err
}
log.Print("[DEBUG] first nil check")

// karbon error check
if messageInfo, ok := res["message_info"]; ok {
return fmt.Errorf("error: %s", messageInfo)
}
if message, ok := res["message"]; ok {
log.Print(message)
return fmt.Errorf("error: %s", message)
}
if errRes.State != "ERROR" {
return nil
}

log.Print("[DEBUG] after errRes.State")
pretty, _ := json.MarshalIndent(errRes, "", " ")
return fmt.Errorf("error: %s", string(pretty))
}
Expand Down Expand Up @@ -319,6 +340,7 @@ func (r *ErrorResponse) Error() string {
for key, value := range r.MessageList {
err = fmt.Sprintf("%d: {message:%s, reason:%s }", key, value.Message, value.Reason)
}

return err
}

Expand All @@ -327,5 +349,6 @@ func fillStruct(data map[string]interface{}, result interface{}) error {
if err != nil {
return err
}

return json.Unmarshal(j, result)
}
19 changes: 13 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import (
"testing"
)

const (
testLibraryVersion = "v3"
testAbsolutePath = "api/nutanix/" + testLibraryVersion
testUserAgent = "nutanix/" + testLibraryVersion
)

func setup() (*http.ServeMux, *Client, *httptest.Server) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)

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

return mux, client, server
}

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

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

if c.UserAgent != userAgent {
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, userAgent)
if c.UserAgent != testUserAgent {
t.Errorf("NewClient UserAgent = %v, expected %v", c.UserAgent, testUserAgent)
}
}

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

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

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

req, _ := c.NewRequest(context.TODO(), http.MethodPost, inURL, inBody)
Expand Down Expand Up @@ -320,6 +326,7 @@ func TestClient_NewUploadRequest(t *testing.T) {
got, err := c.NewUploadRequest(tt.args.ctx, tt.args.method, tt.args.urlStr, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("Client.NewUploadRequest() error = %v, wantErr %v", err, tt.wantErr)

return
}
if !reflect.DeepEqual(got, tt.want) {
Expand Down
38 changes: 38 additions & 0 deletions client/karbon/karbon_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package karbon

import (
"github.com/terraform-providers/terraform-provider-nutanix/client"
)

const (
absolutePath = "karbon"
userAgent = "nutanix"
)

// Client manages the V3 API
type Client struct {
client *client.Client
Cluster ClusterService
PrivateRegistry PrivateRegistryService
}

// NewKarbonAPIClient return a client to operate Karbon resources
func NewKarbonAPIClient(credentials client.Credentials) (*Client, error) {
c, err := client.NewClient(&credentials, userAgent, absolutePath)

if err != nil {
return nil, err
}

f := &Client{
client: c,
Cluster: ClusterOperations{
client: c,
},
PrivateRegistry: PrivateRegistryOperations{
client: c,
},
}

return f, nil
}
Loading

0 comments on commit 27c6973

Please sign in to comment.