Skip to content

Commit

Permalink
Merge pull request nutanix#46 from nutanix/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
crizstian authored May 14, 2018
2 parents b254b27 + cffff89 commit 45615e2
Show file tree
Hide file tree
Showing 23 changed files with 2,550 additions and 1,104 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,67 @@ We'll be working with HashiCorp as code stabilizes to upstream this properly, at

## Install Dependencies

* Terraform

### For developing or build from source

#### Golang

https://github.com/golang/go

#### Dep: Go dependency management tool

On MacOS you can install or upgrade to the latest released version with Homebrew:

```sh
$ brew install dep
$ brew upgrade dep
```

On other platforms you can use the `install.sh` script:

```sh
$ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
```

It will install into your `$GOPATH/bin` directory by default or any other directory you specify using the `INSTALL_DIRECTORY` environment variable.

If your platform is not supported, you'll need to build it manually or let the team (go/dep team) know and we'll consider adding your platform
to the release builds.

More Info: https://github.com/golang/dep

## Install from source

## Install from package

## Building/Developing Provider

Clone repository to: `$GOPATH/src/github.com/terraform-providers/terraform-provider-nutanix`

```sh
$ mkdir $GOPATH/src/github.com/terraform-providers && cd "$_" #if you already created it only change directory

$ git clone https://github.com/nutanix/terraform-provider-nutanix.git
```

Enter the provider directory and build the provider

```sh
$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-nutanix
$ dep ensure #install dependencies
$ cd cmd
$ make build
```

This will create a binary file `terraform-provider-nutanix` you can copy to your terraform specific project.

Alternative build: with our demo

```sh
$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-nutanix
$ dep ensure #install dependencies
$ go build -o examples/terraform-provider-nutanix
$ cd examples
$ terraform init #to try out our demo
```
20 changes: 11 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"io/ioutil"
"net/http"
"net/url"

"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

const (
Expand Down Expand Up @@ -51,7 +49,7 @@ type Credentials struct {
func NewClient(credentials *Credentials) (*Client, error) {

transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // ignore expired SSL certificates
TLSClientConfig: &tls.Config{InsecureSkipVerify: credentials.Insecure}, // ignore expired SSL certificates
}

httpClient := http.DefaultClient
Expand Down Expand Up @@ -100,7 +98,6 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int
req.Header.Add("Authorization", "Basic "+
base64.StdEncoding.EncodeToString([]byte(c.Credentials.Username+":"+c.Credentials.Password)))

// log.Printf("REQUEST BODY \n")
// utils.PrintToJSON(req, "REQUEST BODY")

// requestDump, err := httputil.DumpRequestOut(req, true)
Expand Down Expand Up @@ -156,8 +153,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error
if err != nil {
return err
}

utils.PrintToJSON(v, "RESPONSE BODY")
// utils.PrintToJSON(v, "RESPONSE BODY")
}
}

Expand All @@ -171,6 +167,11 @@ func CheckResponse(r *http.Response) error {
}

data, err := ioutil.ReadAll(r.Body)

if err != nil {
return err
}

res := &ErrorResponse{}
err = json.Unmarshal(data, res)
if err != nil {
Expand All @@ -181,15 +182,16 @@ func CheckResponse(r *http.Response) error {
return fmt.Errorf("Error: %s", string(pretty))
}

// ErrorResponse ...
//ErrorResponse ...
type ErrorResponse struct {
ApiVersion string `json:"api_version"`
APIVersion string `json:"api_version"`
Code int64 `json:"code"`
Kind string `json:"kind"`
MessageList []MessageResource `json:"message_list"`
State string `json:"state"`
}

//MessageResource ...
type MessageResource struct {

// Custom key-value details relevant to the status.
Expand All @@ -205,7 +207,7 @@ type MessageResource struct {
func (r *ErrorResponse) Error() string {
err := ""
for key, value := range r.MessageList {
err = fmt.Sprintf("%s: %s", key, value)
err = fmt.Sprintf("%d: {message:%s, reason:%s }", key, value.Message, value.Reason)
}
return err
}
40 changes: 17 additions & 23 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)

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

Expand All @@ -36,7 +36,7 @@ func teardown() {

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

if err != nil {
t.Errorf("Unexpected Error: %v", err)
Expand All @@ -55,7 +55,7 @@ func TestNewClient(t *testing.T) {

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

if err != nil {
t.Errorf("Unexpected Error: %v", err)
Expand All @@ -79,7 +79,12 @@ func TestNewRequest(t *testing.T) {
}

func TestErrorResponse_Error(t *testing.T) {
err := ErrorResponse{Message: map[string]string{"name": "This field may not be blank."}}
messageResource := MessageResource{Message: "This field may not be blank."}
messageList := make([]MessageResource, 1)
messageList[0] = messageResource

err := ErrorResponse{MessageList: messageList}

if err.Error() == "" {
t.Errorf("Expected non-empty ErrorResponse.Error()")
}
Expand All @@ -89,45 +94,34 @@ func TestGetResponse(t *testing.T) {
res := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(`{"name": "This field may not be blank."}`)),
Body: ioutil.NopCloser(strings.NewReader(`{"api_version": "3.0", "code": 400, "kind": "error", "message_list": [{"message": "This field may not be blank."}], "state": "none"}`)),
}

err := CheckResponse(res).(*ErrorResponse)
err := CheckResponse(res)

if err == nil {
t.Fatal("Expected error response.")
}

expected := &ErrorResponse{
Message: map[string]string{
"name": "This field may not be blank.",
},
}

if !reflect.DeepEqual(err, expected) {
t.Errorf("Error = %#v, expected %#v", err, expected)
if !strings.Contains(fmt.Sprint(err), "This field may not be blank.") {
t.Errorf("Error = %#v, expected %#v", err, "This field may not be blank.")
}
}

func TestCheckResponse(t *testing.T) {
res := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(`{"name": "This field may not be blank."}`)),
Body: ioutil.NopCloser(strings.NewReader(`{"api_version": "3.0", "code": 400, "kind": "error", "message_list": [{"message": "This field may not be blank."}], "state": "none"}`)),
}
err := CheckResponse(res).(*ErrorResponse)
err := CheckResponse(res)

if err == nil {
t.Fatalf("Expected error response.")
}

expected := &ErrorResponse{
Message: map[string]string{
"name": "This field may not be blank.",
},
}
if !reflect.DeepEqual(err, expected) {
t.Errorf("Error = %#v, expected %#v", err, expected)
if !strings.Contains(fmt.Sprint(err), "This field may not be blank.") {
t.Errorf("Error = %#v, expected %#v", err, "This field may not be blank.")
}
}

Expand Down
Loading

0 comments on commit 45615e2

Please sign in to comment.