|
| 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