generated from SAP/repository-template
/
datasource_subaccount_role_collections.go
163 lines (143 loc) · 5.6 KB
/
datasource_subaccount_role_collections.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
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"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 newSubaccountRoleCollectionsDataSource() datasource.DataSource {
return &subaccountRoleCollectionsDataSource{}
}
type subaccountRoleCollectionsRoleType struct {
/* OUTPUT */
RoleTemplateName types.String `tfsdk:"role_template_name"`
RoleTemplateAppId types.String `tfsdk:"role_template_app_id"`
Description types.String `tfsdk:"description"`
Name types.String `tfsdk:"name"`
}
type subaccountRoleCollectionsValueConfig struct {
/* OUTPUT */
Name types.String `tfsdk:"name"`
IsReadOnly types.Bool `tfsdk:"read_only"`
Description types.String `tfsdk:"description"`
Roles []subaccountRoleCollectionsRoleType `tfsdk:"roles"`
}
type subaccountRoleCollectionsDataSourceConfig struct {
/* INPUT */
SubaccountId types.String `tfsdk:"subaccount_id"`
Id types.String `tfsdk:"id"`
/* OUTPUT */
Values []subaccountRoleCollectionsValueConfig `tfsdk:"values"`
}
type subaccountRoleCollectionsDataSource struct {
cli *btpcli.ClientFacade
}
func (ds *subaccountRoleCollectionsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_subaccount_role_collections", req.ProviderTypeName)
}
func (ds *subaccountRoleCollectionsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
ds.cli = req.ProviderData.(*btpcli.ClientFacade)
}
func (ds *subaccountRoleCollectionsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `Gets all role collections.`,
Attributes: map[string]schema.Attribute{
"subaccount_id": schema.StringAttribute{
MarkdownDescription: "The ID of the subaccount.",
Required: true,
Validators: []validator.String{
uuidvalidator.ValidUUID(),
},
},
"id": schema.StringAttribute{ // required by hashicorps terraform plugin testing framework
DeprecationMessage: "Use the `subaccount_id` attribute instead",
MarkdownDescription: "The ID of the subaccount.",
Computed: true,
},
"values": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "The name of the role collection.",
Computed: true,
},
"read_only": schema.BoolAttribute{
MarkdownDescription: "Shows whether the role collection is read-only.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the role collection.",
Computed: true,
},
"roles": schema.SetNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"role_template_name": schema.StringAttribute{
MarkdownDescription: "The name of the referenced role template.",
Computed: true,
},
"role_template_app_id": schema.StringAttribute{
MarkdownDescription: "The name of the referenced template app id",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the referenced role",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the referenced role.",
Computed: true,
},
},
},
Computed: true,
},
},
},
Computed: true,
},
},
}
}
func (ds *subaccountRoleCollectionsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data subaccountRoleCollectionsDataSourceConfig
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
cliRes, _, err := ds.cli.Security.RoleCollection.ListBySubaccount(ctx, data.SubaccountId.ValueString())
if err != nil {
resp.Diagnostics.AddError("API Error Reading Resource Role Collections (Subaccount)", fmt.Sprintf("%s", err))
return
}
data.Id = data.SubaccountId
data.Values = []subaccountRoleCollectionsValueConfig{}
for _, rolecollection := range cliRes {
val := subaccountRoleCollectionsValueConfig{
Name: types.StringValue(rolecollection.Name),
Description: types.StringValue(rolecollection.Description),
IsReadOnly: types.BoolValue(rolecollection.IsReadOnly),
Roles: []subaccountRoleCollectionsRoleType{},
}
for _, ref := range rolecollection.RoleReferences {
val.Roles = append(val.Roles, subaccountRoleCollectionsRoleType{
RoleTemplateName: types.StringValue(ref.RoleTemplateName),
RoleTemplateAppId: types.StringValue(ref.RoleTemplateAppId),
Description: types.StringValue(ref.Description),
Name: types.StringValue(ref.Name),
})
}
// TODO map additional attributes
data.Values = append(data.Values, val)
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}