package cd.casic.module.execute.docker.api;

import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.module.execute.docker.DockerClientFactory;
import cd.casic.module.execute.docker.dao.DockerEndpointDao;
import cd.casic.module.execute.docker.dataobject.convert.DockerEndpointConvert;
import cd.casic.module.execute.docker.dataobject.model.DockerEndpoint;
import com.github.dockerjava.api.DockerClient;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 容器的管理功能
 * @author: mianbin
 * @date: 2025/5/26 11:14
 * @version: 1.0
 */
@Tag(name = "docker管理 - 获取docker列表")
@RestController
@RequestMapping("/api/dockerClient")
@RequiredArgsConstructor
public class DockerClientController {

    private final DockerEndpointDao dockerEndpointDao;
    private final DockerClientFactory dockerClientFactory;

//    @GetMapping
    public CommonResult<List<DockerEndpoint>> list() {
        List<DockerEndpoint> collect = dockerEndpointDao
                .selectList()
                .stream()
                .map(DockerEndpointConvert.INSTANCE::convert)
                .toList();
        return CommonResult.success(collect);
    }

//    @GetMapping
    public CommonResult<Boolean> testDockerClient(String dockerClientId) {
        DockerClient dockerClient = dockerClientFactory.getdockerClient(dockerClientId);
        dockerClient.pingCmd().exec();
        return CommonResult.success(Boolean.TRUE);
    }

}
