This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstack.go
More file actions
164 lines (123 loc) · 3.28 KB
/
Copy pathstack.go
File metadata and controls
164 lines (123 loc) · 3.28 KB
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
161
162
163
164
package stack
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"koding/kites/kloud/stack"
kloudstack "koding/kites/kloud/stack"
"koding/kites/kloud/utils/object"
"koding/klientctl/endpoint/credential"
"koding/klientctl/endpoint/kloud"
"koding/klientctl/endpoint/team"
"github.com/hashicorp/hcl"
yaml "gopkg.in/yaml.v2"
)
type CreateOptions struct {
Team string
Title string
Credentials []string
Template []byte
}
func (opts *CreateOptions) Valid() error {
if opts == nil {
return errors.New("stack: arguments are missing")
}
if len(opts.Template) == 0 {
return errors.New("stack: template data is missing")
}
return nil
}
var DefaultClient = &Client{}
type Client struct {
Kloud *kloud.Client
Credential *credential.Client
}
func (c *Client) Create(opts *CreateOptions) (*stack.ImportResponse, error) {
if err := opts.Valid(); err != nil {
return nil, err
}
data, err := c.jsonReencode(opts.Template)
if err != nil {
return nil, fmt.Errorf("stack: template encoding error: %s", err)
}
providers, err := kloudstack.ReadProviders(data)
if err != nil {
return nil, fmt.Errorf("stack: unable to read providers: %s", err)
}
if len(providers) == 0 {
return nil, errors.New("stack: unable to read providers")
}
req := &stack.ImportRequest{
Template: data,
Team: opts.Team,
Title: opts.Title,
Credentials: make(map[string][]string),
}
if req.Team == "" {
req.Team = team.Used().Name
}
var resp stack.ImportResponse
for _, identifier := range opts.Credentials {
provider, err := c.credential().Provider(identifier)
if err != nil {
return nil, fmt.Errorf("stack: unable to read provider of %q: %s", identifier, err)
}
req.Credentials[provider] = append(req.Credentials[provider], identifier)
}
used := c.credential().Used()
for _, provider := range providers {
if _, ok := req.Credentials[provider]; ok {
continue
}
identifier, ok := used[provider]
if !ok || identifier == "" {
return nil, fmt.Errorf("stack: missing credential for %q provider", provider)
}
req.Credentials[provider] = []string{identifier}
}
if err := c.kloud().Call("import", req, &resp); err != nil {
return nil, fmt.Errorf("stack: unable to communicate with Kloud: %s", err)
}
return &resp, nil
}
func (c *Client) kloud() *kloud.Client {
if c.Kloud != nil {
return c.Kloud
}
return kloud.DefaultClient
}
func (c *Client) credential() *credential.Client {
if c.Credential != nil {
return c.Credential
}
return credential.DefaultClient
}
func (c *Client) jsonReencode(data []byte) ([]byte, error) {
var jsonv interface{}
if err := json.Unmarshal(data, &jsonv); err == nil {
return jsonMarshal(jsonv)
}
var hclv interface{}
if err := hcl.Unmarshal(data, &hclv); err == nil {
object.FixHCL(hclv)
return jsonMarshal(hclv)
}
var ymlv interface{}
if err := yaml.Unmarshal(data, &ymlv); err == nil {
return jsonMarshal(object.FixYAML(ymlv))
}
return nil, errors.New("unknown encoding")
}
func Create(opts *CreateOptions) (*stack.ImportResponse, error) {
return DefaultClient.Create(opts)
}
func jsonMarshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}