From e2000fbabe3ac9e94b7e1b0022e4493356520a8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Magiera?= <magik6k@gmail.com>
Date: Mon, 24 Sep 2018 13:23:03 +0200
Subject: [PATCH] swiftds plugin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

License: MIT
Signed-off-by: Ɓukasz Magiera <magik6k@gmail.com>
---
 plugin/plugins/Rules.mk           |  2 +-
 plugin/plugins/swiftds/swiftds.go | 92 +++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 plugin/plugins/swiftds/swiftds.go

diff --git a/plugin/plugins/Rules.mk b/plugin/plugins/Rules.mk
index 450383be9..8041d16f6 100644
--- a/plugin/plugins/Rules.mk
+++ b/plugin/plugins/Rules.mk
@@ -1,6 +1,6 @@
 include mk/header.mk
 
-$(d)_plugins:=$(d)/git
+$(d)_plugins:=$(d)/git $(d)/swiftds
 $(d)_plugins_so:=$(addsuffix .so,$($(d)_plugins))
 $(d)_plugins_main:=$(addsuffix /main/main.go,$($(d)_plugins))
 
diff --git a/plugin/plugins/swiftds/swiftds.go b/plugin/plugins/swiftds/swiftds.go
new file mode 100644
index 000000000..4a674dcda
--- /dev/null
+++ b/plugin/plugins/swiftds/swiftds.go
@@ -0,0 +1,92 @@
+package swiftds
+
+import (
+	"github.com/ipfs/go-ipfs/plugin"
+	"github.com/ipfs/go-ipfs/repo"
+	"github.com/ipfs/go-ipfs/repo/fsrepo"
+
+	"github.com/ipfs/go-ds-swift"
+)
+
+// Plugins is exported list of plugins that will be loaded
+var Plugins = []plugin.Plugin{
+	&swiftPlugin{},
+}
+
+type swiftPlugin struct{}
+
+var _ plugin.PluginDatastore = (*swiftPlugin)(nil)
+
+func (*swiftPlugin) Name() string {
+	return "ds-swift"
+}
+
+func (*swiftPlugin) Version() string {
+	return "0.0.1"
+}
+
+func (*swiftPlugin) Init() error {
+	return nil
+}
+
+func (*swiftPlugin) DatastoreTypeName() string {
+	return "swiftds"
+}
+
+func (*swiftPlugin) DatastoreConfigParser() fsrepo.ConfigFromMap {
+	return func(conf map[string]interface{}) (fsrepo.DatastoreConfig, error) {
+		// v2 only for now
+
+		c := &swiftConfig{}
+
+		c.AuthUrl = conf["AuthUrl"].(string)
+		c.TenantId = conf["TenantId"].(string)
+		c.Tenant = conf["Tenant"].(string)
+		c.UserName = conf["UserName"].(string)
+		c.ApiKey = conf["ApiKey"].(string)
+		c.Region = conf["Region"].(string)
+		c.Container = conf["Container"].(string)
+
+		return c, nil
+	}
+}
+
+type swiftConfig struct {
+	AuthUrl  string
+	TenantId string
+	Tenant   string
+
+	UserName string
+	ApiKey   string // password
+
+	Region    string
+	Container string
+}
+
+func (c *swiftConfig) DiskSpec() fsrepo.DiskSpec {
+	// TODO: what else can we strip from here
+	return map[string]interface{}{
+		"AuthUrl":   c.AuthUrl,
+		"TenantId":  c.TenantId,
+		"Tenant":    c.Tenant,
+		"UserName":  c.UserName,
+		"ApiKey":    c.ApiKey,
+		"Region":    c.Region,
+		"Container": c.Container,
+	}
+}
+
+func (c *swiftConfig) Create(path string) (repo.Datastore, error) {
+	conf := swiftds.Config{}
+
+	conf.AuthUrl = c.AuthUrl
+	conf.TenantId = c.TenantId
+	conf.Tenant = c.Tenant
+	conf.UserName = c.UserName
+	conf.ApiKey = c.ApiKey
+	conf.Region = c.Region
+	conf.Container = c.Container
+	conf.AuthVersion = 2
+
+	return swiftds.NewSwiftDatastore(conf)
+}
-- 
GitLab