Skip to content

Commit 2ea50e9

Browse files
shomroncodycushing
authored andcommitted
Expose Instance IP addresses as computed fields
1 parent c0cb9d7 commit 2ea50e9

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

core/instance_resource.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ func InstanceResource() *schema.Resource {
8181
Type: schema.TypeString,
8282
Computed: true,
8383
},
84+
"public_ip": {
85+
Type: schema.TypeString,
86+
Required: false,
87+
Computed: true,
88+
},
89+
"private_ip": {
90+
Type: schema.TypeString,
91+
Required: false,
92+
Computed: true,
93+
},
8494
},
8595
}
8696
}

core/instance_resource_crud.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
package core
44

55
import (
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

1115
type InstanceResourceCrud struct {
1216
crud.BaseCrud
1317
Resource *baremetal.Instance
18+
19+
// Computed fields
20+
public_ip string
21+
private_ip string
1422
}
1523

1624
func (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+
77133
func (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

104182
func (s *InstanceResourceCrud) Delete() (e error) {

core_instance_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ func (s *ResourceCoreInstanceTestSuite) SetupTest() {
9696
"subnetid",
9797
opts).Return(s.Res, nil)
9898
s.Client.On("TerminateInstance", s.Res.ID, (*baremetal.IfMatchOptions)(nil)).Return(nil)
99+
100+
listVnicOpts := &baremetal.ListVnicAttachmentsOptions{}
101+
listVnicOpts.AvailabilityDomain = s.Res.AvailabilityDomain
102+
listVnicOpts.InstanceID = s.Res.ID
103+
104+
listVnicOpts2 := &baremetal.ListVnicAttachmentsOptions{}
105+
listVnicOpts2.AvailabilityDomain = "new_availability_domain"
106+
listVnicOpts2.InstanceID = "new_id"
107+
108+
vnic := &baremetal.Vnic{}
109+
vnic.PublicIPAddress = "0.0.0.0"
110+
vnic.PrivateIPAddress = "0.0.0.0"
111+
vnicAttachment := &baremetal.VnicAttachment{
112+
ID: "id1",
113+
AvailabilityDomain: "availabilityid",
114+
CompartmentID: "compartmentid",
115+
DisplayName: "att1",
116+
InstanceID: "instanceid",
117+
State: baremetal.ResourceAttached,
118+
SubnetID: "subnetid",
119+
VnicID: "vnicid",
120+
TimeCreated: time.Now(),
121+
}
122+
vnicList := &baremetal.ListVnicAttachments{
123+
Attachments: []baremetal.VnicAttachment{
124+
*vnicAttachment,
125+
},
126+
}
127+
s.Client.On("ListVnicAttachments", s.Res.CompartmentID, listVnicOpts).Return(vnicList, nil)
128+
s.Client.On("ListVnicAttachments", s.Res.CompartmentID, listVnicOpts2).Return(vnicList, nil)
129+
s.Client.On("GetVnic", "vnicid").Return(vnic, nil)
99130
}
100131

101132
func (s *ResourceCoreInstanceTestSuite) TestCreateResourceCoreInstance() {
@@ -117,6 +148,8 @@ func (s *ResourceCoreInstanceTestSuite) TestCreateResourceCoreInstance() {
117148
resource.TestCheckResourceAttr(s.ResourceName, "image", s.Res.ImageID),
118149
resource.TestCheckResourceAttr(s.ResourceName, "state", s.Res.State),
119150
resource.TestCheckResourceAttr(s.ResourceName, "time_created", s.Res.TimeCreated.String()),
151+
resource.TestCheckResourceAttr(s.ResourceName, "public_ip", "0.0.0.0"),
152+
resource.TestCheckResourceAttr(s.ResourceName, "private_ip", "0.0.0.0"),
120153
),
121154
},
122155
},

0 commit comments

Comments
 (0)