-
Notifications
You must be signed in to change notification settings - Fork 12
/
objectStorage.go
130 lines (111 loc) · 3.73 KB
/
objectStorage.go
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
126
127
128
129
130
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package vultr
import (
"context"
"fmt"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/rs/zerolog/log"
"github.com/vultr/govultr/v3"
)
// GetRegionalObjectStorageClusters determines if a region has object storage clusters available
func (c *VultrConfiguration) GetRegionalObjectStorageClusters() (int, error) {
// Get cluster id of object storage cluster for region
clusters, _, _, err := c.Client.ObjectStorage.ListCluster(c.Context, &govultr.ListOptions{
Region: c.ObjectStorageRegion,
})
if err != nil {
return 0, fmt.Errorf("could not get object storage clusters: %s", err)
}
var clid int = 0
for _, cluster := range clusters {
if cluster.Region == c.ObjectStorageRegion {
clid = cluster.ID
}
}
if clid == 0 {
return 0, fmt.Errorf("could not find object storage cluster for region %s - use a compatible region", c.Region)
}
return clid, nil
}
// CreateObjectStorage creates a Vultr object storage resource
func (c *VultrConfiguration) CreateObjectStorage(storeName string) (govultr.ObjectStorage, error) {
// Get cluster id of object storage cluster for region
clid, err := c.GetRegionalObjectStorageClusters()
if err != nil {
return govultr.ObjectStorage{}, err
}
objst, _, err := c.Client.ObjectStorage.Create(c.Context, clid, storeName)
if err != nil {
return govultr.ObjectStorage{}, err
}
log.Info().Msgf("waiting for vultr object storage %s to be ready", storeName)
for i := 0; i < 60; i++ {
obj, _, err := c.Client.ObjectStorage.Get(c.Context, objst.ID)
if err != nil {
return govultr.ObjectStorage{}, err
}
switch {
case obj.Status == "active":
log.Info().Msgf("vultr object storage %s ready", storeName)
return *obj, nil
case i == 120:
return govultr.ObjectStorage{}, fmt.Errorf("vultr object storage %s is not active", storeName)
}
time.Sleep(time.Second * 1)
}
return govultr.ObjectStorage{}, err
}
// DeleteObjectStorage deletes a Vultr object storage resource
func (c *VultrConfiguration) DeleteObjectStorage(storeName string) error {
// Get object storage id
res, _, _, err := c.Client.ObjectStorage.List(c.Context, &govultr.ListOptions{
Label: storeName,
Region: c.ObjectStorageRegion,
})
if err != nil {
return fmt.Errorf("error listing object storage: %s", err)
}
if len(res) == 0 {
return fmt.Errorf("could not find object storage %s", storeName)
}
err = c.Client.ObjectStorage.Delete(c.Context, res[0].ID)
if err != nil {
return fmt.Errorf("error deleting object storage: %s", err)
}
return nil
}
// GetObjectStorage retrieves all Vultr object storage resources
func (c *VultrConfiguration) GetObjectStorage() ([]govultr.ObjectStorage, error) {
objst, _, _, err := c.Client.ObjectStorage.List(c.Context, &govultr.ListOptions{
Region: c.ObjectStorageRegion,
})
if err != nil {
return []govultr.ObjectStorage{}, err
}
return objst, nil
}
// CreateObjectStorageBucket leverages minio to create a bucket within Vultr object storage
func (c *VultrConfiguration) CreateObjectStorageBucket(cr VultrBucketCredentials, bucketName string) error {
ctx := context.Background()
useSSL := true
// Initialize minio client object.
minioClient, err := minio.New(cr.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(cr.AccessKey, cr.SecretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
return fmt.Errorf("error initializing minio client for vultr: %s", err)
}
location := "us-east-1"
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
return fmt.Errorf("error creating bucket %s for %s: %s", bucketName, cr.Endpoint, err)
}
return nil
}