blob: 8258b1381111aa4f0d88375ef6ff5a83f945d6c7 [file]
/*
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 impl
import (
"fmt"
"github.com/apache/incubator-devlake/migration"
"github.com/apache/incubator-devlake/plugins/core"
"github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}/api"
"github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}/models"
"github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}/models/migrationscripts"
"github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}/tasks"
"github.com/apache/incubator-devlake/plugins/helper"
"github.com/spf13/viper"
"gorm.io/gorm"
)
// make sure interface is implemented
var _ core.PluginMeta = (*{{ .PluginName }})(nil)
var _ core.PluginInit = (*{{ .PluginName }})(nil)
var _ core.PluginTask = (*{{ .PluginName }})(nil)
var _ core.PluginApi = (*{{ .PluginName }})(nil)
var _ core.PluginBlueprintV100 = (*{{ .PluginName }})(nil)
var _ core.CloseablePluginTask = (*{{ .PluginName }})(nil)
type {{ .PluginName }} struct{}
func (plugin {{ .PluginName }}) Description() string {
return "collect some {{ .PluginName }} data"
}
func (plugin {{ .PluginName }}) Init(config *viper.Viper, logger core.Logger, db *gorm.DB) error {
api.Init(config, logger, db)
return nil
}
func (plugin {{ .PluginName }}) SubTaskMetas() []core.SubTaskMeta {
// TODO add your sub task here
return []core.SubTaskMeta{
}
}
func (plugin {{ .PluginName }}) PrepareTaskData(taskCtx core.TaskContext, options map[string]interface{}) (interface{}, error) {
op, err := tasks.DecodeAndValidateTaskOptions(options)
if err != nil {
return nil, err
}
connectionHelper := helper.NewConnectionHelper(
taskCtx,
nil,
)
connection := &models.{{ .PluginName }}Connection{}
err = connectionHelper.FirstById(connection, op.ConnectionId)
if err != nil {
return nil, fmt.Errorf("unable to get {{ .PluginName }} connection by the given connection ID: %v", err)
}
apiClient, err := tasks.New{{ .PluginName }}ApiClient(taskCtx, connection)
if err != nil {
return nil, fmt.Errorf("unable to get {{ .PluginName }} API client instance: %v", err)
}
return &tasks.{{ .PluginName }}TaskData{
Options: op,
ApiClient: apiClient,
}, nil
}
// PkgPath information lost when compiled as plugin(.so)
func (plugin {{ .PluginName }}) RootPkgPath() string {
return "github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}"
}
func (plugin {{ .PluginName }}) MigrationScripts() []migration.Script {
return migrationscripts.All()
}
func (plugin {{ .PluginName }}) ApiResources() map[string]map[string]core.ApiResourceHandler {
return map[string]map[string]core.ApiResourceHandler{
"test": {
"POST": api.TestConnection,
},
"connections": {
"POST": api.PostConnections,
"GET": api.ListConnections,
},
"connections/:connectionId": {
"GET": api.GetConnection,
"PATCH": api.PatchConnection,
"DELETE": api.DeleteConnection,
},
}
}
func (plugin {{ .PluginName }}) MakePipelinePlan(connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, error) {
return api.MakePipelinePlan(plugin.SubTaskMetas(), connectionId, scope)
}
func (plugin {{ .PluginName }}) Close(taskCtx core.TaskContext) error {
data, ok := taskCtx.GetData().(*tasks.{{ .PluginName }}TaskData)
if !ok {
return fmt.Errorf("GetData failed when try to close %+v", taskCtx)
}
data.ApiClient.Release()
return nil
}