-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathservice_server.go
88 lines (73 loc) · 2.23 KB
/
service_server.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package server
import (
"context"
api "github.com/onepanelio/core/api/gen"
v1 "github.com/onepanelio/core/pkg"
"github.com/onepanelio/core/server/auth"
)
// ServiceServer contains actions for installed services
type ServiceServer struct {
api.UnimplementedServiceServiceServer
}
// NewServiceServer creates a new ServiceServer
func NewServiceServer() *ServiceServer {
return &ServiceServer{}
}
func apiService(service *v1.Service) (apiService *api.Service) {
return &api.Service{
Name: service.Name,
Url: service.URL,
}
}
// ListServices returns all of the services in the system
func (c *ServiceServer) ListServices(ctx context.Context, req *api.ListServicesRequest) (*api.ListServicesResponse, error) {
client := getClient(ctx)
allowed, err := auth.IsAuthorized(client, "", "list", "", "onepanel-service", "")
if err != nil || !allowed {
return nil, err
}
services, err := client.ListServices(req.Namespace)
if err != nil {
return nil, err
}
apiServices := make([]*api.Service, len(services))
for i, service := range services {
apiServices[i] = apiService(service)
}
return &api.ListServicesResponse{
Count: int32(len(services)),
Services: apiServices,
Page: 1,
Pages: 1,
TotalCount: int32(len(services)),
}, nil
}
// GetService returns a particular service identified by name
func (c *ServiceServer) GetService(ctx context.Context, req *api.GetServiceRequest) (*api.Service, error) {
client := getClient(ctx)
allowed, err := auth.IsAuthorized(client, "", "get", "", "onepanel-service", "")
if err != nil || !allowed {
return nil, err
}
service, err := client.GetService(req.Namespace, req.Name)
if err != nil {
return nil, err
}
apiService := apiService(service)
return apiService, nil
}
// HasService checks if the cluster has a service set up and enabled
func (c *ServiceServer) HasService(ctx context.Context, req *api.HasServiceRequest) (*api.HasServiceResponse, error) {
client := getClient(ctx)
allowed, err := auth.IsAuthorized(client, "", "get", "", "onepanel-service", "")
if err != nil || !allowed {
return nil, err
}
hasService, err := client.HasService(req.Name)
if err != nil {
return nil, err
}
return &api.HasServiceResponse{
HasService: hasService,
}, nil
}