Skip to content

Commit f9b657e

Browse files
committed
updated handling of users,policies
1 parent 54a0ee2 commit f9b657e

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

internal/provider/protocols/protocols_s3_group_data_source.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type ProtocolsS3GroupDataSourceModel struct {
3636
CxProfileName types.String `tfsdk:"cx_profile_name"`
3737
Name types.String `tfsdk:"name"`
3838
Comment types.String `tfsdk:"comment"`
39-
Users []string `tfsdk:"users"`
40-
Policies []string `tfsdk:"policies"`
39+
Users types.Set `tfsdk:"users"`
40+
Policies types.Set `tfsdk:"policies"`
4141
SVMName types.String `tfsdk:"svm_name"`
4242
ID types.Int64 `tfsdk:"id"`
4343
}
@@ -85,12 +85,12 @@ func (d *ProtocolsS3GroupDataSource) Schema(ctx context.Context, req datasource.
8585
MarkdownDescription: "Additional information about the group",
8686
Computed: true,
8787
},
88-
"users": schema.ListAttribute{
88+
"users": schema.SetAttribute{
8989
Computed: true,
9090
MarkdownDescription: "The list of users who belong to the group",
9191
ElementType: types.StringType,
9292
},
93-
"policies": schema.ListAttribute{
93+
"policies": schema.SetAttribute{
9494
Computed: true,
9595
MarkdownDescription: "The list of policies that are attached to the group",
9696
ElementType: types.StringType,
@@ -147,17 +147,15 @@ func (d *ProtocolsS3GroupDataSource) Read(ctx context.Context, req datasource.Re
147147
return
148148
}
149149

150-
svmUUID, err := interfaces.GetSVMUUID(errorHandler, *client, data.SVMName.ValueString())
150+
// Get SVM info
151+
svm, err := interfaces.GetSvmByName(errorHandler, *client, data.SVMName.ValueString())
151152
if err != nil {
152-
// error reporting done inside GetSVMUUID
153-
return
154-
}
155-
if svmUUID == "" {
153+
// error reporting done inside GetSvmByName
156154
errorHandler.MakeAndReportError("No SVM found", "SVM not found")
157155
return
158156
}
159157

160-
restInfo, err := interfaces.GetProtocolsS3Group(errorHandler, *client, data.Name.ValueString(), svmUUID, cluster.Version)
158+
restInfo, err := interfaces.GetProtocolsS3Group(errorHandler, *client, data.Name.ValueString(), svm.UUID, cluster.Version)
161159
if err != nil {
162160
// error reporting done inside GetProtocolsS3Group
163161
return
@@ -170,19 +168,29 @@ func (d *ProtocolsS3GroupDataSource) Read(ctx context.Context, req datasource.Re
170168
data.Comment = types.StringValue(restInfo.Comment)
171169
data.SVMName = types.StringValue(restInfo.SVM.Name)
172170

173-
// Users - map to simple string list
171+
// users - map to set
174172
var users = make([]string, len(restInfo.Users))
175173
for i, user := range restInfo.Users {
176174
users[i] = user.Name
177175
}
178-
data.Users = users
176+
usersSet, diags := types.SetValueFrom(ctx, types.StringType, users)
177+
resp.Diagnostics.Append(diags...)
178+
if resp.Diagnostics.HasError() {
179+
return
180+
}
181+
data.Users = usersSet
179182

180-
// Policies - map to simple string list
183+
// policies - map to set
181184
var policies = make([]string, len(restInfo.Policies))
182185
for i, policy := range restInfo.Policies {
183186
policies[i] = policy.Name
184187
}
185-
data.Policies = policies
188+
policiesSet, diags := types.SetValueFrom(ctx, types.StringType, policies)
189+
resp.Diagnostics.Append(diags...)
190+
if resp.Diagnostics.HasError() {
191+
return
192+
}
193+
data.Policies = policiesSet
186194
data.ID = types.Int64Value(restInfo.ID)
187195

188196
// Write logs using the tflog package

internal/provider/protocols/protocols_s3_groups_data_source.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func (d *ProtocolsS3GroupsDataSource) Schema(ctx context.Context, req datasource
8282
MarkdownDescription: "Additional information about the group",
8383
Computed: true,
8484
},
85-
"users": schema.ListAttribute{
85+
"users": schema.SetAttribute{
8686
Computed: true,
8787
MarkdownDescription: "The list of users who belong to the group",
8888
ElementType: types.StringType,
8989
},
90-
"policies": schema.ListAttribute{
90+
"policies": schema.SetAttribute{
9191
Computed: true,
9292
MarkdownDescription: "The list of policies that are attached to the group",
9393
ElementType: types.StringType,
@@ -154,44 +154,53 @@ func (d *ProtocolsS3GroupsDataSource) Read(ctx context.Context, req datasource.R
154154
errorHandler.MakeAndReportError("No SVM specified", "either svm_name or filter must be specified")
155155
return
156156
}
157-
svmUUID, err := interfaces.GetSVMUUID(errorHandler, *client, data.Filter.SVMName.ValueString())
157+
158+
// Get SVM info
159+
svm, err := interfaces.GetSvmByName(errorHandler, *client, data.Filter.SVMName.ValueString())
158160
if err != nil {
159-
// error reporting done inside GetSVMUUID
160-
return
161-
}
162-
if svmUUID == "" {
161+
// error reporting done inside GetSvmByName
163162
errorHandler.MakeAndReportError("No SVM found", "SVM not found")
164163
return
165164
}
166165

167-
restInfo, err := interfaces.GetProtocolsS3Groups(errorHandler, *client, svmUUID, cluster.Version)
166+
restInfo, err := interfaces.GetProtocolsS3Groups(errorHandler, *client, svm.UUID, cluster.Version)
168167
if err != nil {
169168
// error reporting done inside GetProtocolsS3Groups
170169
return
171170
}
172171

173172
data.S3Groups = make([]ProtocolsS3GroupDataSourceModel, len(restInfo))
174173
for index, record := range restInfo {
175-
// Users - map to simple string list
174+
// users - map to set
176175
var users = make([]string, len(record.Users))
177176
for i, user := range record.Users {
178177
users[i] = user.Name
179178
}
180-
181-
// Policies - map to simple string list
179+
usersSet, diags := types.SetValueFrom(ctx, types.StringType, users)
180+
resp.Diagnostics.Append(diags...)
181+
if resp.Diagnostics.HasError() {
182+
return
183+
}
184+
185+
// policies - map to set
182186
var policies = make([]string, len(record.Policies))
183187
for i, policy := range record.Policies {
184188
policies[i] = policy.Name
185189
}
190+
policiesSet, diags := types.SetValueFrom(ctx, types.StringType, policies)
191+
resp.Diagnostics.Append(diags...)
192+
if resp.Diagnostics.HasError() {
193+
return
194+
}
186195

187196
data.S3Groups[index] = ProtocolsS3GroupDataSourceModel{
188-
CxProfileName: types.String(data.CxProfileName),
189-
Name: types.StringValue(record.Name),
190-
Comment: types.StringValue(record.Comment),
191-
SVMName: types.StringValue(record.SVM.Name),
192-
Users: users,
193-
Policies: policies,
194-
ID: types.Int64Value(record.ID),
197+
CxProfileName: types.String(data.CxProfileName),
198+
Name: types.StringValue(record.Name),
199+
Comment: types.StringValue(record.Comment),
200+
SVMName: types.StringValue(record.SVM.Name),
201+
Users: usersSet,
202+
Policies: policiesSet,
203+
ID: types.Int64Value(record.ID),
195204
}
196205
}
197206

@@ -201,4 +210,4 @@ func (d *ProtocolsS3GroupsDataSource) Read(ctx context.Context, req datasource.R
201210

202211
// Save data into Terraform state
203212
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
204-
}
213+
}

0 commit comments

Comments
 (0)