Skip to content

Commit

Permalink
Merge branch 'integration/main' of https://github.com/NetApp/terrafor…
Browse files Browse the repository at this point in the history
…m-provider-netapp-ontap into integration/main
  • Loading branch information
wenjun666 committed Oct 7, 2024
2 parents 307d865 + 84a184b commit 1a4ab58
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ DOC FIXES:

BUG FIXES:
* **netapp-ontap_cluster_data_source: fix on nodes to show multiple elements ([#264](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/264))
* **netapp-ontap_protocols_nfs_export_policy_resource: fix id error during the creation ([[#290](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/290)])

## 1.1.3 (2024-08-08)

Expand Down
33 changes: 20 additions & 13 deletions internal/interfaces/protocols_nfs_export_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ type ExportpolicyResourceModel struct {
ID int `mapstructure:"id"`
}

// ExportpolicyResourceBodyDataModelONTAP describes the resource data model.
type ExportpolicyResourceBodyDataModelONTAP struct {
Name string `mapstructure:"name"`
Svm SvmDataModelONTAP `mapstructure:"svm"`
}

// ExportPolicyGetDataModelONTAP describes the GET record data model using go types for mapping.
type ExportPolicyGetDataModelONTAP struct {
Name string `mapstructure:"name"`
Svm string `mapstructure:"svm_name"`
ID int `mapstructure:"id"`
Name string `mapstructure:"name"`
Svm string `mapstructure:"svm_name"`
SvmUUID string `mapstructure:"svm_uuid"`
ID int `mapstructure:"id"`
}

// ExportPolicyGetDataFilterModel describes filter model
Expand All @@ -30,7 +37,7 @@ type ExportPolicyGetDataFilterModel struct {
}

// CreateExportPolicy to create export policy
func CreateExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, data ExportpolicyResourceModel) (*ExportPolicyGetDataModelONTAP, error) {
func CreateExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, data ExportpolicyResourceBodyDataModelONTAP) (*ExportpolicyResourceModel, error) {
var body map[string]interface{}
if err := mapstructure.Decode(data, &body); err != nil {
return nil, errorHandler.MakeAndReportError("error encoding export policy body", fmt.Sprintf("error on encoding export policy body: %s, body: %#v", err, data))
Expand All @@ -42,7 +49,7 @@ func CreateExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClien
return nil, errorHandler.MakeAndReportError("error creating export policy", fmt.Sprintf("error on POST protocols/nfs/export-policies: %s, statusCode %d", err, statusCode))
}

var dataONTAP ExportPolicyGetDataModelONTAP
var dataONTAP ExportpolicyResourceModel
if err := mapstructure.Decode(response.Records[0], &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding export policies info", fmt.Sprintf("error on decode protocols/nfs/export-policies info: %s, statusCode %d, response %#v", err, statusCode, response))
}
Expand All @@ -51,7 +58,7 @@ func CreateExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClien
}

// GetExportPolicy to get export policy
func GetExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) (*ExportPolicyGetDataModelONTAP, error) {
func GetExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) (*ExportpolicyResourceModel, error) {
api := "protocols/nfs/export-policies/" + id
statusCode, response, err := r.GetNilOrOneRecord(api, nil, nil)
if err == nil && response == nil {
Expand All @@ -61,7 +68,7 @@ func GetExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient,
return nil, errorHandler.MakeAndReportError("error reading export policy info", fmt.Sprintf("error on GET protocols/nfs/export-policies/%s: %s", id, err))
}

var dataONTAP ExportPolicyGetDataModelONTAP
var dataONTAP ExportpolicyResourceModel
if err := mapstructure.Decode(response, &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding export policy info", fmt.Sprintf("error on decode protocols/nfs/export-policies/%s: %s, statusCode %d, response %#v", id, err, statusCode, response))
}
Expand All @@ -70,7 +77,7 @@ func GetExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient,
}

// GetNfsExportPolicyByName to get export policy by filter
func GetNfsExportPolicyByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter interface{}) (*ExportPolicyGetDataModelONTAP, error) {
func GetNfsExportPolicyByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter interface{}) (*ExportpolicyResourceModel, error) {
query := r.NewQuery()
query.Fields([]string{"name"})
if filter != nil {
Expand All @@ -85,7 +92,7 @@ func GetNfsExportPolicyByName(errorHandler *utils.ErrorHandler, r restclient.Res
return nil, errorHandler.MakeAndReportError("error reading export policy info", fmt.Sprintf("error on GET protocols/nfs/export-policies: %s", err))
}

var dataONTAP ExportPolicyGetDataModelONTAP
var dataONTAP ExportpolicyResourceModel
if err := mapstructure.Decode(response, &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding export policy info", fmt.Sprintf("error on decode protocols/nfs/export-policies: %s, statusCode %d, response %#v", err, statusCode, response))
}
Expand All @@ -94,7 +101,7 @@ func GetNfsExportPolicyByName(errorHandler *utils.ErrorHandler, r restclient.Res
}

// GetExportPoliciesList to get export policies
func GetExportPoliciesList(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter *ExportPolicyGetDataFilterModel) ([]ExportpolicyResourceModel, error) {
func GetExportPoliciesList(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter *ExportPolicyGetDataFilterModel) ([]ExportPolicyGetDataModelONTAP, error) {
api := "protocols/nfs/export-policies"
query := r.NewQuery()
query.Fields([]string{"name", "id", "svm.name", "svm.uuid"})
Expand All @@ -113,9 +120,9 @@ func GetExportPoliciesList(errorHandler *utils.ErrorHandler, r restclient.RestCl
return nil, errorHandler.MakeAndReportError("error reading export policies info", fmt.Sprintf("error on GET %s: %s, statusCode %d", api, err, statusCode))
}

var dataONTAP []ExportpolicyResourceModel
var dataONTAP []ExportPolicyGetDataModelONTAP
for _, info := range response {
var record ExportpolicyResourceModel
var record ExportPolicyGetDataModelONTAP
if err := mapstructure.Decode(info, &record); err != nil {
return nil, errorHandler.MakeAndReportError(fmt.Sprintf("failed to decode response from GET %s", api),
fmt.Sprintf("error: %s, statusCode %d, info %#v", err, statusCode, info))
Expand All @@ -136,7 +143,7 @@ func DeleteExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClien
}

// UpdateExportPolicy updates export policy
func UpdateExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, data ExportpolicyResourceModel, id string) error {
func UpdateExportPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, data ExportpolicyResourceBodyDataModelONTAP, id string) error {
var body map[string]interface{}
if err := mapstructure.Decode(data, &body); err != nil {
return errorHandler.MakeAndReportError("error encoding export policy body", fmt.Sprintf("error on encoding export policy body: %s, body: %#v", err, data))
Expand Down
43 changes: 25 additions & 18 deletions internal/interfaces/protocols_nfs_export_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,56 @@ import (
"github.com/netapp/terraform-provider-netapp-ontap/internal/utils"
)

// basic get data records
var oneBasicExportPolicyRecord = ExportPolicyGetDataModelONTAP{
Name: "string",
Svm: "string",
SvmUUID: "string",
ID: 123,
}

// basic get data record
var basicExportPolicyRecord = ExportPolicyGetDataModelONTAP{
var basicExportPolicyRecord = ExportpolicyResourceModel{
Name: "string",
Svm: "string",
ID: 122880,
Svm: SvmDataModelONTAP{
Name: "string",
UUID: "string",
},
}

// bad record
var badExportPolicyRecord = struct{ Name int }{123}

// create export policy with basic request body
var basicExportPolicyBody = ExportpolicyResourceModel{
var basicExportPolicyBody = ExportpolicyResourceBodyDataModelONTAP{
Name: "string",
Svm: SvmDataModelONTAP{
Name: "string",
UUID: "string",
},
ID: 122880,
}

// create export policy with empty comment
var badExportPolicyBody = ExportpolicyResourceModel{
var badExportPolicyBody = ExportpolicyResourceBodyDataModelONTAP{
Name: "",
}

// update export policy name
var renameExportPolicyBody = ExportpolicyResourceModel{
var renameExportPolicyBody = ExportpolicyResourceBodyDataModelONTAP{
Name: "newname",
Svm: SvmDataModelONTAP{
Name: "string",
UUID: "string",
},
ID: 122880,
}

// update export policy with basic request body
var updateExportPolicyErrorBody = ExportpolicyResourceModel{
var updateExportPolicyErrorBody = ExportpolicyResourceBodyDataModelONTAP{
Name: "string",
Svm: SvmDataModelONTAP{
Name: "newsvm",
UUID: "string",
},
ID: 122880,
}

func TestGetExportPolicy(t *testing.T) {
Expand Down Expand Up @@ -93,7 +100,7 @@ func TestGetExportPolicy(t *testing.T) {
tests := []struct {
name string
responses []restclient.MockResponse
want *ExportPolicyGetDataModelONTAP
want *ExportpolicyResourceModel
wantErr bool
}{
{name: "test_no_records_1", responses: responses["test_no_records_1"], want: nil, wantErr: true},
Expand Down Expand Up @@ -148,8 +155,8 @@ func TestCreateExportPolicy(t *testing.T) {
tests := []struct {
name string
responses []restclient.MockResponse
requestbody ExportpolicyResourceModel
want *ExportPolicyGetDataModelONTAP
requestbody ExportpolicyResourceBodyDataModelONTAP
want *ExportpolicyResourceModel
wantErr bool
}{
{name: "test_create_basic_record_1", responses: responses["test_create_basic_record_1"], requestbody: basicExportPolicyBody, want: &basicExportPolicyRecord, wantErr: false},
Expand Down Expand Up @@ -229,7 +236,7 @@ func TestUpdateExportPolicy(t *testing.T) {
tests := []struct {
name string
responses []restclient.MockResponse
requestbody ExportpolicyResourceModel
requestbody ExportpolicyResourceBodyDataModelONTAP
wantErr bool
}{
{name: "test_update_rename_export_policy", responses: responses["test_update_rename_export_policy"], requestbody: renameExportPolicyBody, wantErr: false},
Expand Down Expand Up @@ -257,7 +264,7 @@ func TestGetExportPoliciesList(t *testing.T) {
errorHandler := utils.NewErrorHandler(context.Background(), &diag.Diagnostics{})
badRecord := struct{ Name int }{123}
var recordInterface map[string]any
err := mapstructure.Decode(basicExportPolicyBody, &recordInterface)
err := mapstructure.Decode(oneBasicExportPolicyRecord, &recordInterface)
if err != nil {
panic(err)
}
Expand All @@ -272,8 +279,8 @@ func TestGetExportPoliciesList(t *testing.T) {
twoRecordsResponse := restclient.RestResponse{NumRecords: 2, Records: []map[string]any{recordInterface, recordInterface}}
badRecordResponse := restclient.RestResponse{NumRecords: 1, Records: []map[string]any{badRecordInterface}}

var wantOneRecord = []ExportpolicyResourceModel{basicExportPolicyBody}
var wantTwoRecords = []ExportpolicyResourceModel{basicExportPolicyBody, basicExportPolicyBody}
var wantOneRecord = []ExportPolicyGetDataModelONTAP{oneBasicExportPolicyRecord}
var wantTwoRecords = []ExportPolicyGetDataModelONTAP{oneBasicExportPolicyRecord, oneBasicExportPolicyRecord}

responses := map[string][]restclient.MockResponse{
"test_no_records_1": {
Expand All @@ -294,7 +301,7 @@ func TestGetExportPoliciesList(t *testing.T) {
name string
responses []restclient.MockResponse
// args args
want []ExportpolicyResourceModel
want []ExportPolicyGetDataModelONTAP
wantErr bool
}{
{name: "test_no_records_1", responses: responses["test_no_records_1"], want: nil, wantErr: false},
Expand Down
18 changes: 16 additions & 2 deletions internal/interfaces/protocols_nfs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package interfaces

import (
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/mapstructure"
"github.com/netapp/terraform-provider-netapp-ontap/internal/restclient"
Expand All @@ -21,6 +22,19 @@ type ProtocolsNfsServiceGetDataModelONTAP struct {
SVM SvmDataModelONTAP `mapstructure:"svm"`
}

// ProtocolsNfsServiceResourceDataModelONTAP describes the GET record data model using go types for mapping.
type ProtocolsNfsServiceResourceDataModelONTAP struct {
Enabled bool `mapstructure:"enabled"`
Protocol Protocol `mapstructure:"protocol"`
Root Root `mapstructure:"root"`
Security Security `mapstructure:"security"`
ShowmountEnabled bool `mapstructure:"showmount_enabled"`
Transport Transport `mapstructure:"transport"`
VstorageEnabled bool `mapstructure:"vstorage_enabled"`
Windows Windows `mapstructure:"windows"`
SVM SvmDataModelONTAP `mapstructure:"svm"`
}

// Protocol describes the GET record data model using go types for mapping.
type Protocol struct {
V3Enabled bool `mapstructure:"v3_enabled"`
Expand Down Expand Up @@ -159,7 +173,7 @@ func GetProtocolsNfsServices(errorHandler *utils.ErrorHandler, r restclient.Rest
}

// CreateProtocolsNfsService Create a NFS Service
func CreateProtocolsNfsService(errorHandler *utils.ErrorHandler, r restclient.RestClient, data ProtocolsNfsServiceGetDataModelONTAP, svnUUID string) (*ProtocolsNfsServiceGetDataModelONTAP, error) {
func CreateProtocolsNfsService(errorHandler *utils.ErrorHandler, r restclient.RestClient, data ProtocolsNfsServiceResourceDataModelONTAP) (*ProtocolsNfsServiceGetDataModelONTAP, error) {
var body map[string]interface{}
if err := mapstructure.Decode(data, &body); err != nil {
return nil, errorHandler.MakeAndReportError("error encoding NFS Service body", fmt.Sprintf("error on encoding protocols/nfs/services body: %s, body: %#v", err, data))
Expand Down Expand Up @@ -188,7 +202,7 @@ func DeleteProtocolsNfsService(errorHandler *utils.ErrorHandler, r restclient.Re
}

// UpdateProtocolsNfsService Update a NFS service
func UpdateProtocolsNfsService(errorHandler *utils.ErrorHandler, r restclient.RestClient, request ProtocolsNfsServiceGetDataModelONTAP, uuid string) error {
func UpdateProtocolsNfsService(errorHandler *utils.ErrorHandler, r restclient.RestClient, request ProtocolsNfsServiceResourceDataModelONTAP, uuid string) error {
var body map[string]interface{}
if err := mapstructure.Decode(request, &body); err != nil {
return errorHandler.MakeAndReportError("error encoding NFS Services body", fmt.Sprintf("error on encoding NFS Services body: %s, body: %#v", err, request))
Expand Down
7 changes: 4 additions & 3 deletions internal/interfaces/protocols_nfs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ func TestCreateProtocolsNfsService(t *testing.T) {
name string
responses []restclient.MockResponse
// args args
want *ProtocolsNfsServiceGetDataModelONTAP
wantErr bool
requestBody ProtocolsNfsServiceResourceDataModelONTAP
want *ProtocolsNfsServiceGetDataModelONTAP
wantErr bool
}{
{name: "test_one_record_1", responses: responses["test_one_record_1"], want: &nfsServiceRecord, wantErr: false},
{name: "test_one_910_record_1", responses: responses["test_one_910_record_1"], want: &record910, wantErr: false},
Expand All @@ -215,7 +216,7 @@ func TestCreateProtocolsNfsService(t *testing.T) {
if err != nil {
panic(err)
}
got, err := CreateProtocolsNfsService(errorHandler, *r, nfsServiceRecord, "svmname")
got, err := CreateProtocolsNfsService(errorHandler, *r, tt.requestBody)
if err != nil {
fmt.Printf("err: %s\n", err)
}
Expand Down
1 change: 0 additions & 1 deletion internal/interfaces/storage_qtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ type StorageQtreeResourceBodyDataModelONTAP struct {
SVM svm `mapstructure:"svm"`
UnixPermissions int `mapstructure:"unix_permissions,omitempty"`
Volume qtreeVloume `mapstructure:"volume"`
ID int `mapstructure:"id,omitempty"`
SecurityStyle string `mapstructure:"security_style,omitempty"`
ExportPolicy qtreeExportPolicy `mapstructure:"export_policy,omitempty"`
User qtreeUser `mapstructure:"user,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package protocols
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -160,8 +161,8 @@ func (d *ExportPoliciesDataSource) Read(ctx context.Context, req datasource.Read
CxProfileName: types.String(data.CxProfileName),
Name: types.StringValue(record.Name),
ID: types.Int64Value(int64(record.ID)),
SVMName: types.StringValue(record.Svm.Name),
SVMUUID: types.StringValue(record.Svm.UUID),
SVMName: types.StringValue(record.Svm),
SVMUUID: types.StringValue(record.SvmUUID),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package protocols
import (
"context"
"fmt"
"github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection"
"strconv"
"strings"

"github.com/netapp/terraform-provider-netapp-ontap/internal/provider/connection"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -104,7 +105,7 @@ func (r *ExportPolicyResource) Create(ctx context.Context, req resource.CreateRe
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

var request interfaces.ExportpolicyResourceModel
var request interfaces.ExportpolicyResourceBodyDataModelONTAP
errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -201,7 +202,7 @@ func (r *ExportPolicyResource) Update(ctx context.Context, req resource.UpdateRe
return
}

var request interfaces.ExportpolicyResourceModel
var request interfaces.ExportpolicyResourceBodyDataModelONTAP
request.Name = data.Name.ValueString()

err = interfaces.UpdateExportPolicy(errorHandler, *client, request, data.ID.ValueString())
Expand Down
Loading

0 comments on commit 1a4ab58

Please sign in to comment.