Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions dodevops-api/.idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions dodevops-api/.idea/dodevops-api.iml

This file was deleted.

8 changes: 0 additions & 8 deletions dodevops-api/.idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions dodevops-api/.idea/vcs.xml

This file was deleted.

124 changes: 124 additions & 0 deletions dodevops-api/api/task/controller/configansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package controller

import (
"dodevops-api/api/task/service"
"dodevops-api/common/result"
"strconv"

"github.com/gin-gonic/gin"
)

type ConfigAnsibleController struct {
service service.IConfigAnsibleService
}

func NewConfigAnsibleController(service service.IConfigAnsibleService) *ConfigAnsibleController {
return &ConfigAnsibleController{service: service}
}

// Create 创建配置
// @Summary 创建Ansible配置
// @Description 创建Inventory/Vars/Args等配置
// @Tags 配置管理
// @Accept json
// @Produce json
// @Param request body service.CreateConfigRequest true "创建配置请求"
// @Success 200 {object} result.Result{data=model.ConfigAnsible}
// @Router /api/v1/config/ansible [post]
// @Security ApiKeyAuth
func (c *ConfigAnsibleController) Create(ctx *gin.Context) {
var req service.CreateConfigRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
result.Failed(ctx, 400, "参数错误: "+err.Error())
return
}
c.service.Create(ctx, &req)
}

// Update 更新配置
// @Summary 更新Ansible配置
// @Description 更新配置内容
// @Tags 配置管理
// @Accept json
// @Produce json
// @Param id path int true "配置ID"
// @Param request body service.UpdateConfigRequest true "更新配置请求"
// @Success 200 {object} result.Result{data=model.ConfigAnsible}
// @Router /api/v1/config/ansible/{id} [put]
// @Security ApiKeyAuth
func (c *ConfigAnsibleController) Update(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
result.Failed(ctx, 400, "无效的ID")
return
}
var req service.UpdateConfigRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
result.Failed(ctx, 400, "参数错误: "+err.Error())
return
}
c.service.Update(ctx, uint(id), &req)
}

// Delete 删除配置
// @Summary 删除Ansible配置
// @Description 删除指定的配置
// @Tags 配置管理
// @Accept json
// @Produce json
// @Param id path int true "配置ID"
// @Success 200 {object} result.Result
// @Router /api/v1/config/ansible/{id} [delete]
// @Security ApiKeyAuth
func (c *ConfigAnsibleController) Delete(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
result.Failed(ctx, 400, "无效的ID")
return
}
c.service.Delete(ctx, uint(id))
}

// Get 获取配置详情
// @Summary 获取Ansible配置详情
// @Description 根据ID获取配置详情
// @Tags 配置管理
// @Accept json
// @Produce json
// @Param id path int true "配置ID"
// @Success 200 {object} result.Result{data=model.ConfigAnsible}
// @Router /api/v1/config/ansible/{id} [get]
// @Security ApiKeyAuth
func (c *ConfigAnsibleController) Get(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
result.Failed(ctx, 400, "无效的ID")
return
}
c.service.Get(ctx, uint(id))
}

// List 获取配置列表
// @Summary 获取Ansible配置列表
// @Description 分页获取配置列表,支持按名称和类型过滤
// @Tags 配置管理
// @Accept json
// @Produce json
// @Param page query int false "页码" default(1)
// @Param size query int false "每页数量" default(10)
// @Param name query string false "配置名称(模糊查询)"
// @Param type query int false "配置类型(1-inventory 2-global_vars 3-extra_vars 4-cli_args)"
// @Success 200 {object} result.Result{data=dao.ListResponse}
// @Router /api/v1/config/ansible [get]
// @Security ApiKeyAuth
func (c *ConfigAnsibleController) List(ctx *gin.Context) {
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(ctx.DefaultQuery("size", "10"))
name := ctx.Query("name")
configType, _ := strconv.Atoi(ctx.Query("type"))

c.service.List(ctx, page, size, name, configType)
}
69 changes: 69 additions & 0 deletions dodevops-api/api/task/dao/configansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dao

import (
"dodevops-api/api/task/model"

"gorm.io/gorm"
)

type ConfigAnsibleDao struct {
DB *gorm.DB
}

func NewConfigAnsibleDao(db *gorm.DB) *ConfigAnsibleDao {
return &ConfigAnsibleDao{DB: db}
}

// Create 创建配置
func (d *ConfigAnsibleDao) Create(config *model.ConfigAnsible) error {
return d.DB.Create(config).Error
}

// Update 更新配置
func (d *ConfigAnsibleDao) Update(config *model.ConfigAnsible) error {
return d.DB.Save(config).Error
}

// Delete 删除配置
func (d *ConfigAnsibleDao) Delete(id uint) error {
return d.DB.Delete(&model.ConfigAnsible{}, id).Error
}

// GetByID 根据ID获取配置
func (d *ConfigAnsibleDao) GetByID(id uint) (*model.ConfigAnsible, error) {
var config model.ConfigAnsible
err := d.DB.First(&config, id).Error
return &config, err
}

// ListResponse 列表响应结构
type ListResponse struct {
List []model.ConfigAnsible `json:"list"`
Total int64 `json:"total"`
}

// List 获取配置列表(支持多条件查询)
func (d *ConfigAnsibleDao) List(page, size int, name string, configType int) (*ListResponse, error) {
var configs []model.ConfigAnsible
var total int64
db := d.DB.Model(&model.ConfigAnsible{})

if name != "" {
db = db.Where("name LIKE ?", "%"+name+"%")
}
if configType > 0 {
db = db.Where("type = ?", configType)
}

err := db.Count(&total).
Offset((page - 1) * size).
Limit(size).
Order("id desc").
Find(&configs).Error

if err != nil {
return nil, err
}

return &ListResponse{List: configs, Total: total}, nil
}
20 changes: 20 additions & 0 deletions dodevops-api/api/task/model/configansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package model

import "time"

// ConfigAnsible Ansible配置中心
type ConfigAnsible struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:'主键ID'"`
Name string `gorm:"size:100;not null;uniqueIndex:uk_config_ansible_name;comment:'配置名称'"`
Type int `gorm:"not null;index:idx_config_ansible_type;comment:'1-inventory 2-global_vars 3-extra_vars 4-cli_args'"`
Content string `gorm:"type:longtext;not null;comment:'内容:inventory为文本,vars/args为JSON'"`
Remark string `gorm:"size:500;comment:'备注'"`
CreatedBy string `gorm:"size:50;comment:'创建人'"`
UpdatedBy string `gorm:"size:50;comment:'更新人'"`
CreatedAt time.Time `gorm:"not null;comment:'创建时间'"`
UpdatedAt time.Time `gorm:"not null;comment:'更新时间'"`
}

func (ConfigAnsible) TableName() string {
return "config_ansible"
}
43 changes: 43 additions & 0 deletions dodevops-api/api/task/model/taskansiblehistory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package model

import "time"

// TaskAnsibleHistory 任务执行历史记录主表
type TaskAnsibleHistory struct {
ID uint `gorm:"primaryKey;comment:'主键ID'"`
TaskID uint `gorm:"not null;index:idx_history_task_id;comment:'关联的任务ID'"`
UniqId string `gorm:"size:50;not null;comment:'任务唯一标识(每次执行生成)'"`
Status int `json:"status" gorm:"not null;default:1;comment:'执行状态:1-等待中,2-运行中,3-成功,4-异常'"`
ErrorMsg string `gorm:"type:text;comment:'错误信息'"`
TotalDuration int `gorm:"not null;default:0;comment:'任务执行总耗时(秒)'"`
Trigger int `gorm:"not null;default:1;comment:'触发方式:1-手动,2-定时,3-API'"`
OperatorID uint `gorm:"comment:'操作人ID'"`
OperatorName string `gorm:"size:50;comment:'操作人姓名'"`
StartedAt *time.Time
FinishedAt *time.Time
CreatedAt time.Time

TaskAnsible *TaskAnsible `gorm:"foreignKey:TaskID"`
WorkHistories []TaskAnsibleworkHistory `gorm:"foreignKey:HistoryID"`
}

func (TaskAnsibleHistory) TableName() string {
return "task_ansible_history"
}

// TaskAnsibleworkHistory 任务执行历史记录详情表(对应每个host的执行结果)
type TaskAnsibleworkHistory struct {
ID uint `gorm:"primaryKey;comment:'主键ID'"`
HistoryID uint `gorm:"not null;index:idx_work_history_id;comment:'关联的历史记录ID'"`
TaskID uint `gorm:"not null;comment:'关联的任务ID'"` // 为了方便查询保留
WorkID uint `gorm:"comment:'关联的WorkID(如果有)'"`
HostName string `gorm:"size:255;not null;comment:'主机名/IP'"`
Status int `gorm:"not null;default:1;comment:'状态:1-等待,2-执行中,3-成功,4-失败,5-跳过'"`
LogPath string `gorm:"size:255;comment:'日志文件路径'"`
Duration int `gorm:"not null;default:0;comment:'耗时(秒)'"`
CreatedAt time.Time
}

func (TaskAnsibleworkHistory) TableName() string {
return "task_ansiblework_history"
}
15 changes: 15 additions & 0 deletions dodevops-api/api/task/model/taskansibleview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import "time"

// TaskAnsibleView Ansible任务视图表
type TaskAnsibleView struct {
ID uint `gorm:"primaryKey;comment:'主键ID'"`
Name string `gorm:"size:100;not null;uniqueIndex;comment:'视图名称'"`
CreatedAt time.Time `gorm:"autoCreateTime;comment:'创建时间'"`
UpdatedAt time.Time `gorm:"autoUpdateTime;comment:'更新时间'"`
}

func (TaskAnsibleView) TableName() string {
return "task_ansible_view"
}
Loading