Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

40 enhancement netapp ontap storage snapshot policy need to support import #105

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ENHANCEMENTS:
* **netapp-ontap_svm_resource**: Add support for import ([#6](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/6))
* **netapp-ontap_storage_volume_snapshot_resource**: Add support for import ([#42](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/42))
* **netapp-ontap_cluster_schedule_resource**: Add support for import ([#31](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/31))
* **netapp-ontap_storage_snapshot_policy_resource**: Add support for import ([#40](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/40))
* **netapp-ontap_snapmiror_policy**: Add support for import ([#38](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/38))
* **netapp-ontap_networking_ip_interface_resource**: Add support for import ([#32](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/32))
* **netapp-ontap_protocols_nfs_export_policy_rule_resource**: Add support for import ([#35](https://github.com/NetApp/terraform-provider-netapp-ontap/issues/35))
Expand Down
37 changes: 36 additions & 1 deletion docs/resources/storage_snapshot_policy_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,39 @@ Required:
- `name` (String) Some common schedules already defined in the system are hourly, daily, weekly, at 15 minute intervals, and at 5 minute intervals. Snapshot copy policies with custom schedules can be referenced

## Import
Import is currently not support for this Resource.
This Resource supports import, which allows you to import existing storage snapshot policy into the state of this resoruce.
Import require a unique ID composed of the snapshot policy name, svm_name and cx_profile_name, separated by a comma.
id = `name`,`svm_name`,`cx_profile_name`
### Terraform Import
For example
```shell
terraform import netapp-ontap_storage_snapshot_policy_resource.example policy1,svm1,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_snapshot_policy_resource.exp_import
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not match the example in line 120 (exp_import vs policy1_import)

id = "policy1,svm1,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 "policy1,svm1,cluster4"
resource "netapp-ontap_storage_snapshot_policy_resource" "policy1_import" {
cx_profile_name = "cluster4"
name = "policy1"
svm_name = "svm1"
}
```
43 changes: 32 additions & 11 deletions internal/provider/storage_snapshot_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -194,17 +195,27 @@ func (r *SnapshotPolicyResource) Read(ctx context.Context, req resource.ReadRequ
// error reporting done inside NewClient
return
}

restInfo, err := interfaces.GetSnapshotPolicy(errorHandler, *client, data.ID.ValueString())
if err != nil {
// error reporting done inside GetSnapshotPolicy
return
}
if restInfo == nil {
errorHandler.MakeAndReportError("No snapshot policy found", fmt.Sprintf("snapshot policy %s not found.", data.Name.ValueString()))
return
var restInfo *interfaces.SnapshotPolicyGetDataModelONTAP
if data.ID.ValueString() == "" {
restInfo, err = interfaces.GetSnapshotPolicyByName(errorHandler, *client, data.Name.ValueString())
if err != nil {
return
}
if restInfo == nil {
errorHandler.MakeAndReportError("No snapshot policy found", fmt.Sprintf("snapshot policy %s not found.", data.Name.ValueString()))
return
}
} else {
restInfo, err = interfaces.GetSnapshotPolicy(errorHandler, *client, data.ID.ValueString())
if err != nil {
// error reporting done inside GetSnapshotPolicy
return
}
if restInfo == nil {
errorHandler.MakeAndReportError("No snapshot policy found", fmt.Sprintf("snapshot policy %s not found.", data.Name.ValueString()))
return
}
}

data.Name = types.StringValue(restInfo.Name)
data.ID = types.StringValue(restInfo.UUID)

Expand Down Expand Up @@ -354,5 +365,15 @@ func (r *SnapshotPolicyResource) Delete(ctx context.Context, req resource.Delete

// ImportState imports a resource using ID from terraform import command by calling the Read method.
func (r *SnapshotPolicyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
idParts := strings.Split(req.ID, ",")
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: name,svm_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("svm_name"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("cx_profile_name"), idParts[2])...)
}
9 changes: 9 additions & 0 deletions internal/provider/storage_snapshot_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func TestAccStorageSnapshotPolicyResource(t *testing.T) {
resource.TestCheckResourceAttr("netapp-ontap_storage_snapshot_policy_resource.example", "enabled", "true"),
),
},
// Test importing a resource
{
ResourceName: "netapp-ontap_storage_snapshot_policy_resource.example",
ImportState: true,
ImportStateId: fmt.Sprintf("%s,%s,%s", "tfimportpolicy", "carchi-test", "cluster4"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netapp-ontap_storage_snapshot_policy_resource.example", "name", "tfimportpolicy"),
),
},
},
})
}
Expand Down
Loading