Skip to content

Commit

Permalink
add import for #30
Browse files Browse the repository at this point in the history
  • Loading branch information
carchi8py committed Nov 13, 2023
1 parent ee4b87c commit 9c3bb3d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## 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))

## 1.0.1 ()

BUG FIXES:
Expand Down
42 changes: 41 additions & 1 deletion docs/resources/cluster_licensing_license_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,45 @@ resource "netapp-ontap_cluster_licensing_license_resource" "cluster_licensing_li
- `state` (String) State of the license

## Import
Import is currently not support for this Resource.
This resource supports import, which allows you to reference an existing license by name. Import requires a unique ID composed of the license name and connection profile

id = `{License name},{cx_profile_name}`

### Terraform Import
```shell
terraform import netapp-ontap_cluster_licensing_license_resource.import_license fcp,cluster5
```

!> 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.

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

First create the import block

```terrafrom
import {
to = netapp-ontap_cluster_licensing_license_resource.license_import
id = "FCP,cluster4"
}
```

Next run terraform plan to generate the configuration for you

```shell
terraform plan -generate-config-out=generated.tf
```

This will generate a file called generated.tf that will contain the configuration block for the imported resource.
Keys are not stored in the state file, so a fake key is generated for you.

```terraform
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.
# __generated__ by Terraform from "FCP,cluster4"
resource "netapp-ontap_cluster_licensing_license_resource" "license_import" {
cx_profile_name = "cluster4"
keys = ["fake"]
}
```
29 changes: 26 additions & 3 deletions internal/provider/cluster_licensing_license_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package provider
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/path"
"strings"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -176,16 +177,28 @@ func (r *ClusterLicensingLicenseResource) Read(ctx context.Context, req resource
var matchingLicense interfaces.ClusterLicensingLicenseKeyDataModelONTAP

for _, item := range restInfo {
if data.Name.ValueString() == item.Name {
if strings.ToLower(data.Name.ValueString()) == item.Name {
matchingLicense = item
}
}
if matchingLicense.Name == "" {
err := errorHandler.MakeAndReportError("License name not found", "License name not found")
if err != nil {
return
}
}

data.Name = types.StringValue(matchingLicense.Name)
data.State = types.StringValue(matchingLicense.State)
data.Scope = types.StringValue(matchingLicense.Scope)
data.ID = types.StringValue(matchingLicense.Name)
data.SerialNumber = types.StringValue(matchingLicense.Licenses[0].SerialNumber) // TODO: Double check there is only ever 1

// Key are required, but are not saved in the state, so we are going to fake it here as they are not used
if len(data.Keys) == 0 {
data.Keys = []types.String{types.StringValue("fake")}
}

// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Debug(ctx, fmt.Sprintf("read a resource: %#v", data))
Expand Down Expand Up @@ -243,5 +256,15 @@ func (r *ClusterLicensingLicenseResource) Delete(ctx context.Context, req resour

// ImportState imports a resource using ID from terraform import command by calling the Read method.
func (r *ClusterLicensingLicenseResource) 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) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: attr_one,attr_two. 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])...)
}
10 changes: 10 additions & 0 deletions internal/provider/cluster_licensing_license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

func TestLicensingLicenseResouce(t *testing.T) {
testLicense := os.Getenv("TF_ACC_NETAPP_LICENSE")
name := "FCP"
credName := "cluster4"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Expand All @@ -23,6 +25,14 @@ func TestLicensingLicenseResouce(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netapp-ontap_cluster_licensing_license_resource.cluster_licensing_license", "name", "insight_balance")),
},
// Test importing a resource
{
ResourceName: "netapp-ontap_cluster_licensing_license_resource.cluster_licensing_license",
ImportState: true,
ImportStateId: fmt.Sprintf("%s,%s", name, credName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netapp-ontap_cluster_licensing_license_resource.cluster_licensing_license", "name", "insight_balance")),
},
},
})
}
Expand Down

0 comments on commit 9c3bb3d

Please sign in to comment.