/
team_resource.go
215 lines (181 loc) · 6.13 KB
/
team_resource.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
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
package provider
import (
"context"
"fmt"
"github.com/control-monkey/terraform-provider-cm/internal/provider/entities/team"
"github.com/control-monkey/controlmonkey-sdk-go/controlmonkey"
"github.com/control-monkey/terraform-provider-cm/internal/provider/commons"
cmStringValidators "github.com/control-monkey/terraform-provider-cm/internal/provider/validators/string"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ resource.Resource = &TeamResource{}
func NewTeamResource() resource.Resource {
return &TeamResource{}
}
type TeamResource struct {
client *ControlMonkeyAPIClient
}
func (r *TeamResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_team"
}
func (r *TeamResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Creates, updates and destroys teams.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The unique ID of the team.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the team.",
Required: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
"custom_idp_id": schema.StringAttribute{
MarkdownDescription: "Custom ID for identity provider (IdP)",
Optional: true,
Validators: []validator.String{
cmStringValidators.NotBlank(),
},
},
},
}
}
// Configure adds the provider configured client to the data source.
func (r *TeamResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*ControlMonkeyAPIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *ControlMonkeyAPIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
// Read refreshes the Terraform state with the latest data.
func (r *TeamResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
//Get current state
var state team.ResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
res, err := r.client.Client.team.ReadTeam(ctx, id)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(fmt.Sprintf("Failed to read team %s", id), fmt.Sprintf("%s", err))
return
}
team.UpdateStateAfterRead(res, &state)
// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *TeamResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
//Retrieve values from plan
var plan team.ResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
body, _ := team.Converter(&plan, nil, commons.CreateConverter)
res, err := r.client.Client.team.CreateTeam(ctx, body)
if err != nil {
resp.Diagnostics.AddError(
"Team creation failed",
fmt.Sprintf("failed to create team, error: %s", err.Error()),
)
return
}
plan.ID = types.StringValue(controlmonkey.StringValue(res.ID))
// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *TeamResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan team.ResourceModel
var state team.ResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := plan.ID.ValueString()
body, _ := team.Converter(&plan, &state, commons.UpdateConverter)
_, err := r.client.Client.team.UpdateTeam(ctx, id, body)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.Diagnostics.AddError(resourceNotFoundError, fmt.Sprintf("Team '%s' not found", id))
return
}
resp.Diagnostics.AddError(
resourceUpdateFailedError,
fmt.Sprintf("failed to update team %s, error: %s", id, err),
)
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *TeamResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state team.ResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
_, err := r.client.Client.team.DeleteTeam(ctx, id)
if err != nil {
if commons.IsNotFoundResponseError(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Team deletion failed",
fmt.Sprintf("Failed to delete team %s, error: %s", id, err),
)
return
}
}
func (r *TeamResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}