Merge pull request #13290 from swordqiu/hotfix/qj-baremetal-match-by-empty-uuid

fix: baremetal mismatch with empty Guid
This commit is contained in:
Zexi Li
2022-01-24 10:46:39 +08:00
committed by GitHub
6 changed files with 67 additions and 52 deletions
+2 -2
View File
@@ -123,8 +123,8 @@ type HostListInput struct {
// 虚拟机所在的二层网络
ServerIdForNetwork string `json:"server_id_for_network"`
// 宿主机 cpu 架构
CpuArchitecture string `json:"cpu_architecture"`
OsArch string `json:"os_arch"`
CpuArchitecture []string `json:"cpu_architecture"`
OsArch string `json:"os_arch"`
// 按虚拟机数量排序
// enum: asc,desc
+1 -1
View File
@@ -349,7 +349,7 @@ func (req *dhcpRequest) createOrUpdateBaremetal(session *mcclient.ClientSession)
return nil, err
}
}
if len(ret.Data) == 0 {
if len(ret.Data) == 0 && len(req.ClientGuid) > 0 {
// try UUID
ret, err = req.findBaremetalsByUuid(session)
if err != nil {
+22 -40
View File
@@ -376,45 +376,36 @@ func (manager *SHostManager) ListItemFilter(
}
}
if len(query.Rack) > 0 {
q = q.In("rack", query.Rack)
fieldQueryMap := map[string][]string{
"rack": query.Rack,
"slots": query.Slots,
"access_mac": query.AccessMac,
"access_ip": query.AccessIp,
"sn": query.SN,
"storage_type": query.StorageType,
"ipmi_ip": query.IpmiIp,
"host_status": query.HostStatus,
"host_type": query.HostType,
"version": query.Version,
"ovn_version": query.OvnVersion,
"uuid": query.Uuid,
"boot_mode": query.BootMode,
"cpu_architecture": query.CpuArchitecture,
}
if len(query.Slots) > 0 {
q = q.In("slots", query.Slots)
}
if len(query.AccessMac) > 0 {
q = q.In("access_mac", query.AccessMac)
}
if len(query.AccessIp) > 0 {
q = q.In("access_ip", query.AccessIp)
}
if len(query.SN) > 0 {
q = q.In("sn", query.SN)
for f, vars := range fieldQueryMap {
vars = stringutils2.FilterEmpty(vars)
if len(vars) > 0 {
q = q.In(f, vars)
}
}
if len(query.CpuCount) > 0 {
q = q.In("cpu_count", query.CpuCount)
}
if len(query.MemSize) > 0 {
q = q.In("mem_size", query.MemSize)
}
if len(query.StorageType) > 0 {
q = q.In("storage_type", query.StorageType)
}
if len(query.IpmiIp) > 0 {
q = q.In("ipmi_ip", query.IpmiIp)
}
if len(query.HostStatus) > 0 {
q = q.In("host_status", query.HostStatus)
}
if len(query.HostType) > 0 {
q = q.In("host_type", query.HostType)
}
if len(query.Version) > 0 {
q = q.In("version", query.Version)
}
if len(query.OvnVersion) > 0 {
q = q.In("ovn_version", query.OvnVersion)
}
if query.IsMaintenance != nil {
if *query.IsMaintenance {
q = q.IsTrue("is_maintenance")
@@ -436,15 +427,6 @@ func (manager *SHostManager) ListItemFilter(
q = q.IsFalse("enable_pxe_boot")
}
}
if len(query.Uuid) > 0 {
q = q.In("uuid", query.Uuid)
}
if len(query.BootMode) > 0 {
q = q.In("boot_mode", query.BootMode)
}
if len(query.CpuArchitecture) > 0 {
q = q.Equals("cpu_architecture", query.CpuArchitecture)
}
if len(query.OsArch) > 0 {
q = db.ListQueryByArchitecture(q, "cpu_architecture", query.OsArch)
}
+12 -9
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/regutils"
api "yunion.io/x/onecloud/pkg/apis/compute"
@@ -250,16 +251,18 @@ func (self *SNetInterface) Remove(ctx context.Context, userCred mcclient.TokenCr
wire := self.GetWire()
if host != nil && wire != nil {
hw, err := HostwireManager.FetchByHostIdAndMac(host.Id, self.Mac)
if err != nil {
if err != nil && errors.Cause(err) != sql.ErrNoRows {
log.Errorf("NetInterface remove HostwireManager.FetchByIds error %s", err)
return err
return errors.Wrap(err, "HostwireManager.FetchByHostIdAndMac")
}
if hw.WireId != wire.Id {
return fmt.Errorf("NetInterface not attached to this wire???")
}
err = hw.Delete(ctx, userCred)
if err != nil {
return err
if hw != nil {
if hw.WireId != wire.Id {
return fmt.Errorf("NetInterface not attached to this wire???")
}
err = hw.Delete(ctx, userCred)
if err != nil {
return errors.Wrap(err, "hw.Delete")
}
}
}
_, err := db.Update(self, func() error {
@@ -275,7 +278,7 @@ func (self *SNetInterface) Remove(ctx context.Context, userCred mcclient.TokenCr
if err != nil {
log.Errorf("Save Updates: %s", err)
}
return err
return errors.Wrap(err, "db.Update")
}
func (self *SNetInterface) GetCandidateNetworkForIp(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, ipAddr string) (*SNetwork, error) {
+10
View File
@@ -177,3 +177,13 @@ func GenerateRoleName(roleName string) string {
}
return ret
}
func FilterEmpty(input []string) []string {
ret := make([]string, 0)
for i := range input {
if len(input[i]) > 0 {
ret = append(ret, input[i])
}
}
return ret
}
+20
View File
@@ -191,3 +191,23 @@ func TestRoleName(t *testing.T) {
}
}
}
func TestFilterEmpty(t *testing.T) {
cases := []struct {
input []string
want []string
}{
{
input: []string{
"",
},
want: []string{},
},
}
for _, c := range cases {
got := FilterEmpty(c.input)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("want: %#v got: %#v", c.want, got)
}
}
}