Skip to content

Commit

Permalink
fix: Spaces in resource names (#102 - #104).
Browse files Browse the repository at this point in the history
  • Loading branch information
vaerh committed Feb 16, 2023
1 parent 248409c commit 6dafa4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
18 changes: 12 additions & 6 deletions routeros/mikrotik_client_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
)

type RestClient struct {
Expand Down Expand Up @@ -52,7 +53,8 @@ func (c *RestClient) SendRequest(method crudMethod, url *URL, item MikrotikItem,
}

// https://mikrotik + /rest + /interface/vlan + ? + .id=*39
requestUrl := c.HostURL + "/rest" + url.GetRestURL()
// Escaping spaces!
requestUrl := c.HostURL + "/rest" + strings.Replace(url.GetRestURL(), " ", "%20", -1)
ColorizedDebug(c.ctx, restMethodName[method]+" request URL: "+requestUrl)

req, err := http.NewRequest(restMethodName[method], requestUrl, data)
Expand All @@ -70,16 +72,20 @@ func (c *RestClient) SendRequest(method crudMethod, url *URL, item MikrotikItem,

defer func() { _ = res.Body.Close() }()

body, _ := io.ReadAll(res.Body)

if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
var errRes errorResponse
if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil {
return fmt.Errorf("%v %v returned response - %v: '%v' (%v)",

ColorizedDebug(c.ctx, fmt.Sprintf("error response body:\n%s", body))

if err = json.Unmarshal(body, &errRes); err != nil {
return fmt.Errorf("json.Unmarshal - %v", err)
} else {
return fmt.Errorf("%v '%v' returned response code: %v, message: '%v', details: '%v'",
restMethodName[method], requestUrl, res.StatusCode, errRes.Message, errRes.Detail)
}

return fmt.Errorf("unknown error, status code: %d", res.StatusCode)
}
body, _ := io.ReadAll(res.Body)

ColorizedDebug(c.ctx, "response body: "+string(body))

Expand Down
8 changes: 4 additions & 4 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

var (
ErrorMsgPut = "An error was encountered while sending a PUT request to the API"
ErrorMsgGet = "An error was encountered while sending a GET request to the API"
ErrorMsgPatch = "An error was encountered while sending a PATCH request to the API"
ErrorMsgDelete = "An error was encountered while sending a DELETE request to the API"
ErrorMsgPut = "An error was encountered while sending a PUT request to the API: %v"
ErrorMsgGet = "An error was encountered while sending a GET request to the API: %v"
ErrorMsgPatch = "An error was encountered while sending a PATCH request to the API: %v"
ErrorMsgDelete = "An error was encountered while sending a DELETE request to the API: %v"
)

func Provider() *schema.Provider {
Expand Down
15 changes: 7 additions & 8 deletions routeros/resource_default_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ func ResourceCreate(ctx context.Context, s map[string]*schema.Schema, d *schema.

res, err := CreateItem(item, metadata.Path, m.(Client))
if err != nil {
tflog.Error(ctx, ErrorMsgPut)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgPut, err))
return diag.FromErr(err)
}

Expand Down Expand Up @@ -69,7 +68,7 @@ func ResourceCreate(ctx context.Context, s map[string]*schema.Schema, d *schema.
if m.(Client).GetTransport() == TransportAPI {
r, err := ReadItems(&ItemId{Id, res.GetID(Id)}, metadata.Path, m.(Client))
if err != nil {
tflog.Error(ctx, ErrorMsgPut)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgPut, err))
return diag.FromErr(err)
}

Expand All @@ -96,7 +95,7 @@ func ResourceRead(ctx context.Context, s map[string]*schema.Schema, d *schema.Re

res, err := ReadItems(&ItemId{metadata.IdType, d.Id()}, metadata.Path, m.(Client))
if err != nil {
tflog.Error(ctx, ErrorMsgGet)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgGet, err))
return diag.FromErr(err)
}

Expand All @@ -121,13 +120,13 @@ func ResourceUpdate(ctx context.Context, s map[string]*schema.Schema, d *schema.
if err != nil {
// There is nothing to update, because resource id not found
// or some other error.
tflog.Error(ctx, ErrorMsgPatch)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgPatch, err))
return diag.FromErr(err)
}

res, err := UpdateItem(&ItemId{Id, id}, metadata.Path, item, m.(Client))
if err != nil {
tflog.Error(ctx, ErrorMsgPatch)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgPatch, err))
return diag.FromErr(err)
}

Expand All @@ -141,7 +140,7 @@ func ResourceDelete(ctx context.Context, s map[string]*schema.Schema, d *schema.
id, err := dynamicIdLookup(metadata.IdType, metadata.Path, m.(Client), d)
if err != nil {
if err != errorNoLongerExists {
tflog.Error(ctx, ErrorMsgDelete)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgDelete, err))
return diag.FromErr(err)
}

Expand All @@ -156,7 +155,7 @@ func ResourceDelete(ctx context.Context, s map[string]*schema.Schema, d *schema.
}

if err := DeleteItem(&ItemId{Id, id}, metadata.Path, m.(Client)); err != nil {
tflog.Error(ctx, ErrorMsgDelete)
ColorizedDebug(ctx, fmt.Sprintf(ErrorMsgDelete, err))
return diag.FromErr(err)
}

Expand Down

0 comments on commit 6dafa4b

Please sign in to comment.