Skip to content

Commit 1fd3117

Browse files
committed
Feature: VSI Local DIsks
1 parent d7e6dff commit 1fd3117

23 files changed

+1640
-1
lines changed

examples/ibm-is-ng/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,22 @@ data "ibm_is_dedicated_hosts" "dhosts" {
332332
data "ibm_is_dedicated_host" "dhost" {
333333
name = ibm_is_dedicated_host.is_dedicated_host.name
334334
host_group = data.ibm_is_dedicated_host_group.dgroup.id
335+
}
336+
337+
resource "ibm_is_instance_disk_management" "disks"{
338+
instance = ibm_is_instance.instance1.id
339+
disks {
340+
name = "mydisk01"
341+
id = ibm_is_instance.instance1.disks.0.id
342+
}
343+
}
344+
345+
data "ibm_is_instance_disks" "disk1" {
346+
instance = ibm_is_instance.instance1.id
347+
348+
}
349+
350+
data "ibm_is_instance_disk" "disk1" {
351+
instance = ibm_is_instance.instance1.id
352+
disk = data.ibm_is_instance_disks.disk1.disks.0.id
335353
}

ibm/data_source_ibm_is_instance.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,50 @@ func dataSourceIBMISInstance() *schema.Resource {
378378
Computed: true,
379379
Description: "The resource group name in which resource is provisioned",
380380
},
381+
isInstanceDisks: &schema.Schema{
382+
Type: schema.TypeList,
383+
Computed: true,
384+
Description: "Collection of the instance's disks.",
385+
Elem: &schema.Resource{
386+
Schema: map[string]*schema.Schema{
387+
"created_at": &schema.Schema{
388+
Type: schema.TypeString,
389+
Computed: true,
390+
Description: "The date and time that the disk was created.",
391+
},
392+
"href": &schema.Schema{
393+
Type: schema.TypeString,
394+
Computed: true,
395+
Description: "The URL for this instance disk.",
396+
},
397+
"id": &schema.Schema{
398+
Type: schema.TypeString,
399+
Computed: true,
400+
Description: "The unique identifier for this instance disk.",
401+
},
402+
"interface_type": &schema.Schema{
403+
Type: schema.TypeString,
404+
Computed: true,
405+
Description: "The disk interface used for attaching the disk.The enumerated values for this property are expected to expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the resource on which the unexpected property value was encountered.",
406+
},
407+
"name": &schema.Schema{
408+
Type: schema.TypeString,
409+
Computed: true,
410+
Description: "The user-defined name for this disk.",
411+
},
412+
"resource_type": &schema.Schema{
413+
Type: schema.TypeString,
414+
Computed: true,
415+
Description: "The resource type.",
416+
},
417+
"size": &schema.Schema{
418+
Type: schema.TypeInt,
419+
Computed: true,
420+
Description: "The size of the disk in GB (gigabytes).",
421+
},
422+
},
423+
},
424+
},
381425
},
382426
}
383427
}
@@ -725,6 +769,13 @@ func instanceGetByName(d *schema.ResourceData, meta interface{}, name string) er
725769
gpuList := make([]map[string]interface{}, 0)
726770
d.Set(isInstanceGpu, gpuList)
727771

772+
if instance.Disks != nil {
773+
err = d.Set(isInstanceDisks, dataSourceInstanceFlattenDisks(instance.Disks))
774+
if err != nil {
775+
return fmt.Errorf("Error setting disks %s", err)
776+
}
777+
}
778+
728779
if instance.PrimaryNetworkInterface != nil {
729780
primaryNicList := make([]map[string]interface{}, 0)
730781
currentPrimNic := map[string]interface{}{}
@@ -983,3 +1034,39 @@ func isOpenSSHPrivKeyEncrypted(data []byte) (bool, error) {
9831034
}
9841035
return true, nil
9851036
}
1037+
1038+
func dataSourceInstanceFlattenDisks(result []vpcv1.InstanceDisk) (disks []map[string]interface{}) {
1039+
for _, disksItem := range result {
1040+
disks = append(disks, dataSourceInstanceDisksToMap(disksItem))
1041+
}
1042+
1043+
return disks
1044+
}
1045+
1046+
func dataSourceInstanceDisksToMap(disksItem vpcv1.InstanceDisk) (disksMap map[string]interface{}) {
1047+
disksMap = map[string]interface{}{}
1048+
1049+
if disksItem.CreatedAt != nil {
1050+
disksMap["created_at"] = disksItem.CreatedAt.String()
1051+
}
1052+
if disksItem.Href != nil {
1053+
disksMap["href"] = disksItem.Href
1054+
}
1055+
if disksItem.ID != nil {
1056+
disksMap["id"] = disksItem.ID
1057+
}
1058+
if disksItem.InterfaceType != nil {
1059+
disksMap["interface_type"] = disksItem.InterfaceType
1060+
}
1061+
if disksItem.Name != nil {
1062+
disksMap["name"] = disksItem.Name
1063+
}
1064+
if disksItem.ResourceType != nil {
1065+
disksMap["resource_type"] = disksItem.ResourceType
1066+
}
1067+
if disksItem.Size != nil {
1068+
disksMap["size"] = disksItem.Size
1069+
}
1070+
1071+
return disksMap
1072+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package ibm
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
14+
"github.com/IBM/vpc-go-sdk/vpcv1"
15+
)
16+
17+
func dataSourceIbmIsInstanceDisk() *schema.Resource {
18+
return &schema.Resource{
19+
ReadContext: dataSourceIbmIsInstanceDiskRead,
20+
21+
Schema: map[string]*schema.Schema{
22+
"instance": &schema.Schema{
23+
Type: schema.TypeString,
24+
Required: true,
25+
Description: "The instance identifier.",
26+
},
27+
"disk": &schema.Schema{
28+
Type: schema.TypeString,
29+
Required: true,
30+
Description: "The instance disk identifier.",
31+
},
32+
"created_at": &schema.Schema{
33+
Type: schema.TypeString,
34+
Computed: true,
35+
Description: "The date and time that the disk was created.",
36+
},
37+
"href": &schema.Schema{
38+
Type: schema.TypeString,
39+
Computed: true,
40+
Description: "The URL for this instance disk.",
41+
},
42+
"interface_type": &schema.Schema{
43+
Type: schema.TypeString,
44+
Computed: true,
45+
Description: "The disk interface used for attaching the disk.The enumerated values for this property are expected to expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the resource on which the unexpected property value was encountered.",
46+
},
47+
"name": &schema.Schema{
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: "The user-defined name for this disk.",
51+
},
52+
"resource_type": &schema.Schema{
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: "The resource type.",
56+
},
57+
"size": &schema.Schema{
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
Description: "The size of the disk in GB (gigabytes).",
61+
},
62+
},
63+
}
64+
}
65+
66+
func dataSourceIbmIsInstanceDiskRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
67+
vpcClient, err := meta.(ClientSession).VpcV1API()
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
71+
72+
getInstanceDiskOptions := &vpcv1.GetInstanceDiskOptions{}
73+
74+
getInstanceDiskOptions.SetInstanceID(d.Get("instance").(string))
75+
getInstanceDiskOptions.SetID(d.Get("disk").(string))
76+
77+
instanceDisk, response, err := vpcClient.GetInstanceDiskWithContext(context, getInstanceDiskOptions)
78+
if err != nil {
79+
log.Printf("[DEBUG] GetInstanceDiskWithContext failed %s\n%s", err, response)
80+
return diag.FromErr(err)
81+
}
82+
83+
d.SetId(*instanceDisk.ID)
84+
if err = d.Set("created_at", instanceDisk.CreatedAt.String()); err != nil {
85+
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
86+
}
87+
if err = d.Set("href", instanceDisk.Href); err != nil {
88+
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
89+
}
90+
if err = d.Set("interface_type", instanceDisk.InterfaceType); err != nil {
91+
return diag.FromErr(fmt.Errorf("Error setting interface_type: %s", err))
92+
}
93+
if err = d.Set("name", instanceDisk.Name); err != nil {
94+
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
95+
}
96+
if err = d.Set("resource_type", instanceDisk.ResourceType); err != nil {
97+
return diag.FromErr(fmt.Errorf("Error setting resource_type: %s", err))
98+
}
99+
if err = d.Set("size", instanceDisk.Size); err != nil {
100+
return diag.FromErr(fmt.Errorf("Error setting size: %s", err))
101+
}
102+
103+
return nil
104+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package ibm
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13+
)
14+
15+
func TestAccIBMISInstanceDiskDataSource_basic(t *testing.T) {
16+
resName := "data.ibm_is_instance_disk.test1"
17+
var instance string
18+
insResName := "ibm_is_instance.testacc_instance"
19+
vpcname := fmt.Sprintf("tfins-vpc-%d", acctest.RandIntRange(10, 100))
20+
subnetname := fmt.Sprintf("tfins-subnet-%d", acctest.RandIntRange(10, 100))
21+
name := fmt.Sprintf("tf-instnace-%d", acctest.RandIntRange(10, 100))
22+
sshname := fmt.Sprintf("tfins-ssh-%d", acctest.RandIntRange(10, 100))
23+
publicKey := strings.TrimSpace(`
24+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
25+
`)
26+
volname := fmt.Sprintf("tf-vol-%d", acctest.RandIntRange(10, 100))
27+
resource.Test(t, resource.TestCase{
28+
PreCheck: func() { testAccPreCheck(t) },
29+
Providers: testAccProviders,
30+
Steps: []resource.TestStep{
31+
{
32+
Config: testAccCheckIBMISInstanceDisk(vpcname, subnetname, sshname, publicKey, volname, name),
33+
Check: resource.ComposeTestCheckFunc(
34+
testAccCheckIBMISInstanceExists(insResName, instance),
35+
resource.TestCheckResourceAttr(
36+
insResName, "name", name),
37+
resource.TestCheckResourceAttr(
38+
insResName, "zone", ISZoneName),
39+
resource.TestCheckResourceAttr(
40+
insResName, "disks.#", "1"),
41+
resource.TestCheckResourceAttrSet(
42+
insResName, "disks.0.name"),
43+
resource.TestCheckResourceAttrSet(
44+
insResName, "disks.0.size"),
45+
),
46+
},
47+
resource.TestStep{
48+
Config: testAccCheckIBMISInstanceDiskDataSourceConfig(vpcname, subnetname, sshname, publicKey, volname, name),
49+
Check: resource.ComposeTestCheckFunc(
50+
resource.TestCheckResourceAttrSet(resName, "name"),
51+
resource.TestCheckResourceAttrSet(resName, "size"),
52+
),
53+
},
54+
},
55+
})
56+
}
57+
58+
func testAccCheckIBMISInstanceDiskDataSourceConfig(vpcname, subnetname, sshname, publicKey, volname, name string) string {
59+
return testAccCheckIBMISInstanceDisk(vpcname, subnetname, sshname, publicKey, volname, name) + fmt.Sprintf(`
60+
data "ibm_is_instance" "ins" {
61+
name = "%s"
62+
private_key = file("test-fixtures/.ssh/id_rsa")
63+
passphrase = ""
64+
}
65+
data "ibm_is_instance_disks" "test1" {
66+
instance = data.ibm_is_instance.ins.id
67+
68+
}
69+
data "ibm_is_instance_disk" "test1" {
70+
instance = data.ibm_is_instance.ins.id
71+
disk = data.ibm_is_instance_disks.test1.disks.0.id
72+
}`, name)
73+
}

0 commit comments

Comments
 (0)