L'API est réunifié avec le manager (mgr)

This commit is contained in:
Mickael BOURNEUF 2025-02-16 14:54:43 +01:00
parent 0d996b202e
commit 6a9370b30a
3 changed files with 0 additions and 150 deletions

View File

@ -1,93 +0,0 @@
package api
import (
"context"
"regexp"
"strconv"
"time"
"deevirt.fr/compute/pkg/api/proto"
"deevirt.fr/compute/pkg/config"
clientv3 "go.etcd.io/etcd/client/v3"
)
type Domain struct {
Config *config.Config
Etcd *clientv3.Client
proto.UnimplementedDomainServer
}
func (d *Domain) List(ctx context.Context, in *proto.DomainListAllRequest) (*proto.DomainListAllResponse, error) {
var domains = []*proto.DomainListResponse{}
ctx_etcd, cancel := context.WithTimeout(context.Background(), 5*time.Second)
resp, _ := d.Etcd.Get(ctx_etcd, "/cluster/"+d.Config.ClusterID+"/domain", clientv3.WithPrefix(), clientv3.WithKeysOnly())
cancel()
re := regexp.MustCompile(`domain/(?P<domainID>[a-zA-Z1-9-]+)$`)
for _, data := range resp.Kvs {
key := string(data.Key[:])
if re.MatchString(key) {
matches := re.FindStringSubmatch(key)
index := re.SubexpIndex("domainID")
domain, _ := d.Get(context.Background(), &proto.DomainListRequest{
DomainId: matches[index],
})
domains = append(domains, domain)
}
}
return &proto.DomainListAllResponse{
Domains: domains,
}, nil
}
func (d *Domain) Get(ctx context.Context, in *proto.DomainListRequest) (*proto.DomainListResponse, error) {
ctx_etcd, cancel := context.WithTimeout(context.Background(), 5*time.Second)
resp_config, _ := d.Etcd.Get(ctx_etcd, "/cluster/"+d.Config.ClusterID+"/domain/"+in.DomainId)
resp_state, _ := d.Etcd.Get(ctx_etcd, "/cluster/"+d.Config.ClusterID+"/domain/"+in.DomainId+"/state")
cancel()
state, _ := strconv.ParseInt(string(resp_state.Kvs[0].Value), 10, 64)
return &proto.DomainListResponse{
DomainId: in.DomainId,
Config: string(resp_config.Kvs[0].Value),
State: state,
}, nil
}
func (d *Domain) Create(ctx context.Context, in *proto.DomainCreateRequest) (*proto.DomainCreateResponse, error) {
ctx_etcd, cancel := context.WithTimeout(context.Background(), 5*time.Second)
resp_config, _ := d.Etcd.Get(ctx_etcd, "/cluster/"+d.Config.ClusterID)
cancel()
println(string(resp_config.Kvs[0].Value))
/*if d.Config.LibvirtTLS {
libvirt_uri := "qemu+tls://"++"/system"
}
conn, err := libvirt.NewConnect("qemu:///system")
if err != nil {
log.Println("Connexion Error")
}
defer conn.Close()*/
/*
async def Create(self, request, context):
yield domain_pb2.DomainCreateResponse(progress=40)
async with Libvirt() as libvirt:
if await libvirt.define(request.config.decode()):
yield domain_pb2.DomainCreateResponse(progress=100)
else:
context.set_code(grpc.StatusCode.ALREADY_EXISTS)
*/
return &proto.DomainCreateResponse{}, nil
}

View File

@ -1,44 +0,0 @@
package api
import (
"log"
"net"
"strings"
"time"
"deevirt.fr/compute/pkg/api/proto"
"deevirt.fr/compute/pkg/config"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func Server() {
config, _ := config.New()
//listen on the port
lis, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalf("Failed to start server %v", err)
}
etcd, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(config.EtcdURI, ","),
DialTimeout: 5 * time.Second,
})
if err != nil {
log.Fatalf("Error connexion to etcd: %v", err)
}
defer etcd.Close()
grpcServer := grpc.NewServer()
proto.RegisterDomainServer(grpcServer, &Domain{
Config: config,
Etcd: etcd,
})
reflection.Register(grpcServer)
log.Printf("Server started at %v", lis.Addr())
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Failed to start: %v", err)
}
}

View File

@ -1,13 +0,0 @@
package main
/*
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative *.pro
*/
import (
"deevirt.fr/compute/cmd/compute_api/api"
)
func main() {
api.Server()
}