55"fmt"
66
77"github.com/coder/coder/v2/codersdk"
8+ "github.com/coder/terraform-provider-coderd/internal"
89"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
910"github.com/hashicorp/terraform-plugin-framework/datasource"
1011"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -28,9 +29,9 @@ type GroupDataSource struct {
2829// GroupDataSourceModel describes the data source data model.
2930type GroupDataSourceModel struct {
3031// ID or name and organization ID must be set
31- ID UUID `tfsdk:"id"`
32- Name types.String `tfsdk:"name"`
33- OrganizationID UUID `tfsdk:"organization_id"`
32+ ID internal. UUID `tfsdk:"id"`
33+ Name types.String `tfsdk:"name"`
34+ OrganizationID internal. UUID `tfsdk:"organization_id"`
3435
3536DisplayName types.String `tfsdk:"display_name"`
3637AvatarURL types.String `tfsdk:"avatar_url"`
@@ -40,14 +41,14 @@ type GroupDataSourceModel struct {
4041}
4142
4243type Member struct {
43- ID UUID `tfsdk:"id"`
44- Username types.String `tfsdk:"username"`
45- Email types.String `tfsdk:"email"`
46- CreatedAt types.Int64 `tfsdk:"created_at"`
47- LastSeenAt types.Int64 `tfsdk:"last_seen_at"`
48- Status types.String `tfsdk:"status"`
49- LoginType types.String `tfsdk:"login_type"`
50- ThemePreference types.String `tfsdk:"theme_preference"`
44+ ID internal. UUID `tfsdk:"id"`
45+ Username types.String `tfsdk:"username"`
46+ Email types.String `tfsdk:"email"`
47+ CreatedAt types.Int64 `tfsdk:"created_at"`
48+ LastSeenAt types.Int64 `tfsdk:"last_seen_at"`
49+ Status types.String `tfsdk:"status"`
50+ LoginType types.String `tfsdk:"login_type"`
51+ ThemePreference types.String `tfsdk:"theme_preference"`
5152}
5253
5354func (d * GroupDataSource )Metadata (ctx context.Context ,req datasource.MetadataRequest ,resp * datasource.MetadataResponse ) {
@@ -63,7 +64,7 @@ func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaReque
6364MarkdownDescription :"The ID of the group to retrieve. This field will be populated if a name and organization ID is supplied." ,
6465Optional :true ,
6566Computed :true ,
66- CustomType :UUIDType ,
67+ CustomType :internal . UUIDType ,
6768Validators : []validator.String {
6869stringvalidator .AtLeastOneOf (path.Expressions {
6970path .MatchRoot ("name" ),
@@ -78,7 +79,7 @@ func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaReque
7879},
7980"organization_id" : schema.StringAttribute {
8081MarkdownDescription :"The organization ID that the group belongs to. This field will be populated if an ID is supplied. Defaults to the provider default organization ID." ,
81- CustomType :UUIDType ,
82+ CustomType :internal . UUIDType ,
8283Optional :true ,
8384Computed :true ,
8485},
@@ -102,7 +103,7 @@ func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaReque
102103NestedObject : schema.NestedAttributeObject {
103104Attributes :map [string ]schema.Attribute {
104105"id" : schema.StringAttribute {
105- CustomType :UUIDType ,
106+ CustomType :internal . UUIDType ,
106107Computed :true ,
107108},
108109"username" : schema.StringAttribute {
@@ -176,7 +177,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
176177client := d .data .Client
177178
178179if data .OrganizationID .IsNull () {
179- data .OrganizationID = UUIDValue (d .data .DefaultOrganizationID )
180+ data .OrganizationID = internal . UUIDValue (d .data .DefaultOrganizationID )
180181}
181182
182183var (
@@ -187,7 +188,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
187188groupID := data .ID .ValueUUID ()
188189group ,err = client .Group (ctx ,groupID )
189190if err != nil {
190- if isNotFound (err ) {
191+ if internal . IsNotFound (err ) {
191192resp .Diagnostics .AddWarning ("Client Warning" ,fmt .Sprintf ("Group with ID %s not found. Marking as deleted." ,groupID .String ()))
192193resp .State .RemoveResource (ctx )
193194return
@@ -196,19 +197,19 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
196197return
197198}
198199data .Name = types .StringValue (group .Name )
199- data .OrganizationID = UUIDValue (group .OrganizationID )
200+ data .OrganizationID = internal . UUIDValue (group .OrganizationID )
200201}else {
201202group ,err = client .GroupByOrgAndName (ctx ,data .OrganizationID .ValueUUID (),data .Name .ValueString ())
202203if err != nil {
203- if isNotFound (err ) {
204+ if internal . IsNotFound (err ) {
204205resp .Diagnostics .AddWarning ("Client Warning" ,fmt .Sprintf ("Group with name %s not found in organization with ID %s. Marking as deleted." ,data .Name .ValueString (),data .OrganizationID .ValueString ()))
205206resp .State .RemoveResource (ctx )
206207return
207208}
208209resp .Diagnostics .AddError ("Failed to get group by name and org ID" ,err .Error ())
209210return
210211}
211- data .ID = UUIDValue (group .ID )
212+ data .ID = internal . UUIDValue (group .ID )
212213}
213214
214215data .DisplayName = types .StringValue (group .DisplayName )
@@ -217,7 +218,7 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
217218members := make ([]Member ,0 ,len (group .Members ))
218219for _ ,member := range group .Members {
219220members = append (members ,Member {
220- ID :UUIDValue (member .ID ),
221+ ID :internal . UUIDValue (member .ID ),
221222Username :types .StringValue (member .Username ),
222223Email :types .StringValue (member .Email ),
223224CreatedAt :types .Int64Value (member .CreatedAt .Unix ()),