Skip to content

Commit

Permalink
Snapshot and Volume Instance Profiles Development
Browse files Browse the repository at this point in the history
  • Loading branch information
SunithaGudisagarIBM1 committed Dec 1, 2024
1 parent c912909 commit 2a6e883
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,36 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
)

func TestAccIBMIsImageInstanceProfilesDataSourceBasic(t *testing.T) {
name := fmt.Sprintf("tfimg-name-%d", acctest.RandIntRange(10, 100))
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMIsImageInstanceProfilesDataSourceConfigBasic(),
Config: testAccCheckIBMIsImageInstanceProfilesDataSourceConfigBasic(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_is_image_instance_profiles.is_image_instance_profiles_instance", "id"),
resource.TestCheckResourceAttrSet("data.ibm_is_image_instance_profiles.is_image_instance_profiles_instance", "instance_profiles.#"),
resource.TestCheckResourceAttrSet("data.ibm_is_image_instance_profiles.is_image_instance_profiles_instance", "instance_profiles.0.href"),
resource.TestCheckResourceAttrSet("data.ibm_is_image_instance_profiles.is_image_instance_profiles_instance", "instance_profiles.0.name"),
resource.TestCheckResourceAttrSet("data.ibm_is_image_instance_profiles.is_image_instance_profiles_instance", "instance_profiles.0.resource_type"),
),
},
},
})
}

func testAccCheckIBMIsImageInstanceProfilesDataSourceConfigBasic() string {
return fmt.Sprintf(`
func testAccCheckIBMIsImageInstanceProfilesDataSourceConfigBasic(name string) string {
return testAccCheckIBMISImageConfig(name) + fmt.Sprintf(`
data "ibm_is_image_instance_profiles" "is_image_instance_profiles_instance" {
identifier = "id"
identifier = "ibm_is_image.isExampleImage.id"
}
`)
}
119 changes: 119 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_snapshot_instance_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package vpc

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
)

func DataSourceIBMIsSnapshotInstanceProfiles() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsSnapshotInstanceProfilesRead,

Schema: map[string]*schema.Schema{
"identifier": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The snapshot identifier.",
},
"instance_profiles": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "A page of instance profiles compatible with the snapshot.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this virtual server instance profile.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this virtual server instance profile.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
}
}

func dataSourceIBMIsSnapshotInstanceProfilesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_is_snapshot_instance_profiles", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

listSnapshotInstanceProfilesOptions := &vpcv1.ListSnapshotInstanceProfilesOptions{}

listSnapshotInstanceProfilesOptions.SetID(d.Get("identifier").(string))

start := ""
allrecs := []vpcv1.InstanceProfileReference{}
for {
if start != "" {
listSnapshotInstanceProfilesOptions.Start = &start
}
snapshotInstanceProfile, response, err := vpcClient.ListSnapshotInstanceProfiles(listSnapshotInstanceProfilesOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_is_snapshot_instance_profiles", "read")
log.Printf("[DEBUG]\n%s\n%s", tfErr.GetDebugMessage(), response)
return tfErr.GetDiag()
}
start = flex.GetNext(snapshotInstanceProfile.Next)
allrecs = append(allrecs, snapshotInstanceProfile.InstanceProfiles...)
if start == "" {
break
}
}

d.SetId(dataSourceIBMIsSnapshotInstanceProfilesID(d))

mapSlice := []map[string]interface{}{}
for _, modelItem := range allrecs {
modelMap, err := DataSourceIBMIsSnapshotInstanceProfilesInstanceProfileReferenceToMap(&modelItem)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_is_snapshot_instance_profiles", "read")
return tfErr.GetDiag()
}
mapSlice = append(mapSlice, modelMap)
}

if err = d.Set("instance_profiles", mapSlice); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting instance_profiles %s", err), "(Data) ibm_is_snapshot_instance_profiles", "read")
return tfErr.GetDiag()
}

return nil
}

// dataSourceIBMIsSnapshotInstanceProfilesID returns a reasonable ID for the list.
func dataSourceIBMIsSnapshotInstanceProfilesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}

func DataSourceIBMIsSnapshotInstanceProfilesInstanceProfileReferenceToMap(model *vpcv1.InstanceProfileReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["href"] = *model.Href
modelMap["name"] = *model.Name
modelMap["resource_type"] = *model.ResourceType
return modelMap, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package vpc_test

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
)

func TestAccIBMIsSnapshotInstanceProfilesDataSourceBasic(t *testing.T) {
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("tf-instnace-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tf-subnet-%d", acctest.RandIntRange(10, 100))
publicKey := strings.TrimSpace(`
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
`)
sshname := fmt.Sprintf("tf-ssh-%d", acctest.RandIntRange(10, 100))
volname := fmt.Sprintf("tf-vol-%d", acctest.RandIntRange(10, 100))
name1 := fmt.Sprintf("tfsnapshotuat-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMIsSnapshotInstanceProfilesDataSourceConfigBasic(vpcname, subnetname, sshname, publicKey, volname, name, name1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_instance_profiles.is_snapshot_instance_profiles_instance", "id"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_instance_profiles.is_snapshot_instance_profiles_instance", "instance_profiles.#"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_instance_profiles.is_snapshot_instance_profiles_instance", "instance_profiles.0.href"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_instance_profiles.is_snapshot_instance_profiles_instance", "instance_profiles.0.name"),
resource.TestCheckResourceAttrSet("data.ibm_is_snapshot_instance_profiles.is_snapshot_instance_profiles_instance", "instance_profiles.0.resource_type"),
),
},
},
})
}

func testAccCheckIBMIsSnapshotInstanceProfilesDataSourceConfigBasic(vpcname, subnetname, sshname, publicKey, volname, name, name1 string) string {
return testAccCheckIBMISSnapshotConfig(vpcname, subnetname, sshname, publicKey, volname, name, name1) + fmt.Sprintf(`
data "ibm_is_volume_instance_profiles" "is_volume_instance_profiles_instance" {
identifier = "ibm_is_snapshot.testacc_snapshot.id"
}
`)
}
119 changes: 119 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_volume_instance_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package vpc

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
)

func DataSourceIBMIsVolumeInstanceProfiles() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsVolumeInstanceProfilesRead,

Schema: map[string]*schema.Schema{
"identifier": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The volume identifier.",
},
"instance_profiles": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "A page of instance profiles compatible with the volume.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this virtual server instance profile.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this virtual server instance profile.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
}
}

func dataSourceIBMIsVolumeInstanceProfilesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_is_volume_instance_profiles", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}

listvolumeInstanceProfilesOptions := &vpcv1.ListVolumeInstanceProfilesOptions{}

listvolumeInstanceProfilesOptions.SetID(d.Get("identifier").(string))

start := ""
allrecs := []vpcv1.InstanceProfileReference{}
for {
if start != "" {
listvolumeInstanceProfilesOptions.Start = &start
}
volumeInstanceProfile, response, err := vpcClient.ListVolumeInstanceProfiles(listvolumeInstanceProfilesOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_is_volume_instance_profiles", "read")
log.Printf("[DEBUG]\n%s\n%s", tfErr.GetDebugMessage(), response)
return tfErr.GetDiag()
}
start = flex.GetNext(volumeInstanceProfile.Next)
allrecs = append(allrecs, volumeInstanceProfile.InstanceProfiles...)
if start == "" {
break
}
}

d.SetId(dataSourceIBMIsVolumeInstanceProfilesID(d))

mapSlice := []map[string]interface{}{}
for _, modelItem := range allrecs {
modelMap, err := DataSourceIBMIsVolumeInstanceProfilesInstanceProfileReferenceToMap(&modelItem)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_is_volume_instance_profiles", "read")
return tfErr.GetDiag()
}
mapSlice = append(mapSlice, modelMap)
}

if err = d.Set("instance_profiles", mapSlice); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting instance_profiles %s", err), "(Data) ibm_is_volume_instance_profiles", "read")
return tfErr.GetDiag()
}

return nil
}

// dataSourceIBMIsVolumeInstanceProfilesID returns a reasonable ID for the list.
func dataSourceIBMIsVolumeInstanceProfilesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}

func DataSourceIBMIsVolumeInstanceProfilesInstanceProfileReferenceToMap(model *vpcv1.InstanceProfileReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["href"] = *model.Href
modelMap["name"] = *model.Name
modelMap["resource_type"] = *model.ResourceType
return modelMap, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package vpc_test

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
)

func TestAccIBMIsVolumeInstanceProfilesDataSourceBasic(t *testing.T) {
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("tf-instnace-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tf-subnet-%d", acctest.RandIntRange(10, 100))
publicKey := strings.TrimSpace(`
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
`)
sshname := fmt.Sprintf("tf-ssh-%d", acctest.RandIntRange(10, 100))
userData1 := "a"
userTags1 := "tags-0"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMIsVolumeInstanceProfilesDataSourceConfigBasic(vpcname, subnetname, sshname, publicKey, name, userData1, userTags1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_is_volume_instance_profiles.is_volume_instance_profiles_instance", "id"),
resource.TestCheckResourceAttrSet("data.ibm_is_volume_instance_profiles.is_volume_instance_profiles_instance", "instance_profiles.#"),
resource.TestCheckResourceAttrSet("data.ibm_is_volume_instance_profiles.is_volume_instance_profiles_instance", "instance_profiles.0.href"),
resource.TestCheckResourceAttrSet("data.ibm_is_volume_instance_profiles.is_volume_instance_profiles_instance", "instance_profiles.0.name"),
resource.TestCheckResourceAttrSet("data.ibm_is_volume_instance_profiles.is_volume_instance_profiles_instance", "instance_profiles.0.resource_type"),
),
},
},
})
}

func testAccCheckIBMIsVolumeInstanceProfilesDataSourceConfigBasic(vpcname, subnetname, sshname, publicKey, name, userData1, userTags1 string) string {
return testAccCheckIBMISInstanceUserTagConfig(vpcname, subnetname, sshname, publicKey, name, userData1, userTags1) + fmt.Sprintf(`
data "ibm_is_volume_instance_profiles" "is_volume_instance_profiles_instance" {
identifier = "ibm_is_instance.testacc_instance.boot_volume.0.volume_id"
}
`)
}
Loading

0 comments on commit 2a6e883

Please sign in to comment.