forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_obmcs_core_volume_attachments.go
134 lines (115 loc) · 3.23 KB
/
data_source_obmcs_core_volume_attachments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package main
import (
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/oracle/bmcs-go-sdk"
"github.com/oracle/terraform-provider-oci/options"
"github.com/oracle/terraform-provider-oci/crud"
)
func VolumeAttachmentDatasource() *schema.Resource {
return &schema.Resource{
Read: readVolumeAttachments,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"availability_domain": {
Type: schema.TypeString,
Optional: true,
},
"compartment_id": {
Type: schema.TypeString,
Required: true,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
},
"page": {
Type: schema.TypeString,
Optional: true,
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
},
"volume_id": {
Type: schema.TypeString,
Optional: true,
},
"volume_attachments": {
Type: schema.TypeList,
Computed: true,
Elem: VolumeAttachmentResource(),
},
},
}
}
func readVolumeAttachments(d *schema.ResourceData, m interface{}) (e error) {
client := m.(*OracleClients)
sync := &VolumeAttachmentDatasourceCrud{}
sync.D = d
sync.Client = client.client
return crud.ReadResource(sync)
}
type VolumeAttachmentDatasourceCrud struct {
crud.BaseCrud
Res *baremetal.ListVolumeAttachments
}
func (s *VolumeAttachmentDatasourceCrud) Get() (e error) {
compartmentID := s.D.Get("compartment_id").(string)
opts := &baremetal.ListVolumeAttachmentsOptions{}
options.SetListOptions(s.D, &opts.ListOptions)
if val, ok := s.D.GetOk("availability_domain"); ok {
opts.AvailabilityDomain = val.(string)
}
if val, ok := s.D.GetOk("instance_id"); ok {
opts.InstanceID = val.(string)
}
if val, ok := s.D.GetOk("volume_id"); ok {
opts.VolumeID = val.(string)
}
s.Res = &baremetal.ListVolumeAttachments{
VolumeAttachments: []baremetal.VolumeAttachment{},
}
for {
var list *baremetal.ListVolumeAttachments
if list, e = s.Client.ListVolumeAttachments(compartmentID, opts); e != nil {
break
}
s.Res.VolumeAttachments = append(s.Res.VolumeAttachments, list.VolumeAttachments...)
if hasNextPage := options.SetNextPageOption(list.NextPage, &opts.ListOptions.PageListOptions); !hasNextPage {
break
}
}
return
}
func (s *VolumeAttachmentDatasourceCrud) SetData() {
if s.Res == nil {
return
}
// Important, if you don't have an ID, make one up for your datasource
// or things will end in tears
s.D.SetId(time.Now().UTC().String())
resources := []map[string]interface{}{}
for _, v := range s.Res.VolumeAttachments {
res := map[string]interface{}{
"attachment_type": v.AttachmentType,
"availability_domain": v.AvailabilityDomain,
"compartment_id": v.CompartmentID,
"display_name": v.DisplayName,
"id": v.ID,
"instance_id": v.InstanceID,
"state": v.State,
"time_created": v.TimeCreated.String(),
"volume_id": v.VolumeID,
}
resources = append(resources, res)
}
if f, fOk := s.D.GetOk("filter"); fOk {
resources = ApplyFilters(f.(*schema.Set), resources)
}
if err := s.D.Set("volume_attachments", resources); err != nil {
panic(err)
}
return
}