111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
package eci
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/eci"
|
|
"gitlink.org.cn/JointCloud/pcm-participant-cloud/container"
|
|
"gitlink.org.cn/JointCloud/pcm-participant-cloud/platform"
|
|
"gitlink.org.cn/JointCloud/pcm-participant-eci/model"
|
|
"gitlink.org.cn/JointCloud/pcm-participant-eci/service"
|
|
)
|
|
|
|
type Eci struct {
|
|
Container *Container
|
|
opt *Option
|
|
}
|
|
|
|
type Container struct {
|
|
opt *Option
|
|
ft *container.Features
|
|
}
|
|
|
|
type Option struct {
|
|
platform *platform.Platform
|
|
client *eci.Client
|
|
}
|
|
|
|
func New(accessKeyId, accessKeySecret, regionId string, id platform.Id) (*Eci, error) {
|
|
config := sdk.NewConfig()
|
|
credential := credentials.NewAccessKeyCredential(accessKeyId, accessKeySecret)
|
|
client, err := eci.NewClientWithOptions(regionId, config, credential)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
opt := &Option{
|
|
client: client,
|
|
platform: &platform.Platform{
|
|
Id: id,
|
|
Type: platform.Eci,
|
|
},
|
|
}
|
|
container := &Container{
|
|
opt: opt,
|
|
}
|
|
return &Eci{opt: opt, Container: container}, nil
|
|
}
|
|
func (e *Eci) Name() string {
|
|
return e.opt.platform.Name
|
|
}
|
|
func (e *Eci) Type() platform.Type {
|
|
return e.opt.platform.Type
|
|
}
|
|
func (e *Eci) Id() platform.Id {
|
|
return e.opt.platform.Id
|
|
}
|
|
func (c *Container) Create(ctx context.Context, param *container.CreateParam) (interface{}, error) {
|
|
if param.Name == "" {
|
|
return nil, errors.New("Name is required")
|
|
}
|
|
if param.Image == "" {
|
|
return nil, errors.New("Image is required")
|
|
}
|
|
if param.ContainerGroupName == "" {
|
|
return nil, errors.New("ContainerGroupName is required")
|
|
}
|
|
cParam := model.CreateContainerParam{
|
|
Containers: &[]eci.CreateContainerGroupContainer{
|
|
{
|
|
Image: param.Image,
|
|
Name: param.Name,
|
|
Cpu: requests.Float(param.Cpu),
|
|
Memory: requests.Float(param.Memory),
|
|
},
|
|
},
|
|
ContainerGroupName: param.ContainerGroupName,
|
|
}
|
|
resp, err := service.CreateContainer(c.opt.client, &cParam)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
func (c *Container) Delete(ctx context.Context, param *container.DeleteParam) error {
|
|
deleteParam := param.DeleteParameter.(*container.EciDeleteParam)
|
|
err := service.DeleteContainer(c.opt.client, &model.DeleteContainerParam{
|
|
RegionId: deleteParam.RegionId,
|
|
ContainerGroupId: deleteParam.ContainerGroupId,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
func (c *Container) Features() *container.Features {
|
|
return nil
|
|
}
|
|
func (c *Container) Get(ctx context.Context, param *container.GetParam) (interface{}, error) {
|
|
getParam := param.GetParameter.(*container.EciGetParam)
|
|
resp, err := service.GetContainer(c.opt.client, &model.GetContainerParam{
|
|
RegionId: getParam.RegionId,
|
|
ContainerGroupName: getParam.ContainerGroupName,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
}
|