@@ -3,6 +3,7 @@ package provider
33import (
44"context"
55"fmt"
6+ "strings"
67
78"github.com/coder/coder/v2/codersdk"
89"github.com/google/uuid"
@@ -60,7 +61,9 @@ func (r *GroupResource) Metadata(ctx context.Context, req resource.MetadataReque
6061
6162func (r * GroupResource )Schema (ctx context.Context ,req resource.SchemaRequest ,resp * resource.SchemaResponse ) {
6263resp .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>`." ,
6467
6568Attributes :map [string ]schema.Attribute {
6669"id" : schema.StringAttribute {
@@ -324,10 +327,30 @@ func (r *GroupResource) Delete(ctx context.Context, req resource.DeleteRequest,
324327}
325328
326329func (r * GroupResource )ImportState (ctx context.Context ,req resource.ImportStateRequest ,resp * resource.ImportStateResponse ) {
330+ var groupID uuid.UUID
327331client := 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 ("Failed to get organization with name %s: %s" ,idParts [0 ],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 ("Failed to get group with name %s: %s" ,idParts [1 ],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>`" )
331354return
332355}
333356group ,err := client .Group (ctx ,groupID )
@@ -339,5 +362,5 @@ func (r *GroupResource) ImportState(ctx context.Context, req resource.ImportStat
339362resp .Diagnostics .AddError ("Client Error" ,"Cannot import groups created via OIDC" )
340363return
341364}
342- resource . ImportStatePassthroughID ( ctx ,path .Root ("id" ),req , resp )
365+ resp . Diagnostics . Append ( resp . State . SetAttribute ( ctx ,path .Root ("id" ),groupID . String ()) ... )
343366}