Skip to content

Commit

Permalink
refactor: returns err when CloudMeta is not initialzied with providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
VAveryanov8 committed Dec 12, 2024
1 parent 9c78eee commit 57f239d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/cloudmeta/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ func NewCloudMeta() (*CloudMeta, error) {
}, nil
}

// ErrNoProviders will be returned by CloudMeta service, when it hasn't been initialized with any metadata provider.
var ErrNoProviders = errors.New("no metadata providers found")

// GetInstanceMetadata tries to fetch instance metadata from AWS, GCP, Azure providers in order.
func (cloud *CloudMeta) GetInstanceMetadata(ctx context.Context) (InstanceMetadata, error) {
if len(cloud.providers) == 0 {
return InstanceMetadata{}, ErrNoProviders
}
var mErr error
for _, provider := range cloud.providers {
meta, err := cloud.runWithTimeout(ctx, provider)
Expand Down
5 changes: 3 additions & 2 deletions pkg/cloudmeta/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cloudmeta

import (
"context"
"errors"
"fmt"
"testing"
)
Expand All @@ -13,8 +14,8 @@ func TestGetInstanceMetadata(t *testing.T) {
cloudmeta := &CloudMeta{}

meta, err := cloudmeta.GetInstanceMetadata(context.Background())
if err != nil {
t.Fatalf("unexpected err: %v", err)
if !errors.Is(err, ErrNoProviders) {
t.Fatalf("expected err, got: %v", err)
}
if meta.InstanceType != "" {
t.Fatalf("meta.InstanceType should be empty, got %v", meta.InstanceType)
Expand Down

0 comments on commit 57f239d

Please sign in to comment.