Skip to content

Commit

Permalink
Merge branch 'integration/main' into 30-enhancement-netapp-ontap_clus…
Browse files Browse the repository at this point in the history
…ter_licensing_license-needs-to-support-import
  • Loading branch information
carchi8py authored Nov 16, 2023
2 parents 9c3bb3d + cd80371 commit 0383f1a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
## 1.1.0
* ENHANCEMENTS:
* **netapp-ontap_cluster_licensing_license_resource**: Add support for import ([#30](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/30))
* **netapp-ontap_storage_aggregate_resource**: Add support for import ([#39](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/39))


## 1.0.1 ()

BUG FIXES:
* netapp-ontap_name_services_dns_resource: Fixed and Documented Import ([#63](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/63))
* netapp-ontap_cluster_data_source, netapp-ontap_snapmirrors_data_source, netapp-ontap_networking_ip_route_resource and netapp-ontap_sotrage_volume_resource: Fix documentation ([#70](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/70))

## 1.0.0 (2023-11-06)


NOTES

Expand Down
41 changes: 40 additions & 1 deletion docs/resources/storage_aggregate_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,43 @@ resource "netapp-ontap_storage_aggregate_resource" "example" {
- `id` (String) Aggregate identifier

## Import
Import is currently not support for this Resource.
This Resource supports import, which allows you to import existing aggregates into the state of this resoruce.
Import require a unique ID composed of the aggregate name and cx_profile_name, separated by a comma.

id = `name`,`cx_profile_name`

### Terraform Import

For example
```shell
terraform import netapp-ontap_storage_aggregate_resource.example aggr1,cluster4
```

!> The terraform import CLI command can only import resources into the state. Importing via the CLI does not generate configuration. If you want to generate the accompanying configuration for imported resources, use the import block instead.

### Terrafomr Import Block
This requires Terraform 1.5 or higher, and will auto create the configuration for you

First create the block
```terraform
import {
to = netapp-ontap_storage_aggregate_resource.aggr_import
id = "aggr1,cluster4"
}
```
Next run, this will auto create the configuration for you
```shell
terraform plan -generate-config-out=generated.tf
```
This will generate a file called generated.tf, which will contain the configuration for the imported resource
```terraform
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.
# __generated__ by Terraform from "aggr1,cluster4"
resource "netapp-ontap_storage_volume_resource" "aggr_import" {
cx_profile_name = "cluster4"
name = "aggr1"
node = "node1"
disk_count = 11
}
```
5 changes: 3 additions & 2 deletions internal/interfaces/storage_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package interfaces

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/mapstructure"
"github.com/netapp/terraform-provider-netapp-ontap/internal/restclient"
"github.com/netapp/terraform-provider-netapp-ontap/internal/utils"
"strconv"
)

// StorageAggregateGetDataModelONTAP describes the GET record data model using go types for mapping.
Expand Down Expand Up @@ -70,7 +71,7 @@ func GetStorageAggregate(errorHandler *utils.ErrorHandler, r restclient.RestClie
api := "storage/aggregates/"
query := r.NewQuery()
query.Set("uuid", uuid)
query.Fields([]string{"name", "snaplock_type", "block_storage", "data_encryption", "state"})
query.Fields([]string{"name", "node.name", "snaplock_type", "block_storage", "data_encryption", "state"})
statusCode, response, err := r.GetNilOrOneRecord(api, query, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
Expand Down
34 changes: 30 additions & 4 deletions internal/provider/storage_aggregate_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package provider
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -328,14 +330,26 @@ func (r *AggregateResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

aggregate, err := interfaces.GetStorageAggregate(errorHandler, *client, data.ID.ValueString())
if err != nil {
return
var aggregate *interfaces.StorageAggregateGetDataModelONTAP
if data.ID.ValueString() == "" {
aggregate, err = interfaces.GetStorageAggregateByName(errorHandler, *client, data.Name.ValueString())
if err != nil {
return
}
data.ID = types.StringValue(aggregate.UUID)
} else {
aggregate, err = interfaces.GetStorageAggregate(errorHandler, *client, data.ID.ValueString())
if err != nil {
return
}
}

if aggregate == nil {
errorHandler.MakeAndReportError("No aggregate found", fmt.Sprintf("aggregate %s not found.", data.Name.ValueString()))
return
}
tflog.Debug(ctx, fmt.Sprintf("read an aggregate resource: %#v", data))

data.DiskCount = types.Int64Value(aggregate.BlockStorage.Primary.DiskCount)
data.DiskClass = types.StringValue(aggregate.BlockStorage.Primary.DiskClass)
data.RaidType = types.StringValue(aggregate.BlockStorage.Primary.RaidType)
Expand All @@ -346,6 +360,8 @@ func (r *AggregateResource) Read(ctx context.Context, req resource.ReadRequest,
data.SnaplockType = types.StringValue(aggregate.SnaplockType)
data.State = types.StringValue(aggregate.State)
data.Name = types.StringValue(aggregate.Name)
data.Node = types.StringValue(aggregate.Node.Name)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -449,5 +465,15 @@ func (r *AggregateResource) Delete(ctx context.Context, req resource.DeleteReque

// ImportState imports a resource using ID from terraform import command by calling the Read method.
func (r *AggregateResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
tflog.Debug(ctx, fmt.Sprintf("import req an aggregate resource: %#v", req))
idParts := strings.Split(req.ID, ",")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: name,cx_profile_name. Got: %q", req.ID),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("cx_profile_name"), idParts[1])...)
}
9 changes: 9 additions & 0 deletions internal/provider/storage_aggregate_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func TestAccStorageAggregateResource(t *testing.T) {
resource.TestCheckNoResourceAttr("netapp-ontap_storage_aggregate_resource.example", "vol"),
),
},
// Test importing a resource
{
ResourceName: "netapp-ontap_storage_aggregate_resource.example",
ImportState: true,
ImportStateId: fmt.Sprintf("%s,%s", "acc_test_aggr", "cluster4"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netapp-ontap_storage_aggregate_resource.example", "name", "acc_test_aggr"),
),
},
},
})
}
Expand Down

0 comments on commit 0383f1a

Please sign in to comment.