generated from hashicorp/terraform-provider-scaffolding-framework
/
data_user.go
117 lines (101 loc) · 3.49 KB
/
data_user.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
package graph
import (
"context"
"fmt"
"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"
"strings"
)
var _ datasource.DataSource = &UserDataSource{}
func NewUserDataSource() datasource.DataSource {
return &UserDataSource{}
}
type UserDataSource struct {
client *graph.Client
}
type UserDataSourceModel struct {
Descriptor *string `tfsdk:"descriptor"`
DisplayName *string `tfsdk:"display_name"`
MailAddress string `tfsdk:"mail_address"`
Origin *string `tfsdk:"origin"`
OriginId *string `tfsdk:"origin_id"`
ProjectId string `tfsdk:"project_id"`
}
func (d *UserDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_user"
}
func (d *UserDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Use this data source to access information about an existing user within an Azure DevOps project.",
Attributes: map[string]schema.Attribute{
"descriptor": schema.StringAttribute{
MarkdownDescription: "The descriptor of the user.",
Computed: true,
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the user.",
Computed: true,
},
"mail_address": schema.StringAttribute{
MarkdownDescription: "The mail address of the user.",
Required: true,
Validators: []validator.String{
validators.StringNotEmpty(),
},
},
"origin": schema.StringAttribute{
MarkdownDescription: "The type of source provider for the user (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 user.",
Required: true,
Validators: []validator.String{
validators.UUID(),
},
},
},
}
}
func (d *UserDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*clients.AzureDevOpsClient).GraphClient
}
func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model UserDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
users, err := d.client.GetUsers(ctx, model.ProjectId, "")
if err != nil {
resp.Diagnostics.AddError("Unable to retrieve users", err.Error())
return
}
var user *graph.GraphUser
for _, u := range *users {
if strings.EqualFold(*u.MailAddress, model.MailAddress) {
user = &u
break
}
}
if user == nil {
resp.Diagnostics.AddError(fmt.Sprintf("User with mail address '%s' not found", model.MailAddress), "")
return
}
model.Descriptor = user.Descriptor
model.DisplayName = user.DisplayName
model.Origin = user.Origin
model.OriginId = user.OriginId
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}