Skip to content

Commit fe8e0fa

Browse files
committed
Resource updates
1 parent f85c282 commit fe8e0fa

File tree

11 files changed

+145
-34
lines changed

11 files changed

+145
-34
lines changed

docs/resources/dashboard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ resource "devhub_dashboard" "example" {
2929
]
3030
3131
query_details = {
32-
query = "SELECT * FROM users WHERE id = '${user_id}'"
32+
query = "SELECT * FROM users WHERE id = '$${user_id}'"
3333
credential_id = "crd_xxx"
3434
timeout = 10
3535
}

docs/resources/querydesk_database.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ resource "devhub_querydesk_database" "example" {
5454

5555
### Read-Only
5656

57+
- `credential_ids` (Map of String) A map of credential IDs by username.
5758
- `id` (String) Database id.
5859

5960
<a id="nestedatt--credentials"></a>

docs/resources/workflow.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ resource "devhub_workflow" "example" {
3636
headers = [
3737
{ key = "content-type", value = "application/json" }
3838
]
39-
body = "{\"user_id\": \"${user_id}\"}"
39+
body = "{\"user_id\": \"$${user_id}\"}"
4040
}
4141
},
4242
{
@@ -52,7 +52,7 @@ resource "devhub_workflow" "example" {
5252
name = "query-step"
5353
5454
query_action = {
55-
query = "SELECT * FROM users WHERE id = '${user_id}'"
55+
query = "SELECT * FROM users WHERE id = '$${user_id}'"
5656
credential_id = "crd_xxx"
5757
timeout = 10
5858
}
@@ -87,6 +87,7 @@ resource "devhub_workflow" "example" {
8787
### Optional
8888

8989
- `inputs` (Attributes List) (see [below for nested schema](#nestedatt--inputs))
90+
- `trigger_linear_label_name` (String) The name of the Linear label that should trigger the workflow.
9091

9192
### Read-Only
9293

@@ -138,7 +139,7 @@ Required:
138139

139140
Required:
140141

141-
- `required_approvals` (Number) Number of required approvals.
142+
- `reviews_required` (Number) Number of required approvals.
142143

143144

144145
<a id="nestedatt--steps--query_action"></a>

examples/resources/devhub_dashboard/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resource "devhub_dashboard" "example" {
1414
]
1515

1616
query_details = {
17-
query = "SELECT * FROM users WHERE id = '${user_id}'"
17+
query = "SELECT * FROM users WHERE id = '$${user_id}'"
1818
credential_id = "crd_xxx"
1919
timeout = 10
2020
}

examples/resources/devhub_workflow/resource.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resource "devhub_workflow" "example" {
2121
headers = [
2222
{ key = "content-type", value = "application/json" }
2323
]
24-
body = "{\"user_id\": \"${user_id}\"}"
24+
body = "{\"user_id\": \"$${user_id}\"}"
2525
}
2626
},
2727
{
@@ -37,7 +37,7 @@ resource "devhub_workflow" "example" {
3737
name = "query-step"
3838

3939
query_action = {
40-
query = "SELECT * FROM users WHERE id = '${user_id}'"
40+
query = "SELECT * FROM users WHERE id = '$${user_id}'"
4141
credential_id = "crd_xxx"
4242
timeout = 10
4343
}

internal/client/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type WorkflowStepAction struct {
9595
ExpectedStatusCode int64 `json:"expected_status_code"`
9696
IncludeDevhubJwt bool `json:"include_devhub_jwt"`
9797
// ApprovalAction fields
98-
RequiredApprovals int `json:"required_approvals"`
98+
ReviewsRequired int `json:"reviews_required"`
9999
// QueryAction fields
100100
Timeout int `json:"timeout"`
101101
Query string `json:"query"`

internal/provider/devhub_dashboard_resource_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestAccDashboardResource(t *testing.T) {
2121
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.title", "Users"),
2222
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.inputs.0.key", "user_id"),
2323
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.inputs.0.description", "User ID"),
24-
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.query_details.query", "SELECT * FROM users WHERE id = '${user_id}'"),
24+
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.query_details.query", "SELECT * FROM users WHERE id = '$${user_id}'"),
2525
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.query_details.credential_id", "123"),
2626
),
2727
},
@@ -39,7 +39,7 @@ func TestAccDashboardResource(t *testing.T) {
3939
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.title", "Users"),
4040
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.inputs.0.key", "user_id"),
4141
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.inputs.0.description", "User ID"),
42-
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.query_details.query", "SELECT * FROM users WHERE id = '${user_id}'"),
42+
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.query_details.query", "SELECT * FROM users WHERE id = '$${user_id}'"),
4343
resource.TestCheckResourceAttr("devhub_dashboard.test", "panels.0.query_details.credential_id", "123"),
4444
),
4545
},
@@ -64,7 +64,7 @@ resource "devhub_dashboard" "test" {
6464
]
6565
6666
query_details = {
67-
query = "SELECT * FROM users WHERE id = '$${user_id}'"
67+
query = "SELECT * FROM users WHERE id = '$$${user_id}'"
6868
credential_id = "123"
6969
}
7070
},

internal/provider/devhub_querydesk_database_resource.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"strings"
1010
devhub "terraform-provider-devhub/internal/client"
1111

12+
"github.com/hashicorp/terraform-plugin-framework/attr"
1213
"github.com/hashicorp/terraform-plugin-framework/path"
1314
"github.com/hashicorp/terraform-plugin-framework/resource"
1415
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1516
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1719
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1820
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -45,6 +47,7 @@ type databaseResourceModel struct {
4547
SlackChannel types.String `tfsdk:"slack_channel"`
4648
AgentId types.String `tfsdk:"agent_id"`
4749
Credentials []databaseCredentialModel `tfsdk:"credentials"`
50+
CredentialIds types.Map `tfsdk:"credential_ids"`
4851
}
4952

5053
type databaseCredentialModel struct {
@@ -131,6 +134,14 @@ func (r *databaseResource) Schema(_ context.Context, _ resource.SchemaRequest, r
131134
MarkdownDescription: "The agent id for the database.",
132135
Optional: true,
133136
},
137+
"credential_ids": schema.MapAttribute{
138+
ElementType: types.StringType,
139+
Computed: true,
140+
MarkdownDescription: "A map of credential IDs by username.",
141+
PlanModifiers: []planmodifier.Map{
142+
mapplanmodifier.UseStateForUnknown(),
143+
},
144+
},
134145
"credentials": schema.ListNestedAttribute{
135146
Required: true,
136147
NestedObject: schema.NestedAttributeObject{
@@ -226,11 +237,17 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
226237

227238
plan.Id = types.StringValue(database.Id)
228239

240+
credentialIds := make(map[string]attr.Value)
241+
229242
for index, credential := range database.Credentials {
230243
plan.Credentials[index].Id = types.StringValue(credential.Id)
231244
plan.Credentials[index].DefaultCredential = types.BoolValue(credential.DefaultCredential)
245+
246+
credentialIds[credential.Username] = types.StringValue(credential.Id)
232247
}
233248

249+
plan.CredentialIds = types.MapValueMust(types.StringType, credentialIds)
250+
234251
// Set state to fully populated data
235252
diags = resp.State.Set(ctx, plan)
236253
resp.Diagnostics.Append(diags...)
@@ -270,6 +287,12 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
270287
state.Ssl = types.BoolValue(database.Ssl)
271288
state.RestrictAccess = types.BoolValue(database.RestrictAccess)
272289

290+
if database.Group != "" {
291+
state.Group = types.StringValue(database.Group)
292+
} else {
293+
state.Group = types.StringNull()
294+
}
295+
273296
if database.SlackChannel != "" {
274297
state.SlackChannel = types.StringValue(database.SlackChannel)
275298
} else {
@@ -286,13 +309,19 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
286309
state.Credentials = make([]databaseCredentialModel, len(database.Credentials))
287310
}
288311

312+
credentialIds := make(map[string]attr.Value)
313+
289314
for index, credential := range database.Credentials {
290315
state.Credentials[index].Id = types.StringValue(credential.Id)
291316
state.Credentials[index].Username = types.StringValue(credential.Username)
292317
state.Credentials[index].ReviewsRequired = types.Int64Value(int64(credential.ReviewsRequired))
293318
state.Credentials[index].DefaultCredential = types.BoolValue(credential.DefaultCredential)
319+
320+
credentialIds[credential.Username] = types.StringValue(credential.Id)
294321
}
295322

323+
state.CredentialIds = types.MapValueMust(types.StringType, credentialIds)
324+
296325
// Set refreshed state
297326
diags = resp.State.Set(ctx, &state)
298327
resp.Diagnostics.Append(diags...)
@@ -349,7 +378,7 @@ func (r *databaseResource) Update(ctx context.Context, req resource.UpdateReques
349378
}
350379

351380
// Update existing order
352-
_, err := r.client.UpdateDatabase(plan.Id.ValueString(), input)
381+
database, err := r.client.UpdateDatabase(plan.Id.ValueString(), input)
353382
if err != nil {
354383
resp.Diagnostics.AddError(
355384
"Error Updating Database",
@@ -358,6 +387,16 @@ func (r *databaseResource) Update(ctx context.Context, req resource.UpdateReques
358387
return
359388
}
360389

390+
credentialIds := make(map[string]attr.Value)
391+
392+
for index, credential := range database.Credentials {
393+
plan.Credentials[index].Id = types.StringValue(credential.Id)
394+
395+
credentialIds[credential.Username] = types.StringValue(credential.Id)
396+
}
397+
398+
plan.CredentialIds = types.MapValueMust(types.StringType, credentialIds)
399+
361400
diags = resp.State.Set(ctx, plan)
362401
resp.Diagnostics.Append(diags...)
363402
if resp.Diagnostics.HasError() {

internal/provider/devhub_querydesk_database_resource_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func TestAccDatabaseResource(t *testing.T) {
3030
resource.TestCheckResourceAttr("devhub_querydesk_database.test", "credentials.1.password", "password2"),
3131
resource.TestCheckResourceAttr("devhub_querydesk_database.test", "credentials.1.reviews_required", "1"),
3232
resource.TestCheckResourceAttr("devhub_querydesk_database.test", "credentials.1.default_credential", "false"),
33+
resource.TestCheckResourceAttrSet("devhub_querydesk_database.test", "credential_ids.postgres"),
34+
resource.TestCheckResourceAttrSet("devhub_querydesk_database.test", "credential_ids.another"),
3335
),
3436
},
3537
// ImportState testing
@@ -56,6 +58,8 @@ func TestAccDatabaseResource(t *testing.T) {
5658
resource.TestCheckResourceAttr("devhub_querydesk_database.test", "credentials.1.password", "password2"),
5759
resource.TestCheckResourceAttr("devhub_querydesk_database.test", "credentials.1.reviews_required", "1"),
5860
resource.TestCheckResourceAttr("devhub_querydesk_database.test", "credentials.1.default_credential", "false"),
61+
resource.TestCheckResourceAttrSet("devhub_querydesk_database.test", "credential_ids.postgres"),
62+
resource.TestCheckResourceAttrSet("devhub_querydesk_database.test", "credential_ids.another"),
5963
),
6064
},
6165
// Delete testing automatically occurs in TestCase

internal/provider/devhub_workflow_resource.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type workflowApiActionHeadersModel struct {
7171
}
7272

7373
type workflowApprovalActionModel struct {
74-
RequiredApprovals types.Int64 `tfsdk:"required_approvals"`
74+
ReviewsRequired types.Int64 `tfsdk:"reviews_required"`
7575
}
7676

7777
type workflowQueryActionModel struct {
@@ -214,7 +214,7 @@ func (r *workflowResource) Schema(_ context.Context, _ resource.SchemaRequest, r
214214
"approval_action": schema.SingleNestedAttribute{
215215
Optional: true,
216216
Attributes: map[string]schema.Attribute{
217-
"required_approvals": schema.Int64Attribute{
217+
"reviews_required": schema.Int64Attribute{
218218
MarkdownDescription: "Number of required approvals.",
219219
Required: true,
220220
},
@@ -319,8 +319,8 @@ func (r *workflowResource) Create(ctx context.Context, req resource.CreateReques
319319

320320
if step.ApprovalAction != nil {
321321
workflowStep.Action = &devhub.WorkflowStepAction{
322-
Type: "approval",
323-
RequiredApprovals: int(step.ApprovalAction.RequiredApprovals.ValueInt64()),
322+
Type: "approval",
323+
ReviewsRequired: int(step.ApprovalAction.ReviewsRequired.ValueInt64()),
324324
}
325325
}
326326

@@ -456,7 +456,7 @@ func (r *workflowResource) Read(ctx context.Context, req resource.ReadRequest, r
456456
}
457457
case "approval":
458458
stepModel.ApprovalAction = &workflowApprovalActionModel{
459-
RequiredApprovals: types.Int64Value(int64(step.Action.RequiredApprovals)),
459+
ReviewsRequired: types.Int64Value(int64(step.Action.ReviewsRequired)),
460460
}
461461
case "query":
462462
stepModel.QueryAction = &workflowQueryActionModel{
@@ -533,8 +533,8 @@ func (r *workflowResource) Update(ctx context.Context, req resource.UpdateReques
533533
}
534534
} else if step.ApprovalAction != nil {
535535
workflowStep.Action = &devhub.WorkflowStepAction{
536-
Type: "approval",
537-
RequiredApprovals: int(step.ApprovalAction.RequiredApprovals.ValueInt64()),
536+
Type: "approval",
537+
ReviewsRequired: int(step.ApprovalAction.ReviewsRequired.ValueInt64()),
538538
}
539539
} else if step.QueryAction != nil {
540540
workflowStep.Action = &devhub.WorkflowStepAction{

0 commit comments

Comments
 (0)