Skip to content

Commit c8c6324

Browse files
parwezabriangustafson
authored andcommitted
adding boot volumes data source
1 parent e7fb32e commit c8c6324

10 files changed

+334
-19
lines changed

docs/core/boot_volumes.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# oci_core_boot_volumes
3+
4+
## BootVolume DataSource
5+
6+
Gets a list of boot_volumes.
7+
8+
### List Operation
9+
Lists the boot volumes in the specified compartment and Availability Domain.
10+
11+
The following arguments are supported:
12+
13+
* `availability_domain` - (Required) The name of the Availability Domain. Example: `Uocm:PHX-AD-1`
14+
* `compartment_id` - (Required) The OCID of the compartment.
15+
16+
17+
The following attributes are exported:
18+
19+
* `boot_volumes` - The list of boot_volumes.
20+
21+
### Example Usage
22+
23+
```
24+
data "oci_core_boot_volumes" "test_boot_volumes" {
25+
#Required
26+
availability_domain = "${var.boot_volume_availability_domain}"
27+
compartment_id = "${var.compartment_id}"
28+
29+
#Optional
30+
}
31+
```
32+
### BootVolume Reference
33+
34+
The following attributes are exported:
35+
36+
* `availability_domain` - The Availability Domain of the boot volume. Example: `Uocm:PHX-AD-1`
37+
* `compartment_id` - The OCID of the compartment that contains the boot volume.
38+
* `display_name` - A user-friendly name. Does not have to be unique, and it's changeable. Avoid entering confidential information.
39+
* `id` - The boot volume's Oracle ID (OCID).
40+
* `image_id` - The image OCID used to create the boot volume.
41+
* `size_in_gbs` - The size of the boot volume in GBs.
42+
* `size_in_mbs` - The size of the volume in MBs. The value must be a multiple of 1024. This field is deprecated. Please use sizeInGBs.
43+
* `state` - The current state of a boot volume.
44+
* `time_created` - The date and time the boot volume was created. Format defined by RFC3339.

provider/core_boot_volume_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform/helper/resource"
10+
"github.com/hashicorp/terraform/terraform"
11+
)
12+
13+
const (
14+
BootVolumeResourceConfig = BootVolumeResourceDependencies + `
15+
16+
`
17+
BootVolumePropertyVariables = `
18+
variable "InstanceImageOCID" {
19+
type = "map"
20+
default = {
21+
// See https://docs.us-phoenix-1.oraclecloud.com/images/
22+
// Oracle-provided image "Oracle-Linux-7.4-2018.02.21-1"
23+
us-phoenix-1 = "ocid1.image.oc1.phx.aaaaaaaaupbfz5f5hdvejulmalhyb6goieolullgkpumorbvxlwkaowglslq"
24+
us-ashburn-1 = "ocid1.image.oc1.iad.aaaaaaaajlw3xfie2t5t52uegyhiq2npx7bqyu4uvi2zyu3w3mqayc2bxmaa"
25+
eu-frankfurt-1 = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaa7d3fsb6272srnftyi4dphdgfjf6gurxqhmv6ileds7ba3m2gltxq"
26+
uk-london-1 = "ocid1.image.oc1.uk-london-1.aaaaaaaaa6h6gj6v4n56mqrbgnosskq63blyv2752g36zerymy63cfkojiiq"
27+
}
28+
}
29+
`
30+
InstanceResourceConfigs = `
31+
resource "oci_core_instance" "test_instance" {
32+
availability_domain = "${var.subnet_availability_domain}"
33+
compartment_id = "${var.compartment_id}"
34+
display_name = "-tf-instance"
35+
image = "${var.InstanceImageOCID[var.region]}"
36+
shape = "VM.Standard1.1"
37+
subnet_id = "${oci_core_subnet.test_subnet.id}"
38+
metadata {
39+
ssh_authorized_keys = "${var.ssh_public_key}"
40+
}
41+
42+
timeouts {
43+
create = "15m"
44+
}
45+
}`
46+
BootVolumeResourceDependencies = BootVolumePropertyVariables + SubnetPropertyVariables + SubnetRequiredOnlyResource + InstanceResourceConfigs
47+
)
48+
49+
func TestCoreBootVolumeResource_basic(t *testing.T) {
50+
provider := testAccProvider
51+
config := testProviderConfig()
52+
53+
compartmentId2 := getRequiredEnvSetting("compartment_id_for_update")
54+
compartmentIdVariableStr2 := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId2)
55+
datasourceName := "data.oci_core_boot_volumes.test_boot_volumes"
56+
57+
resource.Test(t, resource.TestCase{
58+
Providers: map[string]terraform.ResourceProvider{
59+
"oci": provider,
60+
},
61+
Steps: []resource.TestStep{
62+
// verify datasource
63+
{
64+
Config: config + BootVolumeResourceConfig + `
65+
data "oci_core_boot_volumes" "test_boot_volumes" {
66+
#Required
67+
availability_domain = "${var.subnet_availability_domain}"
68+
compartment_id = "${var.compartment_id}"
69+
70+
}
71+
` + compartmentIdVariableStr2 ,
72+
Check: resource.ComposeTestCheckFunc(
73+
resource.TestCheckResourceAttr(datasourceName, "compartment_id", compartmentId2),
74+
75+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.#"),
76+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.0.availability_domain"),
77+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.0.compartment_id"),
78+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.0.id"),
79+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.0.size_in_mbs"),
80+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.0.state"),
81+
resource.TestCheckResourceAttrSet(datasourceName, "boot_volumes.0.time_created"),
82+
),
83+
},
84+
},
85+
})
86+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"context"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
oci_core "github.com/oracle/oci-go-sdk/core"
10+
11+
"github.com/oracle/terraform-provider-oci/crud"
12+
)
13+
14+
func BootVolumesDataSource() *schema.Resource {
15+
return &schema.Resource{
16+
Read: readBootVolumes,
17+
Schema: map[string]*schema.Schema{
18+
"filter": dataSourceFiltersSchema(),
19+
"availability_domain": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"compartment_id": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"boot_volumes": {
28+
Type: schema.TypeList,
29+
Computed: true,
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
// Required
33+
34+
// Optional
35+
36+
// Computed
37+
"availability_domain": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"compartment_id": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
"display_name": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"image_id": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
"size_in_gbs": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
},
61+
"size_in_mbs": {
62+
Type: schema.TypeInt,
63+
Computed: true,
64+
},
65+
"state": {
66+
Type: schema.TypeString,
67+
Computed: true,
68+
},
69+
"time_created": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
},
73+
},
74+
},
75+
},
76+
},
77+
}
78+
}
79+
80+
func readBootVolumes(d *schema.ResourceData, m interface{}) error {
81+
sync := &BootVolumesDataSourceCrud{}
82+
sync.D = d
83+
sync.Client = m.(*OracleClients).blockstorageClient
84+
85+
return crud.ReadResource(sync)
86+
}
87+
88+
type BootVolumesDataSourceCrud struct {
89+
D *schema.ResourceData
90+
Client *oci_core.BlockstorageClient
91+
Res *oci_core.ListBootVolumesResponse
92+
}
93+
94+
func (s *BootVolumesDataSourceCrud) VoidState() {
95+
s.D.SetId("")
96+
}
97+
98+
func (s *BootVolumesDataSourceCrud) Get() error {
99+
request := oci_core.ListBootVolumesRequest{}
100+
101+
if availabilityDomain, ok := s.D.GetOkExists("availability_domain"); ok {
102+
tmp := availabilityDomain.(string)
103+
request.AvailabilityDomain = &tmp
104+
}
105+
106+
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
107+
tmp := compartmentId.(string)
108+
request.CompartmentId = &tmp
109+
}
110+
111+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core")
112+
113+
response, err := s.Client.ListBootVolumes(context.Background(), request)
114+
if err != nil {
115+
return err
116+
}
117+
118+
s.Res = &response
119+
request.Page = s.Res.OpcNextPage
120+
121+
for request.Page != nil {
122+
listResponse, err := s.Client.ListBootVolumes(context.Background(), request)
123+
if err != nil {
124+
return err
125+
}
126+
127+
s.Res.Items = append(s.Res.Items, listResponse.Items...)
128+
request.Page = listResponse.OpcNextPage
129+
}
130+
131+
return nil
132+
}
133+
134+
func (s *BootVolumesDataSourceCrud) SetData() {
135+
if s.Res == nil {
136+
return
137+
}
138+
139+
s.D.SetId(crud.GenerateDataSourceID())
140+
resources := []map[string]interface{}{}
141+
142+
for _, r := range s.Res.Items {
143+
bootVolume := map[string]interface{}{
144+
"availability_domain": *r.AvailabilityDomain,
145+
"compartment_id": *r.CompartmentId,
146+
}
147+
148+
if r.DisplayName != nil {
149+
bootVolume["display_name"] = *r.DisplayName
150+
}
151+
152+
if r.Id != nil {
153+
bootVolume["id"] = *r.Id
154+
}
155+
156+
if r.ImageId != nil {
157+
bootVolume["image_id"] = *r.ImageId
158+
}
159+
160+
if r.SizeInGBs != nil {
161+
bootVolume["size_in_gbs"] = *r.SizeInGBs
162+
}
163+
164+
if r.SizeInMBs != nil {
165+
bootVolume["size_in_mbs"] = *r.SizeInMBs
166+
}
167+
168+
bootVolume["state"] = r.LifecycleState
169+
170+
bootVolume["time_created"] = r.TimeCreated.String()
171+
172+
resources = append(resources, bootVolume)
173+
}
174+
175+
if f, fOk := s.D.GetOkExists("filter"); fOk {
176+
resources = ApplyFilters(f.(*schema.Set), resources)
177+
}
178+
179+
if err := s.D.Set("boot_volumes", resources); err != nil {
180+
panic(err)
181+
}
182+
183+
return
184+
}

provider/core_instance_resource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func createInstance(d *schema.ResourceData, m interface{}) error {
283283
sync.D = d
284284
sync.Client = m.(*OracleClients).computeClient
285285
sync.VirtualNetworkClient = m.(*OracleClients).virtualNetworkClient
286-
sync.BlockStorageClient = m.(*OracleClients).blockStorageClient
286+
sync.BlockStorageClient = m.(*OracleClients).blockstorageClient
287287

288288
return crud.CreateResource(d, sync)
289289
}
@@ -293,7 +293,7 @@ func readInstance(d *schema.ResourceData, m interface{}) error {
293293
sync.D = d
294294
sync.Client = m.(*OracleClients).computeClient
295295
sync.VirtualNetworkClient = m.(*OracleClients).virtualNetworkClient
296-
sync.BlockStorageClient = m.(*OracleClients).blockStorageClient
296+
sync.BlockStorageClient = m.(*OracleClients).blockstorageClient
297297

298298
return crud.ReadResource(sync)
299299
}
@@ -303,7 +303,7 @@ func updateInstance(d *schema.ResourceData, m interface{}) error {
303303
sync.D = d
304304
sync.Client = m.(*OracleClients).computeClient
305305
sync.VirtualNetworkClient = m.(*OracleClients).virtualNetworkClient
306-
sync.BlockStorageClient = m.(*OracleClients).blockStorageClient
306+
sync.BlockStorageClient = m.(*OracleClients).blockstorageClient
307307

308308
return crud.UpdateResource(d, sync)
309309
}
@@ -313,7 +313,7 @@ func deleteInstance(d *schema.ResourceData, m interface{}) error {
313313
sync.D = d
314314
sync.Client = m.(*OracleClients).computeClient
315315
sync.VirtualNetworkClient = m.(*OracleClients).virtualNetworkClient
316-
sync.BlockStorageClient = m.(*OracleClients).blockStorageClient
316+
sync.BlockStorageClient = m.(*OracleClients).blockstorageClient
317317
sync.DisableNotFoundRetries = true
318318

319319
return crud.DeleteResource(d, sync)

provider/core_volume_backup_resource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,31 @@ func VolumeBackupResource() *schema.Resource {
9999
func createVolumeBackup(d *schema.ResourceData, m interface{}) error {
100100
sync := &VolumeBackupResourceCrud{}
101101
sync.D = d
102-
sync.Client = m.(*OracleClients).blockStorageClient
102+
sync.Client = m.(*OracleClients).blockstorageClient
103103

104104
return crud.CreateResource(d, sync)
105105
}
106106

107107
func readVolumeBackup(d *schema.ResourceData, m interface{}) error {
108108
sync := &VolumeBackupResourceCrud{}
109109
sync.D = d
110-
sync.Client = m.(*OracleClients).blockStorageClient
110+
sync.Client = m.(*OracleClients).blockstorageClient
111111

112112
return crud.ReadResource(sync)
113113
}
114114

115115
func updateVolumeBackup(d *schema.ResourceData, m interface{}) error {
116116
sync := &VolumeBackupResourceCrud{}
117117
sync.D = d
118-
sync.Client = m.(*OracleClients).blockStorageClient
118+
sync.Client = m.(*OracleClients).blockstorageClient
119119

120120
return crud.UpdateResource(d, sync)
121121
}
122122

123123
func deleteVolumeBackup(d *schema.ResourceData, m interface{}) error {
124124
sync := &VolumeBackupResourceCrud{}
125125
sync.D = d
126-
sync.Client = m.(*OracleClients).blockStorageClient
126+
sync.Client = m.(*OracleClients).blockstorageClient
127127
sync.DisableNotFoundRetries = true
128128

129129
return crud.DeleteResource(d, sync)

provider/core_volume_backups_data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func VolumeBackupsDataSource() *schema.Resource {
5454
func readVolumeBackups(d *schema.ResourceData, m interface{}) error {
5555
sync := &VolumeBackupsDataSourceCrud{}
5656
sync.D = d
57-
sync.Client = m.(*OracleClients).blockStorageClient
57+
sync.Client = m.(*OracleClients).blockstorageClient
5858

5959
return crud.ReadResource(sync)
6060
}

0 commit comments

Comments
 (0)