diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index 612ca9f935..da3a610400 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -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 diff --git a/pkg/baremetal/pxe/dhcp.go b/pkg/baremetal/pxe/dhcp.go index 5a00401327..73daf9b8b8 100644 --- a/pkg/baremetal/pxe/dhcp.go +++ b/pkg/baremetal/pxe/dhcp.go @@ -348,7 +348,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 { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 351ce7f719..e4f2f5c915 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -374,45 +374,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") @@ -434,15 +425,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) } diff --git a/pkg/compute/models/netinterfaces.go b/pkg/compute/models/netinterfaces.go index 9b6193fd21..556d40e5da 100644 --- a/pkg/compute/models/netinterfaces.go +++ b/pkg/compute/models/netinterfaces.go @@ -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) { diff --git a/pkg/util/stringutils2/stringutils.go b/pkg/util/stringutils2/stringutils.go index b6bdb86b8c..dcfda0abba 100644 --- a/pkg/util/stringutils2/stringutils.go +++ b/pkg/util/stringutils2/stringutils.go @@ -242,3 +242,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 +} diff --git a/pkg/util/stringutils2/stringutils_test.go b/pkg/util/stringutils2/stringutils_test.go index bd5c1c926d..ac6d772fa7 100644 --- a/pkg/util/stringutils2/stringutils_test.go +++ b/pkg/util/stringutils2/stringutils_test.go @@ -237,3 +237,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) + } + } +}