generated from SAP/repository-template
/
resource_directory_role.go
232 lines (199 loc) · 8.33 KB
/
resource_directory_role.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
package provider
import (
"context"
"fmt"
"strings"
"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/SAP/terraform-provider-btp/internal/btpcli"
"github.com/SAP/terraform-provider-btp/internal/validation/uuidvalidator"
)
func newDirectoryRoleResource() resource.Resource {
return &directoryRoleResource{}
}
type directoryRoleResource struct {
cli *btpcli.ClientFacade
}
func (rs *directoryRoleResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_directory_role", req.ProviderTypeName)
}
func (rs *directoryRoleResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
rs.cli = req.ProviderData.(*btpcli.ClientFacade)
}
func (rs *directoryRoleResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `Creates a role in a directory.
__Further documentation:__
<https://help.sap.com/docs/btp/sap-business-technology-platform/role-collections-and-roles-in-global-accounts-directories-and-subaccounts>`,
Attributes: map[string]schema.Attribute{
"directory_id": schema.StringAttribute{
MarkdownDescription: "The ID of the directory.",
Required: true,
Validators: []validator.String{
uuidvalidator.ValidUUID(),
},
},
"id": schema.StringAttribute{ // required by hashicorps terraform plugin testing framework
DeprecationMessage: "Use the `directory_id`, `name`, `role_template_name` and `app_id` attributes instead",
MarkdownDescription: "The combined unique ID of the role.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the role.",
Required: true,
},
"app_id": schema.StringAttribute{
MarkdownDescription: "The ID of the xsuaa application.",
Required: true,
},
"role_template_name": schema.StringAttribute{
MarkdownDescription: "The name of the role template.",
Required: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The role description.",
Optional: true,
Computed: true,
},
"read_only": schema.BoolAttribute{
MarkdownDescription: "Shows whether the role can be modified or not.",
Computed: true,
},
"scopes": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "The name of the scope.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the scope.",
Computed: true,
},
"custom_grant_as_authority_to_apps": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
},
"custom_granted_apps": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
},
"grant_as_authority_to_apps": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
},
"granted_apps": schema.SetAttribute{
ElementType: types.StringType,
Computed: true,
},
},
},
MarkdownDescription: "Scopes available with this role.",
Computed: true,
},
},
}
}
func (rs *directoryRoleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state directoryRoleType
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
cliRes, _, err := rs.cli.Security.Role.GetByDirectory(ctx,
state.DirectoryId.ValueString(),
state.Name.ValueString(),
state.RoleTemplateAppId.ValueString(),
state.RoleTemplateName.ValueString(),
)
if err != nil {
resp.Diagnostics.AddError("API Error Reading Resource Role (Directory)", fmt.Sprintf("%s", err))
return
}
updatedState, diags := directoryRoleFromValue(ctx, cliRes)
updatedState.DirectoryId = state.DirectoryId
updatedState.Id = state.DirectoryId
if updatedState.Id.IsNull() || updatedState.Id.IsUnknown() {
// Setting ID of state - required by hashicorps terraform plugin testing framework for Import. See issue https://github.com/hashicorp/terraform-plugin-testing/issues/84
updatedState.Id = types.StringValue(fmt.Sprintf("%s,%s,%s,%s", state.DirectoryId.ValueString(), state.Name.ValueString(), state.RoleTemplateName.ValueString(), state.RoleTemplateAppId.ValueString()))
}
resp.Diagnostics.Append(diags...)
diags = resp.State.Set(ctx, &updatedState)
resp.Diagnostics.Append(diags...)
}
func (rs *directoryRoleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan directoryRoleType
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
cliRes, _, err := rs.cli.Security.Role.CreateByDirectory(ctx, &btpcli.DirectoryRoleCreateInput{
RoleName: plan.Name.ValueString(),
AppId: plan.RoleTemplateAppId.ValueString(),
RoleTemplateName: plan.RoleTemplateName.ValueString(),
DirectoryId: plan.DirectoryId.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("API Error Creating Resource Role (Directory)", fmt.Sprintf("%s", err))
return
}
updatedPlan, diags := directoryRoleFromValue(ctx, cliRes)
updatedPlan.DirectoryId = plan.DirectoryId
// Setting ID of state - required by hashicorps terraform plugin testing framework for Create. See issue https://github.com/hashicorp/terraform-plugin-testing/issues/84
updatedPlan.Id = types.StringValue(fmt.Sprintf("%s,%s,%s,%s", plan.DirectoryId.ValueString(), plan.Name.ValueString(), plan.RoleTemplateName.ValueString(), plan.RoleTemplateAppId.ValueString()))
resp.Diagnostics.Append(diags...)
diags = resp.State.Set(ctx, &updatedPlan)
resp.Diagnostics.Append(diags...)
}
func (rs *directoryRoleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan directoryRoleType
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.AddError("API Error Updating Resource Role (Directory)", "This resource is not supposed to be updated")
if resp.Diagnostics.HasError() {
return
}
}
func (rs *directoryRoleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state directoryRoleType
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
_, _, err := rs.cli.Security.Role.DeleteByDirectory(ctx, state.DirectoryId.ValueString(), state.Name.ValueString(), state.RoleTemplateAppId.ValueString(), state.RoleTemplateName.ValueString())
if err != nil {
resp.Diagnostics.AddError("API Error Deleting Resource Role (Directory)", fmt.Sprintf("%s", err))
return
}
}
func (rs *directoryRoleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, ",")
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: directory_id, name, role_template_name, app_id. Got: %q", req.ID),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("directory_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("role_template_name"), idParts[2])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app_id"), idParts[3])...)
}