Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cluster_id to machine, add extra request logging for debugging #7

Merged
merged 2 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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