klesh commented on code in PR #2208:
URL: https://github.com/apache/incubator-devlake/pull/2208#discussion_r899814327


##########
plugins/ae/ae.go:
##########
@@ -82,20 +101,25 @@ func (plugin AE) RootPkgPath() string {
 }
 
 func (plugin AE) MigrationScripts() []migration.Script {
-       return []migration.Script{new(migrationscripts.InitSchemas)}
+       return []migration.Script{
+               new(migrationscripts.InitSchemas),
+               new(migrationscripts.UpdateSchemas20220615),
+       }
 }
 
 func (plugin AE) ApiResources() map[string]map[string]core.ApiResourceHandler {
        return map[string]map[string]core.ApiResourceHandler{
-               "test": {
+               "test/:connectionId": {

Review Comment:
   No, connections/test is used by config-ui to test if the connection works or 
not BEFORE saving it. 
   At that point, no :connectionId yet



##########
plugins/ae/api/connection.go:
##########
@@ -18,88 +18,133 @@ limitations under the License.
 package api
 
 import (
-       "github.com/apache/incubator-devlake/config"
+       "fmt"
+       "net/http"
+       "time"
+
        "github.com/apache/incubator-devlake/plugins/ae/models"
        "github.com/apache/incubator-devlake/plugins/core"
        "github.com/apache/incubator-devlake/plugins/helper"
-       "net/http"
+       "github.com/go-playground/validator/v10"
+       "github.com/spf13/viper"
+       "gorm.io/gorm"
 )
 
 type ApiMeResponse struct {
        Name string `json:"name"`
 }
 
-/*
-GET /plugins/ae/test
-*/
-func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, 
error) {
-       // TODO: implement test connection
-       return &core.ApiResourceOutput{Body: true}, nil
+var connectionHelper *helper.ConnectionApiHelper
+
+func Init(config *viper.Viper, logger core.Logger, database *gorm.DB) {
+       basicRes := helper.NewDefaultBasicRes(config, logger, database)
+       vld := validator.New()
+       connectionHelper = helper.NewConnectionHelper(
+               basicRes,
+               vld,
+       )
 }
 
 /*
-PATCH /plugins/ae/connections/:connectionId
+GET /plugins/ae/test/:connectionId
 */
-func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, 
error) {
-       v := config.GetConfig()
+func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, 
error) {
        connection := &models.AeConnection{}
-       err := helper.EncodeStruct(v, connection, "env")
+       err := connectionHelper.First(connection, input.Params)
        if err != nil {
                return nil, err
        }
-       // update from request and save to .env
-       err = helper.DecodeStruct(v, connection, input.Body, "env")
+
+       // load and process cconfiguration
+       endpoint := connection.Endpoint
+       appId := connection.AppId
+       secretKey := connection.SecretKey
+       proxy := connection.Proxy
+
+       apiClient, err := helper.NewApiClient(endpoint, nil, 3*time.Second, 
proxy, nil)
        if err != nil {
                return nil, err
        }
-       err = config.WriteConfig(v)
+       apiClient.SetBeforeFunction(func(req *http.Request) error {
+               nonceStr := core.RandLetterBytes(8)
+               timestamp := fmt.Sprintf("%v", time.Now().Unix())
+               sign := models.GetSign(req.URL.Query(), appId, secretKey, 
nonceStr, timestamp)
+               req.Header.Set("x-ae-app-id", appId)
+               req.Header.Set("x-ae-timestamp", timestamp)
+               req.Header.Set("x-ae-nonce-str", nonceStr)
+               req.Header.Set("x-ae-sign", sign)
+               return nil
+       })
+       res, err := apiClient.Get("projects", nil, nil)
        if err != nil {
                return nil, err
        }
-       response := models.AeResponse{
-               AeConnection: *connection,
-               Name:         "Ae",
-               ID:           1,
+
+       switch res.StatusCode {
+       case 200: // right StatusCode
+               return &core.ApiResourceOutput{Body: true, Status: 200}, nil
+       case 401: // error secretKey or nonceStr
+               return &core.ApiResourceOutput{Body: false, Status: 
res.StatusCode}, nil
+       default: // unknow what happen , back to user
+               return &core.ApiResourceOutput{Body: res.Body, Status: 
res.StatusCode}, nil
        }
-       return &core.ApiResourceOutput{Body: response, Status: http.StatusOK}, 
nil
 }
 
 /*
-GET /plugins/ae/connections
+POST /plugins/ae/connections
 */
-func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, 
error) {
-       // RETURN ONLY 1 SOURCE (FROM ENV) until multi-connection is developed.
-       v := config.GetConfig()
+func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, 
error) {
        connection := &models.AeConnection{}
-
-       err := helper.EncodeStruct(v, connection, "env")
+       fmt.Printf("%+v\r\n", connectionHelper)

Review Comment:
   Either use logger or remove it .



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to