-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_crowd_settings.go
290 lines (249 loc) · 11.4 KB
/
resource_crowd_settings.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package platform
import (
"context"
"net/http"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"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/booldefault"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/jfrog/terraform-provider-shared/util"
utilfw "github.com/jfrog/terraform-provider-shared/util/fw"
validatorfw_string "github.com/jfrog/terraform-provider-shared/validator/fw/string"
)
func NewCrowdSettingsResource() resource.Resource {
return &CrowdSettingsResource{
JFrogResource: util.JFrogResource{
TypeName: "platform_crowd_settings",
ValidArtifactoryVersion: "7.64.0",
DocumentEndpoint: "access/api/v1/crowd",
},
}
}
type CrowdSettingsResource struct {
util.JFrogResource
}
type CrowdSettingsResourceModel struct {
Enable types.Bool `tfsdk:"enable"`
ServerURL types.String `tfsdk:"server_url"`
ApplicationName types.String `tfsdk:"application_name"`
Password types.String `tfsdk:"password"`
SessionValidationInterval types.Int64 `tfsdk:"session_validation_interval"`
UseDefaultProxy types.Bool `tfsdk:"use_default_proxy"`
AutoUserCreation types.Bool `tfsdk:"auto_user_creation"`
AllowUserToAccessProfile types.Bool `tfsdk:"allow_user_to_access_profile"`
DirectAuthentication types.Bool `tfsdk:"direct_authentication"`
OverrideAllGroupsUponLogin types.Bool `tfsdk:"override_all_groups_upon_login"`
}
func (r *CrowdSettingsResourceModel) toAPIModel(_ context.Context, apiModel *CrowdSettingsAPIModel) diag.Diagnostics {
diags := diag.Diagnostics{}
apiModel.Enable = r.Enable.ValueBool()
apiModel.ServerURL = r.ServerURL.ValueString()
apiModel.ApplicationName = r.ApplicationName.ValueString()
apiModel.Password = r.Password.ValueString()
apiModel.SessionValidationInterval = r.SessionValidationInterval.ValueInt64()
apiModel.UseDefaultProxy = r.UseDefaultProxy.ValueBoolPointer()
apiModel.AutoUserCreation = r.AutoUserCreation.ValueBoolPointer()
apiModel.AllowUserToAccessProfile = r.AllowUserToAccessProfile.ValueBoolPointer()
apiModel.DirectAuthentication = r.DirectAuthentication.ValueBoolPointer()
apiModel.OverrideAllGroupsUponLogin = r.OverrideAllGroupsUponLogin.ValueBoolPointer()
return diags
}
func (r *CrowdSettingsResourceModel) fromAPIModel(_ context.Context, apiModel *CrowdSettingsAPIModel) (ds diag.Diagnostics) {
r.Enable = types.BoolValue(apiModel.Enable)
r.ServerURL = types.StringValue(apiModel.ServerURL)
r.ApplicationName = types.StringValue(apiModel.ApplicationName)
r.SessionValidationInterval = types.Int64Value(apiModel.SessionValidationInterval)
r.UseDefaultProxy = types.BoolPointerValue(apiModel.UseDefaultProxy)
r.AutoUserCreation = types.BoolPointerValue(apiModel.AutoUserCreation)
r.AllowUserToAccessProfile = types.BoolPointerValue(apiModel.AllowUserToAccessProfile)
r.DirectAuthentication = types.BoolPointerValue(apiModel.DirectAuthentication)
r.OverrideAllGroupsUponLogin = types.BoolPointerValue(apiModel.OverrideAllGroupsUponLogin)
return
}
type CrowdSettingsAPIModel struct {
Enable bool `json:"enable_integration"`
ServerURL string `json:"server_url"`
ApplicationName string `json:"application_name"`
Password string `json:"password"`
SessionValidationInterval int64 `json:"session_validation_interval"`
UseDefaultProxy *bool `json:"use_default_proxy"`
AutoUserCreation *bool `json:"auto_user_creation"`
AllowUserToAccessProfile *bool `json:"allow_user_to_access_profile"`
DirectAuthentication *bool `json:"direct_authentication"`
OverrideAllGroupsUponLogin *bool `json:"override_all_groups_upon_login"`
}
func (r *CrowdSettingsResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"enable": schema.BoolAttribute{
Required: true,
MarkdownDescription: "Use this to enable security integration with Atlassian Crowd or JIRA.",
},
"server_url": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
validatorfw_string.IsURLHttpOrHttps(),
},
MarkdownDescription: "The full URL of the server to use.",
},
"application_name": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
MarkdownDescription: "The application name configured for JPD in Crowd/JIRA.",
},
"password": schema.StringAttribute{
Required: true,
Sensitive: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
MarkdownDescription: "The application password configured for JPD in Crowd/JIRA.",
},
"session_validation_interval": schema.Int64Attribute{
Required: true,
Validators: []validator.Int64{
int64validator.AtLeast(0),
},
MarkdownDescription: "The time window (min) during which the session does not need to be validated. If set to `0`, the token expires only when the session expires.",
},
"use_default_proxy": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
MarkdownDescription: "If a default proxy definition exists, it is used to pass through to the Crowd Server. Default value is `false`.",
},
"auto_user_creation": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
MarkdownDescription: "When set, authenticated users are automatically created in Artifactory. When not set, for every request from a Crowd user, the user is temporarily associated with default groups (if such groups are defined), and the permissions for these groups apply. Without automatic user creation, you must manually create the user in Artifactory to manage user permissions not attached to their default groups. Default value is `true`.",
},
"allow_user_to_access_profile": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
MarkdownDescription: "Auto created users will have access to their profile page and will be able to perform actions such as generating an API key. Default value is `false`.",
},
"direct_authentication": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
MarkdownDescription: "This corresponds to 'Users Management Server' option in Artifactory UI (`true` = JIRA, `false` = Crowd). Default value is `false`.",
},
"override_all_groups_upon_login": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
MarkdownDescription: "When a user logs in with CROWD, only groups retrieved from CROWD will be associated with the user. Default value is `false`.",
},
},
MarkdownDescription: "Provides a JFrog [Crowd Settings](https://jfrog.com/help/r/jfrog-platform-administration-documentation/atlassian-crowd-and-jira-integration) resource. This allows you to delegate authentication requests to Atlassian Crowd/JIRA, use authenticated Crowd/JIRA users and have the JPD participate in a transparent SSO environment managed by Crowd/JIRA.",
}
}
func (r *CrowdSettingsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
go util.SendUsageResourceCreate(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var plan CrowdSettingsResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
var crowdSettings CrowdSettingsAPIModel
resp.Diagnostics.Append(plan.toAPIModel(ctx, &crowdSettings)...)
if resp.Diagnostics.HasError() {
return
}
var jfrogErrors util.JFrogErrors
response, err := r.ProviderData.Client.R().
SetBody(crowdSettings).
SetError(&jfrogErrors).
Put(r.DocumentEndpoint)
if err != nil {
utilfw.UnableToCreateResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToCreateResourceError(resp, jfrogErrors.String())
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *CrowdSettingsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
go util.SendUsageResourceRead(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var state CrowdSettingsResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var crowdSettings CrowdSettingsAPIModel
response, err := r.ProviderData.Client.R().
SetResult(&crowdSettings).
Get(r.DocumentEndpoint)
if err != nil {
utilfw.UnableToRefreshResourceError(resp, err.Error())
return
}
// Treat HTTP 404 Not Found status as a signal to recreate resource
// and return early
if response.StatusCode() == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
if response.IsError() {
utilfw.UnableToRefreshResourceError(resp, response.String())
return
}
// Convert from the API data model to the Terraform data model
// and refresh any attribute values.
resp.Diagnostics.Append(state.fromAPIModel(ctx, &crowdSettings)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *CrowdSettingsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
go util.SendUsageResourceUpdate(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var plan CrowdSettingsResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
var crowdSettings CrowdSettingsAPIModel
resp.Diagnostics.Append(plan.toAPIModel(ctx, &crowdSettings)...)
if resp.Diagnostics.HasError() {
return
}
response, err := r.ProviderData.Client.R().
SetBody(crowdSettings).
Put(r.DocumentEndpoint)
if err != nil {
utilfw.UnableToUpdateResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToUpdateResourceError(resp, response.String())
return
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *CrowdSettingsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
go util.SendUsageResourceDelete(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
resp.Diagnostics.AddWarning(
"Unable to Delete Resource",
"Crowd settings cannot be deleted.",
)
}
func (r *CrowdSettingsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("server_url"), req, resp)
}