Skip to content

Commit 2d08455

Browse files
authored
Oauth client connection details (#70)
1 parent 7891441 commit 2d08455

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.82
1+
0.0.84

docs/resources/dbt_cloud_connection.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ description: |-
3030
- `allow_keep_alive` (Boolean) Whether or not the connection should allow client session keep alive
3131
- `allow_sso` (Boolean) Whether or not the connection should allow SSO
3232
- `is_active` (Boolean) Whether the connection is active
33+
- `oauth_client_id` (String) OAuth client identifier
34+
- `oauth_client_secret` (String) OAuth client secret
3335

3436
### Read-Only
3537

pkg/dbt_cloud/connection.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type ConnectionDetails struct {
1515
AllowSSO bool `json:"allow_sso"`
1616
ClientSessionKeepAlive bool `json:"client_session_keep_alive"`
1717
Role string `json:"role"`
18+
OAuthClientID string `json:"oauth_client_id,omitempty"`
19+
OAuthClientSecret string `json:"oauth_client_secret,omitempty"`
1820
}
1921

2022
type Connection struct {
@@ -61,7 +63,7 @@ func (c *Client) GetConnection(connectionID, projectID string) (*Connection, err
6163
return &connectionResponse.Data, nil
6264
}
6365

64-
func (c *Client) CreateConnection(projectID int, name string, connectionType string, isActive bool, account string, database string, warehouse string, role string, allowSSO bool, clientSessionKeepAlive bool) (*Connection, error) {
66+
func (c *Client) CreateConnection(projectID int, name string, connectionType string, isActive bool, account string, database string, warehouse string, role string, allowSSO bool, clientSessionKeepAlive bool, oAuthClientID string, oAuthClientSecret string) (*Connection, error) {
6567
state := STATE_ACTIVE
6668
if !isActive {
6769
state = STATE_DELETED
@@ -74,6 +76,8 @@ func (c *Client) CreateConnection(projectID int, name string, connectionType str
7476
AllowSSO: allowSSO,
7577
ClientSessionKeepAlive: clientSessionKeepAlive,
7678
Role: role,
79+
OAuthClientID: oAuthClientID,
80+
OAuthClientSecret: oAuthClientSecret,
7781
}
7882

7983
newConnection := Connection{
@@ -106,6 +110,11 @@ func (c *Client) CreateConnection(projectID int, name string, connectionType str
106110
return nil, err
107111
}
108112

113+
if (oAuthClientID != "") && (oAuthClientSecret != "") {
114+
connectionResponse.Data.Details.OAuthClientID = oAuthClientID
115+
connectionResponse.Data.Details.OAuthClientSecret = oAuthClientSecret
116+
}
117+
109118
return &connectionResponse.Data, nil
110119
}
111120

pkg/resources/connection.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ func ResourceConnection() *schema.Resource {
8585
Default: false,
8686
Description: "Whether or not the connection should allow client session keep alive",
8787
},
88+
"oauth_client_id": &schema.Schema{
89+
Type: schema.TypeString,
90+
Optional: true,
91+
Default: false,
92+
Description: "OAuth client identifier",
93+
},
94+
"oauth_client_secret": &schema.Schema{
95+
Type: schema.TypeString,
96+
Optional: true,
97+
Default: false,
98+
Description: "OAuth client secret",
99+
},
88100
},
89101

90102
Importer: &schema.ResourceImporter{
@@ -108,8 +120,10 @@ func resourceConnectionCreate(ctx context.Context, d *schema.ResourceData, m int
108120
role := d.Get("role").(string)
109121
allowSSO := d.Get("allow_sso").(bool)
110122
allowKeepAlive := d.Get("allow_keep_alive").(bool)
123+
oAuthClientID := d.Get("oauth_client_id").(string)
124+
oAuthClientSecret := d.Get("oauth_client_secret").(string)
111125

112-
connection, err := c.CreateConnection(projectId, name, connectionType, isActive, account, database, warehouse, role, allowSSO, allowKeepAlive)
126+
connection, err := c.CreateConnection(projectId, name, connectionType, isActive, account, database, warehouse, role, allowSSO, allowKeepAlive, oAuthClientID, oAuthClientSecret)
113127
if err != nil {
114128
return diag.FromErr(err)
115129
}
@@ -134,6 +148,10 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, m inter
134148
return diag.FromErr(err)
135149
}
136150

151+
// TODO: Remove when API returns these
152+
connection.Details.OAuthClientID = d.Get("oauth_client_id").(string)
153+
connection.Details.OAuthClientSecret = d.Get("oauth_client_secret").(string)
154+
137155
if err := d.Set("connection_id", connection.ID); err != nil {
138156
return diag.FromErr(err)
139157
}
@@ -167,6 +185,12 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, m inter
167185
if err := d.Set("allow_keep_alive", connection.Details.ClientSessionKeepAlive); err != nil {
168186
return diag.FromErr(err)
169187
}
188+
if err := d.Set("oauth_client_id", connection.Details.OAuthClientID); err != nil {
189+
return diag.FromErr(err)
190+
}
191+
if err := d.Set("oauth_client_secret", connection.Details.OAuthClientSecret); err != nil {
192+
return diag.FromErr(err)
193+
}
170194

171195
return diags
172196
}
@@ -179,7 +203,7 @@ func resourceConnectionUpdate(ctx context.Context, d *schema.ResourceData, m int
179203

180204
// TODO: add more changes here
181205

182-
if d.HasChange("name") || d.HasChange("type") || d.HasChange("account") || d.HasChange("database") || d.HasChange("warehouse") || d.HasChange("role") || d.HasChange("allow_sso") || d.HasChange("allow_keep_alive") {
206+
if d.HasChange("name") || d.HasChange("type") || d.HasChange("account") || d.HasChange("database") || d.HasChange("warehouse") || d.HasChange("role") || d.HasChange("allow_sso") || d.HasChange("allow_keep_alive") || d.HasChange("oauth_client_id") || d.HasChange("oauth_client_secret") {
183207
connection, err := c.GetConnection(connectionIdString, projectIdString)
184208
if err != nil {
185209
return diag.FromErr(err)
@@ -217,6 +241,14 @@ func resourceConnectionUpdate(ctx context.Context, d *schema.ResourceData, m int
217241
allowKeepAlive := d.Get("allow_keep_alive").(bool)
218242
connection.Details.ClientSessionKeepAlive = allowKeepAlive
219243
}
244+
if d.HasChange("oauth_client_id") {
245+
oAuthClientID := d.Get("oauth_client_id").(string)
246+
connection.Details.OAuthClientID = oAuthClientID
247+
}
248+
if d.HasChange("oauth_client_secret") {
249+
oAuthClientSecret := d.Get("oauth_client_secret").(string)
250+
connection.Details.OAuthClientSecret = oAuthClientSecret
251+
}
220252

221253
_, err = c.UpdateConnection(connectionIdString, projectIdString, *connection)
222254
if err != nil {

pkg/resources/connection_acceptance_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@ func TestAccDbtCloudConnectionResource(t *testing.T) {
1717
connectionName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
1818
connectionName2 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
1919
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
20+
oAuthClientID := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
21+
oAuthClientSecret := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
2022

2123
resource.Test(t, resource.TestCase{
2224
PreCheck: func() { testAccPreCheck(t) },
2325
Providers: testAccProviders,
2426
CheckDestroy: testAccCheckDbtCloudConnectionDestroy,
2527
Steps: []resource.TestStep{
2628
{
27-
Config: testAccDbtCloudConnectionResourceBasicConfig(connectionName, projectName),
29+
Config: testAccDbtCloudConnectionResourceBasicConfig(connectionName, projectName, oAuthClientID, oAuthClientSecret),
2830
Check: resource.ComposeTestCheckFunc(
2931
testAccCheckDbtCloudConnectionExists("dbt_cloud_connection.test_connection"),
3032
resource.TestCheckResourceAttr("dbt_cloud_connection.test_connection", "name", connectionName),
3133
),
3234
},
3335
// RENAME
3436
{
35-
Config: testAccDbtCloudConnectionResourceBasicConfig(connectionName2, projectName),
37+
Config: testAccDbtCloudConnectionResourceBasicConfig(connectionName2, projectName, oAuthClientID, oAuthClientSecret),
3638
Check: resource.ComposeTestCheckFunc(
3739
testAccCheckDbtCloudConnectionExists("dbt_cloud_connection.test_connection"),
3840
resource.TestCheckResourceAttr("dbt_cloud_connection.test_connection", "name", connectionName2),
@@ -52,13 +54,13 @@ func TestAccDbtCloudConnectionResource(t *testing.T) {
5254
ResourceName: "dbt_cloud_connection.test_connection",
5355
ImportState: true,
5456
ImportStateVerify: true,
55-
ImportStateVerifyIgnore: []string{},
57+
ImportStateVerifyIgnore: []string{"oauth_client_id", "oauth_client_secret"},
5658
},
5759
},
5860
})
5961
}
6062

61-
func testAccDbtCloudConnectionResourceBasicConfig(connectionName, projectName string) string {
63+
func testAccDbtCloudConnectionResourceBasicConfig(connectionName, projectName, oAuthClientID, oAuthClientSecret string) string {
6264
return fmt.Sprintf(`
6365
resource "dbt_cloud_project" "test_project" {
6466
name = "%s"
@@ -74,8 +76,10 @@ resource "dbt_cloud_connection" "test_connection" {
7476
role = "user"
7577
allow_sso = false
7678
allow_keep_alive = false
79+
oauth_client_id = "%s"
80+
oauth_client_secret = "%s"
7781
}
78-
`, projectName, connectionName)
82+
`, projectName, connectionName, oAuthClientID, oAuthClientSecret)
7983
}
8084

8185
//

0 commit comments

Comments
 (0)