archs.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package db
  15. import (
  16. "context"
  17. "yunion.io/x/sqlchemy"
  18. "yunion.io/x/onecloud/pkg/apis"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. )
  21. // +onecloud:model-api-gen
  22. type SMultiArchResourceBase struct {
  23. // 操作系统 CPU 架构
  24. // example: x86 arm
  25. OsArch string `width:"16" charset:"ascii" nullable:"true" list:"user" get:"user" create:"optional" update:"domain"`
  26. }
  27. type SMultiArchResourceBaseManager struct{}
  28. func (manager *SMultiArchResourceBaseManager) ListItemFilter(
  29. ctx context.Context,
  30. q *sqlchemy.SQuery,
  31. userCred mcclient.TokenCredential,
  32. query apis.MultiArchResourceBaseListInput,
  33. ) (*sqlchemy.SQuery, error) {
  34. return ListQueryByArchitecture(q, "os_arch", query.OsArch), nil
  35. }
  36. func ListQueryByArchitecture(q *sqlchemy.SQuery, fieldKey string, archs []string) *sqlchemy.SQuery {
  37. if len(archs) == 0 {
  38. return q
  39. }
  40. conditions := []sqlchemy.ICondition{}
  41. for _, arch := range archs {
  42. if len(arch) == 0 {
  43. continue
  44. }
  45. if arch == apis.OS_ARCH_X86 {
  46. conditions = append(conditions, sqlchemy.OR(
  47. sqlchemy.Startswith(q.Field(fieldKey), arch),
  48. sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_I386),
  49. sqlchemy.IsNullOrEmpty(q.Field(fieldKey)),
  50. ))
  51. } else if arch == apis.OS_ARCH_ARM {
  52. conditions = append(conditions, sqlchemy.OR(
  53. sqlchemy.Startswith(q.Field(fieldKey), arch),
  54. sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_AARCH32),
  55. sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_AARCH64),
  56. ))
  57. } else if arch == apis.OS_ARCH_RISCV {
  58. conditions = append(conditions, sqlchemy.OR(
  59. sqlchemy.Startswith(q.Field(fieldKey), arch),
  60. sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_RISCV32),
  61. sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_RISCV64),
  62. ))
  63. } else {
  64. conditions = append(conditions, sqlchemy.Startswith(q.Field(fieldKey), arch))
  65. }
  66. }
  67. if len(conditions) > 0 {
  68. q = q.Filter(sqlchemy.OR(conditions...))
  69. }
  70. return q
  71. }