|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package api |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/apache/incubator-devlake/core/errors" |
| 24 | + "github.com/apache/incubator-devlake/core/plugin" |
| 25 | + "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 26 | + "github.com/apache/incubator-devlake/plugins/q_dev/models" |
| 27 | +) |
| 28 | + |
| 29 | +// 连接项目的CRUD API |
| 30 | + |
| 31 | +// PostConnections 创建新连接 |
| 32 | +func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 33 | + // 创建连接 |
| 34 | + connection := &models.QDevConnection{} |
| 35 | + err := api.Decode(input.Body, connection, vld) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + // 验证 |
| 40 | + // 保存到数据库 |
| 41 | + err = connectionHelper.Create(connection, input) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status: http.StatusOK}, nil |
| 46 | +} |
| 47 | + |
| 48 | +// PatchConnection 更新现有连接 |
| 49 | +func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 50 | + connection := &models.QDevConnection{} |
| 51 | + if err := connectionHelper.First(&connection, input.Params); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + if err := (&models.QDevConnection{}).MergeFromRequest(connection, input.Body); err != nil { |
| 55 | + return nil, errors.Convert(err) |
| 56 | + } |
| 57 | + if err := connectionHelper.SaveWithCreateOrUpdate(connection); err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status: http.StatusOK}, nil |
| 61 | +} |
| 62 | + |
| 63 | +// DeleteConnection 删除连接 |
| 64 | +func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 65 | + conn := &models.QDevConnection{} |
| 66 | + output, err := connectionHelper.Delete(conn, input) |
| 67 | + if err != nil { |
| 68 | + return output, err |
| 69 | + } |
| 70 | + output.Body = conn.Sanitize() |
| 71 | + return output, nil |
| 72 | +} |
| 73 | + |
| 74 | +// ListConnections 列出所有连接 |
| 75 | +func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 76 | + var connections []models.QDevConnection |
| 77 | + err := connectionHelper.List(&connections) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + // 敏感信息脱敏 |
| 82 | + for i := 0; i < len(connections); i++ { |
| 83 | + connections[i] = connections[i].Sanitize() |
| 84 | + } |
| 85 | + return &plugin.ApiResourceOutput{Body: connections}, nil |
| 86 | +} |
| 87 | + |
| 88 | +// GetConnection 获取单个连接详情 |
| 89 | +func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 90 | + connection := &models.QDevConnection{} |
| 91 | + err := connectionHelper.First(connection, input.Params) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + return &plugin.ApiResourceOutput{Body: connection.Sanitize()}, err |
| 96 | +} |
0 commit comments