@@ -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
5053type 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 () {
0 commit comments