Skip to content

Commit b000873

Browse files
committed
Merge remote-tracking branch 'origin/pr/114'
2 parents 92286fa + c3a3e5c commit b000873

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

firecracker.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ package firecracker
1515

1616
import (
1717
"context"
18-
"github.com/go-openapi/strfmt"
1918
"time"
2019

20+
"github.com/go-openapi/strfmt"
21+
2122
"github.com/sirupsen/logrus"
2223

2324
"github.com/firecracker-microvm/firecracker-go-sdk/client"
@@ -234,6 +235,39 @@ func (f *Client) PutMmds(ctx context.Context, metadata interface{}, opts ...PutM
234235
return f.client.Operations.PutMmds(params)
235236
}
236237

238+
// GetMmdsOpt is a functional option to be used for the GetMmds API in setting
239+
// any additional optional fields.
240+
type GetMmdsOpt func(*ops.GetMmdsParams)
241+
242+
// GetMmds is a wrapper for the swagger generated client to make calling of the
243+
// API easier.
244+
func (f *Client) GetMmds(ctx context.Context, opts ...GetMmdsOpt) (*ops.GetMmdsOK, error) {
245+
params := ops.NewGetMmdsParams()
246+
params.SetContext(ctx)
247+
for _, opt := range opts {
248+
opt(params)
249+
}
250+
251+
return f.client.Operations.GetMmds(params)
252+
}
253+
254+
// PatchMmdsOpt is a functional option to be used for the GetMmds API in setting
255+
// any additional optional fields.
256+
type PatchMmdsOpt func(*ops.PatchMmdsParams)
257+
258+
// PatchMmds is a wrapper for the swagger generated client to make calling of the
259+
// API easier.
260+
func (f *Client) PatchMmds(ctx context.Context, metadata interface{}, opts ...PatchMmdsOpt) (*ops.PatchMmdsNoContent, error) {
261+
params := ops.NewPatchMmdsParams()
262+
params.SetContext(ctx)
263+
params.SetBody(metadata)
264+
for _, opt := range opts {
265+
opt(params)
266+
}
267+
268+
return f.client.Operations.PatchMmds(params)
269+
}
270+
237271
// GetMachineConfigurationOpt is a functional option to be used for the
238272
// GetMachineConfiguration API in setting any additional optional fields.
239273
type GetMachineConfigurationOpt func(*ops.GetMachineConfigurationParams)

machine.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package firecracker
1515

1616
import (
1717
"context"
18+
"encoding/json"
1819
"errors"
1920
"fmt"
2021
"io"
@@ -660,6 +661,40 @@ func (m *Machine) SetMetadata(ctx context.Context, metadata interface{}) error {
660661
return nil
661662
}
662663

664+
// UpdateMetadata patches the machine's metadata for MDDS
665+
func (m *Machine) UpdateMetadata(ctx context.Context, metadata interface{}) error {
666+
if _, err := m.client.PatchMmds(ctx, metadata); err != nil {
667+
m.logger.Errorf("Updating metadata: %s", err)
668+
return err
669+
}
670+
671+
m.logger.Printf("UpdateMetadata successful")
672+
return nil
673+
}
674+
675+
// GetMetadata gets the machine's metadata from MDDS and unmarshals it into v
676+
func (m *Machine) GetMetadata(ctx context.Context, v interface{}) error {
677+
resp, err := m.client.GetMmds(ctx)
678+
if err != nil {
679+
m.logger.Errorf("Getting metadata: %s", err)
680+
return err
681+
}
682+
683+
payloadData, err := json.Marshal(resp.Payload)
684+
if err != nil {
685+
m.logger.Errorf("Getting metadata failed parsing payload: %s", err)
686+
return err
687+
}
688+
689+
if err := json.Unmarshal(payloadData, v); err != nil {
690+
m.logger.Errorf("Getting metadata failed parsing payload: %s", err)
691+
return err
692+
}
693+
694+
m.logger.Printf("GetMetadata successful")
695+
return nil
696+
}
697+
663698
// UpdateGuestDrive will modify the current guest drive of ID index with the new
664699
// parameters of the partialDrive.
665700
func (m *Machine) UpdateGuestDrive(ctx context.Context, driveID, pathOnHost string, opts ...PatchGuestDriveByIDOpt) error {

machine_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ func TestMicroVMExecution(t *testing.T) {
330330
t.Run("TestAttachSecondaryDrive", func(t *testing.T) { testAttachSecondaryDrive(ctx, t, m) })
331331
t.Run("TestAttachVsock", func(t *testing.T) { testAttachVsock(ctx, t, m) })
332332
t.Run("SetMetadata", func(t *testing.T) { testSetMetadata(ctx, t, m) })
333+
t.Run("UpdateMetadata", func(t *testing.T) { testUpdateMetadata(ctx, t, m) })
334+
t.Run("GetMetadata", func(t *testing.T) { testGetMetadata(ctx, t, m) }) // Should be after testSetMetadata and testUpdateMetadata
333335
t.Run("TestUpdateGuestDrive", func(t *testing.T) { testUpdateGuestDrive(ctx, t, m) })
334336
t.Run("TestUpdateGuestNetworkInterface", func(t *testing.T) { testUpdateGuestNetworkInterface(ctx, t, m) })
335337
t.Run("TestStartInstance", func(t *testing.T) { testStartInstance(ctx, t, m) })
@@ -667,6 +669,28 @@ func testSetMetadata(ctx context.Context, t *testing.T, m *Machine) {
667669
}
668670
}
669671

672+
func testUpdateMetadata(ctx context.Context, t *testing.T, m *Machine) {
673+
metadata := map[string]string{"patch_key": "patch_value"}
674+
err := m.UpdateMetadata(ctx, metadata)
675+
if err != nil {
676+
t.Errorf("failed to set metadata: %s", err)
677+
}
678+
}
679+
680+
func testGetMetadata(ctx context.Context, t *testing.T, m *Machine) {
681+
metadata := struct {
682+
Key string `json:"key"`
683+
PatchKey string `json:"patch_key"`
684+
}{}
685+
if err := m.GetMetadata(ctx, &metadata); err != nil {
686+
t.Errorf("failed to get metadata: %s", err)
687+
}
688+
689+
if metadata.Key != "value" || metadata.PatchKey != "patch_value" {
690+
t.Error("failed to get expected metadata values")
691+
}
692+
}
693+
670694
func TestLogFiles(t *testing.T) {
671695
cfg := Config{
672696
Debug: true,

0 commit comments

Comments
 (0)