Skip to content

Commit 91ae790

Browse files
jhorwit2prydie
authored andcommitted
Fixes #242 implements intances.InstanceShutdownByProviderID (#295)
1 parent 3c31d78 commit 91ae790

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

pkg/cloudprovider/providers/oci/instances.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,16 @@ func (cp *CloudProvider) InstanceExistsByProviderID(ctx context.Context, provide
180180

181181
// InstanceShutdownByProviderID returns true if the instance is shutdown in cloudprovider.
182182
func (cp *CloudProvider) InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error) {
183-
return false, cloudprovider.NotImplemented
183+
cp.logger.With("instanceID", providerID).Debug("Checking instance is stopped by provider id")
184+
instanceID, err := MapProviderIDToInstanceID(providerID)
185+
if err != nil {
186+
return false, err
187+
}
188+
189+
instance, err := cp.client.Compute().GetInstance(ctx, instanceID)
190+
if err != nil {
191+
return false, err
192+
}
193+
194+
return client.IsInstanceInStoppedState(instance), nil
184195
}

pkg/oci/client/client_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,41 @@ func TestInstanceTerminalState(t *testing.T) {
5757
}
5858
}
5959

60+
func TestInstanceStoppedState(t *testing.T) {
61+
testCases := map[string]struct {
62+
state core.InstanceLifecycleStateEnum
63+
expected bool
64+
}{
65+
"not stopped - running": {
66+
state: core.InstanceLifecycleStateRunning,
67+
expected: false,
68+
},
69+
"not stopped - terminated": {
70+
state: core.InstanceLifecycleStateTerminated,
71+
expected: false,
72+
},
73+
"is stopped - stopped": {
74+
state: core.InstanceLifecycleStateStopped,
75+
expected: true,
76+
},
77+
"is stopped - stopping": {
78+
state: core.InstanceLifecycleStateStopping,
79+
expected: true,
80+
},
81+
}
82+
83+
for name, tc := range testCases {
84+
t.Run(name, func(t *testing.T) {
85+
result := IsInstanceInStoppedState(&core.Instance{
86+
LifecycleState: tc.state,
87+
})
88+
if result != tc.expected {
89+
t.Errorf("IsInstanceInStoppedState(%q) = %v ; wanted %v", tc.state, result, tc.expected)
90+
}
91+
})
92+
}
93+
}
94+
6095
type mockComputeClient struct{}
6196

6297
type mockVirtualNetworkClient struct{}

pkg/oci/client/compute.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ func IsInstanceInTerminalState(instance *core.Instance) bool {
232232
instance.LifecycleState == core.InstanceLifecycleStateTerminating
233233
}
234234

235+
// IsInstanceInStoppedState returns true if the instance is in a stopped or stopping state, false otherwise.
236+
func IsInstanceInStoppedState(instance *core.Instance) bool {
237+
return instance.LifecycleState == core.InstanceLifecycleStateStopped ||
238+
instance.LifecycleState == core.InstanceLifecycleStateStopping
239+
}
240+
235241
func getNonTerminalInstances(instances []core.Instance) []core.Instance {
236242
var result []core.Instance
237243
for _, instance := range instances {

0 commit comments

Comments
 (0)