Skip to content

Commit

Permalink
Implement goal resource
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmc committed Nov 17, 2020
1 parent 3010f3a commit 17a7274
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 5 deletions.
29 changes: 29 additions & 0 deletions docs/resources/goal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
page_title: "plausible_goal Resource - terraform-provider-plausible"
subcategory: ""
description: |-
---

# Resource `plausible_goal`





## Schema

### Required

- **site_id** (String, Required) The domain of the site to create the goal for.

### Optional

- **event_name** (String, Optional) Custom event E.g. `Signup`
- **page_path** (String, Optional) Page path event. E.g. `/success`

### Read-only

- **id** (String, Read-only) The goal ID


2 changes: 1 addition & 1 deletion docs/resources/shared_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ description: |-

### Optional

- **id** (String, Optional) The ID of this resource.
- **password** (String, Optional) Add a password or leave it blank so anyone with the link can see the stats.

### Read-only

- **id** (String, Read-only) The shared link ID
- **link** (String, Read-only) Shared link


5 changes: 1 addition & 4 deletions docs/resources/site.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ description: |-
- **domain** (String, Required)
- **timezone** (String, Required)

### Optional

- **id** (String, Optional) The ID of this resource.

### Read-only

- **id** (String, Read-only) The site ID
- **javascript_snippet** (String, Read-only) Include this snippet in the <head> of your website.


1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func New(version string) func() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"plausible_site": resourceSite(),
"plausible_shared_link": resourceSharedLink(),
"plausible_goal": resourceGoal(),
},
}

Expand Down
118 changes: 118 additions & 0 deletions internal/provider/resource_goal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package provider

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mcalpinefree/terraform-provider-plausible/plausibleclient"
)

func resourceGoal() *schema.Resource {
return &schema.Resource{
Create: resourceGoalCreate,
Read: resourceGoalRead,
Delete: resourceGoalDelete,

Schema: map[string]*schema.Schema{
"id": {
Description: "The goal ID",
Type: schema.TypeString,
Computed: true,
},
"site_id": {
Description: "The domain of the site to create the goal for.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"page_path": {
Description: "Page path event. E.g. `/success`",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"event_name"},
},
"event_name": {
Description: "Custom event E.g. `Signup`",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"page_path"},
},
},
}
}

func resourceGoalCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*apiClient)
domain := d.Get("site_id").(string)

var goalType plausibleclient.GoalType
goal := ""
if v, ok := d.GetOk("page_path"); ok {
goalType = plausibleclient.PagePath
goal = v.(string)
} else if v, ok := d.GetOk("event_name"); ok {
goalType = plausibleclient.EventName
goal = v.(string)
} else {
return fmt.Errorf("page_path or event_name needs to be defined")
}

resp, err := client.plausibleClient.CreateGoal(domain, goalType, goal)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%d", resp.ID))

return resourceGoalSetResourceData(resp, d)
}

func resourceGoalSetResourceData(g *plausibleclient.Goal, d *schema.ResourceData) error {
d.Set("site_id", g.Domain)
if g.PagePath != nil {
d.Set("page_path", *g.PagePath)
} else if g.EventName != nil {
d.Set("event_name", *g.EventName)
} else {
return fmt.Errorf("either PagePath or EventName needs to not be nil")
}
return nil
}

func resourceGoalRead(d *schema.ResourceData, meta interface{}) error {
id := d.Id()

idInt, err := strconv.Atoi(id)
if err != nil {
return err
}
g := &plausibleclient.Goal{
ID: idInt,
Domain: d.Get("site_id").(string),
}

if v, ok := d.GetOk("page_path"); ok {
pagePath := v.(string)
g.PagePath = &pagePath
} else if v, ok := d.GetOk("event_name"); ok {
eventName := v.(string)
g.EventName = &eventName
} else {
return fmt.Errorf("page_path or event_name needs to be defined")
}

return resourceGoalSetResourceData(g, d)
}

func resourceGoalDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*apiClient)
id := d.Id()
domain := d.Get("site_id").(string)
idInt, err := strconv.Atoi(id)
if err != nil {
return err
}
return client.plausibleClient.DeleteGoal(domain, idInt)
}
39 changes: 39 additions & 0 deletions internal/provider/resource_goal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package provider

import (
"fmt"
"regexp"
"testing"

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

func TestAccResourceGoal(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccResourceGoal(acctest.RandomWithPrefix("testacc-tf")),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("plausible_goal.testacc", "id", regexp.MustCompile(`^[0-9]+$`)),
),
},
},
})
}

func testAccResourceGoal(domain string) string {
return fmt.Sprintf(`
resource "plausible_site" "testacc" {
domain = "%s"
timezone = "Pacific/Auckland"
}
resource "plausible_goal" "testacc" {
site_id = plausible_site.testacc.id
page_path = "/success"
}
`, domain)
}
5 changes: 5 additions & 0 deletions internal/provider/resource_shared_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ func resourceSharedLink() *schema.Resource {
Delete: resourceSharedLinkDelete,

Schema: map[string]*schema.Schema{
"id": {
Description: "The shared link ID",
Type: schema.TypeString,
Computed: true,
},
"site_id": {
Description: "The domain of the site to create the shared link for.",
Type: schema.TypeString,
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/resource_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func resourceSite() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"id": {
Description: "The site ID",
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Required: true,
Expand Down
3 changes: 3 additions & 0 deletions plausibleclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Client struct {
username string
password string
loggedIn bool
mutexkv *MutexKV
}

func (c *Client) login() error {
Expand Down Expand Up @@ -39,5 +40,7 @@ func NewClient(username, password string) *Client {
Jar: jar,
}

c.mutexkv = NewMutexKV()

return &c
}
Loading

0 comments on commit 17a7274

Please sign in to comment.