proxy_matches.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/sqlchemy"
  20. api "yunion.io/x/onecloud/pkg/apis/cloudproxy"
  21. cloudproxy_api "yunion.io/x/onecloud/pkg/apis/cloudproxy"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type SProxyMatch struct {
  27. db.SVirtualResourceBase
  28. ProxyEndpointId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" update:"user"`
  29. MatchScope string `width:"16" charset:"ascii" nullable:"false" list:"user" create:"required" update:"user"`
  30. MatchValue string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" update:"user"`
  31. }
  32. type SProxyMatchManager struct {
  33. db.SVirtualResourceBaseManager
  34. }
  35. var ProxyMatchManager *SProxyMatchManager
  36. func init() {
  37. ProxyMatchManager = &SProxyMatchManager{
  38. SVirtualResourceBaseManager: db.NewVirtualResourceBaseManager(
  39. SProxyMatch{},
  40. "proxy_matches_tbl",
  41. "proxy_match",
  42. "proxy_matches",
  43. ),
  44. }
  45. ProxyMatchManager.SetVirtualObject(ProxyMatchManager)
  46. }
  47. func (man *SProxyMatchManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
  48. matchScopeV := validators.NewStringChoicesValidator("match_scope", api.PM_SCOPES)
  49. endpointV := validators.NewModelIdOrNameValidator("proxy_endpoint", ProxyEndpointManager.Keyword(), ownerId)
  50. for _, v := range []validators.IValidator{
  51. matchScopeV,
  52. endpointV,
  53. } {
  54. if err := v.Validate(ctx, data); err != nil {
  55. return nil, err
  56. }
  57. }
  58. return data, nil
  59. }
  60. func (pm *SProxyMatch) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
  61. matchScopeV := validators.NewStringChoicesValidator("match_scope", api.PM_SCOPES)
  62. endpointV := validators.NewModelIdOrNameValidator("proxy_endpoint", ProxyEndpointManager.Keyword(), userCred)
  63. for _, v := range []validators.IValidator{
  64. matchScopeV,
  65. endpointV,
  66. } {
  67. v.Optional(true)
  68. if err := v.Validate(ctx, data); err != nil {
  69. return nil, err
  70. }
  71. }
  72. return data, nil
  73. }
  74. func (man *SProxyMatchManager) findMatch(ctx context.Context, networkId, vpcId string) *SProxyMatch {
  75. q := man.Query()
  76. qfScope := q.Field("match_scope")
  77. qfValue := q.Field("match_value")
  78. q = q.Filter(
  79. sqlchemy.OR(
  80. sqlchemy.AND(
  81. sqlchemy.Equals(qfScope, api.PM_SCOPE_VPC),
  82. sqlchemy.Equals(qfValue, vpcId),
  83. ),
  84. sqlchemy.AND(
  85. sqlchemy.Equals(qfScope, api.PM_SCOPE_NETWORK),
  86. sqlchemy.Equals(qfValue, networkId),
  87. ),
  88. ),
  89. )
  90. var pms []SProxyMatch
  91. if err := db.FetchModelObjects(man, q, &pms); err != nil {
  92. return nil
  93. }
  94. var r *SProxyMatch
  95. for _, pm := range pms {
  96. if pm.MatchScope == api.PM_SCOPE_NETWORK {
  97. return &pm
  98. }
  99. r = &pm
  100. }
  101. return r
  102. }
  103. func (man *SProxyMatchManager) ListItemFilter(
  104. ctx context.Context,
  105. q *sqlchemy.SQuery,
  106. userCred mcclient.TokenCredential,
  107. input cloudproxy_api.ProxyMatchListInput,
  108. ) (*sqlchemy.SQuery, error) {
  109. q, err := man.SVirtualResourceBaseManager.ListItemFilter(ctx, q, userCred, input.VirtualResourceListInput)
  110. if err != nil {
  111. return nil, errors.Wrap(err, "SVirtualResourceBaseManager.ListItemFilter")
  112. }
  113. if len(input.ProxyEndpointId) > 0 {
  114. _, err := validators.ValidateModel(ctx, userCred, ProxyEndpointManager, &input.ProxyEndpointId)
  115. if err != nil {
  116. return nil, err
  117. }
  118. q = q.Equals("proxy_endpoint_id", input.ProxyEndpointId)
  119. }
  120. return q, nil
  121. }