@@ -3,6 +3,7 @@ package provider
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/coder/coder/v2/codersdk"
8
9
"github.com/google/uuid"
@@ -60,7 +61,9 @@ func (r *GroupResource) Metadata(ctx context.Context, req resource.MetadataReque
60
61
61
62
func (r * GroupResource ) Schema (ctx context.Context , req resource.SchemaRequest , resp * resource.SchemaResponse ) {
62
63
resp .Schema = schema.Schema {
63
- MarkdownDescription : "A group on the Coder deployment.\n \n Creating groups requires an Enterprise license." ,
64
+ MarkdownDescription : "A group on the Coder deployment.\n \n " +
65
+ "Creating groups requires an Enterprise license.\n \n " +
66
+ "When importing, the ID supplied can be either a group UUID retrieved via the API or `<organization-name>/<group-name>`." ,
64
67
65
68
Attributes : map [string ]schema.Attribute {
66
69
"id" : schema.StringAttribute {
@@ -324,10 +327,30 @@ func (r *GroupResource) Delete(ctx context.Context, req resource.DeleteRequest,
324
327
}
325
328
326
329
func (r * GroupResource ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
330
+ var groupID uuid.UUID
327
331
client := r .data .Client
328
- groupID , err := uuid .Parse (req .ID )
329
- if err != nil {
330
- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to parse import group ID as UUID, got error: %s" , err ))
332
+ idParts := strings .Split (req .ID , "/" )
333
+ if len (idParts ) == 1 {
334
+ var err error
335
+ groupID , err = uuid .Parse (req .ID )
336
+ if err != nil {
337
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to parse import group ID as UUID, got error: %s" , err ))
338
+ return
339
+ }
340
+ } else if len (idParts ) == 2 {
341
+ org , err := client .OrganizationByName (ctx , idParts [0 ])
342
+ if err != nil {
343
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get imported group organization, got error: %s" , err ))
344
+ return
345
+ }
346
+ group , err := client .GroupByOrgAndName (ctx , org .ID , idParts [1 ])
347
+ if err != nil {
348
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get imported group, got error: %s" , err ))
349
+ return
350
+ }
351
+ groupID = group .ID
352
+ } else {
353
+ resp .Diagnostics .AddError ("Client Error" , "Invalid import ID format, expected a single UUID or `<organization-name>/<group-name>`" )
331
354
return
332
355
}
333
356
group , err := client .Group (ctx , groupID )
@@ -339,5 +362,5 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat
339
362
resp .Diagnostics .AddError ("Client Error" , "Cannot import groups created via OIDC" )
340
363
return
341
364
}
342
- resource . ImportStatePassthroughID ( ctx , path .Root ("id" ), req , resp )
365
+ resp . Diagnostics . Append ( resp . State . SetAttribute ( ctx , path .Root ("id" ), groupID . String ()) ... )
343
366
}
0 commit comments