Skip to content

Commit

Permalink
Added resource notes (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
naitmare01 authored Nov 22, 2022
1 parent 48b39af commit 51ca528
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 10 deletions.
50 changes: 50 additions & 0 deletions docs/resources/notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Resource: pipedrive_notes

## Example Usage

```hcl
resource "pipedrive_notes" "example" {
content = "The content of the note in HTML format. Subject to sanitization on the back-end."
deal_id = "123"
}
```

## Notes example when using a tracked deal

```hcl
data "pipedrive_organizations" "example"{
id = "123"
}
resource "pipedrive_deals" "example" {
title = "Example Deals"
org_id = data.pipedrive_organizations
}
resource "pipedrive_notes" "example" {
content = "The content of the note in HTML format. Subject to sanitization on the back-end."
deal_id = pipedrive_deals.example.id
}
```

## Argument Reference

The following arguments are supported:

* `content` - (Required) The content of the note in HTML format. Subject to sanitization on the back-end.
* `deal_id` - (Required) The ID of the deal the note will be attached to.

## Attributes Reference

In addition to all argument, the following attributes are exported:

* `id` - The ID of this resource
* `add_time` - The time the note was added
* `deal_attached` - The title of the deal attached to the note
* `update_time` - The time the note was updated

## Import

Pipedrive notes can be imported using the `id` eg,

`terraform import pipedrive_notes.example 123`
2 changes: 1 addition & 1 deletion pipedrive/data_source_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func dataSourceNotes() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNotesRead,
Schema: map[string]*schema.Schema{
Schema: map[string]*schema.Schema{ //TODO add option get all notes
"id": {
Type: schema.TypeString,
Required: true,
Expand Down
27 changes: 18 additions & 9 deletions pipedrive/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Provider() *schema.Provider {
},
ResourcesMap: map[string]*schema.Resource{
"pipedrive_deals": resourceDeals(),
"pipedrive_notes": resourceNotes(),
},
DataSourcesMap: map[string]*schema.Resource{
"pipedrive_deals": dataSourceDeals(),
Expand Down Expand Up @@ -52,18 +53,26 @@ func DealsBody(d *schema.ResourceData) *strings.Reader {
resultstart := `{`
resultend := `}`

title := d.Get("title").(string)
status := d.Get("status").(string)
org_id := d.Get("org_id").(string)
title, title_true := d.GetOk("title")
status, status_true := d.GetOk("status")
org_id, org_id_true := d.GetOk("org_id")
content, content_true := d.GetOk("content")
deal_id, deal_id_true := d.GetOk("deal_id")

if title != "" {
result += `"title": "` + title + `",`
if title_true {
result += `"title": "` + title.(string) + `",`
}
if status != "" {
result += `"status": "` + status + `",`
if status_true {
result += `"status": "` + status.(string) + `",`
}
if org_id != "" {
result += `"org_id": "` + org_id + `",`
if org_id_true {
result += `"org_id": "` + org_id.(string) + `",`
}
if content_true {
result += `"content": "` + content.(string) + `",`
}
if deal_id_true {
result += `"deal_id": "` + deal_id.(string) + `",`
}

result = strings.TrimSuffix(result, ",")
Expand Down
203 changes: 203 additions & 0 deletions pipedrive/resource_notes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package pipedrive

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

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

func resourceNotes() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Required: true,
},
"deal_id": {
Type: schema.TypeString,
Required: true,
},
"add_time": {
Type: schema.TypeString,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
"deal_attached": {
Type: schema.TypeString,
Computed: true,
},
},
CreateContext: resourceNotesCreate,
ReadContext: resourceNotesRead,
UpdateContext: resourceNotesUpdate,
DeleteContext: resourceNotesDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

func resourceNotesCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := &http.Client{Timeout: 10 * time.Second}
apikey := m.(*Client).apitoken
baseurl := m.(*Client).baseurl
apiurl := fmt.Sprintf("%s/notes%s", baseurl, apikey)
payload := DealsBody(d)

// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

req, err := http.NewRequest("POST", apiurl, payload)
if err != nil {
return diag.FromErr(err)
}

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

r, err := client.Do(req)
if err != nil {
return diag.FromErr(err)
}
defer r.Body.Close()

var result map[string]any
err = json.NewDecoder(r.Body).Decode(&result)
if err != nil {
return diag.FromErr(err)
}

id := result["data"].(map[string]interface{})["id"]
id_string := fmt.Sprintf("%v", id)
add_time := result["data"].(map[string]interface{})["add_time"]
update_time := result["data"].(map[string]interface{})["update_time"]
deal_attached := result["data"].(map[string]interface{})["deal"].(map[string]interface{})["title"]

d.Set("add_time", add_time)
d.Set("update_time", update_time)
d.Set("deal_attached", deal_attached)
d.SetId(id_string)

return diags
}

func resourceNotesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := &http.Client{Timeout: 10 * time.Second}
id := d.Id()
apikey := m.(*Client).apitoken
baseurl := m.(*Client).baseurl
apiurl := fmt.Sprintf("%s/notes/%s%s", baseurl, id, apikey)

// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

req, err := http.NewRequest("GET", apiurl, nil)
if err != nil {
return diag.FromErr(err)
}

r, err := client.Do(req)
if r.StatusCode == 404 && err != nil {
d.SetId("")
diag.FromErr(err)
}
if err != nil {
return diag.FromErr(err)
}
defer r.Body.Close()

var result map[string]any
err = json.NewDecoder(r.Body).Decode(&result)
if err != nil {
return diag.FromErr(err)
}

content := result["data"].(map[string]interface{})["content"]
deal_id := result["data"].(map[string]interface{})["deal_id"]
add_time := result["data"].(map[string]interface{})["add_time"]
update_time := result["data"].(map[string]interface{})["update_time"]
deal_attached := result["data"].(map[string]interface{})["deal"].(map[string]interface{})["title"]

d.Set("content", content)
d.Set("deal_id", deal_id)
d.Set("add_time", add_time)
d.Set("update_time", update_time)
d.Set("deal_attached", deal_attached)

return diags
}

func resourceNotesUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := &http.Client{Timeout: 10 * time.Second}
id := d.Id()
apikey := m.(*Client).apitoken
baseurl := m.(*Client).baseurl
apiurl := fmt.Sprintf("%s/notes/%s%s", baseurl, id, apikey)
payload := DealsBody(d)

req, err := http.NewRequest("PUT", apiurl, payload)

if err != nil {
return diag.FromErr(err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

r, err := client.Do(req)
if err != nil {
return diag.FromErr(err)
}
defer r.Body.Close()

return resourceNotesRead(ctx, d, m)
}

func resourceNotesDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := &http.Client{Timeout: 10 * time.Second}
id := d.Id()
apikey := m.(*Client).apitoken
baseurl := m.(*Client).baseurl
apiurl := fmt.Sprintf("%s/notes/%s%s", baseurl, id, apikey)

// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
req, err := http.NewRequest("DELETE", apiurl, nil)

if err != nil {
return diag.FromErr(err)
}

r, err := client.Do(req)
if err != nil {
return diag.FromErr(err)
}
defer r.Body.Close()

var result map[string]any
err = json.NewDecoder(r.Body).Decode(&result)
if err != nil {
return diag.FromErr(err)
}

success := result["success"]

if success == false {
error_msg := result["error"]
error_msg_verbose := result["error_info"]
return diag.Errorf("%s %s", error_msg, error_msg_verbose)
}

// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
d.SetId("")

return diags
}

0 comments on commit 51ca528

Please sign in to comment.