Skip to content

Commit 7bd5a8c

Browse files
committed
Adds OCI support for AllocatePublicIP constraint.
1 parent 13f403b commit 7bd5a8c

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

provider/oci/environ.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ func (e *Environ) ConstraintsValidator(ctx envcontext.ProviderCallContext) (cons
332332
constraints.Container,
333333
constraints.VirtType,
334334
constraints.Tags,
335-
constraints.AllocatePublicIP,
336335
}
337336

338337
validator := constraints.NewValidator()
@@ -606,7 +605,11 @@ func (e *Environ) StartInstance(ctx envcontext.ProviderCallContext, args environ
606605
rootDiskSizeGB = MinVolumeSizeMB / 1024
607606
}
608607

609-
assignPublicIp := true
608+
allocatePublicIP := true
609+
if args.Constraints.HasAllocatePublicIP() {
610+
allocatePublicIP = *args.Constraints.AllocatePublicIP
611+
}
612+
610613
bootSource := ociCore.InstanceSourceViaImageDetails{
611614
ImageId: &image,
612615
BootVolumeSizeInGBs: &rootDiskSizeGB,
@@ -618,7 +621,7 @@ func (e *Environ) StartInstance(ctx envcontext.ProviderCallContext, args environ
618621
Shape: &spec.InstanceType.Name,
619622
CreateVnicDetails: &ociCore.CreateVnicDetails{
620623
SubnetId: network.Id,
621-
AssignPublicIp: &assignPublicIp,
624+
AssignPublicIp: &allocatePublicIP,
622625
DisplayName: &hostname,
623626
},
624627
DisplayName: &hostname,
@@ -654,9 +657,11 @@ func (e *Environ) StartInstance(ctx envcontext.ProviderCallContext, args environ
654657
displayName := shortenMachineId(machineId, 6)
655658

656659
if desiredStatus == ociCore.InstanceLifecycleStateRunning {
657-
if err := instance.waitForPublicIP(ctx); err != nil {
658-
providerCommon.HandleCredentialError(err, ctx)
659-
return nil, errors.Trace(err)
660+
if allocatePublicIP {
661+
if err := instance.waitForPublicIP(ctx); err != nil {
662+
providerCommon.HandleCredentialError(err, ctx)
663+
return nil, errors.Trace(err)
664+
}
660665
}
661666
}
662667

provider/oci/environ_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,9 @@ func (e *environSuite) TestAllRunningInstancesExtraUnrelatedInstance(c *gc.C) {
835835
c.Check(len(ids), gc.Equals, 2)
836836
}
837837

838-
func (e *environSuite) setupLaunchInstanceExpectations(isController bool, tags map[string]string) {
838+
func (e *environSuite) setupLaunchInstanceExpectations(
839+
isController bool, tags map[string]string, publicIP bool, launchInstanceMatcher gomock.Matcher,
840+
) {
839841
inst := ociCore.Instance{
840842
AvailabilityDomain: makeStringPointer("fakeZone1"),
841843
CompartmentId: &e.testCompartment,
@@ -849,7 +851,7 @@ func (e *environSuite) setupLaunchInstanceExpectations(isController bool, tags m
849851
responseLaunch := ociCore.LaunchInstanceResponse{
850852
Instance: inst,
851853
}
852-
e.compute.EXPECT().LaunchInstance(context.Background(), gomock.Any()).Return(responseLaunch, nil)
854+
e.compute.EXPECT().LaunchInstance(context.Background(), launchInstanceMatcher).Return(responseLaunch, nil)
853855

854856
getInst := inst
855857
if isController {
@@ -890,8 +892,12 @@ func (e *environSuite) setupLaunchInstanceExpectations(isController bool, tags m
890892
},
891893
})
892894

893-
e.compute.EXPECT().ListVnicAttachments(context.Background(), attachRequest).Return(attachResponse, nil)
894-
e.netw.EXPECT().GetVnic(context.Background(), vnicRequest[0]).Return(vnicResponse[0], nil)
895+
// These calls are only expected if we assign a public IP.
896+
// They occur when polling for the IP after the instance is started.
897+
if publicIP {
898+
e.compute.EXPECT().ListVnicAttachments(context.Background(), attachRequest).Return(attachResponse, nil)
899+
e.netw.EXPECT().GetVnic(context.Background(), vnicRequest[0]).Return(vnicResponse[0], nil)
900+
}
895901
}
896902
}
897903

@@ -904,7 +910,8 @@ func (e *environSuite) setupEnsureNetworksExpectations(vcnId string, machineTags
904910
e.setupListSubnetsExpectations(vcnId, "fakeRouteTableId", machineTags, 1)
905911
}
906912

907-
func (e *environSuite) setupStartInstanceExpectations(isController bool) {
913+
func (e *environSuite) setupStartInstanceExpectations(
914+
isController bool, publicIP bool, launchInstanceMatcher gomock.Matcher) {
908915
vcnId := "fakeVCNId"
909916
machineTags := map[string]string{
910917
tags.JujuController: testing.ControllerTag.Id(),
@@ -917,14 +924,41 @@ func (e *environSuite) setupStartInstanceExpectations(isController bool) {
917924

918925
e.setupEnsureNetworksExpectations(vcnId, machineTags)
919926
e.setupListImagesExpectations()
920-
e.setupLaunchInstanceExpectations(isController, machineTags)
927+
e.setupLaunchInstanceExpectations(isController, machineTags, publicIP, launchInstanceMatcher)
921928
}
922929

923930
func (e *environSuite) TestBootstrap(c *gc.C) {
924931
ctrl := e.patchEnv(c)
925932
defer ctrl.Finish()
926933

927-
e.setupStartInstanceExpectations(true)
934+
e.setupStartInstanceExpectations(true, true, gomock.Any())
935+
936+
ctx := envtesting.BootstrapContext(c)
937+
_, err := e.env.Bootstrap(ctx, nil,
938+
environs.BootstrapParams{
939+
ControllerConfig: testing.FakeControllerConfig(),
940+
AvailableTools: makeToolsList("trusty"),
941+
BootstrapSeries: "trusty",
942+
SupportedBootstrapSeries: testing.FakeSupportedJujuSeries,
943+
})
944+
c.Assert(err, gc.IsNil)
945+
}
946+
947+
type noPublicIPMatcher struct{}
948+
949+
func (noPublicIPMatcher) Matches(arg interface{}) bool {
950+
li := arg.(ociCore.LaunchInstanceRequest)
951+
assign := *li.CreateVnicDetails.AssignPublicIp
952+
return !assign
953+
}
954+
955+
func (noPublicIPMatcher) String() string { return "" }
956+
957+
func (e *environSuite) TestBootstrapNoAllocatePublicIP(c *gc.C) {
958+
ctrl := e.patchEnv(c)
959+
defer ctrl.Finish()
960+
961+
e.setupStartInstanceExpectations(true, false, noPublicIPMatcher{})
928962

929963
ctx := envtesting.BootstrapContext(c)
930964
_, err := e.env.Bootstrap(ctx, nil,
@@ -933,6 +967,7 @@ func (e *environSuite) TestBootstrap(c *gc.C) {
933967
AvailableTools: makeToolsList("trusty"),
934968
BootstrapSeries: "trusty",
935969
SupportedBootstrapSeries: testing.FakeSupportedJujuSeries,
970+
BootstrapConstraints: constraints.MustParse("allocate-public-ip=false"),
936971
})
937972
c.Assert(err, gc.IsNil)
938973
}

0 commit comments

Comments
 (0)