Skip to content

Commit 49ed3a4

Browse files
authored
Merge pull request #47 from FlowCI/develop
0.21.21
2 parents 4e00a2c + a4dad43 commit 49ed3a4

24 files changed

+499
-327
lines changed

api/client.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ var (
3737

3838
type (
3939
Client interface {
40-
Connect(*domain.AgentInit) error
40+
Connect(*domain.AgentInit) (*domain.AgentConfig, error)
4141
ReConn() <-chan struct{}
4242

4343
UploadLog(filePath string) error
44-
ReportProfile(*domain.Resource) error
44+
ReportProfile(profile *domain.AgentProfile) error
4545

4646
GetCmdIn() <-chan []byte
4747
SendCmdOut(out domain.CmdOut) error
@@ -81,10 +81,10 @@ type (
8181
}
8282
)
8383

84-
func (c *client) Connect(init *domain.AgentInit) error {
84+
func (c *client) Connect(init *domain.AgentInit) (*domain.AgentConfig, error) {
8585
u, err := url.Parse(c.server)
8686
if err != nil {
87-
return err
87+
return nil, err
8888
}
8989

9090
u.Scheme = "ws"
@@ -103,37 +103,33 @@ func (c *client) Connect(init *domain.AgentInit) error {
103103
// build connection
104104
c.conn, _, err = dialer.Dial(u.String(), header)
105105
if err != nil {
106-
return err
106+
return nil, err
107107
}
108108

109109
c.conn.SetReadLimit(bufferSize)
110110
c.setConnState(connStateConnected)
111111

112112
// send init connect event
113-
_, err = c.sendMessageWithResp(eventConnect, init)
113+
resp := &domain.AgentConfigResponse{}
114+
err = c.sendMessageWithResp(eventConnect, init, resp)
114115
if err != nil {
115-
return err
116+
return nil, err
116117
}
117118

118119
// start to read message
119120
go c.readMessage()
120121
go c.consumePendingMessage()
121122

122123
util.LogInfo("Agent is connected to server %s", c.server)
123-
return nil
124+
return resp.Data, nil
124125
}
125126

126127
func (c *client) ReConn() <-chan struct{} {
127128
return c.reConn
128129
}
129130

130-
func (c *client) ReportProfile(r *domain.Resource) (err error) {
131-
body, err := json.Marshal(r)
132-
if err != nil {
133-
return
134-
}
135-
136-
_, err = c.send("POST", "profile", util.HttpMimeJson, bytes.NewBuffer(body))
131+
func (c *client) ReportProfile(r *domain.AgentProfile) (err error) {
132+
_ = c.sendMessageWithJson(eventProfile, r)
137133
return
138134
}
139135

@@ -291,25 +287,39 @@ func (c *client) GetSecret(name string) (secret domain.Secret, err error) {
291287
out, err := ioutil.ReadAll(resp.Body)
292288
util.PanicIfErr(err)
293289

294-
base := &domain.SecretBase{}
295-
err = json.Unmarshal(out, base)
290+
secretResp, err := c.parseResponse(out, &domain.SecretResponse{})
291+
util.PanicIfErr(err)
292+
293+
body := secretResp.(*domain.SecretResponse)
294+
util.PanicIfNil(body.Data, "secret data")
295+
296+
base := body.Data
297+
baseRaw := &domain.SecretResponseRaw{}
298+
err = json.Unmarshal(out, baseRaw)
296299
util.PanicIfErr(err)
297300

298301
if base.Category == domain.SecretCategoryAuth {
299302
auth := &domain.AuthSecret{}
300-
err = json.Unmarshal(out, auth)
303+
err = json.Unmarshal(baseRaw.Raw, auth)
301304
util.PanicIfErr(err)
302305
return auth, nil
303306
}
304307

305308
if base.Category == domain.SecretCategorySshRsa {
306309
rsa := &domain.RSASecret{}
307-
err = json.Unmarshal(out, rsa)
310+
err = json.Unmarshal(baseRaw.Raw, rsa)
308311
util.PanicIfErr(err)
309312
return rsa, nil
310313
}
311314

312-
return nil, fmt.Errorf("unsupport secret type")
315+
if base.Category == domain.SecretCategoryToken {
316+
token := &domain.TokenSecret{}
317+
err = json.Unmarshal(baseRaw.Raw, token)
318+
util.PanicIfErr(err)
319+
return token, nil
320+
}
321+
322+
return nil, fmt.Errorf("secret '%s' category '%s' is unsupported", base.GetName(), base.GetCategory())
313323
}
314324

315325
func (c *client) Close() {
@@ -454,7 +464,7 @@ func (c *client) sendMessageWithBytes(event string, body []byte) error {
454464
return nil
455465
}
456466

457-
func (c *client) sendMessageWithResp(event string, msg interface{}) (resp *domain.Response, out error) {
467+
func (c *client) sendMessageWithResp(event string, msg interface{}, resp domain.ResponseMessage) (out error) {
458468
defer util.RecoverPanic(func(e error) {
459469
out = e
460470
})
@@ -468,10 +478,8 @@ func (c *client) sendMessageWithResp(event string, msg interface{}) (resp *domai
468478
_, data, err := c.conn.ReadMessage()
469479
util.PanicIfErr(err)
470480

471-
message, err := c.parseResponse(data, &domain.Response{})
481+
_, err = c.parseResponse(data, resp)
472482
util.PanicIfErr(err)
473-
474-
resp = message.(*domain.Response)
475483
return
476484
}
477485

api/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
const (
44
eventConnect = "connect___"
5+
eventProfile = "profile___"
56
eventCmdOut = "cmd_out___"
67
eventShellLog = "slog______"
78
eventTtyLog = "tlog______"

0 commit comments

Comments
 (0)