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

[New Data Source] fsx_ontap_storage_virtual_machine #32621

Merged
Merged
7 changes: 7 additions & 0 deletions .changelog/32621.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_fsx_ontap_storage_virtual_machine
```

```release-note:bug
resource/aws_fsx_ontap_storage_virtual_machine: Change `file_system_id` to [ForceNew](https://developer.hashicorp.com/terraform/plugin/sdkv2/schemas/schema-behaviors#forcenew)
```
38 changes: 38 additions & 0 deletions internal/service/fsx/common_schema_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,41 @@ func DataSourceSnapshotFiltersSchema() *schema.Schema {
},
}
}

func BuildStorageVirtualMachineFiltersDataSource(set *schema.Set) []*fsx.StorageVirtualMachineFilter {
var filters []*fsx.StorageVirtualMachineFilter
for _, v := range set.List() {
m := v.(map[string]interface{})
var filterValues []*string
for _, e := range m["values"].([]interface{}) {
filterValues = append(filterValues, aws.String(e.(string)))
}

filters = append(filters, &fsx.StorageVirtualMachineFilter{
Name: aws.String(m["name"].(string)),
Values: filterValues,
})
}

return filters
}

func DataSourceStorageVirtualMachineFiltersSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}
1 change: 1 addition & 0 deletions internal/service/fsx/ontap_storage_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func ResourceONTAPStorageVirtualMachine() *schema.Resource {
"file_system_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(11, 21),
},
"name": {
Expand Down
260 changes: 260 additions & 0 deletions internal/service/fsx/ontap_storage_virtual_machine_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package fsx

import (
"context"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// @SDKDataSource("aws_fsx_ontap_storage_virtual_machine", name="ONTAP Storage Virtual Machine")
func DataSourceOntapStorageVirtualMachine() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceOntapStorageVirtualMachineRead,

Schema: map[string]*schema.Schema{
"active_directory_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"netbios_name": {
Type: schema.TypeString,
Computed: true,
},
"self_managed_active_directory_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_ips": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"domain_name": {
Type: schema.TypeString,
Computed: true,
},
"file_system_administrators_group": {
Type: schema.TypeString,
Computed: true,
},
"organizational_unit_distinguished_name": {
Type: schema.TypeString,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
"endpoints": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"iscsi": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_name": {
Type: schema.TypeString,
Computed: true,
},
"ip_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"management": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_name": {
Type: schema.TypeString,
Computed: true,
},
"ip_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"nfs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_name": {
Type: schema.TypeString,
Computed: true,
},
"ip_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"smb": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_name": {
Type: schema.TypeString,
Computed: true,
},
"ip_addresses": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},
"file_system_id": {
Type: schema.TypeString,
Computed: true,
},
"filter": DataSourceStorageVirtualMachineFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"lifecycle_status": {
Type: schema.TypeString,
Computed: true,
},
"lifecycle_transition_reason": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"message": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"subtype": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
"uuid": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceOntapStorageVirtualMachineRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FSxConn(ctx)
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

input := &fsx.DescribeStorageVirtualMachinesInput{}

if id, ok := d.GetOk("id"); ok {
input.StorageVirtualMachineIds = []*string{aws.String(id.(string))}
}

input.Filters = BuildStorageVirtualMachineFiltersDataSource(
d.Get("filter").(*schema.Set),
)

if len(input.Filters) == 0 {
input.Filters = nil
}

svm, err := findStorageVirtualMachine(ctx, conn, input, tfslices.PredicateTrue[*fsx.StorageVirtualMachine]())

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading FSx ONTAP Storage Virtual Machine: %s", err)
}

d.SetId(aws.StringValue(svm.StorageVirtualMachineId))
if err := d.Set("active_directory_configuration", flattenSvmActiveDirectoryConfiguration(d, svm.ActiveDirectoryConfiguration)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting svm_active_directory: %s", err)
}
d.Set("arn", svm.ResourceARN)
d.Set("creation_time", svm.CreationTime.Format(time.RFC3339))
if err := d.Set("endpoints", flattenSvmEndpoints(svm.Endpoints)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting endpoints: %s", err)
}
d.Set("file_system_id", svm.FileSystemId)
d.Set("lifecycle_status", svm.Lifecycle)
if err := d.Set("lifecycle_transition_reason", flattenLifecycleTransitionReason(svm.LifecycleTransitionReason)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting lifecycle_transition_reason: %s", err)
}
d.Set("name", svm.Name)
d.Set("subtype", svm.Subtype)
d.Set("uuid", svm.UUID)

tags := KeyValueTags(ctx, svm.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}

return diags
}

func flattenLifecycleTransitionReason(rs *fsx.LifecycleTransitionReason) []interface{} {
if rs == nil {
return []interface{}{}
}

m := make(map[string]interface{})

if rs.Message != nil {
m["message"] = aws.StringValue(rs.Message)
}

return []interface{}{m}
}
Loading
Loading