Skip to content

Commit

Permalink
Merge pull request #16 from BESTSELLER/deletion_improvements
Browse files Browse the repository at this point in the history
Deletion improvements
  • Loading branch information
Andrei-Predoiu authored Nov 8, 2022
2 parents a0532b6 + 37a5ecb commit 34de2f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
secret-injector: bestsellerit/[email protected].7
secret-injector: bestsellerit/[email protected].8
cci-common: bestsellerit/[email protected]

commands:
Expand Down
14 changes: 8 additions & 6 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (client *Client) InsertTableRow(tableID string, tableData interface{}) (*mo

func (client *Client) DeleteTableRow(tableID string, sysID string) error {
rowPath := fmt.Sprintf("/table/%s/%s", tableID, sysID)
_, err := client.sendRequest(http.MethodDelete, rowPath, nil, 204)
_, err := client.sendRequest(http.MethodDelete, rowPath, nil, 204, 404)
if err != nil {
return err
}
return nil
}

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

b := new(bytes.Buffer)
Expand Down Expand Up @@ -100,12 +100,14 @@ func (client *Client) sendRequest(method, path string, payload interface{}, expe
return nil, err
}

if expectedStatusCode != 0 {
if resp.StatusCode != expectedStatusCode {
return nil, fmt.Errorf("[ERROR] unexpected status code got: %v expected: %v \n %v \n %v", resp.StatusCode, expectedStatusCode, string(body), url)
if len(expectedStatusCodes) > 0 {
for _, code := range expectedStatusCodes {
if resp.StatusCode == code {
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)
}

return &body, nil
}

Expand Down
16 changes: 9 additions & 7 deletions internal/resource/resource_database_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ func tableRowRead(_ context.Context, data *schema.ResourceData, m interface{}) d
if err != nil {
return diag.FromErr(err)
}

parseResultDiag := ParsedResultToSchema(data, rowData)
diags = append(diags, parseResultDiag...)
if len(parseResultDiag) > 0 {
return diags
if len(rowData.SysData) == 0 {
data.SetId("")
} else {
parseResultDiag := ParsedResultToSchema(data, rowData)
diags = append(diags, parseResultDiag...)
if len(parseResultDiag) > 0 {
return diags
}
data.SetId(fmt.Sprintf("%s/%s", tableID, sysID))
}
data.SetId(fmt.Sprintf("%s/%s", tableID, sysID))

return diags
}

Expand Down

0 comments on commit 34de2f9

Please sign in to comment.