@@ -16,6 +16,7 @@ package client
1616
1717import (
1818 "context"
19+ "log"
1920 "strings"
2021
2122 "github.com/oracle/oci-go-sdk/v65/core"
@@ -33,6 +34,8 @@ type ComputeInterface interface {
3334
3435 GetPrimaryVNICForInstance (ctx context.Context , compartmentID , instanceID string ) (* core.Vnic , error )
3536
37+ GetSecondaryVNICForInstance (ctx context.Context , compartmentID , instanceID string ) (* core.Vnic , error )
38+
3639 VolumeAttachmentInterface
3740}
3841
@@ -151,6 +154,55 @@ func (c *client) GetPrimaryVNICForInstance(ctx context.Context, compartmentID, i
151154 return nil , errors .WithStack (errNotFound )
152155}
153156
157+ func (c * client ) GetSecondaryVNICForInstance (ctx context.Context , compartmentID , instanceID string ) (* core.Vnic , error ) {
158+ logger := c .logger .With ("instanceID" , instanceID , "compartmentID" , compartmentID )
159+
160+ var page * string
161+ for {
162+ resp , err := c .listVNICAttachments (ctx , core.ListVnicAttachmentsRequest {
163+ InstanceId : & instanceID ,
164+ CompartmentId : & compartmentID ,
165+ Page : page ,
166+ RequestMetadata : c .requestMetadata ,
167+ })
168+
169+ if err != nil {
170+ return nil , err
171+ }
172+
173+ for _ , attachment := range resp .Items {
174+ if attachment .LifecycleState != core .VnicAttachmentLifecycleStateAttached {
175+ logger .With ("vnicAttachmentID" , * attachment .Id ).Info ("VNIC attachment is not in attached state" )
176+ log .Println (logger )
177+ continue
178+ }
179+
180+ if attachment .VnicId == nil {
181+ // Should never happen but lets be extra cautious as field is non-mandatory in OCI API.
182+ logger .With ("vnicAttachmentID" , * attachment .Id ).Error ("VNIC attachment is attached but has no VNIC ID" )
183+ log .Println (logger )
184+ continue
185+ }
186+
187+ // TODO(apryde): Cache map[instanceID]primaryVNICID.
188+ vnic , err := c .GetVNIC (ctx , * attachment .VnicId )
189+ if err != nil {
190+ return nil , err
191+ }
192+
193+ if ! * vnic .IsPrimary {
194+ return vnic , nil
195+ }
196+ }
197+
198+ if page = resp .OpcNextPage ; resp .OpcNextPage == nil {
199+ break
200+ }
201+ }
202+
203+ return nil , errors .WithStack (errNotFound )
204+ }
205+
154206func (c * client ) GetInstanceByNodeName (ctx context.Context , compartmentID , vcnID , nodeName string ) (* core.Instance , error ) {
155207 // First try lookup by display name.
156208 instance , err := c .getInstanceByDisplayName (ctx , compartmentID , nodeName )
0 commit comments