33package core
44
55import (
6+ "log"
7+
68 "github.com/MustWin/baremetal-sdk-go"
79
10+ "github.com/oracle/terraform-provider-baremetal/options"
11+
812 "github.com/oracle/terraform-provider-baremetal/crud"
913)
1014
1115type InstanceResourceCrud struct {
1216 crud.BaseCrud
1317 Resource * baremetal.Instance
18+
19+ // Computed fields
20+ public_ip string
21+ private_ip string
1422}
1523
1624func (s * InstanceResourceCrud ) ID () string {
@@ -74,8 +82,75 @@ func (s *InstanceResourceCrud) Create() (e error) {
7482 return
7583}
7684
85+ /*
86+ * Return the id of the first VNIC attached to this Instance.
87+ *
88+ * NOTE while the instance is still being created, calls to this function
89+ * can return an error priort to the Vnic being attached.
90+ */
91+ func (s * InstanceResourceCrud ) getInstanceVnicId () (vnic_id string , e error ) {
92+ compartmentID := s .Resource .CompartmentID
93+
94+ opts := & baremetal.ListVnicAttachmentsOptions {}
95+ options .SetListOptions (s .D , & opts .ListOptions )
96+ opts .AvailabilityDomain = s .Resource .AvailabilityDomain
97+ opts .InstanceID = s .Resource .ID
98+
99+ var list * baremetal.ListVnicAttachments
100+ if list , e = s .Client .ListVnicAttachments (compartmentID , opts ); e != nil {
101+ return "" , e
102+ }
103+
104+ if len (list .Attachments ) < 1 {
105+ log .Printf ("[DEBUG] GetInstanceVnicID - InstanceID: %q, State: %q, no vnic attachments: %q" , s .Resource .ID , s .Resource .State , e )
106+ return "" , e
107+ }
108+
109+ return list .Attachments [0 ].VnicID , nil
110+ }
111+
112+ /*
113+ * Return the public, private IP pair associated with the instance's first Vnic.
114+ *
115+ * NOTE while the instance is still being created, calls to this function
116+ * can return an error priort to the Vnic being attached.
117+ */
118+ func (s * InstanceResourceCrud ) getInstanceIPs () (public_ip string , private_ip string , e error ) {
119+ vnicID , e := s .getInstanceVnicId ()
120+ if e != nil {
121+ return "" , "" , e
122+ }
123+
124+ // Lookup Vnic by id
125+ vnic , e := s .Client .GetVnic (vnicID )
126+ if e != nil {
127+ return "" , "" , e
128+ }
129+
130+ return vnic .PublicIPAddress , vnic .PrivateIPAddress , nil
131+ }
132+
77133func (s * InstanceResourceCrud ) Get () (e error ) {
78134 s .Resource , e = s .Client .GetInstance (s .D .Id ())
135+
136+ if e != nil {
137+ return e
138+ }
139+
140+ // Compute instance IPs through attached Vnic
141+ // (Not available while state==PROVISIONING)
142+ public_ip , private_ip , e2 := s .getInstanceIPs ()
143+ if e2 != nil {
144+ log .Printf ("[DEBUG] no vnic yet, skipping" )
145+ }
146+
147+ if public_ip != "" {
148+ s .public_ip = public_ip
149+ }
150+ if private_ip != "" {
151+ s .private_ip = private_ip
152+ }
153+
79154 return
80155}
81156
@@ -99,6 +174,9 @@ func (s *InstanceResourceCrud) SetData() {
99174 s .D .Set ("shape" , s .Resource .Shape )
100175 s .D .Set ("state" , s .Resource .State )
101176 s .D .Set ("time_created" , s .Resource .TimeCreated .String ())
177+
178+ s .D .Set ("public_ip" , s .public_ip )
179+ s .D .Set ("private_ip" , s .private_ip )
102180}
103181
104182func (s * InstanceResourceCrud ) Delete () (e error ) {
0 commit comments