forked from remind101/empire
/
apps.go
91 lines (75 loc) · 1.99 KB
/
apps.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
package cloudformation
import (
"fmt"
"golang.org/x/net/context"
"github.com/jinzhu/gorm"
"github.com/remind101/empire"
"github.com/remind101/empire/pkg/cloudformation/customresources"
)
// appClient mocks the Empire interface we use.
type appClient interface {
AppsFind(empire.AppsQuery) (*empire.App, error)
Create(context.Context, empire.CreateOpts) (*empire.App, error)
Destroy(context.Context, empire.DestroyOpts) error
}
// AppProperties represents the properties for the Custom::EmpireApp
type AppProperties struct {
Name *string
}
// EmpireAppResource is a Provisioner that will manage an Empire application
type EmpireAppResource struct {
empire appClient
}
func (p *EmpireAppResource) Properties() interface{} {
return &AppProperties{}
}
func (p *EmpireAppResource) Provision(ctx context.Context, req customresources.Request) (id string, data interface{}, err error) {
user := newUser()
properties := req.ResourceProperties.(*AppProperties)
switch req.RequestType {
case customresources.Create:
name := properties.Name
app, err := p.empire.AppsFind(empire.AppsQuery{
Name: name,
})
if err != nil && err != gorm.RecordNotFound {
return "", nil, err
}
app, err = p.empire.Create(ctx, empire.CreateOpts{
User: user,
Name: *name,
Message: "Creating app via Cloudformation",
})
if err != nil {
return "", nil, err
}
return app.ID, nil, nil
case customresources.Delete:
id := req.PhysicalResourceId
app, err := p.empire.AppsFind(empire.AppsQuery{
ID: &id,
})
if err != nil {
return id, nil, err
}
err = p.empire.Destroy(ctx, empire.DestroyOpts{
User: user,
App: app,
Message: "Destroying app via Cloudformation",
})
if err != nil {
return id, nil, err
}
return id, nil, nil
case customresources.Update:
id := req.PhysicalResourceId
_, err := p.empire.AppsFind(empire.AppsQuery{
ID: &id,
})
if err != nil {
return id, nil, err
}
return id, nil, fmt.Errorf("Updates are not supported")
}
return
}