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

Move storage implementations to a new package #79

Merged
merged 4 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 7 additions & 6 deletions command/history_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/minamijoyo/tfmigrate/config"
"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestHistoryRunnerPlan(t *testing.T) {
Expand Down Expand Up @@ -207,15 +208,15 @@ migration "mock" "test4" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
migrationDir := setupMigrationDir(t, tc.migrations)
storage := &history.MockStorageConfig{
mockConfig := &mock.Config{
Data: tc.historyFile,
WriteError: false,
ReadError: false,
}
config := &config.TfmigrateConfig{
MigrationDir: migrationDir,
History: &history.Config{
Storage: storage,
Storage: mockConfig,
},
}
r, err := NewHistoryRunner(context.Background(), tc.filename, config, nil)
Expand All @@ -234,7 +235,7 @@ migration "mock" "test4" {
if err != nil {
t.Fatalf("failed to parse history file (want): %s", err)
}
data := storage.StorageData()
data := mockConfig.Storage().Data()
got, err := history.ParseHistoryFile([]byte(data))
if err != nil {
t.Fatalf("failed to parse history file (got): %s", err)
Expand Down Expand Up @@ -712,15 +713,15 @@ migration "mock" "test4" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
migrationDir := setupMigrationDir(t, tc.migrations)
storage := &history.MockStorageConfig{
mockConfig := &mock.Config{
Data: tc.historyFile,
WriteError: tc.writeError,
ReadError: tc.readError,
}
config := &config.TfmigrateConfig{
MigrationDir: migrationDir,
History: &history.Config{
Storage: storage,
Storage: mockConfig,
},
}
r, err := NewHistoryRunner(context.Background(), tc.filename, config, nil)
Expand All @@ -739,7 +740,7 @@ migration "mock" "test4" {
if err != nil {
t.Fatalf("failed to parse history file (want): %s", err)
}
data := storage.StorageData()
data := mockConfig.Storage().Data()
got, err := history.ParseHistoryFile([]byte(data))
if err != nil {
t.Fatalf("failed to parse history file (got): %s", err)
Expand Down
3 changes: 2 additions & 1 deletion command/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/minamijoyo/tfmigrate/config"
"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestListMigrations(t *testing.T) {
Expand Down Expand Up @@ -92,7 +93,7 @@ migration "mock" "test4" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
migrationDir := setupMigrationDir(t, tc.migrations)
storage := &history.MockStorageConfig{
storage := &mock.Config{
Data: tc.historyFile,
WriteError: false,
ReadError: false,
Expand Down
3 changes: 2 additions & 1 deletion config/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseHistoryBlock(t *testing.T) {
Expand All @@ -27,7 +28,7 @@ tfmigrate {
}
`,
want: &history.Config{
Storage: &history.LocalStorageConfig{
Storage: &local.Config{
Path: "tmp/history.json",
},
},
Expand Down
27 changes: 15 additions & 12 deletions config/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/local"
"github.com/minamijoyo/tfmigrate/storage/mock"
"github.com/minamijoyo/tfmigrate/storage/s3"
)

// StorageBlock represents a block for migration history data store in HCL.
Expand All @@ -22,8 +25,8 @@ type StorageBlock struct {
Remain hcl.Body `hcl:",remain"`
}

// parseStorageBlock parses a storage block and returns a history.StorageConfig.
func parseStorageBlock(b StorageBlock) (history.StorageConfig, error) {
// parseStorageBlock parses a storage block and returns a storage.Config.
func parseStorageBlock(b StorageBlock) (storage.Config, error) {
switch b.Type {
case "mock": // only for testing
return parseMockStorageBlock(b)
Expand All @@ -39,9 +42,9 @@ func parseStorageBlock(b StorageBlock) (history.StorageConfig, error) {
}
}

// parseMockStorageBlock parses a storage block for mock and returns a history.StorageConfig.
func parseMockStorageBlock(b StorageBlock) (history.StorageConfig, error) {
var config history.MockStorageConfig
// parseMockStorageBlock parses a storage block for mock and returns a storage.Config.
func parseMockStorageBlock(b StorageBlock) (storage.Config, error) {
var config mock.Config
diags := gohcl.DecodeBody(b.Remain, nil, &config)
if diags.HasErrors() {
return nil, diags
Expand All @@ -50,9 +53,9 @@ func parseMockStorageBlock(b StorageBlock) (history.StorageConfig, error) {
return &config, nil
}

// parseLocalStorageBlock parses a storage block for local and returns a history.StorageConfig.
func parseLocalStorageBlock(b StorageBlock) (history.StorageConfig, error) {
var config history.LocalStorageConfig
// parseLocalStorageBlock parses a storage block for local and returns a storage.Config.
func parseLocalStorageBlock(b StorageBlock) (storage.Config, error) {
var config local.Config
diags := gohcl.DecodeBody(b.Remain, nil, &config)
if diags.HasErrors() {
return nil, diags
Expand All @@ -61,9 +64,9 @@ func parseLocalStorageBlock(b StorageBlock) (history.StorageConfig, error) {
return &config, nil
}

// parseS3StorageBlock parses a storage block for s3 and returns a history.StorageConfig.
func parseS3StorageBlock(b StorageBlock) (history.StorageConfig, error) {
var config history.S3StorageConfig
// parseS3StorageBlock parses a storage block for s3 and returns a storage.Config.
func parseS3StorageBlock(b StorageBlock) (storage.Config, error) {
var config s3.Config
diags := gohcl.DecodeBody(b.Remain, nil, &config)
if diags.HasErrors() {
return nil, diags
Expand Down
7 changes: 4 additions & 3 deletions config/storage_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseLocalStorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -25,7 +26,7 @@ tfmigrate {
}
}
`,
want: &history.LocalStorageConfig{
want: &local.Config{
Path: "tmp/history.json",
},
ok: true,
Expand Down
7 changes: 4 additions & 3 deletions config/storage_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestParseMockStorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -27,7 +28,7 @@ tfmigrate {
}
}
`,
want: &history.MockStorageConfig{
want: &mock.Config{
Data: "foo",
WriteError: true,
ReadError: false,
Expand Down
9 changes: 5 additions & 4 deletions config/storage_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/s3"
)

func TestParseS3StorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -26,7 +27,7 @@ tfmigrate {
}
}
`,
want: &history.S3StorageConfig{
want: &s3.Config{
Bucket: "tfmigrate-test",
Key: "tfmigrate/history.json",
},
Expand All @@ -53,7 +54,7 @@ tfmigrate {
}
}
`,
want: &history.S3StorageConfig{
want: &s3.Config{
Bucket: "tfmigrate-test",
Key: "tfmigrate/history.json",
Region: "ap-northeast-1",
Expand Down
7 changes: 4 additions & 3 deletions config/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseStorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -25,7 +26,7 @@ tfmigrate {
}
}
`,
want: &history.LocalStorageConfig{
want: &local.Config{
Path: "tmp/history.json",
},
ok: true,
Expand Down
5 changes: 3 additions & 2 deletions config/tfmigrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseConfigurationFile(t *testing.T) {
Expand All @@ -29,7 +30,7 @@ tfmigrate {
want: &TfmigrateConfig{
MigrationDir: "tfmigrate",
History: &history.Config{
Storage: &history.LocalStorageConfig{
Storage: &local.Config{
Path: "tmp/history.json",
},
},
Expand All @@ -50,7 +51,7 @@ tfmigrate {
want: &TfmigrateConfig{
MigrationDir: ".",
History: &history.Config{
Storage: &history.LocalStorageConfig{
Storage: &local.Config{
Path: "tmp/history.json",
},
},
Expand Down
4 changes: 3 additions & 1 deletion history/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package history

import "github.com/minamijoyo/tfmigrate/storage"

// Config is a set of configurations for migration history management.
type Config struct {
// MigrationDir is a path to directory where migration files are stored.
MigrationDir string
// Storage is an interface of factory method for Storage
Storage StorageConfig
Storage storage.Config
}
4 changes: 3 additions & 1 deletion history/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sort"
"strings"
"time"

"github.com/minamijoyo/tfmigrate/storage"
)

// Controller manages a migration history.
Expand Down Expand Up @@ -81,7 +83,7 @@ func loadMigrationFileNames(dir string) ([]string, error) {

// loadHistory loads a history file from a storage.
// If a given history is not found, create a new one.
func loadHistory(ctx context.Context, c StorageConfig) (*History, error) {
func loadHistory(ctx context.Context, c storage.Config) (*History, error) {
s, err := c.NewStorage()
if err != nil {
return nil, err
Expand Down
Loading