Skip to content

Commit

Permalink
fix computed members issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chuyich committed Dec 19, 2023
1 parent 9625a63 commit 7b416d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
9 changes: 3 additions & 6 deletions internal/interfaces/protocols_cifs_local_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func CreateCifsLocalGroup(errorHandler *utils.ErrorHandler, r restclient.RestCli
if err := mapstructure.Decode(response.Records[0], &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding protocols_cifs_local_group info", fmt.Sprintf("error on decode storage/protocols_cifs_local_groups info: %s, statusCode %d, response %#v", err, statusCode, response))
}
// Create API does not return the ID of the record, so we need to read it again later
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Create protocols_cifs_local_group source - udata: %#v", dataONTAP))
return &dataONTAP, nil
}
Expand Down Expand Up @@ -162,12 +163,8 @@ func UpdateCifsLocalGroup(errorHandler *utils.ErrorHandler, r restclient.RestCli
if err != nil {
return nil, errorHandler.MakeAndReportError("error updating protocols_cifs_local_group", fmt.Sprintf("error on PUT %s: %s, statusCode %d", api, err, statusCode))
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("$$$$$$$Update response status=%v, response=%v", statusCode, response))
// Update API does not return a record, so we need to read it again later
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Update protocols_cifs_local_group resource - response status=%#v, response=%#v", statusCode, response))

// var dataONTAP CifsLocalGroupGetDataModelONTAP
// if err := mapstructure.Decode(response.Records[0], &dataONTAP); err != nil {
// return nil, errorHandler.MakeAndReportError("### error decoding protocols_cifs_local_group info", fmt.Sprintf("error on decode storage/protocols_cifs_local_groups info: %s, statusCode %d, response %#v", err, statusCode, response))
// }
// tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Update protocols_cifs_local_group source - udata: %#v", dataONTAP))
return nil, nil
}
46 changes: 16 additions & 30 deletions internal/provider/protocols_cifs_local_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ type CifsLocalGroupResourceModel struct {
ID types.String `tfsdk:"id"`
Description types.String `tfsdk:"description"`
Members types.Set `tfsdk:"members"`
// Members []GroupMember `tfsdk:"members"`
// Members []types.Object `tfsdk:"members"`
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -89,13 +87,9 @@ func (r *CifsLocalGroupResource) Schema(ctx context.Context, req resource.Schema
"name": schema.StringAttribute{
MarkdownDescription: "Cifs Local Group member",
Computed: true,
// Optional: true,
},
},
},
// PlanModifiers: []planmodifier.Set{
// setplanmodifier.UseStateForUnknown(),
// },
},
"id": schema.StringAttribute{
MarkdownDescription: "CifsLocalGroup ID",
Expand Down Expand Up @@ -127,10 +121,9 @@ func (r *CifsLocalGroupResource) Configure(ctx context.Context, req resource.Con
// Read refreshes the Terraform state with the latest data.
func (r *CifsLocalGroupResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data CifsLocalGroupResourceModel
tflog.Debug(ctx, fmt.Sprintf("RRR read in get cifs local group resource: %#v", ctx))
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
tflog.Debug(ctx, fmt.Sprintf("$$$ read after get cifs local group resource: %#v", data.Name))

if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -161,24 +154,11 @@ func (r *CifsLocalGroupResource) Read(ctx context.Context, req resource.ReadRequ
return
}

// if !strings.Contains(data.Name.ValueString(), "\\") {
// // rawName in the format as XXXX/name in read response
// rawName := strings.Split(restInfo.Name, "\\")
// data.Name = types.StringValue(rawName[len(rawName)-1])
// }
if restInfo.Description != "" {
data.Description = types.StringValue(restInfo.Description)
}
// data.SVMName = types.StringValue(restInfo.SVM.Name)
// if len(restInfo.Members) > 0 {

// comment out the code for memebers type []GroupMember
// data.Members = make([]GroupMember, len(restInfo.Members))
// for i, member := range restInfo.Members {
// data.Members[i].Name = types.StringValue(member.Name)
// }
data.SVMName = types.StringValue(restInfo.SVM.Name)

// }
data.Members = memberSliceToLSet(ctx, restInfo.Members, &resp.Diagnostics)

// Write logs using the tflog package
Expand All @@ -189,31 +169,39 @@ func (r *CifsLocalGroupResource) Read(ctx context.Context, req resource.ReadRequ
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

// attrTypes returns a map of the attribute types for the resource.
func (o GroupMember) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
}
}

// memberSliceToLSet converts a slice of GroupMember to a types.Set
func memberSliceToLSet(ctx context.Context, membersSliceIn []interfaces.Member, diags *diag.Diagnostics) types.Set {
keys, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: GroupMember{}.attrTypes()}, membersSliceIn)
members := make([]GroupMember, len(membersSliceIn))
for i, member := range membersSliceIn {
members[i].Name = types.StringValue(member.Name)
}

keys, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: GroupMember{}.attrTypes()}, members)
diags.Append(d...)

return keys
}

// Create a resource and retrieve UUID
func (r *CifsLocalGroupResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *CifsLocalGroupResourceModel
tflog.Debug(ctx, fmt.Sprintf("### create in get cifs local group resource: %#v", ctx))

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
tflog.Debug(ctx, fmt.Sprintf("### create after get cifs local group resource: %#v", data.Name))

errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)

if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, fmt.Sprintf("### create start cifs local group resource: %#v", data))

var body interfaces.CifsLocalGroupResourceBodyDataModelONTAP
body.Name = data.Name.ValueString()
body.SVM.Name = data.SVMName.ValueString()
Expand All @@ -232,7 +220,7 @@ func (r *CifsLocalGroupResource) Create(ctx context.Context, req resource.Create
return
}

// read the resource back to get the ID cause create doesn't return it
// read the resource back to get the ID cause create API does not return the record
restInfo, err := interfaces.GetCifsLocalGroupByName(errorHandler, *client, resource.Name, data.SVMName.ValueString())
if err != nil {
// error reporting done inside GetCifsLocalGroup
Expand All @@ -243,12 +231,10 @@ func (r *CifsLocalGroupResource) Create(ctx context.Context, req resource.Create

data.Members = memberSliceToLSet(ctx, restInfo.Members, &resp.Diagnostics)

tflog.Debug(ctx, fmt.Sprintf("### create set ID cifs local group resource: %#v", data))
tflog.Trace(ctx, "created a resource")

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
tflog.Debug(ctx, fmt.Sprintf("### create end cifs local group resource: %#v", data))
}

// Update updates the resource and sets the updated Terraform state on success.
Expand Down Expand Up @@ -295,7 +281,7 @@ func (r *CifsLocalGroupResource) Update(ctx context.Context, req resource.Update
return
}

// read the resource back to get the ID cause create doesn't return it
// read the resource back to get the ID cause create does not return the record
restInfo, err := interfaces.GetCifsLocalGroupByName(errorHandler, *client, data.Name.ValueString(), data.SVMName.ValueString())
if err != nil {
// error reporting done inside GetCifsLocalGroup
Expand Down
24 changes: 12 additions & 12 deletions internal/provider/protocols_cifs_local_group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ func TestAccCifsLocalGroupResource(t *testing.T) {
Config: testAccCifsLocalGroupResourceConfigMissingVars("non-existant"),
ExpectError: regexp.MustCompile("Missing required argument"),
},
// // create with basic argument
// {
// Config: testAccCifsLocalGroupResourceConfig("ansibleSVM", "group1"),
// Check: resource.ComposeTestCheckFunc(
// // check name
// resource.TestCheckResourceAttr("netapp-ontap_protocols_cifs_local_group_resource.example1", "name", "group1"),
// // check svm_name
// resource.TestCheckResourceAttr("netapp-ontap_protocols_cifs_local_group_resource.example1", "svm_name", "ansibleSVM"),
// // check ID
// resource.TestCheckResourceAttrSet("netapp-ontap_protocols_cifs_local_group_resource.example1", "id"),
// ),
// },
// create with basic argument
{
Config: testAccCifsLocalGroupResourceConfig("ansibleSVM", "group1"),
Check: resource.ComposeTestCheckFunc(
// check name
resource.TestCheckResourceAttr("netapp-ontap_protocols_cifs_local_group_resource.example1", "name", "group1"),
// check svm_name
resource.TestCheckResourceAttr("netapp-ontap_protocols_cifs_local_group_resource.example1", "svm_name", "ansibleSVM"),
// check ID
resource.TestCheckResourceAttrSet("netapp-ontap_protocols_cifs_local_group_resource.example1", "id"),
),
},
// update test
{
Config: testAccCifsLocalGroupResourceConfig("ansibleSVM", "newgroup"),
Expand Down

0 comments on commit 7b416d7

Please sign in to comment.