Skip to content

Commit 90eb4f1

Browse files
committed
Implement InstanceExists
1 parent 95065b5 commit 90eb4f1

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

cloud/linode/instances.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,30 @@ func (i *instances) InstanceShutdownByProviderID(ctx context.Context, providerID
210210
return false, nil
211211
}
212212

213-
// TODO(PR): move code from instancesv1 over here
214213
func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
215-
return false, nil // TODO(PR): fix
214+
providerID := node.Spec.ProviderID
215+
216+
ctx = sentry.SetHubOnContext(ctx)
217+
sentry.SetTag(ctx, "provider_id", providerID)
218+
219+
id, err := parseProviderID(providerID)
220+
if err != nil {
221+
sentry.CaptureError(ctx, err)
222+
return false, err
223+
}
224+
225+
sentry.SetTag(ctx, "linode_id", strconv.Itoa(id))
226+
227+
_, err = linodeByID(ctx, i.client, id)
228+
if err != nil {
229+
if apiError, ok := err.(*linodego.Error); ok && apiError.Code == http.StatusNotFound {
230+
return false, nil
231+
}
232+
sentry.CaptureError(ctx, err)
233+
return false, err
234+
}
235+
236+
return true, nil
216237
}
217238

218239
func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {

cloud/linode/instances_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,42 @@ func TestInstanceExistsByProviderID(t *testing.T) {
2424
instances := newInstances(client)
2525

2626
t.Run("should propagate generic api error", func(t *testing.T) {
27-
providerID := providerIDPrefix + "123"
27+
node := &v1.Node{Spec: v1.NodeSpec{
28+
ProviderID: providerIDPrefix + "123",
29+
}}
2830
expectedErr := errors.New("some error")
2931
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(nil, expectedErr)
3032

31-
exists, err := instances.InstanceExistsByProviderID(ctx, providerID)
33+
exists, err := instances.InstanceExists(ctx, node)
3234
assert.ErrorIs(t, err, expectedErr)
3335
assert.False(t, exists)
3436
})
3537

3638
t.Run("should return false if linode does not exist", func(t *testing.T) {
37-
providerID := providerIDPrefix + "123"
39+
node := &v1.Node{Spec: v1.NodeSpec{
40+
ProviderID: providerIDPrefix + "123",
41+
}}
3842
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(nil, &linodego.Error{
3943
Code: http.StatusNotFound,
4044
})
4145

42-
exists, err := instances.InstanceExistsByProviderID(ctx, providerID)
46+
exists, err := instances.InstanceExists(ctx, node)
4347
assert.NoError(t, err)
4448
assert.False(t, exists)
4549
})
4650

4751
t.Run("should return true if linode exists", func(t *testing.T) {
48-
providerID := providerIDPrefix + "123"
52+
node := &v1.Node{Spec: v1.NodeSpec{
53+
ProviderID: providerIDPrefix + "123",
54+
}}
4955
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(&linodego.Instance{
5056
ID: 123,
5157
Label: "mock",
5258
Region: "us-east",
5359
Type: "g6-standard-2",
5460
}, nil)
5561

56-
exists, err := instances.InstanceExistsByProviderID(ctx, providerID)
62+
exists, err := instances.InstanceExists(ctx, node)
5763
assert.NoError(t, err)
5864
assert.True(t, exists)
5965
})

0 commit comments

Comments
 (0)