74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gitlink.org.cn/JointCloud/pcm-participant-cloud/container"
|
|
"gitlink.org.cn/JointCloud/pcm-participant-cloud/platform"
|
|
eci "gitlink.org.cn/JointCloud/pcm-participant-eci"
|
|
k8s "gitlink.org.cn/JointCloud/pcm-participant-k8s"
|
|
|
|
"sync"
|
|
)
|
|
|
|
type Service struct {
|
|
containerMap map[platform.Id]*Container
|
|
rlock sync.Mutex
|
|
dlock sync.Mutex
|
|
tlock sync.Mutex
|
|
}
|
|
|
|
func NewService(platforms ...platform.IPlatform) (*Service, error) {
|
|
containerMap := make(map[platform.Id]*Container)
|
|
for _, pf := range platforms {
|
|
switch pf.Type() {
|
|
case platform.K8s:
|
|
k8s, ok := pf.(*k8s.Kubernetes)
|
|
if !ok {
|
|
|
|
}
|
|
containerMap[pf.Id()] = NewContainer(k8s.Container)
|
|
case platform.Eci:
|
|
eci, ok := pf.(*eci.Eci)
|
|
if !ok {
|
|
|
|
}
|
|
containerMap[pf.Id()] = NewContainer(eci.Container)
|
|
}
|
|
}
|
|
|
|
return &Service{containerMap: containerMap}, nil
|
|
}
|
|
|
|
func (s *Service) CreateContainer(ctx context.Context, pid string, param *container.CreateParam) (interface{}, error) {
|
|
fmt.Println(s.containerMap)
|
|
for id := range s.containerMap {
|
|
fmt.Println(s.containerMap[id])
|
|
}
|
|
svc := s.containerMap[platform.Id(pid)]
|
|
fmt.Println(svc)
|
|
resp, err := svc.Create(ctx, param)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
func (s *Service) DeleteContainer(ctx context.Context, pid string, param *container.DeleteParam) error {
|
|
svc := s.containerMap[platform.Id(pid)]
|
|
err := svc.Delete(ctx, param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) GetContainer(ctx context.Context, pid string, param *container.GetParam) (interface{}, error) {
|
|
svc := s.containerMap[platform.Id(pid)]
|
|
resp, err := svc.Get(ctx, param)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
}
|