Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds gcp metadata provider support to cloudmeta package #4155

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions pkg/cloudmeta/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,32 @@ func (gcp *gcpMetadata) Metadata(ctx context.Context) (InstanceMetadata, error)
}

func (gcp *gcpMetadata) getMachineType(ctx context.Context) (string, error) {
// The machine type for this VM. This value has the following format: projects/PROJECT_NUM/machineTypes/MACHINE_TYPE.
machineType, err := gcp.meta.GetWithContext(ctx, "instance/machine-type")
machineTypeResp, err := gcp.meta.GetWithContext(ctx, "instance/machine-type")
if err != nil {
return "", errors.Wrap(err, "gcp.meta.GetWithContext")
}

parts := strings.Split(machineType, "/")
if len(parts) < 2 {
return "", errors.Errorf("unexpected machine-type format: %s", machineType)
machineType, err := parseMachineTypeResponse(machineTypeResp)
if err != nil {
return "", err
}

return machineType, nil
}

// The machine type for this VM. This value has the following format: projects/PROJECT_NUM/machineTypes/MACHINE_TYPE.
// See https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys#instance-metadata.
func parseMachineTypeResponse(resp string) (string, error) {
errUnexpectedFormat := errors.Errorf("unexpected machineType response format: %s", resp)

parts := strings.SplitN(resp, "/", 4)
if len(parts) != 4 {
return "", errUnexpectedFormat
}
Michal-Leszczynski marked this conversation as resolved.
Show resolved Hide resolved

if parts[2] != "machineType" {
return "", errUnexpectedFormat
}
Michal-Leszczynski marked this conversation as resolved.
Show resolved Hide resolved

return parts[len(parts)-1], nil
return parts[3], nil
}
60 changes: 60 additions & 0 deletions pkg/cloudmeta/gcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 2024 ScyllaDB

package cloudmeta

import "testing"

func TestParseMachineTypeResponse(t *testing.T) {
testCases := []struct {
name string
machineTypeResponse string

expectedErr bool
expected string
}{
{
name: "everything is fine",
machineTypeResponse: "projects/project1/machineType/machineType1",

expectedErr: false,
expected: "machineType1",
},
{
name: "new response part is added",
machineTypeResponse: "projects/project1/zone/zone1/machineType/machineType1",

expectedErr: true,
expected: "",
},
{
name: "parts are mixed up",
machineTypeResponse: "machineType/machineType1/projects/project1",

expectedErr: true,
expected: "",
},
{
name: "empty response",
machineTypeResponse: "",

expectedErr: true,
expected: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
machineType, err := parseMachineTypeResponse(tc.machineTypeResponse)
if tc.expectedErr && err == nil {
t.Fatalf("expected err, but got %v", err)
}
if !tc.expectedErr && err != nil {
t.Fatalf("unexpected err: %v", err)
}

if tc.expected != machineType {
t.Fatalf("machineType(%s) != expected(%s)", machineType, tc.expected)
}
})
}
}
Loading