aboutsummaryrefslogtreecommitdiff
path: root/storage/cloud/swift.go
blob: 38eb616cc50fef102e23b1989a132a9ea51817ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cloud

import (
	"github.com/ncw/swift"
	"github.com/silvasur/petrific/config"
	"github.com/silvasur/petrific/storage"
	"time"
)

type SwiftCloudStorage struct {
	con       *swift.Connection
	container string
}

type SwiftConfig struct {
	// Mandatory options
	Container string `toml:"container"`
	UserName  string `toml:"user_name"`
	ApiKey    string `toml:"api_key"`
	AuthURL   string `toml:"auth_url"`

	// Optional... options
	Domain         string `toml:"domain,omitempty"`
	DomainID       string `toml:"domain_id,omitempty"`
	UserID         string `toml:"user_id,omitempty"`
	Retries        int    `toml:"retries,omitempty"`
	Region         string `toml:"region,omitempty"`
	AuthVersion    int    `toml:"auth_version,omitempty"`
	Tenant         string `toml:"tenant,omitempty"`
	TenantID       string `toml:"tenant_id,omitempty"`
	TenantDomain   string `toml:"tenant_domain,omitempty"`
	TenantDomainID string `toml:"tenant_domain_id,omitempty"`
	TrustID        string `toml:"trust_id,omitempty"`
	ConnectTimeout string `toml:"connect_timeout,omitempty"`
	Timeout        string `toml:"timeout,omitempty"`
}

// SwiftStorageCreator creates an object storage that saves the objects to an openstack swift storage.
// Use the method "openstack-swift" in your config and refer to the `SwiftConfig` structure for additional config keys.
func SwiftStorageCreator() storage.CreateStorageFromConfig {
	return cloudStorageCreator(func(conf config.Config, name string) (CloudStorage, error) {
		var storage_conf SwiftConfig

		if err := conf.GetStorageConfData(name, &storage_conf); err != nil {
			return nil, err
		}

		storage := SwiftCloudStorage{}
		storage.con = new(swift.Connection)

		storage.container = storage_conf.Container
		storage.con.UserName = storage_conf.UserName
		storage.con.ApiKey = storage_conf.ApiKey
		storage.con.AuthUrl = storage_conf.AuthURL

		storage.con.Domain = storage_conf.Domain
		storage.con.DomainId = storage_conf.DomainID
		storage.con.UserId = storage_conf.UserID
		storage.con.Retries = storage_conf.Retries
		storage.con.Region = storage_conf.Region
		storage.con.AuthVersion = storage_conf.AuthVersion
		storage.con.Tenant = storage_conf.Tenant
		storage.con.TenantId = storage_conf.TenantID
		storage.con.TenantDomain = storage_conf.TenantDomain
		storage.con.TenantDomainId = storage_conf.TenantDomainID
		storage.con.TrustId = storage_conf.TrustID

		if storage_conf.ConnectTimeout != "" {
			d, err := time.ParseDuration(storage_conf.ConnectTimeout)
			if err != nil {
				return nil, err
			}
			storage.con.ConnectTimeout = d
		}

		if storage_conf.Timeout != "" {
			d, err := time.ParseDuration(storage_conf.Timeout)
			if err != nil {
				return nil, err
			}
			storage.con.Timeout = d
		}

		if err := storage.con.Authenticate(); err != nil {
			return nil, err
		}

		return storage, nil
	})
}

func (scs SwiftCloudStorage) Get(key string) ([]byte, error) {
	return scs.con.ObjectGetBytes(scs.container, key)
}

func (scs SwiftCloudStorage) Has(key string) (bool, error) {
	switch _, _, err := scs.con.Object(scs.container, key); err {
	case nil:
		return true, nil
	case swift.ObjectNotFound:
		return false, nil
	default:
		return false, err
	}
}

func (scs SwiftCloudStorage) Put(key string, content []byte) error {
	err := scs.con.ObjectPutBytes(scs.container, key, content, "application/octet-stream")

	return err
}

func (scs SwiftCloudStorage) Delete(key string) error {
	return scs.con.ObjectDelete(scs.container, key)
}

func (scs SwiftCloudStorage) List(prefix string) ([]string, error) {
	return scs.con.ObjectNamesAll(scs.container, &swift.ObjectsOpts{
		Prefix: prefix,
	})
}

func (scs SwiftCloudStorage) Close() error {
	return nil
}