2 * Copyright 2020 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 "github.com/sirupsen/logrus"
31 // Plugin adapter which decides a specific client based on plugin info
32 // TODO PluginInfo to have other information about plugins to find the client and implementation to handle accordingly.
33 type PluginAdapter struct {
38 // Constructor of PluginAdapter
39 func NewPluginAdapter(pluginInfo string, logger *logrus.Logger) *PluginAdapter {
40 return &PluginAdapter{pluginInfo: pluginInfo, logger: logger}
43 // Instantiate application
44 func (c *PluginAdapter) Instantiate(pluginInfo string, host string, deployArtifact string) (workloadId string, error error, status string) {
45 c.logger.Infof("Instantation started")
46 clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: chunkSize, RootCertificate: rootCertificate, Logger: c.logger}
47 var client, err = plugin.NewClientGRPC(clientConfig)
49 c.logger.Errorf("failed to create client: %v", err)
50 return "", err, "Failure"
53 ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
57 workloadId, status, err = client.Instantiate(ctx, deployArtifact, host)
59 c.logger.Errorf("server failed to respond %s", err.Error())
60 return "", err, "Failure"
62 c.logger.Infof("Instantiation completed with workloadId %s, status: %s ", workloadId, status)
63 return workloadId, nil, status
67 func (c *PluginAdapter) Query(pluginInfo string, host string, workloadId string) (status string, error error) {
68 c.logger.Infof("Query started")
69 clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: chunkSize, RootCertificate: rootCertificate}
70 var client, err = plugin.NewClientGRPC(clientConfig)
72 c.logger.Errorf("failed to create client: %v", err)
76 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
80 status, err = client.Query(ctx, host, workloadId)
82 c.logger.Errorf("failed to query: %v", err)
85 c.logger.Infof("query status: ", status)
89 // Terminate application
90 func (c *PluginAdapter) Terminate(pluginInfo string, host string, workloadId string) (status string, error error) {
91 c.logger.Infof("Terminate started")
92 clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: chunkSize, RootCertificate: rootCertificate}
93 var client, err = plugin.NewClientGRPC(clientConfig)
95 c.logger.Errorf("failed to create client: %v", err)
99 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
103 status, err = client.Terminate(ctx, host, workloadId)
106 c.logger.Errorf("failed to instantiate: %v", err)
107 return "Failure", err
110 c.logger.Infof("termination success with status: ", status)