forked from sonatype-nexus-community/gonexus
/
groovyBlobStore.go
94 lines (76 loc) · 2.48 KB
/
groovyBlobStore.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
package nexusrm
import (
"bytes"
"fmt"
"text/template"
)
/*
type blobStoreDelete struct {
Name string
}
const groovyDeleteBlobStore = `blobStore.delete('{{.Name}}')`
*/
type blobStoreFile struct {
Name, Path string
}
const groovyCreateFileBlobStore = `blobStore.createFileBlobStore('{{.Name}}', '{{.Path}}')`
// BlobStoreS3 encapsulates the needed options for creating an S3 blob store
type BlobStoreS3 struct {
Name, BucketName, AwsAccessKey, AwsSecret, AwsIamRole, AwsRegion string
}
const groovyCreateS3BlobStore = `def config = [:]
config['bucket'] = '{{.BucketName}}'
config['accessKeyId'] = '{{.AwsAccessKey}}'
config['secretAccessKey'] = '{{.AwsSecret}}'
config['assumeRole'] = '{{.AwsIamRole}}'
config['region'] = '{{.AwsRegion}}'
blobStore.createS3BlobStore('{{.Name}}', config)`
type blobStoreGroup struct {
Name string
BlobStores []string
}
const groovyCreateBlobStoreGroup = `blobStore.createBlobStoreGroup('{{.Name}}', [{{range .BlobStores}}'{{.}}',{{end}}], 'writeToFirst')`
/*
// DeleteBlobStore creates a blobstore
func DeleteBlobStore(rm RM, name string) error {
tmpl, err := template.New("dbs").Parse(groovyDeleteBlobStore)
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, blobStoreDelete{name})
if err != nil {
return err
}
_, err = ScriptRunOnce(rm, newAnonGroovyScript(buf.String()), nil)
return err
}
*/
// CreateFileBlobStore creates a blobstore
func CreateFileBlobStore(rm RM, name, path string) error {
tmpl, err := template.New("fbs").Parse(groovyCreateFileBlobStore)
if err != nil {
return fmt.Errorf("could not parse template: %v", err)
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, blobStoreFile{name, path})
if err != nil {
return fmt.Errorf("could not create file blobstore from template: %v", err)
}
_, err = ScriptRunOnce(rm, newAnonGroovyScript(buf.String()), nil)
return fmt.Errorf("could not create file blobstore: %v", err)
}
// CreateBlobStoreGroup creates a blobstore
func CreateBlobStoreGroup(rm RM, name string, blobStores []string) error {
tmpl, err := template.New("group").Parse(groovyCreateBlobStoreGroup)
if err != nil {
return fmt.Errorf("could not parse template: %v", err)
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, blobStoreGroup{name, blobStores})
if err != nil {
return fmt.Errorf("could not create group blobstore from template: %v", err)
}
_, err = ScriptRunOnce(rm, newAnonGroovyScript(buf.String()), nil)
return fmt.Errorf("could not create group blobstore: %v", err)
}