Skip to content

Commit

Permalink
File share Mount target resource rename (#4604)
Browse files Browse the repository at this point in the history
* Deprecated share target and added share mount target resources and data sources

* added maturity

* updated beta sdk version and removed custom sdk

* updated read methods

* pr review comments fix

* fix for concurrent target create and delete issue
  • Loading branch information
deepaksibm authored Jun 13, 2023
1 parent d76a09a commit 8651619
Show file tree
Hide file tree
Showing 23 changed files with 1,522 additions and 30 deletions.
15 changes: 15 additions & 0 deletions examples/ibm-is-ng/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,21 @@ data "ibm_is_share_targets" "is_share_targets" {
share = ibm_is_share.is_share.id
}

resource "ibm_is_share_mount_target" "is_share_mount_target" {
share = ibm_is_share.is_share.id
vpc = ibm_is_vpc.vpc1.id
name = "my-share-target-1"
}

data "ibm_is_share_mount_target" "is_share_mount_target" {
share = ibm_is_share.is_share.id
mount_target = ibm_is_share_mount_target.is_share_target.mount_target
}

data "ibm_is_share_mount_targets" "is_share_mount_targets" {
share = ibm_is_share.is_share.id
}

data "ibm_is_share" "is_share" {
share = ibm_is_share.is_share.id
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/IBM/scc-go-sdk/v4 v4.0.2
github.com/IBM/schematics-go-sdk v0.2.1
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.0
github.com/IBM/vpc-beta-go-sdk v0.1.0
github.com/IBM/vpc-beta-go-sdk v0.4.0
github.com/IBM/vpc-go-sdk v0.38.0
github.com/ScaleFT/sshkeys v0.0.0-20200327173127-6142f742bca5
github.com/Shopify/sarama v1.29.1
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/IBM/schematics-go-sdk v0.2.1 h1:byATysGD+Z1k/wdtNqQmKALcAPjgSLuSyzcab
github.com/IBM/schematics-go-sdk v0.2.1/go.mod h1:Tw2OSAPdpC69AxcwoyqcYYaGTTW6YpERF9uNEU+BFRQ=
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.0 h1:Lx4Bvim/MfoHEYR+n312bty5DirAJypBGGS9YZo3zCw=
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.0/go.mod h1:jagqWmjZ0zUEqh5jdGB42ApSQS40fu2LWw6pdg8JJko=
github.com/IBM/vpc-beta-go-sdk v0.1.0 h1:+kdF+Y/0KY189HhpkqDrue9o0LluAr7rlOU5Zhu7hck=
github.com/IBM/vpc-beta-go-sdk v0.1.0/go.mod h1:TGfLgJVmgQy6XvXc4lya8Vxvw7fvk83nqs5hdP+/VGo=
github.com/IBM/vpc-beta-go-sdk v0.4.0 h1:OxABcc2GeclCMN/bpcQfM2cmNB2GuajwuocsPJSFdl0=
github.com/IBM/vpc-beta-go-sdk v0.4.0/go.mod h1:fzHDAQIqH/5yJmYsKodKHLcqxMDT+yfH6vZjdiw8CQA=
github.com/IBM/vpc-go-sdk v0.38.0 h1:iB2/ukDciK6s2VBnMmNQeZqenOaUXJSt+V2M4fMjAnM=
github.com/IBM/vpc-go-sdk v0.38.0/go.mod h1:MgZrbITC067AlcE5oy4hwylasFvrePL4RVxeF6GTdKQ=
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56 h1:vuquMR410psHNax14XKNWa0Ae/kYgWJcXi0IFuX60N0=
Expand Down Expand Up @@ -686,7 +686,6 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/onsi/gomega v1.18.0/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/onsi/gomega v1.20.0/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo=
github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo=
github.com/onsi/gomega v1.21.1/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8luStNc=
github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU=
Expand Down
4 changes: 4 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ func Provider() *schema.Provider {
"ibm_is_share_profiles": vpc.DataSourceIbmIsShareProfiles(),
"ibm_is_share_target": vpc.DataSourceIbmIsShareTarget(),
"ibm_is_share_targets": vpc.DataSourceIbmIsShareTargets(),
"ibm_is_share_mount_target": vpc.DataSourceIBMIsShareTarget(),
"ibm_is_share_mount_targets": vpc.DataSourceIBMIsShareTargets(),
"ibm_is_volume": vpc.DataSourceIBMISVolume(),
"ibm_is_volumes": vpc.DataSourceIBMIsVolumes(),
"ibm_is_volume_profile": vpc.DataSourceIBMISVolumeProfile(),
Expand Down Expand Up @@ -1003,6 +1005,7 @@ func Provider() *schema.Provider {
"ibm_is_share": vpc.ResourceIbmIsShare(),
"ibm_is_share_replica_operations": vpc.ResourceIbmIsShareReplicaOperations(),
"ibm_is_share_target": vpc.ResourceIbmIsShareMountTarget(),
"ibm_is_share_mount_target": vpc.ResourceIBMIsShareMountTarget(),
"ibm_is_subnet": vpc.ResourceIBMISSubnet(),
"ibm_is_subnet_reserved_ip": vpc.ResourceIBMISReservedIP(),
"ibm_is_subnet_network_acl_attachment": vpc.ResourceIBMISSubnetNetworkACLAttachment(),
Expand Down Expand Up @@ -1402,6 +1405,7 @@ func Validator() validate.ValidatorDict {
"ibm_is_share": vpc.ResourceIbmIsShareValidator(),
"ibm_is_share_replica_operations": vpc.ResourceIbmIsShareReplicaOperationsValidator(),
"ibm_is_share_target": vpc.ResourceIbmIsShareMountTargetValidator(),
"ibm_is_share_mount_target": vpc.ResourceIBMIsShareMountTargetValidator(),
"ibm_is_snapshot": vpc.ResourceIBMISSnapshotValidator(),
"ibm_is_ssh_key": vpc.ResourceIBMISSHKeyValidator(),
"ibm_is_subnet": vpc.ResourceIBMISSubnetValidator(),
Expand Down
4 changes: 2 additions & 2 deletions ibm/service/vpc/data_source_ibm_is_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ func dataSourceIbmIsShareRead(context context.Context, d *schema.ResourceData, m
return diag.FromErr(fmt.Errorf("Error setting source_share %s", err))
}
}
if share.Targets != nil {
err = d.Set("share_targets", dataSourceShareFlattenTargets(share.Targets))
if share.MountTargets != nil {
err = d.Set("share_targets", dataSourceShareFlattenTargets(share.MountTargets))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting targets %s", err))
}
Expand Down
305 changes: 305 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_share_mount_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package vpc

import (
"context"
"fmt"
"log"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/vpc-beta-go-sdk/vpcbetav1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceIBMIsShareTarget() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsShareTargetRead,

Schema: map[string]*schema.Schema{
"share": {
Type: schema.TypeString,
Optional: true,
Description: "The file share identifier.",
},
"mount_target": {
Type: schema.TypeString,
Optional: true,
Description: "The share target identifier.",
},
"share_name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"share", "share_name"},
Description: "The file share name.",
},
"mount_target_name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"mount_target", "mount_target_name"},
Description: "The share target name.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the share target was created.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this share target.",
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the mount target.",
},
"mount_path": {
Type: schema.TypeString,
Computed: true,
Description: "The mount path for the share.The IP addresses used in the mount path are currently within the IBM services IP range, but are expected to change to be within one of the VPC's subnets in the future.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this share target.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"subnet": {
Type: schema.TypeList,
Computed: true,
Description: "The subnet associated with this file share target.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this subnet.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this subnet.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this subnet.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this subnet.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"vpc": {
Type: schema.TypeList,
Computed: true,
Description: "The VPC to which this share target is allowing to mount the file share.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this VPC.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this VPC.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPC.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this VPC.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
}
}

func dataSourceIBMIsShareTargetRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1BetaAPI()
if err != nil {
return diag.FromErr(err)
}

share_id := d.Get("share").(string)
share_name := d.Get("share_name").(string)
share_target := d.Get("mount_target").(string)
share_target_name := d.Get("mount_target_name").(string)
var shareTarget *vpcbetav1.ShareMountTarget
if share_name != "" {
listSharesOptions := &vpcbetav1.ListSharesOptions{}
listSharesOptions.Name = &share_name
shareCollection, response, err := vpcClient.ListSharesWithContext(context, listSharesOptions)
if err != nil {
log.Printf("[DEBUG] ListSharesWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
for _, sharesItem := range shareCollection.Shares {
if *sharesItem.Name == share_name {
share_id = *sharesItem.ID
break
}
}
}
if share_target_name != "" {
listShareTargetsOptions := &vpcbetav1.ListShareMountTargetsOptions{}

listShareTargetsOptions.SetShareID(share_id)
listShareTargetsOptions.SetName(share_target_name)

shareTargetCollection, response, err := vpcClient.ListShareMountTargetsWithContext(context, listShareTargetsOptions)
if err != nil {
log.Printf("[DEBUG] ListShareTargetsWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
for _, targetsItem := range shareTargetCollection.MountTargets {
if *targetsItem.Name == share_target_name {
shareTarget = &targetsItem
break
}
}
} else {
getShareTargetOptions := &vpcbetav1.GetShareMountTargetOptions{}
getShareTargetOptions.SetShareID(share_id)
getShareTargetOptions.SetID(share_target)
shareTarget1, response, err := vpcClient.GetShareMountTargetWithContext(context, getShareTargetOptions)
if err != nil {
log.Printf("[DEBUG] GetShareTargetWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
shareTarget = shareTarget1
}

d.SetId(fmt.Sprintf("%s/%s", share_id, *shareTarget.ID))
if err = d.Set("created_at", shareTarget.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("href", shareTarget.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("lifecycle_state", shareTarget.LifecycleState); err != nil {
return diag.FromErr(fmt.Errorf("Error setting lifecycle_state: %s", err))
}
if err = d.Set("mount_path", shareTarget.MountPath); err != nil {
return diag.FromErr(fmt.Errorf("Error setting mount_path: %s", err))
}
if err = d.Set("name", shareTarget.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("resource_type", shareTarget.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_type: %s", err))
}

if shareTarget.VPC != nil {
err = d.Set("vpc", dataSourceShareMountTargetFlattenVpc(*shareTarget.VPC))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting vpc %s", err))
}
}

return nil
}

func dataSourceShareMountTargetFlattenVpc(result vpcbetav1.VPCReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceShareMountTargetVpcToMap(result)
finalList = append(finalList, finalMap)

return finalList
}

func dataSourceShareMountTargetVpcToMap(vpcItem vpcbetav1.VPCReference) (vpcMap map[string]interface{}) {
vpcMap = map[string]interface{}{}

if vpcItem.CRN != nil {
vpcMap["crn"] = vpcItem.CRN
}
if vpcItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceShareTargetVpcDeletedToMap(*vpcItem.Deleted)
deletedList = append(deletedList, deletedMap)
vpcMap["deleted"] = deletedList
}
if vpcItem.Href != nil {
vpcMap["href"] = vpcItem.Href
}
if vpcItem.ID != nil {
vpcMap["id"] = vpcItem.ID
}
if vpcItem.Name != nil {
vpcMap["name"] = vpcItem.Name
}
/*
if vpcItem.ResourceType != nil {
vpcMap["resource_type"] = vpcItem.ResourceType
}
*/

return vpcMap
}

func dataSourceShareMountTargetVpcDeletedToMap(deletedItem vpcbetav1.VPCReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}

if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}

return deletedMap
}
Loading

0 comments on commit 8651619

Please sign in to comment.