-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathteam_permission_resource.go
More file actions
234 lines (212 loc) · 6.8 KB
/
team_permission_resource.go
File metadata and controls
234 lines (212 loc) · 6.8 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package provider
import (
"context"
"fmt"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var (
_ resource.Resource = &teamPermissionResource{}
_ resource.ResourceWithConfigure = &teamPermissionResource{}
)
type (
teamPermissionResource struct {
client *dtrack.Client
semver *Semver
}
teamPermissionResourceModel struct {
TeamID types.String `tfsdk:"team"`
Permission types.String `tfsdk:"permission"`
}
)
func NewTeamPermissionResource() resource.Resource {
return &teamPermissionResource{}
}
func (*teamPermissionResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_team_permission"
}
func (*teamPermissionResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages the attachment of a Permission to a Team. Conflicts with `dependencytrack_team_permissions`.",
Attributes: map[string]schema.Attribute{
"team": schema.StringAttribute{
Description: "UUID for the Team for which to manage the permission.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"permission": schema.StringAttribute{
Description: "Permission name to attach to the Team.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
func (r *teamPermissionResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan teamPermissionResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
team, diag := TryParseUUID(plan.TeamID, LifecycleCreate, path.Root("team"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
permission := dtrack.Permission{
Name: plan.Permission.ValueString(),
}
tflog.Debug(ctx, "Creating Team Permission", map[string]any{
"team": team.String(),
"permission": permission.Name,
})
_, err := r.client.Permission.AddPermissionToTeam(ctx, permission, team)
if err != nil {
resp.Diagnostics.AddError(
"Error creating team permission",
"Unexpected error: "+err.Error(),
)
return
}
plan.TeamID = types.StringValue(team.String())
plan.Permission = types.StringValue(permission.Name)
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Created Team Permission", map[string]any{
"team": plan.TeamID.ValueString(),
"permission": plan.Permission.ValueString(),
})
}
func (r *teamPermissionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Fetch state.
var state teamPermissionResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Refresh.
teamID, diag := TryParseUUID(state.TeamID, LifecycleRead, path.Root("team"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
tflog.Debug(ctx, "Reading Team Permission", map[string]any{
"team": teamID.String(),
"permission": state.Permission.ValueString(),
})
team, err := r.client.Team.Get(ctx, teamID)
if err != nil {
resp.Diagnostics.AddError(
"Unable to get updated team",
"Error with reading team: "+teamID.String()+", in original error: "+err.Error(),
)
return
}
permission, err := Find(team.Permissions, func(permission dtrack.Permission) bool {
return permission.Name == state.Permission.ValueString()
})
if err != nil {
resp.Diagnostics.AddError(
"Within Read, unable to identify team permission",
"Unexpected Error from: "+err.Error(),
)
return
}
state.TeamID = types.StringValue(team.UUID.String())
state.Permission = types.StringValue(permission.Name)
// Update state.
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read Team Permission", map[string]any{
"team": state.TeamID.ValueString(),
"permission": state.Permission.ValueString(),
})
}
func (*teamPermissionResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Nothing to Update. This resource only has Create, Delete actions.
// Get State.
var plan teamPermissionResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Update State.
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Updated Team Permission", map[string]any{
"team": plan.TeamID.ValueString(),
"permission": plan.Permission.ValueString(),
})
}
func (r *teamPermissionResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Load state.
var state teamPermissionResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map TF to SDK.
team, diag := TryParseUUID(state.TeamID, LifecycleDelete, path.Root("team"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
permission := dtrack.Permission{
Name: state.Permission.ValueString(),
}
// Execute.
tflog.Debug(ctx, "Deleting Team Permission", map[string]any{
"team": team.String(),
"permission": permission.Name,
})
_, err := r.client.Permission.RemovePermissionFromTeam(ctx, permission, team)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete team permission",
"Unexpected error when trying to delete team permission: "+team.String()+", error: "+err.Error(),
)
return
}
tflog.Debug(ctx, "Deleted Team Permission", map[string]any{
"team": state.TeamID.ValueString(),
"permission": state.Permission.ValueString(),
})
}
func (r *teamPermissionResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.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
}
r.client = clientInfoData.client
r.semver = clientInfoData.semver
}