Skip to content

Commit f6e3463

Browse files
Gary JamesGary James
authored andcommitted
Feature - Add support for Projects
1 parent deb82f0 commit f6e3463

10 files changed

Lines changed: 294 additions & 19 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ check-docs: docs
2626

2727
deps:
2828
go mod tidy
29+
30+
release:
31+
git tag "v$(VERSION)"
32+
git push origin "v$(VERSION)"

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.41
1+
0.0.46

pkg/data_sources/project.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ var projectSchema = map[string]*schema.Schema{
2020
Computed: true,
2121
Description: "Given name for project",
2222
},
23+
"connection_id": &schema.Schema{
24+
Type: schema.TypeInt,
25+
Computed: true,
26+
Description: "ID of the connection associated with the project",
27+
},
28+
"repository_id": &schema.Schema{
29+
Type: schema.TypeInt,
30+
Computed: true,
31+
Description: "ID of the repository associated with the project",
32+
},
2333
"state": &schema.Schema{
2434
Type: schema.TypeInt,
2535
Computed: true,
@@ -52,6 +62,12 @@ func datasourceProjectRead(ctx context.Context, d *schema.ResourceData, m interf
5262
if err := d.Set("name", project.Name); err != nil {
5363
return diag.FromErr(err)
5464
}
65+
if err := d.Set("connection_id", project.ConnectionID); err != nil {
66+
return diag.FromErr(err)
67+
}
68+
if err := d.Set("repository_id", project.RepositoryID); err != nil {
69+
return diag.FromErr(err)
70+
}
5571
if err := d.Set("state", project.State); err != nil {
5672
return diag.FromErr(err)
5773
}

pkg/data_sources/project_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func TestAccDbtCloudProjectDataSource(t *testing.T) {
2222
resource.TestCheckResourceAttr("data.dbt_cloud_job.test", "project_id", randomID),
2323
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "name"),
2424
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "id"),
25+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "connection_id"),
26+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "repository_id"),
2527
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "state"),
2628
)
2729

pkg/dbt_cloud/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010
)
1111

12-
const HostURL string = "https://cloud.getdbt.com/api/v2"
12+
const HostURL string = "https://cloud.getdbt.com/api"
1313

1414
// Client -
1515
type Client struct {

pkg/dbt_cloud/job.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77
"strings"
88
)
99

10-
type responseStatus struct {
11-
Code int `json:"code"`
12-
Is_Success bool `json:"is_success"`
13-
User_Message string `json:"user_message"`
14-
Developer_Message string `json:"developer_message"`
15-
}
16-
1710
type JobTrigger struct {
1811
Github_Webhook bool `json:"github_webhook"`
1912
Schedule bool `json:"schedule"`
@@ -42,7 +35,7 @@ type JobSchedule struct {
4235

4336
type JobResponse struct {
4437
Data Job `json:"data"`
45-
Status responseStatus `json:"status"`
38+
Status ResponseStatus `json:"status"`
4639
}
4740

4841
type Job struct {
@@ -62,7 +55,7 @@ type Job struct {
6255
}
6356

6457
func (c *Client) GetJob(jobID string) (*Job, error) {
65-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/jobs/%s/", c.AccountURL, jobID), nil)
58+
req, err := http.NewRequest("GET", fmt.Sprintf("v2/%s/jobs/%s/", c.AccountURL, jobID), nil)
6659
if err != nil {
6760
return nil, err
6861
}
@@ -138,7 +131,7 @@ func (c *Client) CreateJob(projectId int, environmentId int, name string, execut
138131
return nil, err
139132
}
140133

141-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/jobs/", c.AccountURL), strings.NewReader(string(newJobData)))
134+
req, err := http.NewRequest("POST", fmt.Sprintf("v2/%s/jobs/", c.AccountURL), strings.NewReader(string(newJobData)))
142135
if err != nil {
143136
return nil, err
144137
}
@@ -163,7 +156,7 @@ func (c *Client) UpdateJob(jobId string, job Job) (*Job, error) {
163156
return nil, err
164157
}
165158

166-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/jobs/%s/", c.AccountURL, jobId), strings.NewReader(string(jobData)))
159+
req, err := http.NewRequest("POST", fmt.Sprintf("v2/%s/jobs/%s/", c.AccountURL, jobId), strings.NewReader(string(jobData)))
167160
if err != nil {
168161
return nil, err
169162
}

pkg/dbt_cloud/project.go

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,82 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"strings"
78
)
89

910
type Project struct {
10-
ID *int `json:"id"`
11-
Name string `json:"name"`
12-
State int `json:"state"`
11+
ID *int `json:"id"`
12+
Name string `json:"name"`
13+
DbtProjectSubdirectory *string `json:"dbt_project_subdirectory"`
14+
ConnectionID *int `json:"connection_id"`
15+
RepositoryID *int `json:"repository_id"`
16+
State int `json:"state"`
1317
}
1418

1519
type ProjectResponse struct {
16-
Data Project `json:"data"`
20+
Data Project `json:"data"`
21+
Status ResponseStatus `json:"status"`
1722
}
1823

1924
func (c *Client) GetProject(projectID string) (*Project, error) {
20-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/projects/%s/", c.AccountURL, projectID), nil)
25+
req, err := http.NewRequest("GET", fmt.Sprintf("v3/%s/projects/%s/", c.AccountURL, projectID), nil)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
body, err := c.doRequest(req)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
projectResponse := ProjectResponse{}
36+
err = json.Unmarshal(body, &projectResponse)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return &projectResponse.Data, nil
42+
}
43+
44+
func (c *Client) CreateProject(name string, dbtProjectSubdirectory string, connectionID int, repositoryID int) (*Project, error) {
45+
newProject := Project{
46+
Name: name,
47+
DbtProjectSubdirectory: &dbtProjectSubdirectory,
48+
ConnectionID: &connectionID,
49+
RepositoryID: &repositoryID,
50+
State: 1,
51+
}
52+
newProjectData, err := json.Marshal(newProject)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
req, err := http.NewRequest("POST", fmt.Sprintf("v3/%s/projects/", c.AccountURL), strings.NewReader(string(newProjectData)))
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
body, err := c.doRequest(req)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
projectResponse := ProjectResponse{}
68+
err = json.Unmarshal(body, &projectResponse)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
return &projectResponse.Data, nil
74+
}
75+
76+
func (c *Client) UpdateProject(projectID string, project Project) (*Project, error) {
77+
projectData, err := json.Marshal(project)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
req, err := http.NewRequest("POST", fmt.Sprintf("v3/%s/projects/%s/", c.AccountURL, projectID), strings.NewReader(string(projectData)))
2183
if err != nil {
2284
return nil, err
2385
}

pkg/provider/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func Provider() *schema.Provider {
3232
"dbt_cloud_project": data_sources.DatasourceProject(),
3333
},
3434
ResourcesMap: map[string]*schema.Resource{
35-
"dbt_cloud_job": resources.ResourceJob(),
35+
"dbt_cloud_job": resources.ResourceJob(),
36+
"dbt_cloud_project": resources.ResourceProject(),
3637
},
3738
ConfigureContextFunc: providerConfigure,
3839
}

pkg/resources/project.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package resources
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
var projectSchema = map[string]*schema.Schema{
13+
"name": &schema.Schema{
14+
Type: schema.TypeString,
15+
Required: true,
16+
Description: "Project name",
17+
},
18+
"dbt_project_subdirectory": &schema.Schema{
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Description: "DBT project subdirectory path",
22+
},
23+
"connection_id": &schema.Schema{
24+
Type: schema.TypeInt,
25+
Optional: true,
26+
Description: "Connection ID",
27+
},
28+
"repository_id": &schema.Schema{
29+
Type: schema.TypeInt,
30+
Optional: true,
31+
Description: "Repository ID",
32+
},
33+
}
34+
35+
func ResourceProject() *schema.Resource {
36+
return &schema.Resource{
37+
CreateContext: resourceProjectCreate,
38+
ReadContext: resourceProjectRead,
39+
UpdateContext: resourceProjectUpdate,
40+
DeleteContext: resourceProjectDelete,
41+
42+
Schema: projectSchema,
43+
Importer: &schema.ResourceImporter{
44+
StateContext: schema.ImportStatePassthroughContext,
45+
},
46+
}
47+
}
48+
49+
func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
50+
c := m.(*dbt_cloud.Client)
51+
52+
var diags diag.Diagnostics
53+
54+
projectID := d.Id()
55+
56+
project, err := c.GetProject(projectID)
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
61+
if err := d.Set("name", project.Name); err != nil {
62+
return diag.FromErr(err)
63+
}
64+
if err := d.Set("dbt_project_subdirectory", project.DbtProjectSubdirectory); err != nil {
65+
return diag.FromErr(err)
66+
}
67+
if err := d.Set("connection_id", project.ConnectionID); err != nil {
68+
return diag.FromErr(err)
69+
}
70+
if err := d.Set("repository_id", project.RepositoryID); err != nil {
71+
return diag.FromErr(err)
72+
}
73+
if err := d.Set("state", project.State); err != nil {
74+
return diag.FromErr(err)
75+
}
76+
77+
return diags
78+
}
79+
80+
func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
81+
c := m.(*dbt_cloud.Client)
82+
83+
var diags diag.Diagnostics
84+
85+
name := d.Get("name").(string)
86+
dbtProjectSubdirectory := d.Get("dbt_project_subdirectory").(string)
87+
connectionID := d.Get("connection_id").(int)
88+
repositoryID := d.Get("repository_id").(int)
89+
90+
p, err := c.CreateProject(name, dbtProjectSubdirectory, connectionID, repositoryID)
91+
if err != nil {
92+
return diag.FromErr(err)
93+
}
94+
95+
d.SetId(strconv.Itoa(*p.ID))
96+
97+
resourceProjectRead(ctx, d, m)
98+
99+
return diags
100+
}
101+
102+
func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
103+
c := m.(*dbt_cloud.Client)
104+
projectID := d.Id()
105+
106+
if d.HasChange("name") || d.HasChange("dbt_project_subdirectory") || d.HasChange("connection_id") || d.HasChange("repository_id") {
107+
project, err := c.GetProject(projectID)
108+
if err != nil {
109+
return diag.FromErr(err)
110+
}
111+
112+
if d.HasChange("name") {
113+
name := d.Get("name").(string)
114+
project.Name = name
115+
}
116+
if d.HasChange("dbt_project_subdirectory") {
117+
dbtProjectSubdirectory := d.Get("dbt_project_subdirectory").(string)
118+
project.DbtProjectSubdirectory = &dbtProjectSubdirectory
119+
}
120+
if d.HasChange("connection_id") {
121+
connectionID := d.Get("connection_id").(int)
122+
project.ConnectionID = &connectionID
123+
}
124+
if d.HasChange("repository_id") {
125+
repositoryID := d.Get("repository_id").(int)
126+
project.RepositoryID = &repositoryID
127+
}
128+
129+
_, err = c.UpdateProject(projectID, *project)
130+
if err != nil {
131+
return diag.FromErr(err)
132+
}
133+
}
134+
135+
return resourceJobRead(ctx, d, m)
136+
}
137+
138+
func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
139+
c := m.(*dbt_cloud.Client)
140+
projectID := d.Id()
141+
142+
var diags diag.Diagnostics
143+
144+
project, err := c.GetProject(projectID)
145+
if err != nil {
146+
return diag.FromErr(err)
147+
}
148+
149+
project.State = 2
150+
_, err = c.UpdateProject(projectID, *project)
151+
if err != nil {
152+
return diag.FromErr(err)
153+
}
154+
155+
return diags
156+
}

0 commit comments

Comments
 (0)