Просмотр исходного кода

Added a simple StorageType identifier for monitoring

Matt Bolt 4 лет назад
Родитель
Сommit
dac3513c07

+ 5 - 0
pkg/storage/azurestorage.go

@@ -241,6 +241,11 @@ func (as *AzureStorage) Name() string {
 	return as.name
 }
 
+// StorageType returns a string identifier for the type of storage used by the implementation.
+func (as *AzureStorage) StorageType() StorageType {
+	return StorageTypeBucketAzure
+}
+
 // FullPath returns the storage working path combined with the path provided
 func (as *AzureStorage) FullPath(name string) string {
 	name = trimLeading(name)

+ 5 - 0
pkg/storage/filestorage.go

@@ -21,6 +21,11 @@ func NewFileStorage(baseDir string) Storage {
 	return &FileStorage{baseDir}
 }
 
+// StorageType returns a string identifier for the type of storage used by the implementation.
+func (fs *FileStorage) StorageType() StorageType {
+	return StorageTypeFile
+}
+
 // FullPath returns the storage working path combined with the path provided
 func (fs *FileStorage) FullPath(path string) string {
 	return gopath.Join(fs.baseDir, path)

+ 5 - 0
pkg/storage/gcsstorage.go

@@ -76,6 +76,11 @@ func (gs *GCSStorage) Name() string {
 	return gs.name
 }
 
+// StorageType returns a string identifier for the type of storage used by the implementation.
+func (gs *GCSStorage) StorageType() StorageType {
+	return StorageTypeBucketGCS
+}
+
 // FullPath returns the storage working path combined with the path provided
 func (gs *GCSStorage) FullPath(name string) string {
 	name = trimLeading(name)

+ 5 - 0
pkg/storage/s3storage.go

@@ -307,6 +307,11 @@ func (s3 *S3Storage) Name() string {
 	return s3.name
 }
 
+// StorageType returns a string identifier for the type of storage used by the implementation.
+func (s3 *S3Storage) StorageType() StorageType {
+	return StorageTypeBucketS3
+}
+
 // validate checks to see the config options are set.
 func validate(conf S3Config) error {
 	if conf.Endpoint == "" {

+ 3 - 0
pkg/storage/storage.go

@@ -21,6 +21,9 @@ type StorageInfo struct {
 
 // Storage provides an API for storing binary data
 type Storage interface {
+	// StorageType returns a string identifier for the type of storage used by the implementation.
+	StorageType() StorageType
+
 	// FullPath returns the storage working path combined with the path provided
 	FullPath(path string) string
 

+ 51 - 0
pkg/storage/storagetypes.go

@@ -0,0 +1,51 @@
+package storage
+
+import "strings"
+
+/*
+ NOTE: This format is to provide monitoring a simple way to identify the storage type with
+ NOTE: minimal changes to the Storage interface. It's not very robust, so use caution if
+ NOTE: leveraging this type in other systems.
+*/
+
+// StorageType is a string identifier for the type of storage used by a Storage implementation.
+// The string format is "backend|provider" where backend is the represents the generic storage
+// facility, and the provider is the specific implementation.
+type StorageType string
+
+const (
+	StorageTypeFile        StorageType = "file"
+	StorageTypeBucketS3    StorageType = "bucket|s3"
+	StorageTypeBucketGCS   StorageType = "bucket|gcs"
+	StorageTypeBucketAzure StorageType = "bucket|azure"
+)
+
+// IsFileStorage returns true if the StorageType is a file storage type.
+func (st StorageType) IsFileStorage() bool {
+	return st.BackendType() == "file"
+}
+
+// IsBucketStorage returns true if the StorageType is a bucket storage type.
+func (st StorageType) IsBucketStorage() bool {
+	return st.BackendType() == "bucket"
+}
+
+// BackendType returns the backend type if applicable for the storage type.
+func (st StorageType) BackendType() string {
+	index := strings.Index(string(st), "|")
+	if index > 0 {
+		return string(st)[:index]
+	}
+	return string(st)
+}
+
+// ProviderType returns the provider type if applicable for the storage type.
+func (st StorageType) ProviderType() string {
+	index := strings.Index(string(st), "|")
+
+	if index > 0 && index < len(string(st))-1 {
+		return string(st)[index+1:]
+	}
+
+	return ""
+}

+ 32 - 0
pkg/storage/storagetypes_test.go

@@ -0,0 +1,32 @@
+package storage
+
+import "testing"
+
+func assert(t *testing.T, condition bool, msg string) {
+	if !condition {
+		t.Error(msg)
+	}
+}
+
+func TestStorageTypes(t *testing.T) {
+	fileType := StorageTypeFile
+	s3Type := StorageTypeBucketS3
+	gcsType := StorageTypeBucketGCS
+	azureType := StorageTypeBucketAzure
+
+	assert(t, fileType.BackendType() == "file", "StorageTypeFile.BackendType() should return 'file'")
+	assert(t, s3Type.BackendType() == "bucket", "StorageTypeBucketS3.BackendType() should return 'bucket'")
+	assert(t, gcsType.BackendType() == "bucket", "StorageTypeBucketGCS.BackendType() should return 'bucket'")
+	assert(t, azureType.BackendType() == "bucket", "StorageTypeBucketAzure.BackendType() should return 'bucket'")
+
+	assert(t, fileType.ProviderType() == "", "StorageTypeFile.ProviderType() should return ''")
+	assert(t, s3Type.ProviderType() == "s3", "StorageTypeBucketS3.ProviderType() should return 's3'")
+	assert(t, gcsType.ProviderType() == "gcs", "StorageTypeBucketGCS.ProviderType() should return 'gcs'")
+	assert(t, azureType.ProviderType() == "azure", "StorageTypeBucketAzure.ProviderType() should return 'azure'")
+
+	assert(t, fileType.IsFileStorage(), "StorageTypeFile.IsFileStorage() should return true")
+	assert(t, s3Type.IsBucketStorage(), "StorageTypeBucketS3.IsBucketStorage() should return true")
+	assert(t, gcsType.IsBucketStorage(), "StorageTypeBucketGCS.IsBucketStorage() should return true")
+	assert(t, azureType.IsBucketStorage(), "StorageTypeBucketAzure.IsBucketStorage() should return true")
+
+}