@@ -12,14 +12,14 @@ import (
12
12
13
13
"github.com/juju/errors"
14
14
"github.com/juju/juju/core/status"
15
+ ociCore "github.com/oracle/oci-go-sdk/core"
15
16
16
17
envcontext "github.com/juju/juju/environs/context"
17
18
"github.com/juju/juju/environs/instances"
18
19
"github.com/juju/juju/instance"
19
20
"github.com/juju/juju/network"
20
- "github.com/juju/juju/provider/oci/common"
21
-
22
- ociCore "github.com/oracle/oci-go-sdk/core"
21
+ "github.com/juju/juju/provider/common"
22
+ ocicommon "github.com/juju/juju/provider/oci/common"
23
23
)
24
24
25
25
const (
@@ -46,9 +46,9 @@ type vnicWithIndex struct {
46
46
47
47
var _ instance.Instance = (* ociInstance )(nil )
48
48
var maxPollIterations = 30
49
- var pollTime time. Duration = 10 * time .Second
49
+ var pollTime = 10 * time .Second
50
50
51
- var statusMap map [ociCore. InstanceLifecycleStateEnum ]status. Status = map [ociCore.InstanceLifecycleStateEnum ]status.Status {
51
+ var statusMap = map [ociCore.InstanceLifecycleStateEnum ]status.Status {
52
52
ociCore .InstanceLifecycleStateProvisioning : status .Provisioning ,
53
53
ociCore .InstanceLifecycleStateRunning : status .Running ,
54
54
ociCore .InstanceLifecycleStateStarting : status .Provisioning ,
@@ -66,13 +66,12 @@ func newInstance(raw ociCore.Instance, env *Environ) (*ociInstance, error) {
66
66
"Instance response does not contain an ID" ,
67
67
)
68
68
}
69
- instance := & ociInstance {
69
+
70
+ return & ociInstance {
70
71
raw : raw ,
71
72
env : env ,
72
73
arch : "amd64" ,
73
- }
74
-
75
- return instance , nil
74
+ }, nil
76
75
}
77
76
78
77
// SetInstance sets the raw property of ociInstance{}
@@ -93,7 +92,7 @@ func (o *ociInstance) Id() instance.Id {
93
92
// Status implements instance.Instance
94
93
func (o * ociInstance ) Status (ctx envcontext.ProviderCallContext ) instance.InstanceStatus {
95
94
if err := o .refresh (); err != nil {
96
- common .HandleCredentialError (err , ctx )
95
+ ocicommon .HandleCredentialError (err , ctx )
97
96
return instance.InstanceStatus {}
98
97
}
99
98
state , ok := statusMap [o .raw .LifecycleState ]
@@ -135,8 +134,8 @@ func (o *ociInstance) getAddresses() ([]network.Address, error) {
135
134
if err != nil {
136
135
return nil , errors .Trace (err )
137
136
}
138
- addresses := []network.Address {}
139
137
138
+ var addresses []network.Address
140
139
for _ , val := range vnics {
141
140
if val .Vnic .PrivateIp != nil {
142
141
privateAddress := network.Address {
@@ -161,7 +160,7 @@ func (o *ociInstance) getAddresses() ([]network.Address, error) {
161
160
// Addresses implements instance.Instance
162
161
func (o * ociInstance ) Addresses (ctx envcontext.ProviderCallContext ) ([]network.Address , error ) {
163
162
addresses , err := o .getAddresses ()
164
- common .HandleCredentialError (err , ctx )
163
+ ocicommon .HandleCredentialError (err , ctx )
165
164
return addresses , err
166
165
}
167
166
@@ -180,7 +179,7 @@ func (o *ociInstance) waitForPublicIP(ctx envcontext.ProviderCallContext) error
180
179
for {
181
180
addresses , err := o .Addresses (ctx )
182
181
if err != nil {
183
- common .HandleCredentialError (err , ctx )
182
+ ocicommon .HandleCredentialError (err , ctx )
184
183
return errors .Trace (err )
185
184
}
186
185
if iteration >= maxPollIterations {
@@ -217,7 +216,7 @@ func (o *ociInstance) deleteInstance(ctx envcontext.ProviderCallContext) error {
217
216
}
218
217
response , err := o .env .Compute .TerminateInstance (context .Background (), request )
219
218
if err != nil && ! o .env .isNotFound (response .RawResponse ) {
220
- common .HandleCredentialError (err , ctx )
219
+ ocicommon .HandleCredentialError (err , ctx )
221
220
return err
222
221
}
223
222
iteration := 0
@@ -226,7 +225,7 @@ func (o *ociInstance) deleteInstance(ctx envcontext.ProviderCallContext) error {
226
225
if errors .IsNotFound (err ) {
227
226
break
228
227
}
229
- common .HandleCredentialError (err , ctx )
228
+ ocicommon .HandleCredentialError (err , ctx )
230
229
return err
231
230
}
232
231
logger .Infof ("Waiting for machine to transition to Terminating: %s" , o .raw .LifecycleState )
@@ -304,3 +303,65 @@ func (o *ociInstance) refresh() error {
304
303
o .raw = response .Instance
305
304
return nil
306
305
}
306
+
307
+ // OpenPorts (InstanceFirewaller) ensures that the input ingress rule is
308
+ // permitted for machine with the input ID.
309
+ func (o * ociInstance ) OpenPorts (
310
+ ctx envcontext.ProviderCallContext , _ string , rules []network.IngressRule ,
311
+ ) error {
312
+ client , err := o .getInstanceConfigurator (ctx )
313
+ if err != nil {
314
+ return errors .Trace (err )
315
+ }
316
+ return errors .Trace (client .ChangeIngressRules ("" , true , rules ))
317
+ }
318
+
319
+ // OpenPorts (InstanceFirewaller) ensures that the input ingress rule is
320
+ // restricted for machine with the input ID.
321
+ func (o * ociInstance ) ClosePorts (
322
+ ctx envcontext.ProviderCallContext , _ string , rules []network.IngressRule ,
323
+ ) error {
324
+ client , err := o .getInstanceConfigurator (ctx )
325
+ if err != nil {
326
+ return errors .Trace (err )
327
+ }
328
+ return errors .Trace (client .ChangeIngressRules ("" , false , rules ))
329
+ }
330
+
331
+ // IngressRules (InstanceFirewaller) returns the ingress rules that have been
332
+ // applied to the input machine ID.
333
+ func (o * ociInstance ) IngressRules (
334
+ ctx envcontext.ProviderCallContext , _ string ,
335
+ ) ([]network.IngressRule , error ) {
336
+ client , err := o .getInstanceConfigurator (ctx )
337
+ if err != nil {
338
+ return nil , errors .Trace (err )
339
+ }
340
+
341
+ rules , err := client .FindIngressRules ()
342
+ return rules , errors .Trace (err )
343
+ }
344
+
345
+ func (o * ociInstance ) getInstanceConfigurator (
346
+ ctx envcontext.ProviderCallContext ,
347
+ ) (common.InstanceConfigurator , error ) {
348
+ addresses , err := o .Addresses (ctx )
349
+ if err != nil {
350
+ return nil , errors .Trace (err )
351
+ }
352
+ if len (addresses ) == 0 {
353
+ return nil , errors .NotFoundf ("addresses for instance %q" , o .Id ())
354
+ }
355
+
356
+ // Try to find a public address.
357
+ // Different models use different VCNs (and therefore subnets),
358
+ // so the cloud-local IPs are no good if a controller is trying to
359
+ // configure an instance in another model.
360
+ for _ , addr := range addresses {
361
+ if addr .Scope == network .ScopePublic {
362
+ return common .NewSshInstanceConfigurator (addr .Value ), nil
363
+ }
364
+ }
365
+
366
+ return nil , errors .NotFoundf ("public address for instance %q" , o .Id ())
367
+ }
0 commit comments