nlb.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SNlb struct {
  26. multicloud.SLoadbalancerBase
  27. AliyunTags
  28. region *SRegion
  29. LoadBalancerId string `json:"LoadBalancerId"`
  30. LoadBalancerName string `json:"LoadBalancerName"`
  31. LoadBalancerStatus string `json:"LoadBalancerStatus"`
  32. LoadBalancerBusinessStatus string `json:"LoadBalancerBusinessStatus"`
  33. LoadBalancerType string `json:"LoadBalancerType"`
  34. AddressType string `json:"AddressType"`
  35. AddressIpVersion string `json:"AddressIpVersion"`
  36. Ipv6AddressType string `json:"Ipv6AddressType"`
  37. VpcId string `json:"VpcId"`
  38. RegionId string `json:"RegionId"`
  39. ZoneId string `json:"ZoneId"`
  40. DNSName string `json:"DNSName"`
  41. NetworkType string `json:"NetworkType"`
  42. InternetChargeType string `json:"InternetChargeType"`
  43. LoadBalancerBillingConfig map[string]interface{} `json:"LoadBalancerBillingConfig"`
  44. ModificationProtectionConfig map[string]interface{} `json:"ModificationProtectionConfig"`
  45. DeletionProtectionConfig map[string]interface{} `json:"DeletionProtectionConfig"`
  46. LoadBalancerOperationLocks []map[string]interface{} `json:"LoadBalancerOperationLocks"`
  47. ZoneMappings []NlbZoneMapping `json:"ZoneMappings"`
  48. CreateTime time.Time `json:"CreateTime"`
  49. ResourceGroupId string `json:"ResourceGroupId"`
  50. CpsEnabled bool `json:"CpsEnabled"`
  51. CrossZoneEnabled bool `json:"CrossZoneEnabled"`
  52. TrafficAffinityEnabled bool `json:"TrafficAffinityEnabled"`
  53. BandwidthPackageId string `json:"BandwidthPackageId"`
  54. }
  55. type NlbZoneMapping struct {
  56. ZoneId string `json:"ZoneId"`
  57. VSwitchId string `json:"VSwitchId"`
  58. AllocationId string `json:"AllocationId"`
  59. PrivateIPv4Address string `json:"PrivateIPv4Address"`
  60. IPv6Address string `json:"IPv6Address"`
  61. LoadBalancerAddresses []NlbLoadBalancerAddress `json:"LoadBalancerAddresses"`
  62. }
  63. type NlbLoadBalancerAddress struct {
  64. AllocationId string `json:"AllocationId"`
  65. EipType string `json:"EipType"`
  66. PrivateIPv4Address string `json:"PrivateIPv4Address"`
  67. PrivateIPv6Address string `json:"PrivateIPv6Address"`
  68. PublicIPv4Address string `json:"PublicIPv4Address"`
  69. PublicIPv6Address string `json:"PublicIPv6Address"`
  70. }
  71. func (nlb *SNlb) GetName() string {
  72. return nlb.LoadBalancerName
  73. }
  74. func (nlb *SNlb) GetId() string {
  75. return nlb.LoadBalancerId
  76. }
  77. func (nlb *SNlb) GetGlobalId() string {
  78. return nlb.LoadBalancerId
  79. }
  80. func (nlb *SNlb) GetStatus() string {
  81. switch nlb.LoadBalancerStatus {
  82. case "Active":
  83. return api.LB_STATUS_ENABLED
  84. case "Provisioning", "Configuring":
  85. return api.LB_STATUS_UNKNOWN
  86. case "Stopped":
  87. return api.LB_STATUS_DISABLED
  88. default:
  89. return api.LB_STATUS_UNKNOWN
  90. }
  91. }
  92. func (nlb *SNlb) GetAddress() string {
  93. return nlb.DNSName
  94. }
  95. func (nlb *SNlb) GetAddressType() string {
  96. return nlb.AddressType
  97. }
  98. func (nlb *SNlb) GetNetworkType() string {
  99. return "vpc"
  100. }
  101. func (nlb *SNlb) GetNetworkIds() []string {
  102. ret := []string{}
  103. for _, zone := range nlb.ZoneMappings {
  104. if len(zone.VSwitchId) > 0 {
  105. ret = append(ret, zone.VSwitchId)
  106. }
  107. }
  108. return ret
  109. }
  110. func (nlb *SNlb) GetZoneId() string {
  111. if len(nlb.ZoneMappings) > 0 {
  112. zone, err := nlb.region.getZoneById(nlb.ZoneMappings[0].ZoneId)
  113. if err != nil {
  114. log.Errorf("failed to find zone for nlb %s error: %v", nlb.LoadBalancerName, err)
  115. return ""
  116. }
  117. return zone.GetGlobalId()
  118. }
  119. return ""
  120. }
  121. func (nlb *SNlb) GetZone1Id() string {
  122. if len(nlb.ZoneMappings) > 1 {
  123. zone, err := nlb.region.getZoneById(nlb.ZoneMappings[1].ZoneId)
  124. if err != nil {
  125. log.Errorf("failed to find zone for nlb %s error: %v", nlb.LoadBalancerName, err)
  126. return ""
  127. }
  128. return zone.GetGlobalId()
  129. }
  130. return ""
  131. }
  132. func (nlb *SNlb) IsEmulated() bool {
  133. return false
  134. }
  135. func (nlb *SNlb) GetVpcId() string {
  136. return nlb.VpcId
  137. }
  138. func (nlb *SNlb) Refresh() error {
  139. loadbalancer, err := nlb.region.GetNlbDetail(nlb.LoadBalancerId)
  140. if err != nil {
  141. return err
  142. }
  143. return jsonutils.Update(nlb, loadbalancer)
  144. }
  145. func (nlb *SNlb) Delete(ctx context.Context) error {
  146. return nlb.region.DeleteNlb(nlb.LoadBalancerId)
  147. }
  148. func (nlb *SNlb) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) {
  149. groups, err := nlb.region.GetNlbServerGroups()
  150. if err != nil {
  151. return nil, err
  152. }
  153. igroups := []cloudprovider.ICloudLoadbalancerBackendGroup{}
  154. for i := 0; i < len(groups); i++ {
  155. // 过滤出属于当前负载均衡器的服务器组
  156. for _, lbId := range groups[i].RelatedLoadBalancerIds {
  157. if lbId == nlb.LoadBalancerId {
  158. groups[i].nlb = nlb
  159. igroups = append(igroups, &groups[i])
  160. break
  161. }
  162. }
  163. }
  164. return igroups, nil
  165. }
  166. func (nlb *SNlb) CreateILoadBalancerBackendGroup(group *cloudprovider.SLoadbalancerBackendGroup) (cloudprovider.ICloudLoadbalancerBackendGroup, error) {
  167. serverGroup, err := nlb.region.CreateNlbServerGroup(group, nlb.VpcId)
  168. if err != nil {
  169. return nil, err
  170. }
  171. serverGroup.nlb = nlb
  172. return serverGroup, nil
  173. }
  174. func (nlb *SNlb) GetILoadBalancerBackendGroupById(groupId string) (cloudprovider.ICloudLoadbalancerBackendGroup, error) {
  175. groups, err := nlb.GetILoadBalancerBackendGroups()
  176. if err != nil {
  177. return nil, err
  178. }
  179. for i := 0; i < len(groups); i++ {
  180. if groups[i].GetGlobalId() == groupId {
  181. return groups[i], nil
  182. }
  183. }
  184. return nil, cloudprovider.ErrNotFound
  185. }
  186. func (nlb *SNlb) CreateILoadBalancerListener(ctx context.Context, listener *cloudprovider.SLoadbalancerListenerCreateOptions) (cloudprovider.ICloudLoadbalancerListener, error) {
  187. return nlb.region.CreateNlbListener(nlb, listener)
  188. }
  189. func (nlb *SNlb) GetILoadBalancerListenerById(listenerId string) (cloudprovider.ICloudLoadbalancerListener, error) {
  190. listeners, err := nlb.GetILoadBalancerListeners()
  191. if err != nil {
  192. return nil, err
  193. }
  194. for i := 0; i < len(listeners); i++ {
  195. if listeners[i].GetGlobalId() == listenerId {
  196. return listeners[i], nil
  197. }
  198. }
  199. return nil, cloudprovider.ErrNotFound
  200. }
  201. func (nlb *SNlb) GetILoadBalancerListeners() ([]cloudprovider.ICloudLoadbalancerListener, error) {
  202. listeners, err := nlb.region.GetNlbListeners(nlb.LoadBalancerId)
  203. if err != nil {
  204. return nil, err
  205. }
  206. ilisteners := []cloudprovider.ICloudLoadbalancerListener{}
  207. for i := 0; i < len(listeners); i++ {
  208. listeners[i].nlb = nlb
  209. ilisteners = append(ilisteners, &listeners[i])
  210. }
  211. return ilisteners, nil
  212. }
  213. func (nlb *SNlb) GetChargeType() string {
  214. return api.LB_CHARGE_TYPE_BY_TRAFFIC
  215. }
  216. func (nlb *SNlb) GetCreatedAt() time.Time {
  217. return nlb.CreateTime
  218. }
  219. func (nlb *SNlb) GetEgressMbps() int {
  220. return 0
  221. }
  222. func (nlb *SNlb) GetIEIPs() ([]cloudprovider.ICloudEIP, error) {
  223. ret := []cloudprovider.ICloudEIP{}
  224. for _, zone := range nlb.ZoneMappings {
  225. for _, addr := range zone.LoadBalancerAddresses {
  226. if strings.HasPrefix(addr.AllocationId, "eip-") {
  227. eip, err := nlb.region.GetEip(addr.AllocationId)
  228. if err != nil {
  229. return nil, err
  230. }
  231. eip.region = nlb.region
  232. ret = append(ret, eip)
  233. }
  234. }
  235. }
  236. return ret, nil
  237. }
  238. func (nlb *SNlb) Start() error {
  239. return cloudprovider.ErrNotSupported
  240. }
  241. func (nlb *SNlb) Stop() error {
  242. return cloudprovider.ErrNotSupported
  243. }
  244. func (nlb *SNlb) GetLoadbalancerSpec() string {
  245. return nlb.LoadBalancerType
  246. }
  247. func (nlb *SNlb) GetProjectId() string {
  248. return nlb.ResourceGroupId
  249. }
  250. // region methods
  251. func (region *SRegion) GetNlbs() ([]SNlb, error) {
  252. params := map[string]string{
  253. "RegionId": region.RegionId,
  254. "MaxResults": "100",
  255. }
  256. nlbs := []SNlb{}
  257. nextToken := ""
  258. for {
  259. if nextToken != "" {
  260. params["NextToken"] = nextToken
  261. }
  262. body, err := region.nlbRequest("ListLoadBalancers", params)
  263. if err != nil {
  264. return nil, err
  265. }
  266. pageNlbs := []SNlb{}
  267. err = body.Unmarshal(&pageNlbs, "LoadBalancers")
  268. if err != nil {
  269. return nil, err
  270. }
  271. for i := 0; i < len(pageNlbs); i++ {
  272. pageNlbs[i].region = region
  273. }
  274. nlbs = append(nlbs, pageNlbs...)
  275. nextToken, _ = body.GetString("NextToken")
  276. if nextToken == "" {
  277. break
  278. }
  279. }
  280. return nlbs, nil
  281. }
  282. func (region *SRegion) GetNlbDetail(nlbId string) (*SNlb, error) {
  283. params := map[string]string{
  284. "RegionId": region.RegionId,
  285. "LoadBalancerId": nlbId,
  286. }
  287. body, err := region.nlbRequest("GetLoadBalancerAttribute", params)
  288. if err != nil {
  289. return nil, err
  290. }
  291. nlb := &SNlb{region: region}
  292. err = body.Unmarshal(nlb)
  293. if err != nil {
  294. return nil, err
  295. }
  296. return nlb, nil
  297. }
  298. func (region *SRegion) DeleteNlb(nlbId string) error {
  299. params := map[string]string{
  300. "RegionId": region.RegionId,
  301. "LoadBalancerId": nlbId,
  302. }
  303. _, err := region.nlbRequest("DeleteLoadBalancer", params)
  304. return err
  305. }