Uniform formatting and volume as hostpath
[ealt-edge.git] / mecm / mepm / applcm / broker / pkg / handlers / adapter / pluginAdapter / pluginAdapter.go
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package pluginAdapter
17
18 import (
19         "broker/pkg/plugin"
20         "context"
21         "time"
22
23         "github.com/sirupsen/logrus"
24 )
25
26 const (
27         chunkSize       = 1024
28         rootCertificate = ""
29 )
30
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 {
34         pluginInfo string
35         logger     *logrus.Logger
36 }
37
38 // Constructor of PluginAdapter
39 func NewPluginAdapter(pluginInfo string, logger *logrus.Logger) *PluginAdapter {
40         return &PluginAdapter{pluginInfo: pluginInfo, logger: logger}
41 }
42
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)
48         if err != nil {
49                 c.logger.Errorf("failed to create client: %v", err)
50                 return "", err, "Failure"
51         }
52
53         ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
54         defer cancel()
55
56         //Instantiate
57         workloadId, status, err = client.Instantiate(ctx, deployArtifact, host)
58         if err != nil {
59                 c.logger.Errorf("server failed to respond %s", err.Error())
60                 return "", err, "Failure"
61         }
62         c.logger.Infof("Instantiation completed with workloadId %s, status: %s ", workloadId, status)
63         return workloadId, nil, status
64 }
65
66 // Query application
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)
71         if err != nil {
72                 c.logger.Errorf("failed to create client: %v", err)
73                 return "", err
74         }
75
76         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
77         defer cancel()
78
79         //Query
80         status, err = client.Query(ctx, host, workloadId)
81         if err != nil {
82                 c.logger.Errorf("failed to query: %v", err)
83                 return "", err
84         }
85         c.logger.Infof("query status: ", status)
86         return status, nil
87 }
88
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)
94         if err != nil {
95                 c.logger.Errorf("failed to create client: %v", err)
96                 return "Failure", err
97         }
98
99         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
100         defer cancel()
101
102         //Terminate
103         status, err = client.Terminate(ctx, host, workloadId)
104
105         if err != nil {
106                 c.logger.Errorf("failed to instantiate: %v", err)
107                 return "Failure", err
108         }
109
110         c.logger.Infof("termination success with status: ", status)
111         return status, nil
112 }