package cn.reghao.devops.web.mgr.machine.model.po;

import cn.reghao.devops.web.util.DefaultSetting;
import cn.reghao.devops.web.admin.util.BaseEntity;
import lombok.*;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author reghao
 * @date 2021-06-29 09:36:13
 */
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "devops_machine_host")
public class MachineHost extends BaseEntity {
    @NotNull
    @OneToOne
    private MachineInfo machineInfo;
    @Column(unique = true, nullable = false)
    private String machineId;
    @NotNull
    private String machineIpv4;
    @NotNull
    private String env;
    private SshAuth sshAuth;

    // TODO 根据机器所属的地区，机房等维度来分组
    public MachineHost(MachineInfo machineInfo) {
        this.machineInfo = machineInfo;
        this.machineId = machineInfo.getMachineId();
        List<NetworkInfo> list = machineInfo.getNetworkInfos();
        if (!list.isEmpty()) {
            this.machineIpv4 = list.get(0).getIpv4();
        } else {
            this.machineIpv4 = "127.0.0.1";
        }
        this.env = DefaultSetting.getDefaultEnv();
    }
}
