modelsets.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 models
  15. import (
  16. "time"
  17. "yunion.io/x/onecloud/pkg/apihelper"
  18. )
  19. type ModelSetsMaxUpdatedAt struct {
  20. ProxyEndpoints time.Time
  21. Forwards time.Time
  22. }
  23. func NewModelSetsMaxUpdatedAt() *ModelSetsMaxUpdatedAt {
  24. return &ModelSetsMaxUpdatedAt{
  25. ProxyEndpoints: apihelper.PseudoZeroTime,
  26. Forwards: apihelper.PseudoZeroTime,
  27. }
  28. }
  29. type ModelSets struct {
  30. ProxyEndpoints ProxyEndpoints
  31. Forwards Forwards
  32. }
  33. func NewModelSets() *ModelSets {
  34. return &ModelSets{
  35. ProxyEndpoints: ProxyEndpoints{},
  36. Forwards: Forwards{},
  37. }
  38. }
  39. func (mss *ModelSets) ModelSetList() []apihelper.IModelSet {
  40. // it's ordered this way to favour creation, not deletion
  41. return []apihelper.IModelSet{
  42. mss.ProxyEndpoints,
  43. mss.Forwards,
  44. }
  45. }
  46. func (mss *ModelSets) NewEmpty() apihelper.IModelSets {
  47. return NewModelSets()
  48. }
  49. func (mss *ModelSets) copy_() *ModelSets {
  50. mssCopy := &ModelSets{
  51. ProxyEndpoints: mss.ProxyEndpoints.Copy().(ProxyEndpoints),
  52. Forwards: mss.Forwards.Copy().(Forwards),
  53. }
  54. return mssCopy
  55. }
  56. func (mss *ModelSets) Copy() apihelper.IModelSets {
  57. return mss.copy_()
  58. }
  59. func (mss *ModelSets) CopyJoined() apihelper.IModelSets {
  60. mssCopy := mss.copy_()
  61. mssCopy.join()
  62. return mssCopy
  63. }
  64. func (mss *ModelSets) ApplyUpdates(mssNews apihelper.IModelSets) apihelper.ModelSetsUpdateResult {
  65. r := apihelper.ModelSetsUpdateResult{
  66. Changed: false,
  67. Correct: true,
  68. }
  69. mssList := mss.ModelSetList()
  70. mssNewsList := mssNews.ModelSetList()
  71. for i, mss := range mssList {
  72. mssNews := mssNewsList[i]
  73. msR := apihelper.ModelSetApplyUpdates(mss, mssNews)
  74. if !r.Changed && msR.Changed {
  75. r.Changed = true
  76. }
  77. }
  78. if r.Changed {
  79. r.Correct = mss.join()
  80. }
  81. return r
  82. }
  83. func (mss *ModelSets) join() bool {
  84. var p []bool
  85. p = append(p, mss.ProxyEndpoints.joinForwards(mss.Forwards))
  86. for _, b := range p {
  87. if !b {
  88. return false
  89. }
  90. }
  91. return true
  92. }