proxy_agents.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/onecloud/pkg/cloudcommon/db"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  20. "yunion.io/x/onecloud/pkg/httperrors"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. )
  23. // bind_addr, default 0.0.0.0
  24. // advertise_addr, default default route adddr, maybe k8s cluster ip
  25. type SProxyAgent struct {
  26. db.SStandaloneResourceBase
  27. BindAddr string `width:"16" charset:"ascii" nullable:"false" list:"user" create:"optional" update:"admin"`
  28. AdvertiseAddr string `width:"16" charset:"ascii" nullable:"false" list:"user" create:"optional" update:"admin"`
  29. }
  30. type SProxyAgentManager struct {
  31. db.SStandaloneResourceBaseManager
  32. }
  33. var ProxyAgentManager *SProxyAgentManager
  34. func init() {
  35. ProxyAgentManager = &SProxyAgentManager{
  36. SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
  37. SProxyAgent{},
  38. "proxy_agents_tbl",
  39. "proxy_agent",
  40. "proxy_agents",
  41. ),
  42. }
  43. ProxyAgentManager.SetVirtualObject(ProxyAgentManager)
  44. }
  45. func (man *SProxyAgentManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
  46. vs := []validators.IValidator{
  47. validators.NewIPv4AddrValidator("bind_addr").Optional(true),
  48. validators.NewIPv4AddrValidator("advertise_addr").Optional(true),
  49. }
  50. for _, v := range vs {
  51. if err := v.Validate(ctx, data); err != nil {
  52. return nil, err
  53. }
  54. }
  55. return data, nil
  56. }
  57. func (proxyagent *SProxyAgent) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
  58. vs := []validators.IValidator{
  59. validators.NewIPv4AddrValidator("bind_addr"),
  60. validators.NewIPv4AddrValidator("advertise_addr"),
  61. }
  62. for _, v := range vs {
  63. v.Optional(true)
  64. if err := v.Validate(ctx, data); err != nil {
  65. return nil, err
  66. }
  67. }
  68. return data, nil
  69. }
  70. func (proxyagent *SProxyAgent) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
  71. q := ForwardManager.Query().Equals("proxy_agent_id", proxyagent.Id)
  72. if count, err := q.CountWithError(); err != nil {
  73. return httperrors.NewServerError("count forwards using proxy endpoint %s(%s)",
  74. proxyagent.Name, proxyagent.Id)
  75. } else if count > 0 {
  76. return httperrors.NewConflictError("proxy endpoint %s(%s) is still used by %d forward(s)",
  77. proxyagent.Name, proxyagent.Id, count)
  78. } else {
  79. return nil
  80. }
  81. }
  82. func (man *SProxyAgentManager) allAgents(ctx context.Context) ([]SProxyAgent, error) {
  83. var (
  84. agents []SProxyAgent
  85. q = man.Query()
  86. )
  87. if err := db.FetchModelObjects(man, q, &agents); err != nil {
  88. return nil, httperrors.NewServerError("query forwards by agent failed: %v", err)
  89. }
  90. return agents, nil
  91. }