From dae67e6be654592c1d87f2d9a553f16a0fca8537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Sun, 31 Oct 2021 12:01:24 +0100 Subject: [PATCH] Get snapshot handler It translates snapshot keys into metadata structure --- apps/snapshots.go | 14 ++++++++++++++ apps/snapshots_test.go | 6 ++++++ handlers_nats.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/apps/snapshots.go b/apps/snapshots.go index 5414654..412d028 100644 --- a/apps/snapshots.go +++ b/apps/snapshots.go @@ -249,6 +249,20 @@ func (s *SnapshotProcessor) ListAppsSnapshotsByLabel(desiredLabel string) ([]Sna return snapshots, nil } +// GetSnapshot returns a single snapshot's metadata for given key. +// In fact this is just key translation method. It parses the key and returns +// what's inside. Doesn't check if the snapshot actually exist. +func (s *SnapshotProcessor) GetSnapshot(key string) (Snapshot, error) { + snapshot := Snapshot{} + + snapshot, err := DecodeKeyName(key) + if err != nil { + return snapshot, err + } + + return snapshot, nil +} + // DeleteSnapshot delete's one snapshot func (s *SnapshotProcessor) DeleteSnapshot(key string) error { err := s.Driver.Delete(key) diff --git a/apps/snapshots_test.go b/apps/snapshots_test.go index ca5ac38..fd8ce6c 100644 --- a/apps/snapshots_test.go +++ b/apps/snapshots_test.go @@ -154,6 +154,12 @@ func TestCreateRestoreListSnapshot(t *testing.T) { } +func TestGetSnapshot(t *testing.T) { + snapshot, err := snapshotProcessor.GetSnapshot("app_0102:1634510035:eyJhcHBfbmFtZSI6ImFwcF8wMTAyIiwidHMiOjE2MzQ1MTAwMzUsImxhYmVscyI6WyJ1c2VyaWQ6MSJdfQ==") + assert.Nil(t, err) + assert.Equal(t, "app_0102", snapshot.AppName) +} + func TestListAppsSnapshotsByLabel(t *testing.T) { appName := "app_0102" diff --git a/handlers_nats.go b/handlers_nats.go index bf1187a..0b00b0c 100644 --- a/handlers_nats.go +++ b/handlers_nats.go @@ -64,6 +64,7 @@ func _messageHandler(m *nats.Msg) error { "list_snapshots": listSnapshotsEventHandler, "list_apps_snapshots": listAppsSnapshotsEventHandler, "list_snapshots_by_label": listSnapshotsByLabelEventHandler, + "get_snapshot": getSnapshotEventHandler, "delete_snapshot": deleteSnapshotEventHandler, "delete_app_snapshots": deleteAppSnapshotsEventHandler, } @@ -1049,6 +1050,48 @@ func listSnapshotsByLabelEventHandler(m *nats.Msg, message *RequestMessage) erro return nil } +/* +getSnapshotEventHandler returns a single snapshot for given key from the request payload. + +Payload: snapshot's key +Response: snapshot metadata +*/ +func getSnapshotEventHandler(m *nats.Msg, message *RequestMessage) error { + processor := apps.SnapshotProcessor{ + AppsPath: config.AppsPath, + TmpSnapshotPath: config.SnapshotsPath, + + Driver: drivers.S3Driver{ + S3AccessKey: config.SnapshotsS3AccessKey, + S3SecretKey: config.SnapshotsS3SecretKey, + S3Endpoint: config.SnapshotsS3Endpoint, + S3SSL: config.SnapshotsS3SSL, + Bucket: config.SnapshotsS3Bucket, + }, + } + + snapshot, err := processor.GetSnapshot(message.Payload) + if err != nil { + return errorReplyFormater(m, "backend error", err) + } + output := SnapshotMetadata{ + Key: snapshot.KeyName(), + Metadata: snapshot, + } + + data, err := json.Marshal(output) + if err != nil { + return errorReplyFormater(m, "reply formatter error", err) + } + + err = m.Respond(data) + if err != nil { + log.Println("ERROR: get snapshot:", err.Error()) + } + + return nil +} + /* deleteSnapshotEventHandler delete a single snapshot. This is not bound to application name.