-
Notifications
You must be signed in to change notification settings - Fork 375
/
snippet_client.go
120 lines (100 loc) · 3.95 KB
/
snippet_client.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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pipeline_snippet_client
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle/apierrors"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/http/httputil"
)
var snippetClientMap map[string]*apistructs.DicePipelineSnippetClient
func SetSnippetClientMap(clientMap map[string]*apistructs.DicePipelineSnippetClient) {
snippetClientMap = clientMap
}
type querySnippetYamlResp struct {
apistructs.Header
Data string `json:"data"`
}
type batchQuerySnippetYamlResp struct {
apistructs.Header
Data []apistructs.BatchSnippetConfigYml `json:"data"`
}
func BatchGetSnippetPipelineYml(snippetConfig []apistructs.SnippetConfig) ([]apistructs.BatchSnippetConfigYml, error) {
var configs = map[string][]apistructs.SnippetConfig{}
for _, v := range snippetConfig {
configs[v.Source] = append(configs[v.Source], v)
}
var results []apistructs.BatchSnippetConfigYml
for key, v := range configs {
clientConfig := snippetClientMap[key]
if clientConfig == nil {
return nil, fmt.Errorf("getSnippetPipelineYml error: can not find snippet host %v client", key)
}
var buffer bytes.Buffer
r, err := httpclient.New(httpclient.WithTimeout(time.Second, time.Second*3)).Post(clientConfig.Host).
Path(clientConfig.Extra.UrlPathPrefix+"/actions/batch-query-snippet-yml").
Header(httputil.InternalHeader, "pipeline_snippet_client").
JSONBody(&v).
Do().Body(&buffer)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() {
return nil, apierrors.ErrInvoke.InternalError(fmt.Errorf("query snippet yml fail: please check snippet is exist, httpcode: %v, body: %s", r.StatusCode(), buffer.String()))
}
var resp batchQuerySnippetYamlResp
err = json.NewDecoder(&buffer).Decode(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(fmt.Errorf("body: %s, decode error %v", buffer.String(), err))
}
if !resp.Success {
return nil, apierrors.ErrInvoke.InternalError(fmt.Errorf("http client error: httpcode %v", r.StatusCode()))
}
if resp.Data != nil {
results = append(results, resp.Data...)
}
}
return results, nil
}
func GetSnippetPipelineYml(snippetConfig apistructs.SnippetConfig) (string, error) {
clientConfig := snippetClientMap[snippetConfig.Source]
if clientConfig == nil {
return "", fmt.Errorf("getSnippetPipelineYml error: can not find snippet %s client", snippetConfig.Name)
}
var buffer bytes.Buffer
r, err := httpclient.New(httpclient.WithTimeout(time.Second, time.Second*3)).Post(clientConfig.Host).
Path(clientConfig.Extra.UrlPathPrefix+"/actions/query-snippet-yml").
Header(httputil.InternalHeader, "pipeline_snippet_client").
JSONBody(&snippetConfig).
Do().Body(&buffer)
if err != nil {
return "", apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() {
return "", apierrors.ErrInvoke.InternalError(fmt.Errorf("query snippet yml fail: please check snippet is exist, httpcode: %v, body: %s", r.StatusCode(), buffer.String()))
}
var resp querySnippetYamlResp
err = json.NewDecoder(&buffer).Decode(&resp)
if err != nil {
return "", apierrors.ErrInvoke.InternalError(fmt.Errorf("body: %s, decode error %v", buffer.String(), err))
}
if resp.Success {
return resp.Data, nil
}
return "", apierrors.ErrInvoke.InternalError(fmt.Errorf("http client error: httpcode %v", r.StatusCode()))
}