nlbservergroupserver.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 aliyun
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SNlbServerGroupServer struct {
  24. multicloud.SResourceBase
  25. AliyunTags
  26. nlbServerGroup *SNlbServerGroup
  27. NlbServer
  28. }
  29. func (server *SNlbServerGroupServer) GetName() string {
  30. return server.ServerId
  31. }
  32. func (server *SNlbServerGroupServer) GetId() string {
  33. return fmt.Sprintf("%s/%s/%d", server.nlbServerGroup.ServerGroupId, server.ServerId, server.Port)
  34. }
  35. func (server *SNlbServerGroupServer) GetGlobalId() string {
  36. return server.GetId()
  37. }
  38. func (server *SNlbServerGroupServer) GetStatus() string {
  39. switch server.Status {
  40. case "Available":
  41. return api.LB_STATUS_ENABLED
  42. case "Configuring":
  43. return api.LB_STATUS_UNKNOWN
  44. default:
  45. return api.LB_STATUS_ENABLED
  46. }
  47. }
  48. func (server *SNlbServerGroupServer) IsEmulated() bool {
  49. return false
  50. }
  51. func (server *SNlbServerGroupServer) Refresh() error {
  52. serverGroup, err := server.nlbServerGroup.nlb.region.GetNlbServerGroup(server.nlbServerGroup.ServerGroupId)
  53. if err != nil {
  54. return err
  55. }
  56. for _, s := range serverGroup.Servers {
  57. if s.ServerId == server.ServerId && s.Port == server.Port {
  58. return jsonutils.Update(server, &s)
  59. }
  60. }
  61. return cloudprovider.ErrNotFound
  62. }
  63. func (server *SNlbServerGroupServer) GetWeight() int {
  64. return server.Weight
  65. }
  66. func (server *SNlbServerGroupServer) GetPort() int {
  67. return server.Port
  68. }
  69. func (server *SNlbServerGroupServer) GetBackendType() string {
  70. return api.LB_BACKEND_GUEST
  71. }
  72. func (server *SNlbServerGroupServer) GetBackendRole() string {
  73. return api.LB_BACKEND_ROLE_DEFAULT
  74. }
  75. func (server *SNlbServerGroupServer) GetBackendId() string {
  76. return server.ServerId
  77. }
  78. func (server *SNlbServerGroupServer) GetIpAddress() string {
  79. return server.ServerIp
  80. }
  81. func (server *SNlbServerGroupServer) GetProjectId() string {
  82. return server.nlbServerGroup.GetProjectId()
  83. }
  84. func (server *SNlbServerGroupServer) Update(ctx context.Context, opts *cloudprovider.SLoadbalancerBackend) error {
  85. return server.nlbServerGroup.nlb.region.UpdateNlbServerGroupServerAttribute(
  86. server.nlbServerGroup.ServerGroupId,
  87. server.ServerId,
  88. opts.Port,
  89. opts.Weight,
  90. )
  91. }
  92. // region methods for NLB server group server operations
  93. func (region *SRegion) UpdateNlbServerGroupServerAttribute(serverGroupId, serverId string, port, weight int) error {
  94. params := map[string]string{
  95. "RegionId": region.RegionId,
  96. "ServerGroupId": serverGroupId,
  97. }
  98. servers := jsonutils.NewArray()
  99. servers.Add(jsonutils.Marshal(map[string]interface{}{
  100. "ServerId": serverId,
  101. "ServerType": "Ecs",
  102. "Port": port,
  103. "Weight": weight,
  104. }))
  105. params["Servers"] = servers.String()
  106. _, err := region.nlbRequest("UpdateServerGroupServersAttribute", params)
  107. return err
  108. }