-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoidc_group_mappings_data_source.go
More file actions
138 lines (125 loc) · 4.03 KB
/
oidc_group_mappings_data_source.go
File metadata and controls
138 lines (125 loc) · 4.03 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package provider
import (
"context"
"fmt"
"slices"
"strings"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Interface impl check.
var (
_ datasource.DataSource = &oidcGroupMappingsDataSource{}
_ datasource.DataSourceWithConfigure = &oidcGroupMappingsDataSource{}
)
type (
oidcGroupMappingsDataSource struct {
client *dtrack.Client
semver *Semver
}
oidcGroupMappingsDataSourceModel struct {
GroupID types.String `tfsdk:"group"`
Teams []oidcGroupMappingsTeamsModel `tfsdk:"teams"`
}
oidcGroupMappingsTeamsModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
)
func NewOidcGroupMappingsDataSource() datasource.DataSource {
return &oidcGroupMappingsDataSource{}
}
func (*oidcGroupMappingsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_oidc_group_mappings"
}
func (*oidcGroupMappingsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetch all teams for an OIDC Group Mapping.",
Attributes: map[string]schema.Attribute{
"group": schema.StringAttribute{
Description: "UUID for the OIDC Group.",
Required: true,
},
"teams": schema.ListNestedAttribute{
Description: "List of teams mapped to the OIDC Group.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "UUID of the team mapped to the group.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "Name of the team mapped to the group.",
Computed: true,
},
},
},
},
},
}
}
func (d *oidcGroupMappingsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state oidcGroupMappingsDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
groupID, diag := TryParseUUID(state.GroupID, LifecycleRead, path.Root("group"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
tflog.Debug(ctx, "Reading OIDC Group Mapping Teams", map[string]any{
"group": groupID,
})
teams, err := d.client.OIDC.GetAllTeamsOf(ctx, dtrack.OIDCGroup{UUID: groupID, Name: ""})
if err != nil {
resp.Diagnostics.AddError(
"Within Read, unable to fetch OIDC Group Mapping Teams.",
"Unexpected error within: "+err.Error(),
)
return
}
slices.SortStableFunc(teams, func(a, b dtrack.Team) int {
return strings.Compare(a.Name, b.Name)
})
newState := oidcGroupMappingsDataSourceModel{
GroupID: types.StringValue(groupID.String()),
Teams: Map(teams, func(team dtrack.Team) oidcGroupMappingsTeamsModel {
return oidcGroupMappingsTeamsModel{
ID: types.StringValue(team.UUID.String()),
Name: types.StringValue(team.Name),
}
}),
}
diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read OIDC Group Mapping Teams", map[string]any{
"group": newState.GroupID.ValueString(),
"teams.#": len(newState.Teams),
})
}
func (d *oidcGroupMappingsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
clientInfoData, ok := req.ProviderData.(clientInfo)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Configure Type",
fmt.Sprintf("Expected provider.clientInfo, got %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = clientInfoData.client
d.semver = clientInfoData.semver
}