alb.go 9.6 KB

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