Skip to content

Commit 58f8c9a

Browse files
WIP runtime cell client implementation
1 parent f44c587 commit 58f8c9a

3 files changed

Lines changed: 182 additions & 7 deletions

File tree

client/client.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"context"
3535
"crypto/tls"
3636
"crypto/x509"
37+
"errors"
3738
"fmt"
3839
"log"
3940
"net"
@@ -43,13 +44,19 @@ import (
4344
"google.golang.org/grpc/credentials"
4445

4546
"github.com/aurae-runtime/ae/client/pkg/config"
47+
"github.com/aurae-runtime/ae/client/pkg/runtime"
4648
)
4749

48-
type AuraeClient interface{}
50+
var ErrOptionNotAvailable = errors.New("option is not available")
51+
52+
type AuraeClient interface {
53+
Runtime() (runtime.Runtime, error)
54+
}
4955

5056
type auraeClient struct {
51-
cfg *config.Configs
52-
conn grpc.ClientConnInterface
57+
cfg *config.Configs
58+
conn grpc.ClientConnInterface
59+
runtime runtime.Runtime
5360
}
5461

5562
func New(ctx context.Context, cfg ...config.Config) (AuraeClient, error) {
@@ -80,9 +87,17 @@ func New(ctx context.Context, cfg ...config.Config) (AuraeClient, error) {
8087
return nil, err
8188
}
8289

90+
r, err := runtime.New(ctx, conn)
91+
if err != nil {
92+
log.Fatal("Cannot crete runtime client", err)
93+
94+
return nil, err
95+
}
96+
8397
c := &auraeClient{
84-
cfg: cf,
85-
conn: conn,
98+
cfg: cf,
99+
conn: conn,
100+
runtime: r,
86101
}
87102

88103
return c, nil
@@ -112,3 +127,11 @@ func loadTLSCredentials(auth config.Auth) (credentials.TransportCredentials, err
112127

113128
return credentials.NewTLS(config), nil
114129
}
130+
131+
func (c *auraeClient) Runtime() (runtime.Runtime, error) {
132+
if c.runtime == nil {
133+
return nil, fmt.Errorf("configuration: %w", ErrOptionNotAvailable)
134+
}
135+
136+
return c.runtime, nil
137+
}

client/client_test.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,60 @@ package client
3333
import (
3434
"context"
3535
"testing"
36+
37+
runtimev0 "github.com/aurae-runtime/ae/client/pkg/api/v0/runtime"
38+
)
39+
40+
const (
41+
testCellName = "MyBrandNewCell"
3642
)
3743

38-
func TestClient(t *testing.T) {
39-
_, err := New(context.TODO())
44+
func TestClientRuntimeAllocate(t *testing.T) {
45+
c, err := New(context.Background())
46+
if err != nil {
47+
t.Errorf("Cannot create client")
48+
}
49+
50+
req := &runtimev0.CellServiceAllocateRequest{
51+
Cell: &runtimev0.Cell{
52+
Name: testCellName,
53+
CpuQuota: 400000,
54+
CpuShares: 2,
55+
},
56+
}
57+
58+
r, err := c.Runtime()
59+
if err != nil {
60+
t.Errorf("Runtime service not available")
61+
}
62+
63+
rsp, err := r.AllocateCell(context.Background(), req)
64+
if err != nil {
65+
t.Errorf("AllocateCell should have NOT returned an error")
66+
}
67+
68+
if rsp.CellName != testCellName {
69+
t.Error("Cell name wrong")
70+
}
71+
}
72+
73+
func TestClientRuntimeFree(t *testing.T) {
74+
c, err := New(context.Background())
4075
if err != nil {
4176
t.Errorf("Cannot create client")
4277
}
78+
79+
req := &runtimev0.CellServiceFreeRequest{
80+
CellName: testCellName,
81+
}
82+
83+
r, err := c.Runtime()
84+
if err != nil {
85+
t.Errorf("Runtime service not available")
86+
}
87+
88+
err = r.FreeCell(context.Background(), req)
89+
if err != nil {
90+
t.Errorf("FreeCell should have NOT returned an error")
91+
}
4392
}

client/pkg/runtime/runtime.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* -------------------------------------------------------------------------- *\
2+
* Apache 2.0 License Copyright © 2022 The Aurae Authors *
3+
* *
4+
* +--------------------------------------------+ *
5+
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
6+
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
7+
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
8+
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
9+
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
10+
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
11+
* +--------------------------------------------+ *
12+
* *
13+
* Distributed Systems Runtime *
14+
* *
15+
* -------------------------------------------------------------------------- *
16+
* *
17+
* Licensed under the Apache License, Version 2.0 (the "License"); *
18+
* you may not use this file except in compliance with the License. *
19+
* You may obtain a copy of the License at *
20+
* *
21+
* http://www.apache.org/licenses/LICENSE-2.0 *
22+
* *
23+
* Unless required by applicable law or agreed to in writing, software *
24+
* distributed under the License is distributed on an "AS IS" BASIS, *
25+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
26+
* See the License for the specific language governing permissions and *
27+
* limitations under the License. *
28+
* *
29+
\* -------------------------------------------------------------------------- */
30+
31+
package runtime
32+
33+
import (
34+
"context"
35+
"log"
36+
37+
"google.golang.org/grpc"
38+
39+
runtimev0 "github.com/aurae-runtime/ae/client/pkg/api/v0/runtime"
40+
)
41+
42+
type Runtime interface {
43+
AllocateCell(context.Context, *runtimev0.CellServiceAllocateRequest) (*runtimev0.CellServiceAllocateResponse, error)
44+
FreeCell(context.Context, *runtimev0.CellServiceFreeRequest) error
45+
StartCell(context.Context, *runtimev0.CellServiceStartRequest) (*runtimev0.CellServiceStartResponse, error)
46+
StopCell(context.Context, *runtimev0.CellServiceStopRequest) error
47+
}
48+
49+
type runtime struct {
50+
cellClient runtimev0.CellServiceClient
51+
}
52+
53+
func New(ctx context.Context, conn grpc.ClientConnInterface) (Runtime, error) {
54+
r := &runtime{
55+
cellClient: runtimev0.NewCellServiceClient(conn),
56+
}
57+
58+
return r, nil
59+
}
60+
61+
func (r *runtime) AllocateCell(ctx context.Context, req *runtimev0.CellServiceAllocateRequest) (*runtimev0.CellServiceAllocateResponse, error) {
62+
rsp, err := r.cellClient.Allocate(ctx, req)
63+
if err != nil {
64+
log.Fatal("Cannot call Allocate ", err)
65+
66+
return nil, err
67+
}
68+
69+
return rsp, nil
70+
}
71+
72+
func (r *runtime) FreeCell(ctx context.Context, req *runtimev0.CellServiceFreeRequest) error {
73+
_, err := r.cellClient.Free(ctx, req)
74+
if err != nil {
75+
log.Fatal("Cannot call Free ", err)
76+
77+
return err
78+
}
79+
80+
return nil
81+
}
82+
83+
func (r *runtime) StartCell(ctx context.Context, req *runtimev0.CellServiceStartRequest) (*runtimev0.CellServiceStartResponse, error) {
84+
rsp, err := r.cellClient.Start(ctx, req)
85+
if err != nil {
86+
log.Fatal("Cannot call Start ", err)
87+
88+
return nil, err
89+
}
90+
91+
return rsp, nil
92+
}
93+
94+
func (r *runtime) StopCell(ctx context.Context, req *runtimev0.CellServiceStopRequest) error {
95+
_, err := r.cellClient.Stop(ctx, req)
96+
if err != nil {
97+
log.Fatal("Cannot call Stop ", err)
98+
99+
return err
100+
}
101+
102+
return nil
103+
}

0 commit comments

Comments
 (0)