generated from hashicorp/terraform-provider-scaffolding-framework
/
data_groups.go
120 lines (106 loc) · 3.85 KB
/
data_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package graph
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/scordonnier/terraform-provider-azuredevops/internal/clients"
"github.com/scordonnier/terraform-provider-azuredevops/internal/clients/graph"
"github.com/scordonnier/terraform-provider-azuredevops/internal/validators"
)
var _ datasource.DataSource = &GroupsDataSource{}
func NewGroupsDataSource() datasource.DataSource {
return &GroupsDataSource{}
}
type GroupsDataSource struct {
client *graph.Client
}
type GroupsDataSourceModel struct {
ProjectId string `tfsdk:"project_id"`
Groups []GroupDataSourceModel `tfsdk:"groups"`
}
func (d *GroupsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_groups"
}
func (d *GroupsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Use this data source to access information about existing groups within an Azure DevOps project.",
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
MarkdownDescription: "The ID of the project.",
Required: true,
Validators: []validator.String{
validators.UUID(),
},
},
"groups": schema.ListNestedAttribute{
MarkdownDescription: "The list of groups within the project.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"description": schema.StringAttribute{
MarkdownDescription: "The description of the group.",
Computed: true,
},
"descriptor": schema.StringAttribute{
MarkdownDescription: "The descriptor of the group.",
Computed: true,
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the group.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the group.",
Computed: true,
},
"origin": schema.StringAttribute{
MarkdownDescription: "The type of source provider for the group (eg. AD, AAD, MSA).",
Computed: true,
},
"origin_id": schema.StringAttribute{
MarkdownDescription: "The unique identifier from the system of origin.",
Computed: true,
},
"project_id": schema.StringAttribute{
MarkdownDescription: "The project ID of the group.",
Computed: true,
},
},
},
},
},
}
}
func (d *GroupsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*clients.AzureDevOpsClient).GraphClient
}
func (d *GroupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model GroupsDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
groups, err := d.client.GetGroups(ctx, model.ProjectId, "")
if err != nil {
resp.Diagnostics.AddError("Unable to retrieve groups", err.Error())
return
}
var groupModels []GroupDataSourceModel
for _, group := range *groups {
groupModels = append(groupModels, GroupDataSourceModel{
Description: group.Description,
Descriptor: group.Descriptor,
DisplayName: *group.DisplayName,
Name: group.PrincipalName,
Origin: group.Origin,
OriginId: group.OriginId,
ProjectId: model.ProjectId,
})
}
model.Groups = groupModels
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}