/
houston.go
210 lines (176 loc) · 4.99 KB
/
houston.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package houston
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/pkg/errors"
// "github.com/sirupsen/logrus"
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/pkg/httputil"
)
var (
createDeploymentRequest = `
mutation CreateDeployment {
createDeployment(
title: "%s",
organizationUuid: "",
teamUuid: "",
type: "airflow",
version: ""
) {
success,
message,
id,
code
}
}`
createTokenRequest = `
mutation createToken {
createToken(
identity:"%s",
password:"%s"
) {
success
message
token
decoded {
id
sU
}
}
}`
fetchDeploymentsRequest = `
query FetchAllDeployments {
fetchDeployments {
uuid
type
title
release_name
version
}
}`
fetchDeploymentRequest = `
query FetchDeployment {
fetchDeployments(
deploymentUuid: "%s"
) {
uuid
type
title
release_name
version
}
}`
// log = logrus.WithField("package", "houston")
)
// Client containers the logger and HTTPClient used to communicate with the HoustonAPI
type Client struct {
HTTPClient *httputil.HTTPClient
}
// NewHoustonClient returns a new Client with the logger and HTTP client setup.
func NewHoustonClient(c *httputil.HTTPClient) *Client {
return &Client{
HTTPClient: c,
}
}
// GraphQLQuery wraps a graphql query string
type GraphQLQuery struct {
Query string `json:"query"`
}
// QueryHouston executes a query against the Houston API
func (c *Client) QueryHouston(query string) (*HoustonResponse, error) {
// logger := log.WithField("function", "QueryHouston")
doOpts := httputil.DoOptions{
Data: GraphQLQuery{query},
Headers: map[string]string{
"Accept": "application/json",
},
}
// set headers
if config.CFG.CloudAPIToken.GetString() != "" {
doOpts.Headers["authorization"] = config.CFG.CloudAPIToken.GetString()
}
// if config.GetString(config.OrgIDCFG) != "" {
// doOpts.Headers["organization"] = config.GetString(config.OrgIDCFG)
// }
var response httputil.HTTPResponse
httpResponse, err := c.HTTPClient.Do("POST", config.APIURL(), &doOpts)
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
// strings.NewReader(jsonStream)
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
response = httputil.HTTPResponse{
Raw: httpResponse,
Body: string(body),
}
// logger.Debug(query)
// logger.Debug(response.Body)
decode := HoustonResponse{}
err = json.NewDecoder(strings.NewReader(response.Body)).Decode(&decode)
if err != nil {
//logger.Error(err)
return nil, errors.Wrap(err, "Failed to JSON decode Houston response")
}
return &decode, nil
}
// CreateDeployment will send request to Houston to create a new AirflowDeployment
// Returns a StatusResponse which contains the unique id of deployment
func (c *Client) CreateDeployment(title string) (*StatusResponse, error) {
// logger := log.WithField("method", "CreateDeployment")
// logger.Debug("Entered CreateDeployment")
request := fmt.Sprintf(createDeploymentRequest, title)
response, err := c.QueryHouston(request)
if err != nil {
// logger.Error(err)
return nil, errors.Wrap(err, "CreateDeployment Failed")
}
return response.Data.CreateDeployment, nil
}
// CreateToken will request a new token from Houston, passing the users e-mail and password.
// Returns a Token structure with the users ID and Token inside.
func (c *Client) CreateToken(email string, password string) (*Token, error) {
// logger := log.WithField("method", "CreateToken")
// logger.Debug("Entered CreateToken")
request := fmt.Sprintf(createTokenRequest, email, password)
response, err := c.QueryHouston(request)
if err != nil {
// logger.Error(err)
return nil, errors.Wrap(err, "CreateToken Failed")
}
return response.Data.CreateToken, nil
}
// FetchDeployments will request all airflow deployments from Houston
// Returns a []Deployment structure with deployment details
func (c *Client) FetchDeployments() ([]Deployment, error) {
// logger := log.WithField("method", "FetchDeployments")
// logger.Debug("Entered FetchDeployments")
request := fetchDeploymentsRequest
response, err := c.QueryHouston(request)
if err != nil {
// logger.Error(err)
return nil, errors.Wrap(err, "FetchDeployments Failed")
}
return response.Data.FetchDeployments, nil
}
// FetchDeployment will request a specific airflow deployments from Houston by uuid
// Returns a Deployment structure with deployment details
func (c *Client) FetchDeployment(deploymentUuid string) (*Deployment, error) {
// logger := log.WithField("method", "FetchDeployments")
// logger.Debug("Entered FetchDeployments")
request := fmt.Sprintf(fetchDeploymentRequest, deploymentUuid)
response, err := c.QueryHouston(request)
if err != nil {
// logger.Error(err)
return nil, errors.Wrap(err, "FetchDeployment Failed")
}
if len(response.Data.FetchDeployments) == 0 {
return nil, fmt.Errorf("deployment not found for uuid \"%s\"", deploymentUuid)
}
return &response.Data.FetchDeployments[0], nil
}