-
Notifications
You must be signed in to change notification settings - Fork 501
/
connection.go
148 lines (130 loc) · 4.01 KB
/
connection.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 api
import (
"context"
"fmt"
"net/http"
"time"
"github.com/apache/incubator-devlake/plugins/azure/models"
"github.com/apache/incubator-devlake/plugins/core"
"github.com/apache/incubator-devlake/plugins/helper"
"github.com/apache/incubator-devlake/utils"
"github.com/mitchellh/mapstructure"
)
func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
// decode
var err error
var connection models.TestConnectionRequest
err = mapstructure.Decode(input.Body, &connection)
if err != nil {
return nil, err
}
// validate
err = vld.Struct(connection)
if err != nil {
return nil, err
}
// test connection
encodedToken := utils.GetEncodedToken(connection.Username, connection.Password)
apiClient, err := helper.NewApiClient(
context.TODO(),
connection.Endpoint,
map[string]string{
"Authorization": fmt.Sprintf("Basic %v", encodedToken),
},
3*time.Second,
connection.Proxy,
basicRes,
)
if err != nil {
return nil, err
}
res, err := apiClient.Get("", nil, nil)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}
return nil, nil
}
/*
POST /plugins/zaure/connections
{
"name": "zaure data connection name",
"endpoint": "zaure api endpoint, i.e. https://ci.zaure.io/",
"username": "username, usually should be email address",
"password": "zaure api access token"
}
*/
func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
// create a new connection
connection := &models.AzureConnection{}
// update from request and save to database
err := connectionHelper.Create(connection, input)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil
}
/*
PATCH /plugins/zaure/connections/connectionId
{
"name": "zaure data connection name",
"endpoint": "zaure api endpoint, i.e. https://ci.zaure.io/",
"username": "username, usually should be email address",
"password": "zaure api access token"
}
*/
func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
connection := &models.AzureConnection{}
err := connectionHelper.Patch(connection, input)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connection}, nil
}
/*
DELETE /plugins/zaure/connections/connectionId
*/
func DeleteConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
connection := &models.AzureConnection{}
err := connectionHelper.First(connection, input.Params)
if err != nil {
return nil, err
}
err = connectionHelper.Delete(connection)
return &core.ApiResourceOutput{Body: connection}, err
}
/*
GET /plugins/zaure/connections
*/
func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
var connections []models.AzureConnection
err := connectionHelper.List(&connections)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connections, Status: http.StatusOK}, nil
}
/*
GET /plugins/zaure/connections/connectionId
*/
func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
connection := &models.AzureConnection{}
err := connectionHelper.First(connection, input.Params)
return &core.ApiResourceOutput{Body: connection}, err
}