Skip to content

Commit

Permalink
feat: add cluster_id to machine, add extra request logging for debugg…
Browse files Browse the repository at this point in the history
…ing (#7)
  • Loading branch information
jaredscheib authored Jun 28, 2020
1 parent 484b3b1 commit 99aaa27
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/terraform-provider-paperspace/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ func (c *ClientConfig) Client() (paperspaceClient PaperspaceClient, err error) {
transport.Set("ps_client_name", "terraform-provider-paperspace")
client.Transport = transport

log.Printf("[DEBUG] Paperspace client transport %v", transport)

paperspaceClient = PaperspaceClient{
APIKey: c.APIKey,
APIHost: c.APIHost,
Region: c.Region,
HttpClient: client,
}

log.Printf("[DEBUG] Paperspace client config %v", paperspaceClient)

return paperspaceClient, nil
}

Expand Down
9 changes: 8 additions & 1 deletion src/terraform-provider-paperspace/datasource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -79,6 +80,11 @@ func dataSourceNetworkRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return fmt.Errorf("Error constructing GetNetworks request: %s", err)
}
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
return fmt.Errorf("Error constructing GetNetwork request: %s", err)
}
log.Print("[INFO] Request:", string(requestDump))

resp, err := paperspaceClient.HttpClient.Do(req)
if err != nil {
Expand All @@ -92,7 +98,8 @@ func dataSourceNetworkRead(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error reading paperspace network: networks not found")
}
if statusCode != 200 {
return fmt.Errorf("Error reading paperspace network: Response: %s", resp.Body)
responseDump, _ := httputil.DumpResponse(resp, true)
return fmt.Errorf("Error reading paperspace network: Response: %s", string(responseDump))
}

var f interface{}
Expand Down
9 changes: 8 additions & 1 deletion src/terraform-provider-paperspace/datasource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -87,6 +88,11 @@ func dataSourceTemplateRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return fmt.Errorf("Error constructing GetTemplates request: %s", err)
}
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
return fmt.Errorf("Error constructing GetTemplates request: %s", err)
}
log.Print("[INFO] Request:", string(requestDump))

resp, err := paperspaceClient.HttpClient.Do(req)
if err != nil {
Expand All @@ -100,7 +106,8 @@ func dataSourceTemplateRead(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error reading paperspace template: templates not found")
}
if statusCode != 200 {
return fmt.Errorf("Error reading paperspace template: Response: %s", resp.Body)
responseDump, _ := httputil.DumpResponse(resp, true)
return fmt.Errorf("Error reading paperspace template: Response: %s", string(responseDump))
}

var f interface{}
Expand Down
11 changes: 9 additions & 2 deletions src/terraform-provider-paperspace/datasource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -71,6 +72,11 @@ func dataSourceUserRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return fmt.Errorf("Error constructing GetUsers request: %s", err)
}
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
return fmt.Errorf("Error constructing GetUsers request: %s", err)
}
log.Print("[INFO] Request:", string(requestDump))

resp, err := paperspaceClient.HttpClient.Do(req)
if err != nil {
Expand All @@ -84,15 +90,16 @@ func dataSourceUserRead(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error reading paperspace user: users not found")
}
if statusCode != 200 {
return fmt.Errorf("Error reading paperspace user: Response: %s", resp.Body)
responseDump, _ := httputil.DumpResponse(resp, true)
return fmt.Errorf("Error reading paperspace user: Response: %s", string(responseDump))
}

var f interface{}
err = json.NewDecoder(resp.Body).Decode(&f)
if err != nil {
return fmt.Errorf("Error decoding GetUsers response body: %s", err)
}
LogHttpResponse("paperspace dataSourceTemplateRead", req.URL, resp, f, err)
LogHttpResponse("paperspace dataSourceUserRead", req.URL, resp, f, err)

mpa := f.([]interface{})
if len(mpa) > 1 {
Expand Down
9 changes: 8 additions & 1 deletion src/terraform-provider-paperspace/resource_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func resourceMachineCreate(d *schema.ResourceData, m interface{}) error {
body.AppendAsIfSet(d, "user_id", "userId")
body.AppendAsIfSet(d, "team_id", "teamId")
body.AppendAsIfSet(d, "script_id", "scriptId")
body.AppendAsIfSet(d, "network_id", "networkId")
body.AppendAsIfSet(d, "cluster_id", "clusterId")

// fields not tested when this project was picked back up for https://github.com/Paperspace/terraform-provider-paperspace/pull/3
body.AppendAsIfSet(d, "network_id", "networkId")
body.AppendIfSet(d, "email")
body.AppendIfSet(d, "password")
body.AppendAsIfSet(d, "firstname", "firstName")
Expand Down Expand Up @@ -87,6 +88,7 @@ func resourceMachineCreate(d *schema.ResourceData, m interface{}) error {
SetResDataFrom(d, body, "team_id", "teamId")
SetResDataFrom(d, body, "script_id", "scriptId")
SetResDataFrom(d, body, "dt_last_run", "dtLastRun")
SetResDataFrom(d, body, "cluster_id", "clusterId")

d.SetId(id)

Expand Down Expand Up @@ -128,6 +130,7 @@ func resourceMachineRead(d *schema.ResourceData, m interface{}) error {
SetResDataFrom(d, mp, "team_id", "teamId")
SetResDataFrom(d, mp, "script_id", "scriptId")
SetResDataFrom(d, mp, "dt_last_run", "dtLastRun")
SetResDataFrom(d, mp, "cluster_id", "clusterId")

return nil
}
Expand Down Expand Up @@ -310,6 +313,10 @@ func resourceMachine() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"cluster_id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Expand Down
9 changes: 8 additions & 1 deletion src/terraform-provider-paperspace/resource_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httputil"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand Down Expand Up @@ -38,6 +39,11 @@ func resourceScriptCreate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return fmt.Errorf("Error constructing CreateScript request: %s", err)
}
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
return fmt.Errorf("Error constructing CreateScript request: %s", err)
}
log.Print("[INFO] Request:", string(requestDump))

resp, err := paperspaceClient.HttpClient.Do(req)
if err != nil {
Expand All @@ -48,7 +54,8 @@ func resourceScriptCreate(d *schema.ResourceData, m interface{}) error {
statusCode := resp.StatusCode
log.Printf("[INFO] paperspace resourceScriptCreate StatusCode: %v", statusCode)
if statusCode != 200 {
return fmt.Errorf("Error creating paperspace script: Response: %s", resp.Body)
responseDump, _ := httputil.DumpResponse(resp, true)
return fmt.Errorf("Error reading paperspace script: Response: %s", string(responseDump))
}

var f interface{}
Expand Down

0 comments on commit 99aaa27

Please sign in to comment.