bucketstorage.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package storage
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "gopkg.in/yaml.v2"
  7. )
  8. // StorageProvider is the type of provider used for storage if not leveraging a file implementation.
  9. type StorageProvider string
  10. const (
  11. S3 StorageProvider = "S3"
  12. // AZURE StorageProvider = "AZURE"
  13. // GCS StorageProvider = "GCS"
  14. )
  15. // StorageConfig is the configuration type used as the "parent" configuration. It contains a type, which will
  16. // specify the bucket storage implementation, and a configuration object specific to that storage implementation.
  17. type StorageConfig struct {
  18. Type StorageProvider `yaml:"type"`
  19. Config interface{} `yaml:"config"`
  20. }
  21. // NewBucketStorage initializes and returns new Storage implementation leveraging the storage provider
  22. // configuration. This configuration type uses the layout provided in thanos: https://thanos.io/tip/thanos/storage.md/
  23. func NewBucketStorage(config []byte) (Storage, error) {
  24. storageConfig := &StorageConfig{}
  25. if err := yaml.UnmarshalStrict(config, storageConfig); err != nil {
  26. return nil, errors.Wrap(err, "parsing config YAML file")
  27. }
  28. // Because the Config property is specific to the storage implementation, we'll marshal back into yaml, and allow
  29. // the specific implementation to unmarshal back into a concrete configuration type.
  30. config, err := yaml.Marshal(storageConfig.Config)
  31. if err != nil {
  32. return nil, errors.Wrap(err, "marshal content of storage configuration")
  33. }
  34. var storage Storage
  35. switch strings.ToUpper(string(storageConfig.Type)) {
  36. case string(S3):
  37. storage, err = NewS3Storage(config)
  38. //case string(GCS):
  39. // storage, err = NewGCSStorage(config)
  40. //case string(AZURE):
  41. // storage, err = NewAzureStorage(config)
  42. default:
  43. return nil, errors.Errorf("storage with type %s is not supported", storageConfig.Type)
  44. }
  45. if err != nil {
  46. return nil, errors.Wrap(err, fmt.Sprintf("create %s client", storageConfig.Type))
  47. }
  48. return storage, nil
  49. }