@@ -4,6 +4,7 @@ package provider
44
55import (
66 "context"
7+ "log"
78
89 "github.com/hashicorp/terraform/helper/schema"
910 oci_core "github.com/oracle/oci-go-sdk/core"
@@ -132,71 +133,79 @@ func (s *VolumeAttachmentsDataSourceCrud) SetData() {
132133 resources := []map [string ]interface {}{}
133134
134135 for _ , r := range s .Res .Items {
135- iscsiVolumeAttachment , castOk := r .(oci_core.IScsiVolumeAttachment )
136- if ! castOk {
137- panic ("unexpected VolumeAttachment type on item." )
138- }
136+ resources = append (resources , volumeAttachmentToMap (r ))
137+ }
139138
140- volumeAttachment := map [string ]interface {}{
141- "compartment_id" : * r .GetCompartmentId (),
142- "attachment_type" : IScsiVolumeAttachmentDiscriminator ,
143- }
139+ if f , fOk := s .D .GetOkExists ("filter" ); fOk {
140+ resources = ApplyFilters (f .(* schema.Set ), resources )
141+ }
144142
145- if iscsiVolumeAttachment . AvailabilityDomain != nil {
146- volumeAttachment [ "availability_domain" ] = * iscsiVolumeAttachment . AvailabilityDomain
147- }
143+ if err := s . D . Set ( "volume_attachments" , resources ); err != nil {
144+ panic ( err )
145+ }
148146
149- if iscsiVolumeAttachment .DisplayName != nil {
150- volumeAttachment ["display_name" ] = * iscsiVolumeAttachment .DisplayName
151- }
147+ return
148+ }
152149
153- if iscsiVolumeAttachment .Id != nil {
154- volumeAttachment ["id" ] = * iscsiVolumeAttachment .Id
155- }
150+ func volumeAttachmentToMap (r oci_core.VolumeAttachment ) map [string ]interface {} {
151+ volumeAttachment := map [string ]interface {}{
152+ "compartment_id" : * r .GetCompartmentId (),
153+ }
156154
157- if iscsiVolumeAttachment . InstanceId != nil {
158- volumeAttachment ["instance_id " ] = * iscsiVolumeAttachment . InstanceId
159- }
155+ if availabilityDomain := r . GetAvailabilityDomain (); availabilityDomain != nil {
156+ volumeAttachment ["availability_domain " ] = * availabilityDomain
157+ }
160158
161- volumeAttachment ["state" ] = iscsiVolumeAttachment .LifecycleState
159+ if displayName := r .GetDisplayName (); displayName != nil {
160+ volumeAttachment ["display_name" ] = * displayName
161+ }
162162
163- volumeAttachment ["time_created" ] = iscsiVolumeAttachment .TimeCreated .String ()
163+ if id := r .GetId (); id != nil {
164+ volumeAttachment ["id" ] = * id
165+ }
164166
165- if iscsiVolumeAttachment .VolumeId != nil {
166- volumeAttachment ["volume_id" ] = * iscsiVolumeAttachment .VolumeId
167- }
167+ if instanceId := r .GetInstanceId (); instanceId != nil {
168+ volumeAttachment ["instance_id" ] = * instanceId
169+ }
170+
171+ volumeAttachment ["state" ] = string (r .GetLifecycleState ())
172+
173+ if timeCreated := r .GetTimeCreated (); timeCreated != nil {
174+ volumeAttachment ["time_created" ] = timeCreated .String ()
175+ }
176+
177+ if volumeId := r .GetVolumeId (); volumeId != nil {
178+ volumeAttachment ["volume_id" ] = * volumeId
179+ }
180+
181+ switch typedValue := r .(type ) {
182+ case oci_core.IScsiVolumeAttachment :
183+ volumeAttachment ["attachment_type" ] = IScsiVolumeAttachmentDiscriminator
168184
169185 // IScsiVolumeAttachment-specific fields:
170- if iscsiVolumeAttachment .ChapSecret != nil {
171- volumeAttachment ["chap_secret" ] = * iscsiVolumeAttachment .ChapSecret
186+ if typedValue .ChapSecret != nil {
187+ volumeAttachment ["chap_secret" ] = * typedValue .ChapSecret
172188 }
173189
174- if iscsiVolumeAttachment .ChapUsername != nil {
175- volumeAttachment ["chap_username" ] = * iscsiVolumeAttachment .ChapUsername
190+ if typedValue .ChapUsername != nil {
191+ volumeAttachment ["chap_username" ] = * typedValue .ChapUsername
176192 }
177193
178- if iscsiVolumeAttachment .Ipv4 != nil {
179- volumeAttachment ["ipv4" ] = * iscsiVolumeAttachment .Ipv4
194+ if typedValue .Ipv4 != nil {
195+ volumeAttachment ["ipv4" ] = * typedValue .Ipv4
180196 }
181197
182- if iscsiVolumeAttachment .Iqn != nil {
183- volumeAttachment ["iqn" ] = * iscsiVolumeAttachment .Iqn
198+ if typedValue .Iqn != nil {
199+ volumeAttachment ["iqn" ] = * typedValue .Iqn
184200 }
185201
186- if iscsiVolumeAttachment .Port != nil {
187- volumeAttachment ["port" ] = * iscsiVolumeAttachment .Port
202+ if typedValue .Port != nil {
203+ volumeAttachment ["port" ] = * typedValue .Port
188204 }
189-
190- resources = append (resources , volumeAttachment )
205+ default :
206+ volumeAttachment ["attachment_type" ] = "Unknown"
207+ log .Printf ("[WARNING] Retrieved a volume attachment of unknown type." )
191208 }
192209
193- if f , fOk := s .D .GetOkExists ("filter" ); fOk {
194- resources = ApplyFilters (f .(* schema.Set ), resources )
195- }
196-
197- if err := s .D .Set ("volume_attachments" , resources ); err != nil {
198- panic (err )
199- }
200-
201- return
210+ return volumeAttachment
202211}
0 commit comments