Skip to content

Commit

Permalink
Feature: AWS read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed May 11, 2021
1 parent f8ce0e8 commit a2b2ab3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
13 changes: 8 additions & 5 deletions internal/elastic/core/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/models/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions internal/models/mock/general.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/models/options.go
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion internal/reindexer/core/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 16 additions & 1 deletion scripts/esctl/repository.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "github.com/baking-bad/bcdhub/internal/models"

type createRepoCommand struct{}

var createRepoCmd createRepoCommand
Expand All @@ -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...)
}

0 comments on commit a2b2ab3

Please sign in to comment.