netinterface.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 netinterface
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  24. "yunion.io/x/onecloud/pkg/scheduler/data_manager/common"
  25. )
  26. var manager common.IResourceManager[models.SNetInterface]
  27. func GetManager() common.IResourceManager[models.SNetInterface] {
  28. if manager != nil {
  29. return manager
  30. }
  31. manager = NewResourceManager()
  32. return manager
  33. }
  34. func NewResourceManager() common.IResourceManager[models.SNetInterface] {
  35. cm := common.NewCommonResourceManager(
  36. "netinterface",
  37. 10*time.Minute,
  38. NewResourceStore(),
  39. )
  40. return cm
  41. }
  42. func GetId(hostId, wireId, mac string, vlanId int) string {
  43. return fmt.Sprintf("%s/%s/%s/%d", hostId, wireId, mac, vlanId)
  44. }
  45. func NewResourceStore() common.IResourceStore[models.SNetInterface] {
  46. return common.NewJointResourceStore(
  47. models.NetInterfaceManager,
  48. compute.Netinterfaces,
  49. func(o models.SNetInterface) string {
  50. return GetId(o.BaremetalId, o.WireId, o.Mac, o.VlanId)
  51. },
  52. func(o *jsonutils.JSONDict) string {
  53. hostId, _ := o.GetString("baremetal_id")
  54. wireId, _ := o.GetString("wire_id")
  55. mac, _ := o.GetString("mac")
  56. vlan, _ := o.Int("vlan_id")
  57. return GetId(hostId, wireId, mac, int(vlan))
  58. },
  59. func(man db.IModelManager, id string, obj *jsonutils.JSONDict) (db.IModel, error) {
  60. ids := strings.Split(id, "/")
  61. if len(ids) < 3 {
  62. return nil, errors.Errorf("Invalid id: %q", id)
  63. }
  64. q := man.Query()
  65. hostId := ids[0]
  66. wireId := ids[1]
  67. mac := ids[2]
  68. q = q.Equals("baremetal_id", hostId).Equals("wire_id", wireId).Equals("mac", mac)
  69. objs, err := db.FetchIModelObjects(man, q)
  70. errHint := fmt.Sprintf("hostId %q, wireId %q, mac %q", hostId, wireId, mac)
  71. if err != nil {
  72. return nil, errors.Wrapf(err, "db.FetchIModelObjects by %s", errHint)
  73. }
  74. if len(objs) != 1 {
  75. return nil, errors.Errorf("Found %d objects by %s", len(objs), errHint)
  76. }
  77. return objs[0], nil
  78. },
  79. )
  80. }
  81. func GetByHost(hostId string) []models.SNetInterface {
  82. return GetManager().GetStore().GetByPrefix(hostId)
  83. }