Skip to content

Commit

Permalink
feat: check version prior to querying SVM by name
Browse files Browse the repository at this point in the history
  • Loading branch information
acch committed Jan 6, 2025
1 parent 9e9ea16 commit 19f6f0b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
23 changes: 15 additions & 8 deletions internal/interfaces/svm.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func GetSvmByNameIgnoreNotFound(errorHandler *utils.ErrorHandler, r restclient.R
}

// GetSvmByNameDataSource to get data source svm info
func GetSvmByNameDataSource(errorHandler *utils.ErrorHandler, r restclient.RestClient, name string) (*SvmGetDataSourceModel, error) {
func GetSvmByNameDataSource(errorHandler *utils.ErrorHandler, r restclient.RestClient, name string, version versionModelONTAP) (*SvmGetDataSourceModel, error) {
api := "svm/svms"
query := r.NewQuery()
query.Fields([]string{
fields := []string{
"name",
"ipspace",
"snapshot_policy",
Expand All @@ -141,9 +141,13 @@ func GetSvmByNameDataSource(errorHandler *utils.ErrorHandler, r restclient.RestC
"language",
"max_volumes",
"aggregates",
"storage.limit",
})
}
if version.Generation >= 9 && version.Major >= 13 {
fields = append(fields, "storage.limit")
}
query.Fields(fields)
query.Add("name", name)

statusCode, response, err := r.GetNilOrOneRecord(api, query, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
Expand All @@ -161,10 +165,10 @@ func GetSvmByNameDataSource(errorHandler *utils.ErrorHandler, r restclient.RestC
}

// GetSvmsByName to get data source list svm info
func GetSvmsByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter *SvmDataSourceFilterModel) ([]SvmGetDataSourceModel, error) {
func GetSvmsByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter *SvmDataSourceFilterModel, version versionModelONTAP) ([]SvmGetDataSourceModel, error) {
api := "svm/svms"
query := r.NewQuery()
query.Fields([]string{
fields := []string{
"name",
"ipspace",
"snapshot_policy",
Expand All @@ -173,8 +177,11 @@ func GetSvmsByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, fi
"language",
"max_volumes",
"aggregates",
"storage.limit",
})
}
if version.Generation >= 9 && version.Major >= 13 {
fields = append(fields, "storage.limit")
}
query.Fields(fields)

if filter != nil {
var filterMap map[string]interface{}
Expand Down
12 changes: 11 additions & 1 deletion internal/provider/svm/svm_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,17 @@ func (d *SvmDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

restInfo, err := interfaces.GetSvmByNameDataSource(errorHandler, *client, data.Name.ValueString())
cluster, err := interfaces.GetCluster(errorHandler, *client)
if err != nil {
// error reporting done inside GetCluster
return
}
if cluster == nil {
errorHandler.MakeAndReportError("No cluster found", "cluster not found")
return
}

restInfo, err := interfaces.GetSvmByNameDataSource(errorHandler, *client, data.Name.ValueString(), cluster.Version)
if err != nil {
// error reporting done inside GetSvm
return
Expand Down
13 changes: 12 additions & 1 deletion internal/provider/svm/svm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,23 @@ func (r *SvmResource) Read(ctx context.Context, req resource.ReadRequest, resp *
// error reporting done inside NewClient
return
}

cluster, err := interfaces.GetCluster(errorHandler, *client)
if err != nil {
// error reporting done inside GetCluster
return
}
if cluster == nil {
errorHandler.MakeAndReportError("No cluster found", "cluster not found")
return
}

tflog.Debug(ctx, fmt.Sprintf("read a svm resource: %#v", data))
var svm *interfaces.SvmGetDataSourceModel
if data.ID.ValueString() != "" {
svm, err = interfaces.GetSvm(errorHandler, *client, data.ID.ValueString())
} else {
svm, err = interfaces.GetSvmByNameDataSource(errorHandler, *client, data.Name.ValueString())
svm, err = interfaces.GetSvmByNameDataSource(errorHandler, *client, data.Name.ValueString(), cluster.Version)
}
if err != nil {
return
Expand Down
13 changes: 12 additions & 1 deletion internal/provider/svm/svms_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,18 @@ func (d *SvmsDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
Name: data.Filter.Name.ValueString(),
}
}
restInfo, err := interfaces.GetSvmsByName(errorHandler, *client, filter)

cluster, err := interfaces.GetCluster(errorHandler, *client)
if err != nil {
// error reporting done inside GetCluster
return
}
if cluster == nil {
errorHandler.MakeAndReportError("No cluster found", "cluster not found")
return
}

restInfo, err := interfaces.GetSvmsByName(errorHandler, *client, filter, cluster.Version)
if err != nil {
// error reporting done inside GetSvms
return
Expand Down

0 comments on commit 19f6f0b

Please sign in to comment.