diff --git a/internal/elastic/core/snapshots.go b/internal/elastic/core/snapshots.go index 0d6afac79..71f4217c0 100644 --- a/internal/elastic/core/snapshots.go +++ b/internal/elastic/core/snapshots.go @@ -15,16 +15,19 @@ import ( ) // CreateAWSRepository - -func (e *Elastic) CreateAWSRepository(name, awsBucketName, awsRegion string) error { +func (e *Elastic) CreateAWSRepository(name, awsBucketName, awsRegion string, opts ...models.CreateRepositoryOption) error { query := map[string]interface{}{ "type": "s3", "settings": map[string]interface{}{ - "bucket": awsBucketName, - "endpoint": fmt.Sprintf("s3.%s.amazonaws.com", awsRegion), - "compress": "true", - "max_retries": 3, + "bucket": awsBucketName, + "endpoint": fmt.Sprintf("s3.%s.amazonaws.com", awsRegion), }, } + + for i := range opts { + opts[i](query) + } + var buf bytes.Buffer if err := json.NewEncoder(&buf).Encode(query); err != nil { return err diff --git a/internal/models/interface.go b/internal/models/interface.go index 80b4c6c2a..9059ccdeb 100644 --- a/internal/models/interface.go +++ b/internal/models/interface.go @@ -19,7 +19,7 @@ type GeneralRepository interface { UpdateFields(index, id string, data interface{}, fields ...string) error GetEvents([]SubscriptionRequest, int64, int64) ([]Event, error) SearchByText(string, int64, []string, map[string]interface{}, bool) (Result, error) - CreateAWSRepository(string, string, string) error + CreateAWSRepository(string, string, string, ...CreateRepositoryOption) error ListRepositories() ([]Repository, error) CreateSnapshots(string, string, []string) error RestoreSnapshots(string, string, []string) error diff --git a/internal/models/mock/general.go b/internal/models/mock/general.go index 08fc600a7..c0ae45690 100644 --- a/internal/models/mock/general.go +++ b/internal/models/mock/general.go @@ -180,10 +180,10 @@ func (mr *MockGeneralRepositoryMockRecorder) UpdateDoc(model interface{}) *gomoc } // UpdateFields mocks base method -func (m *MockGeneralRepository) UpdateFields(arg0, arg1 string, arg2 interface{}, arg3 ...string) error { +func (m *MockGeneralRepository) UpdateFields(index, id string, data interface{}, fields ...string) error { m.ctrl.T.Helper() - varargs := []interface{}{arg0, arg1, arg2} - for _, a := range arg3 { + varargs := []interface{}{index, id, data} + for _, a := range fields { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "UpdateFields", varargs...) @@ -192,9 +192,9 @@ func (m *MockGeneralRepository) UpdateFields(arg0, arg1 string, arg2 interface{} } // UpdateFields indicates an expected call of UpdateFields -func (mr *MockGeneralRepositoryMockRecorder) UpdateFields(arg0, arg1, arg2 interface{}, arg3 ...interface{}) *gomock.Call { +func (mr *MockGeneralRepositoryMockRecorder) UpdateFields(index, id, data interface{}, fields ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{arg0, arg1, arg2}, arg3...) + varargs := append([]interface{}{index, id, data}, fields...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateFields", reflect.TypeOf((*MockGeneralRepository)(nil).UpdateFields), varargs...) } @@ -229,17 +229,22 @@ func (mr *MockGeneralRepositoryMockRecorder) SearchByText(arg0, arg1, arg2, arg3 } // CreateAWSRepository mocks base method -func (m *MockGeneralRepository) CreateAWSRepository(arg0, arg1, arg2 string) error { +func (m *MockGeneralRepository) CreateAWSRepository(arg0, arg1, arg2 string, arg3 ...models.CreateRepositoryOption) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateAWSRepository", arg0, arg1, arg2) + varargs := []interface{}{arg0, arg1, arg2} + for _, a := range arg3 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "CreateAWSRepository", varargs...) ret0, _ := ret[0].(error) return ret0 } // CreateAWSRepository indicates an expected call of CreateAWSRepository -func (mr *MockGeneralRepositoryMockRecorder) CreateAWSRepository(arg0, arg1, arg2 interface{}) *gomock.Call { +func (mr *MockGeneralRepositoryMockRecorder) CreateAWSRepository(arg0, arg1, arg2 interface{}, arg3 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAWSRepository", reflect.TypeOf((*MockGeneralRepository)(nil).CreateAWSRepository), arg0, arg1, arg2) + varargs := append([]interface{}{arg0, arg1, arg2}, arg3...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAWSRepository", reflect.TypeOf((*MockGeneralRepository)(nil).CreateAWSRepository), varargs...) } // ListRepositories mocks base method diff --git a/internal/models/options.go b/internal/models/options.go new file mode 100644 index 000000000..8ebf78bc7 --- /dev/null +++ b/internal/models/options.go @@ -0,0 +1,25 @@ +package models + +// CreateRepositoryOption - +type CreateRepositoryOption func(map[string]interface{}) + +// WithReadOnly - +func WithReadOnly() CreateRepositoryOption { + return func(data map[string]interface{}) { + data["readonly"] = true + } +} + +// WithCompress - +func WithCompress() CreateRepositoryOption { + return func(data map[string]interface{}) { + data["compress"] = "true" + } +} + +// WithMaxRetries - +func WithMaxRetries(maxRetries int64) CreateRepositoryOption { + return func(data map[string]interface{}) { + data["max_retries"] = maxRetries + } +} diff --git a/internal/reindexer/core/snapshots.go b/internal/reindexer/core/snapshots.go index ae813dfbb..56e1e3f65 100644 --- a/internal/reindexer/core/snapshots.go +++ b/internal/reindexer/core/snapshots.go @@ -7,7 +7,7 @@ import ( ) // CreateAWSRepository - -func (r *Reindexer) CreateAWSRepository(name, awsBucketName, awsRegion string) error { +func (r *Reindexer) CreateAWSRepository(name, awsBucketName, awsRegion string, opts ...models.CreateRepositoryOption) error { return nil } diff --git a/scripts/esctl/repository.go b/scripts/esctl/repository.go index c77bc33a1..afbb5e71f 100644 --- a/scripts/esctl/repository.go +++ b/scripts/esctl/repository.go @@ -1,5 +1,7 @@ package main +import "github.com/baking-bad/bcdhub/internal/models" + type createRepoCommand struct{} var createRepoCmd createRepoCommand @@ -11,5 +13,18 @@ func (x *createRepoCommand) Execute(_ []string) error { return err } - return ctx.Storage.CreateAWSRepository(name, creds.BucketName, creds.Region) + opts := []models.CreateRepositoryOption{ + models.WithCompress(), + models.WithMaxRetries(3), + } + + readOnlyAnswer, err := askQuestion("Read-only (yes/no):") + if err != nil { + return err + } + if readOnlyAnswer == "yes" { + opts = append(opts, models.WithReadOnly()) + } + + return ctx.Storage.CreateAWSRepository(name, creds.BucketName, creds.Region, opts...) }