|
| 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 | + "context" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/apache/incubator-devlake/core/errors" |
| 25 | + "github.com/apache/incubator-devlake/core/plugin" |
| 26 | + helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 27 | + "github.com/apache/incubator-devlake/plugins/testmo/models" |
| 28 | +) |
| 29 | + |
| 30 | +func testConnection(ctx context.Context, connection models.TestmoConn) (*plugin.ApiResourceOutput, errors.Error) { |
| 31 | + if vld != nil { |
| 32 | + if err := vld.Struct(connection); err != nil { |
| 33 | + return nil, errors.Default.Wrap(err, "error validating target") |
| 34 | + } |
| 35 | + } |
| 36 | + apiClient, err := helper.NewApiClientFromConnection(ctx, basicRes, &connection) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + response, err := apiClient.Get("projects", nil, nil) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + if response.StatusCode == http.StatusUnauthorized { |
| 45 | + return nil, errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while testing connection") |
| 46 | + } |
| 47 | + if response.StatusCode == http.StatusOK { |
| 48 | + return &plugin.ApiResourceOutput{Body: nil, Status: http.StatusOK}, nil |
| 49 | + } |
| 50 | + return &plugin.ApiResourceOutput{Body: nil, Status: response.StatusCode}, errors.HttpStatus(response.StatusCode).Wrap(err, "could not validate connection") |
| 51 | +} |
| 52 | + |
| 53 | +func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 54 | + var connection models.TestmoConn |
| 55 | + err := helper.Decode(input.Body, &connection, vld) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + testConnectionResult, testConnectionErr := testConnection(context.TODO(), connection) |
| 60 | + if testConnectionErr != nil { |
| 61 | + return nil, plugin.WrapTestConnectionErrResp(basicRes, testConnectionErr) |
| 62 | + } |
| 63 | + return testConnectionResult, nil |
| 64 | +} |
| 65 | + |
| 66 | +// TestExistingConnection test testmo connection |
| 67 | +// @Summary test testmo connection |
| 68 | +// @Description Test Testmo Connection |
| 69 | +// @Tags plugins/testmo |
| 70 | +// @Param connectionId path int true "connection ID" |
| 71 | +// @Success 200 {object} shared.ApiBody "Success" |
| 72 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 73 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 74 | +// @Router /plugins/testmo/connections/{connectionId}/test [POST] |
| 75 | +func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 76 | + connection, err := dsHelper.ConnApi.GetMergedConnection(input) |
| 77 | + if err != nil { |
| 78 | + return nil, errors.BadInput.Wrap(err, "find connection from db") |
| 79 | + } |
| 80 | + if err := helper.DecodeMapStruct(input.Body, connection, false); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + testConnectionResult, testConnectionErr := testConnection(context.TODO(), connection.TestmoConn) |
| 84 | + if testConnectionErr != nil { |
| 85 | + return nil, plugin.WrapTestConnectionErrResp(basicRes, testConnectionErr) |
| 86 | + } |
| 87 | + return testConnectionResult, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Connection CRUD operations |
| 91 | +func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 92 | + return dsHelper.ConnApi.Post(input) |
| 93 | +} |
| 94 | + |
| 95 | +func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 96 | + return dsHelper.ConnApi.Patch(input) |
| 97 | +} |
| 98 | + |
| 99 | +func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 100 | + return dsHelper.ConnApi.Delete(input) |
| 101 | +} |
| 102 | + |
| 103 | +func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 104 | + return dsHelper.ConnApi.GetAll(input) |
| 105 | +} |
| 106 | + |
| 107 | +func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 108 | + return dsHelper.ConnApi.GetDetail(input) |
| 109 | +} |
0 commit comments