46 lines
1.0 KiB
Go

package api
import (
"context"
"regexp"
"time"
"deevirt.fr/compute/cmd/compute_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) ListAll(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())
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")
domains = append(domains, &proto.DomainListResponse{
DomainID: matches[index],
})
}
}
return &proto.DomainListAllResponse{
All: domains,
}, nil
}