-
Notifications
You must be signed in to change notification settings - Fork 1
/
gateway_client_api.go
59 lines (50 loc) · 1.96 KB
/
gateway_client_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
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 gateway_health provides a client for using the cloud health service from
// federated gateways. This allows the gateway health manager to send health updates
// without knowing about the RPC implementation.
package gateway_health
import (
"context"
"fmt"
"github.com/golang/glog"
"google.golang.org/grpc"
"github.com/go-magma/magma/gateway/go/service_registry"
"github.com/go-magma/magma/lib/go/errors"
"github.com/go-magma/magma/modules/feg/cloud/go/protos"
)
// getHealthClient is a utility function to get an RPC connection to the
// cloud Health service from the feg
func getHealthClient(cloudRegistry service_registry.GatewayRegistry) (protos.HealthClient, *grpc.ClientConn, error) {
if cloudRegistry == nil {
return nil, nil, fmt.Errorf("Nil cloud registry provided")
}
conn, err := cloudRegistry.GetCloudConnection("HEALTH")
if err != nil {
initErr := errors.NewInitError(err, "HEALTH")
glog.Error(initErr)
return nil, nil, initErr
}
return protos.NewHealthClient(conn), conn, nil
}
// UpdateHealth sends a health update using a HealthRequest to the cloud and returns
// back a health response and any potential error that occurred
func UpdateHealth(cloudReg service_registry.GatewayRegistry, req *protos.HealthRequest) (*protos.HealthResponse, error) {
if req == nil {
return nil, fmt.Errorf("Nil HealthRequest")
}
client, conn, err := getHealthClient(cloudReg)
if err != nil {
return nil, err
}
defer conn.Close()
return client.UpdateHealth(context.Background(), req)
}