fix: hostfile delete validation (#22492)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2025-05-08 23:49:57 +08:00
committed by GitHub
parent 84199a13b8
commit 2c55d15fc2
2 changed files with 31 additions and 0 deletions

View File

@@ -138,3 +138,14 @@ func (manager *SHostFileManager) FetchCustomizeColumns(
}
return rows
}
func (hostfile *SHostFile) ValidateDeleteCondition(ctx context.Context, info api.HostFileDetails) error {
err := hostfile.SInfrasResourceBase.ValidateDeleteCondition(ctx, jsonutils.Marshal(info))
if err != nil {
return errors.Wrap(err, "SInfrasResourceBase.ValidateDeleteCondition")
}
if len(info.Hosts) > 0 {
return httperrors.NewNotEmptyError("hostfile is used by hosts")
}
return nil
}

View File

@@ -15,6 +15,8 @@
package compute
import (
"os"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/cmd/climc/shell"
@@ -52,17 +54,35 @@ type HostFileUpdateOptions struct {
options.BaseIdOptions
computeapi.HostFileUpdateInput
File string `json:"file"`
}
func (opts *HostFileUpdateOptions) Params() (jsonutils.JSONObject, error) {
if len(opts.File) > 0 {
cont, err := os.ReadFile(opts.File)
if err != nil {
return nil, err
}
opts.Content = string(cont)
}
return jsonutils.Marshal(opts), nil
}
type HostFileCreateOptions struct {
computeapi.HostFileCreateInput
File string `json:"file"`
}
func (opts *HostFileCreateOptions) Params() (jsonutils.JSONObject, error) {
if len(opts.File) > 0 {
cont, err := os.ReadFile(opts.File)
if err != nil {
return nil, err
}
opts.Content = string(cont)
}
return jsonutils.Marshal(opts), nil
}