-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathteam_resource.go
More file actions
255 lines (230 loc) · 6.51 KB
/
team_resource.go
File metadata and controls
255 lines (230 loc) · 6.51 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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 = &teamResource{}
_ resource.ResourceWithConfigure = &teamResource{}
_ resource.ResourceWithImportState = &teamResource{}
)
type (
teamResource struct {
client *dtrack.Client
semver *Semver
}
teamResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
)
func NewTeamResource() resource.Resource {
return &teamResource{}
}
func (*teamResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_team"
}
func (*teamResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a Team.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "UUID for the Team as generated by DependencyTrack.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Description: "Name of the Team.",
Required: true,
},
},
}
}
func (r *teamResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan teamResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
teamReq := dtrack.Team{
Name: plan.Name.ValueString(),
}
tflog.Debug(ctx, "Creating Team", map[string]any{
"name": teamReq.Name,
})
teamRes, err := r.client.Team.Create(ctx, teamReq)
if err != nil {
resp.Diagnostics.AddError(
"Error creating team",
"Unexpected error: "+err.Error(),
)
return
}
plan.ID = types.StringValue(teamRes.UUID.String())
plan.Name = types.StringValue(teamRes.Name)
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Created Team", map[string]any{
"id": plan.ID,
"name": plan.Name,
})
}
func (r *teamResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Fetch state.
var state teamResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Refresh.
teamID, diag := TryParseUUID(state.ID, LifecycleRead, path.Root("id"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
tflog.Debug(ctx, "Reading Team", map[string]any{
"id": teamID.String(),
"name": state.Name.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
}
state.ID = types.StringValue(team.UUID.String())
state.Name = types.StringValue(team.Name)
// Update state.
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read Team", map[string]any{
"id": state.ID.ValueString(),
"name": state.Name.ValueString(),
})
}
func (r *teamResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get State.
var plan teamResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map TF to SDK.
id, diag := TryParseUUID(plan.ID, LifecycleUpdate, path.Root("id"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
teamReq := dtrack.Team{
UUID: id,
Name: plan.Name.ValueString(),
}
// Execute.
tflog.Debug(ctx, "Updating Team", map[string]any{
"id": teamReq.UUID.String(),
"name": teamReq.Name,
})
teamRes, err := r.client.Team.Update(ctx, teamReq)
if err != nil {
resp.Diagnostics.AddError(
"Unable to update team",
"Error in: "+id.String()+", from: "+err.Error(),
)
return
}
// Map SDK to TF.
plan.ID = types.StringValue(teamRes.UUID.String())
plan.Name = types.StringValue(teamRes.Name)
// Update State.
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Updated Team", map[string]any{
"id": plan.ID.ValueString(),
"name": plan.Name.ValueString(),
})
}
func (r *teamResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Load state.
var state teamResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map TF to SDK.
id, diag := TryParseUUID(state.ID, LifecycleDelete, path.Root("id"))
if diag != nil {
resp.Diagnostics.Append(diag)
return
}
team := dtrack.Team{
UUID: id,
}
// Execute.
tflog.Debug(ctx, "Deleting Team", map[string]any{
"id": team.UUID.String(),
})
err := r.client.Team.Delete(ctx, team)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete team",
"Unexpected error when trying to delete team: "+id.String()+", error: "+err.Error(),
)
return
}
tflog.Debug(ctx, "Deleted Team", map[string]any{
"id": state.ID.ValueString(),
"name": state.Name.ValueString(),
})
}
func (*teamResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
tflog.Debug(ctx, "Importing Team", map[string]any{
"id": req.ID,
})
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Imported Team", map[string]any{
"id": req.ID,
})
}
func (r *teamResource) 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
}