-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathorganization_group_project.go
283 lines (237 loc) · 8.68 KB
/
organization_group_project.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
package organization
import (
"context"
"fmt"
"github.com/aiven/aiven-go-client/v2"
"github.com/aiven/go-client-codegen/handler/account"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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"
"github.com/aiven/terraform-provider-aiven/internal/plugin/errmsg"
"github.com/aiven/terraform-provider-aiven/internal/plugin/util"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
)
var (
_ resource.Resource = &organizationGroupProjectResource{}
_ resource.ResourceWithConfigure = &organizationGroupProjectResource{}
_ resource.ResourceWithImportState = &organizationGroupProjectResource{}
_ util.TypeNameable = &organizationGroupProjectResource{}
)
// NewOrganizationGroupProjectResource is a constructor for the organization group project relation resource.
func NewOrganizationGroupProjectResource() resource.Resource {
return &organizationGroupProjectResource{}
}
// organizationGroupUserResource is the organization group project relation resource implementation.
type organizationGroupProjectResource struct {
// client is the instance of the Aiven client to use.
client *aiven.Client
// typeName is the name of the resource type.
typeName string
}
// organizationGroupProjectResourceModel is the model for the organization group project relation resource.
type organizationGroupProjectResourceModel struct {
// ID is the compound identifier of the organization group project relation.
ID types.String `tfsdk:"id"`
// Project is the name of the project.
Project types.String `tfsdk:"project"`
// GroupID is the identifier of the organization group.
GroupID types.String `tfsdk:"group_id"`
// Role is the role of the organization group project relation.
Role types.String `tfsdk:"role"`
// Timeouts is the configuration for resource-specific timeouts.
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
// Metadata returns the metadata for the organization group project relation resource.
func (r *organizationGroupProjectResource) Metadata(
_ context.Context,
req resource.MetadataRequest,
resp *resource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_organization_group_project"
r.typeName = resp.TypeName
}
// TypeName returns the resource type name.
func (r *organizationGroupProjectResource) TypeName() string {
return r.typeName
}
// Schema returns the schema for the resource.
func (r *organizationGroupProjectResource) Schema(
ctx context.Context,
_ resource.SchemaRequest,
resp *resource.SchemaResponse) {
resp.Schema = util.GeneralizeSchema(ctx, schema.Schema{
Description: `Adds and manages a group of users as members of a project.
**This resource is deprecated.** Use ` + "`aiven_organization_permission`" + ` and
[migrate existing aiven_organization_group_project resources](https://registry.terraform.io/providers/aiven/aiven/latest/docs/guides/update-deprecated-resources)
to the new resource. **Do not use the aiven_organization_group_project and aiven_organization_permission resources together**.
`,
DeprecationMessage: "This resource is deprecated. Use aiven_organization_permission instead.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "A compound identifier of the resource in the format `project/group_id`.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"project": schema.StringAttribute{
Description: "The project that the users in the group are members of.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"group_id": schema.StringAttribute{
Description: "The ID of the user group.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"role": schema.StringAttribute{
Description: userconfig.Desc("[Project-level role](https://aiven.io/docs/platform/reference/project-member-privileges) assigned to all users in the group.").PossibleValuesString(account.TeamTypeChoices()...).Build(),
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.OneOf(account.TeamTypeChoices()...),
},
},
},
})
}
// Configure is called to configure the resource.
func (r *organizationGroupProjectResource) Configure(
_ context.Context,
req resource.ConfigureRequest,
resp *resource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*aiven.Client)
if !ok {
resp.Diagnostics = util.DiagErrorUnexpectedProviderDataType(resp.Diagnostics, req.ProviderData)
return
}
r.client = client
}
// fillModel fills the organization group project relation model from the Aiven API.
func (r *organizationGroupProjectResource) fillModel(
ctx context.Context,
model *organizationGroupProjectResourceModel,
) error {
list, err := r.client.ProjectOrganization.List(ctx, model.Project.ValueString())
if err != nil {
return err
}
var group *aiven.ProjectUserGroup
for _, g := range list {
if g.OrganizationGroupID == model.GroupID.ValueString() {
group = g
break
}
}
if group == nil {
return fmt.Errorf(
errmsg.AivenResourceNotFound,
r.TypeName(),
util.ComposeID(model.Project.ValueString(), model.GroupID.ValueString()),
)
}
model.GroupID = types.StringValue(group.OrganizationGroupID)
model.Role = types.StringValue(group.Role)
// There is no API endpoint to get the permissions of the organization group project relation.
return nil
}
// Create creates an organization group project relation resource.
func (r *organizationGroupProjectResource) Create(
ctx context.Context,
req resource.CreateRequest,
resp *resource.CreateResponse,
) {
var plan organizationGroupProjectResourceModel
if !util.PlanStateToModel(ctx, &req.Plan, &plan, &resp.Diagnostics) {
return
}
if err := r.client.ProjectOrganization.Add(
ctx,
plan.Project.ValueString(),
plan.GroupID.ValueString(),
plan.Role.ValueString(),
); err != nil {
resp.Diagnostics = util.DiagErrorCreatingResource(resp.Diagnostics, r, err)
return
}
plan.ID = types.StringValue(util.ComposeID(plan.Project.ValueString(), plan.GroupID.ValueString()))
err := r.fillModel(ctx, &plan)
if err != nil {
resp.Diagnostics = util.DiagErrorCreatingResource(resp.Diagnostics, r, err)
return
}
if !util.ModelToPlanState(ctx, plan, &resp.State, &resp.Diagnostics) {
return
}
}
// Read reads the existing state of the resource.
func (r *organizationGroupProjectResource) Read(
ctx context.Context,
req resource.ReadRequest,
resp *resource.ReadResponse,
) {
var state organizationGroupProjectResourceModel
if !util.PlanStateToModel(ctx, &req.State, &state, &resp.Diagnostics) {
return
}
err := r.fillModel(ctx, &state)
if err != nil {
resp.Diagnostics = util.DiagErrorReadingResource(resp.Diagnostics, r, err)
return
}
if !util.ModelToPlanState(ctx, state, &resp.State, &resp.Diagnostics) {
return
}
}
// Update updates an organization group project resource.
func (r *organizationGroupProjectResource) Update(
_ context.Context,
_ resource.UpdateRequest,
resp *resource.UpdateResponse,
) {
resp.Diagnostics = util.DiagErrorUpdatingResourceNotSupported(resp.Diagnostics, r)
}
// Delete deletes an organization group project relation resource.
func (r *organizationGroupProjectResource) Delete(
ctx context.Context,
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) {
var plan organizationGroupProjectResourceModel
if !util.PlanStateToModel(ctx, &req.State, &plan, &resp.Diagnostics) {
return
}
if err := r.client.ProjectOrganization.Delete(
ctx,
plan.Project.ValueString(),
plan.GroupID.ValueString(),
); err != nil {
resp.Diagnostics = util.DiagErrorDeletingResource(resp.Diagnostics, r, err)
return
}
}
// ImportState imports an existing resource into Terraform.
func (r *organizationGroupProjectResource) ImportState(
ctx context.Context,
req resource.ImportStateRequest,
resp *resource.ImportStateResponse,
) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
util.UnpackCompoundID(ctx, req, resp, "project", "group_id")
}