Skip to content

Commit

Permalink
Add field sys domain path (#57)
Browse files Browse the repository at this point in the history
* added some logging

* added missing field to model: sys_domain_path

* added sys_domain_path to the documentation

---------

Co-authored-by: Andrei Predoiu <[email protected]>
  • Loading branch information
marcosdiez and Andrei-Predoiu authored Oct 12, 2023
1 parent ad5fb11 commit 70e5a13
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/table_row.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A row in a SN table
- `sys_created_by` (String)
- `sys_created_on` (String)
- `sys_domain` (String)
- `sys_domain_path` (String)
- `sys_mod_count` (String)
- `sys_tags` (String)
- `sys_updated_by` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/table_row.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A row in a SN table
- `sys_created_by` (String)
- `sys_created_on` (String)
- `sys_domain` (String)
- `sys_domain_path` (String)
- `sys_id` (String)
- `sys_mod_count` (String)
- `sys_tags` (String)
Expand Down
18 changes: 15 additions & 3 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/models"
"io"
"net/http"
"strings"
"sync"
"time"

"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/models"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Client holds the client info for netbox
type Client struct {
ctx context.Context
url string
user string
pass string
Expand All @@ -23,21 +27,23 @@ var httpClient *http.Client
var once sync.Once

// NewClient creates common settings
func NewClient(url, user, pass string) *Client {
func NewClient(ctx context.Context, url, user, pass string) *Client {
once.Do(func() {
httpClient = &http.Client{
Timeout: time.Duration(30) * time.Second,
}
})

return &Client{
ctx: ctx,
url: url,
user: user,
pass: pass,
}
}

func (client *Client) GetTableRow(tableID string, params map[string]interface{}) (*models.ParsedResult, error) {
tflog.Info(client.ctx, fmt.Sprintf("GetTableRow: tableID=%s, params=%s", tableID, params))
if params == nil || len(params) == 0 {
return nil, fmt.Errorf("sys_id and params cannot be both empty")
}
Expand All @@ -51,7 +57,10 @@ func (client *Client) GetTableRow(tableID string, params map[string]interface{})
if err != nil {
return nil, err
}
return parseRawListData(rawData)
result, err := parseRawListData(rawData)
tflog.Info(client.ctx, fmt.Sprintf("GetTableRow: err=%d, result=%s", err, result))
return result, err

}

func (client *Client) InsertTableRow(tableID string, tableData interface{}) (*models.ParsedResult, error) {
Expand All @@ -74,6 +83,7 @@ func (client *Client) DeleteTableRow(tableID string, sysID string) error {

func (client *Client) sendRequest(method, path string, payload interface{}, expectedStatusCodes ...int) (value *[]byte, err error) {
url := client.url + "/api/now" + path
tflog.Info(client.ctx, fmt.Sprintf("sendRequest: url=%s, method=%s", url, method))

b := new(bytes.Buffer)
err = json.NewEncoder(b).Encode(payload)
Expand Down Expand Up @@ -105,11 +115,13 @@ func (client *Client) sendRequest(method, path string, payload interface{}, expe
if len(expectedStatusCodes) > 0 {
for _, code := range expectedStatusCodes {
if resp.StatusCode == code {
tflog.Info(client.ctx, fmt.Sprintf("sendRequest response: %d %s", resp.StatusCode, string(body)))
return &body, nil
}
}
return nil, fmt.Errorf("[ERROR] unexpected status code got: %v expected: %v \n %v \n %v", resp.StatusCode, expectedStatusCodes, string(body), url)
}
tflog.Info(client.ctx, fmt.Sprintf("sendRequest response: %d %s", resp.StatusCode, string(body)))
return &body, nil
}

Expand Down
7 changes: 4 additions & 3 deletions internal/datasource/data_source_database_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package datasource
import (
"context"
"fmt"

"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/client"
"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/models"
"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/resource"
"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 @@ -33,7 +35,8 @@ func TableRowDatasource() *schema.Resource {
}
}

func tableRowRead(_ context.Context, data *schema.ResourceData, m interface{}) diag.Diagnostics {
func tableRowRead(ctx context.Context, data *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("tableRowRead: data=%+v", data))
c := m.(*client.Client)
var err error
// Warning or errors can be collected in a slice type
Expand All @@ -59,7 +62,6 @@ func tableRowRead(_ context.Context, data *schema.ResourceData, m interface{}) d
} else {
payload = map[string]interface{}{"sys_id": sysID}
}

rowData, err := c.GetTableRow(tableID.(string), payload)
if err != nil {
return diag.FromErr(err)
Expand All @@ -74,6 +76,5 @@ func tableRowRead(_ context.Context, data *schema.ResourceData, m interface{}) d
diags = append(diags, resource.ParsedResultToSchema(data, rowData)...)

data.SetId(fmt.Sprintf("%s/%s", tableID, rowData.SysData["sys_id"]))

return diags
}
6 changes: 6 additions & 0 deletions internal/models/sn_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -45,6 +46,11 @@ var DefaultSystemColumns = map[string]*schema.Schema{
Optional: true,
Computed: true,
},
"sys_domain_path": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"sys_class_name": {
Type: schema.TypeString,
Optional: true,
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"

"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/client"
"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/datasource"
"github.com/BESTSELLER/terraform-provider-servicenow-data/internal/resource"
Expand Down Expand Up @@ -79,7 +80,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
return nil, diags
}

c := client.NewClient(*host, *username, *password)
c := client.NewClient(ctx, *host, *username, *password)

return c, diags
}

0 comments on commit 70e5a13

Please sign in to comment.