1
18
19 package faas_openfaas
20
21 import (
22 "encoding/json"
23 "scheduler/log"
24 "scheduler/types"
25 )
26
27 func GenFunctionsGet(host string) ([]Function, *types.FaasApiResponse, error) {
28 res, err := functionsGetApiCall(host)
29 if err != nil {
30 log.Log.Debugf("Cannot get services from openfaas: %s", err.Error())
31 return nil, res, err
32 }
33
34 var functions []Function
35 err = json.Unmarshal([]byte(res.Body), &functions)
36 if err != nil {
37 log.Log.Debugf("Cannot decode services from openfaas: %s: %s", res.Body, err.Error())
38 return nil, res, err
39 }
40 return functions, res, nil
41 }
42
43 func GenFunctionGet(host string, functionName string) (*Function, *types.FaasApiResponse, error) {
44 res, err := functionGetApiCall(host, functionName)
45 if err != nil {
46 log.Log.Debugf("Cannot get service from openfaas: %s", err.Error())
47 return nil, res, err
48 }
49
50 var function Function
51 err = json.Unmarshal([]byte(res.Body), &function)
52
53 if err != nil {
54 log.Log.Debugf("Reply is (%d) [%s]: %s", res.StatusCode, res.StatusCode, res.Body)
55 log.Log.Debugf("Cannot decode service from openfaas: %s", err.Error())
56 return nil, res, err
57 }
58 return &function, res, nil
59 }
60
61 func GenFunctionDeploy(host string, function Function) (*types.FaasApiResponse, error) {
62 res, err := functionDeployApiCall(host, function)
63 if err != nil {
64 log.Log.Debugf("Cannot deploy service" + function.Name + ": " + err.Error())
65 }
66 return res, err
67 }
68
69 func GenFunctionExecute(host string, functionName string, payload []byte, contentType string) (*types.FaasApiResponse, error) {
70 var res *types.FaasApiResponse
71 var err error
72
73 if payload == nil {
74 res, err = functionExecuteApiCall(host, functionName)
75 } else {
76 res, err = functionExecutePostApiCall(host, functionName, payload, contentType)
77 }
78
79 if err != nil {
80 return nil, err
81 }
82 if res.StatusCode == 404 {
83 return res, ErrorFunctionNotFound{}
84 }
85 if res.StatusCode >= 500 {
86 return res, ErrorInternal{string(res.Body)}
87 }
88 if res.StatusCode < 200 || res.StatusCode >= 300 {
89 return res, ErrorGeneric{string(res.Body)}
90 }
91 return res, nil
92 }
93
94 func GenFunctionScale(host string, functionName string, replicas uint) (*types.FaasApiResponse, error) {
95 res, err := functionScaleApiCall(host, functionName, replicas)
96 if err != nil {
97 return nil, err
98 }
99 if res.StatusCode == 404 {
100 return res, ErrorFunctionNotFound{}
101 }
102 if res.StatusCode >= 500 {
103 return res, ErrorInternal{string(res.Body)}
104 }
105 if res.StatusCode < 200 || res.StatusCode >= 300 {
106 return res, ErrorGeneric{string(res.Body)}
107 }
108 return res, nil
109 }
110
111 func GenFunctionScaleByOne(host string, functionName string) (*types.FaasApiResponse, error) {
112 reps, err := GenFunctionGetReplicas(host, functionName)
113 if err != nil {
114 log.Log.Debugf("Could not scale by one service %s: %s", functionName, err.Error())
115 return nil, err
116 }
117 return functionScaleApiCall(host, functionName, reps+1)
118 }
119
120 func GenFunctionScaleDownByOne(host string, functionName string) (*types.FaasApiResponse, error) {
121 reps, err := GenFunctionGetReplicas(host, functionName)
122 if err != nil {
123 log.Log.Debugf("Could not scale down by one service %s: %s", functionName, err.Error())
124 return nil, err
125 }
126 return functionScaleApiCall(host, functionName, reps-1)
127 }
128
View as plain text