/
storage.go
160 lines (144 loc) · 4.04 KB
/
storage.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package command
import (
"errors"
"fmt"
"github.com/ysugimoto/ginger/config"
"github.com/ysugimoto/ginger/entity"
"github.com/ysugimoto/ginger/logger"
"github.com/ysugimoto/ginger/request"
"github.com/ysugimoto/go-args"
)
const (
STORAGEDEPLOY = "deploy"
STORAGEMOUNT = "mount"
STORAGEUNMOUNT = "unmount"
)
type Storage struct {
Command
log *logger.Logger
}
func NewStorage() *Storage {
return &Storage{
log: logger.WithNamespace("ginger.storage"),
}
}
func (s *Storage) Help() string {
return commandHeader() + `
storage - (AWS S3) management command.
Usage:
$ ginger storage [operation] [options]
Operation:
mount : Mount function to destination path
unmount : Unmount function from destination path
deploy : Deploy functions
help : Show this help
Options:
-p, --path : [mount] Path name
-b, --bucket : [mount] Bucket name (default use configuation)
-d, --directory : [mount] Mount target directory on S3
`
}
func (s *Storage) Run(ctx *args.Context) error {
c := config.Load()
if !c.Exists() {
s.log.Error("Configuration file could not load. Run `ginger init` before.")
return errors.New("")
}
var err error
defer func() {
if err != nil {
s.log.Error(err.Error())
debugTrace(err)
}
c.Write()
}()
switch ctx.At(1) {
case STORAGEDEPLOY:
err = NewDeploy().deployStorage(c, ctx)
case STORAGEMOUNT:
err = s.mountStorage(c, ctx)
case STORAGEUNMOUNT:
err = s.unmountStorage(c, ctx)
default:
fmt.Println(s.Help())
}
return err
}
// mountStorage makes integration between API Gateway resource and S3 storage
func (s *Storage) mountStorage(c *config.Config, ctx *args.Context) error {
bucket := ctx.String("bucket")
if bucket == "" {
bucket = c.S3BucketName
}
if bucket != c.S3BucketName {
s.log.Warnf("Target bucket %s is external bucket. ginger only manages integration.\n", bucket)
}
path := ctx.String("path")
if path == "" {
path = c.ChooseResource()
}
if _, err := c.LoadResource(path); err != nil {
return exception("Resource %s could not find in your project.\n", path)
}
rs, err := c.LoadResource(path + "/{proxy+}")
if err != nil {
s.log.Warnf("Create %s/{proxy+} sub-resource in order to map sub-path request.\n", path)
rs = entity.NewResource("", path+"/{proxy+}")
c.Resources = append(c.Resources, rs)
}
dir := ctx.String("directory")
if dir == "" {
dir = "/"
}
// On storage integration, we only support "GET" method.
method := "GET"
ig := rs.GetIntegration(method)
if ig == nil {
ig = entity.NewIntegration("s3", bucket+dir, rs.Path)
rs.AddIntegration(method, ig)
s.log.Infof("mounted storage %s to resource %s.\n", bucket+dir, path)
return nil
}
switch ig.IntegrationType {
case "lambda":
return exception("Resource already mounted as lambda. Unmount before use it.")
case "s3":
return exception("Resource already mounted as storage. Unmount before use it.")
}
return exception("Undefined integration type.")
}
// mountStorage makes integration between API Gateway resource and S3 storage
func (s *Storage) unmountStorage(c *config.Config, ctx *args.Context) error {
path := ctx.String("path")
if path == "" {
path = c.ChooseResource()
}
if _, err := c.LoadResource(path); err != nil {
return exception("Resource %s could not find in your project.\n", path)
}
rs, err := c.LoadResource(path + "/{proxy+}")
if err != nil {
return exception("Proxy resource could not find in %s.\n", path)
}
// On storage integration, we only support "GET" method.
method := "GET"
ig := rs.GetIntegration(method)
if ig == nil {
return exception("No integration found, Abort.")
}
switch ig.IntegrationType {
case "lambda":
return exception("Lambda function already integrated. to remove it, run 'ginger function unmount'.")
case "s3":
if rs.Id != "" {
api := request.NewAPIGateway(c)
api.DeleteMethod(c.RestApiId, rs.Id, method)
api.DeleteIntegration(c.RestApiId, rs.Id, method)
}
rs.DeleteIntegration(method)
default:
return exception("Undefined integration type.")
}
s.log.Infof("Storage unmounted for resource %s.\n", path)
return nil
}