Skip to content

Commit 9a59345

Browse files
authored
Merge pull request #24 from PostHog/vdekrijger-make-project-id-otional
chore: Make the project_id optional and allow it to be passed to the existing resources
2 parents fa5ac62 + 67c5e7e commit 9a59345

29 files changed

+525
-375
lines changed

docs/resources/alert.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Manage PostHog alerts. Alerts notify you when an insight's value crosses a thres
2828
- `condition_type` (String) Condition type: `absolute_value`, `relative_increase`, or `relative_decrease`.
2929
- `enabled` (Boolean) Whether the alert is enabled. Defaults to true.
3030
- `name` (String) Name of the alert.
31+
- `project_id` (String) Project ID (environment) for this resource. Overrides the provider-level project_id.
3132
- `series_index` (Number) Index of the trend series to monitor (0-based). Used for trends alerts.
3233
- `skip_weekend` (Boolean) Whether to skip checking the alert on weekends.
3334
- `threshold_lower` (Number) Lower bound of the threshold. Alert fires when value goes below this.

docs/resources/dashboard.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PostHog Dashboard resource
2424
- `deleted` (Boolean) Whether the dashboard is deleted (soft delete)
2525
- `description` (String) Dashboard description
2626
- `pinned` (Boolean) Whether the dashboard is pinned
27+
- `project_id` (String) Project ID (environment) for this resource. Overrides the provider-level project_id.
2728
- `tags` (Set of String) Set of tags for the dashboard
2829

2930
### Read-Only

docs/resources/feature_flag.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PostHog Feature Flag resource
2424
- `active` (Boolean) Whether the feature flag is active
2525
- `filters` (String) Feature flag filters as JSON
2626
- `name` (String) Feature flag name/description
27+
- `project_id` (String) Project ID (environment) for this resource. Overrides the provider-level project_id.
2728
- `rollout_percentage` (Number) Rollout percentage (0-100)
2829
- `tags` (Set of String) Set of tags for the feature flag
2930

docs/resources/hog_function.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Manage PostHog Hog functions. Hog functions enable destinations, webhooks, and t
2727
- `mappings_json` (String) JSON array of mapping configurations. Each mapping can have its own `name`, `inputs_schema`, `inputs`, and `filters`.
2828
- `masking_json` (String) JSON object configuring PII masking for the Hog function. Includes `ttl`, `threshold`, and `hash` properties.
2929
- `name` (String) Name of the Hog function.
30+
- `project_id` (String) Project ID (environment) for this resource. Overrides the provider-level project_id.
3031
- `template_id` (String) ID of a template to use as the basis for this Hog function. The template provides default code, inputs schema, and configuration.
3132
- `type` (String) Type of Hog function: `destination`, `site_destination`, `internal_destination`, `source_webhook`, `site_app`, or `transformation`.
3233

docs/resources/insight.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Manage PostHog insights via the insights endpoints.
2727
- `derived_name` (String) Insight derived name. This is auto-generated by PostHog when the name is not set.
2828
- `description` (String) Insight description.
2929
- `name` (String) Insight name.
30+
- `project_id` (String) Project ID (environment) for this resource. Overrides the provider-level project_id.
3031
- `tags` (Set of String) List of tags to apply to the insight.
3132

3233
### Read-Only

docs/resources/project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Manages a PostHog project within an organization.
1818
### Required
1919

2020
- `name` (String) The name of the project.
21-
- `organization_id` (String) The identifier of the organization this project belongs to.
21+
- `organization_id` (String) The identifier of the organization this resource belongs to.
2222

2323
### Optional
2424

internal/data/provider_data.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import (
66

77
// ProviderData passes configured client to resources.
88
type ProviderData struct {
9-
Client httpclient.PosthogClient
9+
Client httpclient.PosthogClient
10+
DefaultProjectID string
1011
}

internal/httpclient/alerts.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,23 @@ type AlertCheck struct {
7979
TargetsNotified bool `json:"targets_notified"`
8080
}
8181

82-
func (c *PosthogClient) CreateAlert(ctx context.Context, input AlertRequest) (Alert, error) {
83-
path := fmt.Sprintf("/api/environments/%s/alerts/", c.projectID)
82+
func (c *PosthogClient) CreateAlert(ctx context.Context, projectID string, input AlertRequest) (Alert, error) {
83+
path := fmt.Sprintf("/api/environments/%s/alerts/", projectID)
8484
result, _, err := doPost[Alert](c, ctx, path, input)
8585
return result, err
8686
}
8787

88-
func (c *PosthogClient) GetAlert(ctx context.Context, id string) (Alert, HTTPStatusCode, error) {
89-
path := fmt.Sprintf("/api/environments/%s/alerts/%s/", c.projectID, id)
88+
func (c *PosthogClient) GetAlert(ctx context.Context, projectID, id string) (Alert, HTTPStatusCode, error) {
89+
path := fmt.Sprintf("/api/environments/%s/alerts/%s/", projectID, id)
9090
return doGet[Alert](c, ctx, path)
9191
}
9292

93-
func (c *PosthogClient) UpdateAlert(ctx context.Context, id string, input AlertRequest) (Alert, HTTPStatusCode, error) {
94-
path := fmt.Sprintf("/api/environments/%s/alerts/%s/", c.projectID, id)
93+
func (c *PosthogClient) UpdateAlert(ctx context.Context, projectID, id string, input AlertRequest) (Alert, HTTPStatusCode, error) {
94+
path := fmt.Sprintf("/api/environments/%s/alerts/%s/", projectID, id)
9595
return doPatch[Alert](c, ctx, path, input)
9696
}
9797

98-
func (c *PosthogClient) DeleteAlert(ctx context.Context, id string) (HTTPStatusCode, error) {
99-
path := fmt.Sprintf("/api/environments/%s/alerts/%s/", c.projectID, id)
98+
func (c *PosthogClient) DeleteAlert(ctx context.Context, projectID, id string) (HTTPStatusCode, error) {
99+
path := fmt.Sprintf("/api/environments/%s/alerts/%s/", projectID, id)
100100
return doDelete(c, ctx, path)
101101
}

internal/httpclient/client.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ type HTTPStatusCode int
1818
type PosthogClient struct {
1919
host string
2020
apiKey string
21-
projectID string
2221
version string
2322
httpClient *http.Client
2423
}
2524

26-
func NewDefaultClient(host, apiKey, projectID, version string, opts ...ClientOption) PosthogClient {
25+
func NewDefaultClient(host, apiKey, version string, opts ...ClientOption) PosthogClient {
2726
httpClient := &http.Client{
2827
Timeout: 30 * time.Second,
2928
Transport: &http.Transport{
@@ -43,21 +42,19 @@ func NewDefaultClient(host, apiKey, projectID, version string, opts ...ClientOpt
4342
return PosthogClient{
4443
host: host,
4544
apiKey: apiKey,
46-
projectID: projectID,
4745
version: version,
4846
httpClient: httpClient,
4947
}
5048
}
5149

52-
func NewClient(client *http.Client, host, apiKey, projectID, version string, opts ...ClientOption) PosthogClient {
50+
func NewClient(client *http.Client, host, apiKey, version string, opts ...ClientOption) PosthogClient {
5351
for _, opt := range opts {
5452
opt(client)
5553
}
5654

5755
return PosthogClient{
5856
host: host,
5957
apiKey: apiKey,
60-
projectID: projectID,
6158
version: version,
6259
httpClient: client,
6360
}

internal/httpclient/dashboard.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,25 @@ type DashboardRequest struct {
2222
Deleted *bool `json:"deleted,omitempty"`
2323
}
2424

25-
func (c *PosthogClient) CreateDashboard(ctx context.Context, input DashboardRequest) (Dashboard, error) {
26-
path := fmt.Sprintf("/api/projects/%s/dashboards/", c.projectID)
25+
func (c *PosthogClient) CreateDashboard(ctx context.Context, projectID string, input DashboardRequest) (Dashboard, error) {
26+
path := fmt.Sprintf("/api/projects/%s/dashboards/", projectID)
2727
result, _, err := doPost[Dashboard](c, ctx, path, input)
2828
return result, err
2929
}
3030

31-
func (c *PosthogClient) GetDashboard(ctx context.Context, id string) (Dashboard, HTTPStatusCode, error) {
32-
path := fmt.Sprintf("/api/projects/%s/dashboards/%s/", c.projectID, id)
31+
func (c *PosthogClient) GetDashboard(ctx context.Context, projectID, id string) (Dashboard, HTTPStatusCode, error) {
32+
path := fmt.Sprintf("/api/projects/%s/dashboards/%s/", projectID, id)
3333
return doGet[Dashboard](c, ctx, path)
3434
}
3535

36-
func (c *PosthogClient) UpdateDashboard(ctx context.Context, id string, input DashboardRequest) (Dashboard, HTTPStatusCode, error) {
37-
path := fmt.Sprintf("/api/projects/%s/dashboards/%s/", c.projectID, id)
36+
func (c *PosthogClient) UpdateDashboard(ctx context.Context, projectID, id string, input DashboardRequest) (Dashboard, HTTPStatusCode, error) {
37+
path := fmt.Sprintf("/api/projects/%s/dashboards/%s/", projectID, id)
3838
return doPatch[Dashboard](c, ctx, path, input)
3939
}
4040

41-
func (c *PosthogClient) DeleteDashboard(ctx context.Context, id string) (HTTPStatusCode, error) {
41+
func (c *PosthogClient) DeleteDashboard(ctx context.Context, projectID, id string) (HTTPStatusCode, error) {
4242
deleted := true
4343
input := DashboardRequest{Deleted: &deleted}
44-
_, statusCode, err := c.UpdateDashboard(ctx, id, input)
44+
_, statusCode, err := c.UpdateDashboard(ctx, projectID, id, input)
4545
return statusCode, err
4646
}

0 commit comments

Comments
 (0)