d3707f068507bdc7ef7d0f2873b42a222c4c742f
[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         "log"
22         "os"
23         "time"
24 )
25
26 func Instantiate(pluginInfo string, host string, deployArtifact string) (workloadId string, error error) {
27         logger := log.New(os.Stdout, "broker ", log.LstdFlags|log.Lshortfile)
28         clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: 1024, RootCertificate: ""}
29         var client, err = plugin.NewClientGRPC(clientConfig)
30         if err != nil {
31                 logger.Fatalf("failed to create client: %v", err)
32                 return "", err
33         }
34         log.Printf("pluginInfo: ", pluginInfo)
35         log.Printf("host: ", host)
36         log.Printf("deployArtifact: ", deployArtifact)
37         ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
38         defer cancel()
39
40         //Instantiate
41         workloadId, status, err := client.Instantiate(ctx, deployArtifact, host)
42         if err != nil {
43                 logger.Println("server failed to respond %s", err.Error())
44         } else {
45                 logger.Println(workloadId, status)
46                 return workloadId, nil
47         }
48         return "", err
49 }
50
51 func Query(pluginInfo string, host string, workloadId string) (status string, error error) {
52         logger := log.New(os.Stdout, "broker ", log.LstdFlags|log.Lshortfile)
53         clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: 1024, RootCertificate: ""}
54         var client, err = plugin.NewClientGRPC(clientConfig)
55         if err != nil {
56                 logger.Fatalf("failed to create client: %v", err)
57                 return "", err
58         }
59         log.Printf("pluginInfo: ", pluginInfo)
60         log.Printf("host: ", host)
61         log.Printf("workloadId: ", workloadId)
62         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
63         defer cancel()
64
65         //Query
66         stats := client.Query(ctx, host, workloadId)
67         if err != nil {
68                 logger.Fatalf("failed to instantiate: %v", err)
69                 return stats, err
70         }
71         logger.Println("query status: ", stats)
72         return stats, nil
73 }
74
75 func Terminate(pluginInfo string, host string, workloadId string) (status string, error error) {
76         logger := log.New(os.Stdout, "broker ", log.LstdFlags|log.Lshortfile)
77         clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: 1024, RootCertificate: ""}
78         var client, err = plugin.NewClientGRPC(clientConfig)
79         if err != nil {
80                 logger.Fatalf("failed to create client: %v", err)
81                 return
82         }
83         log.Printf("pluginInfo: ", pluginInfo)
84         log.Printf("host: ", host)
85         log.Printf("workloadId: ", workloadId)
86         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
87         defer cancel()
88
89         //Terminate
90         ts := client.Terminate(ctx, host, workloadId)
91         if err != nil {
92                 logger.Fatalf("failed to instantiate: %v", err)
93                 return ts, err
94         }
95
96         logger.Println("termination status: ", ts)
97         return ts, nil
98 }