models.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. "fmt"
  17. "sort"
  18. "yunion.io/x/log"
  19. compute_models "yunion.io/x/onecloud/pkg/compute/models"
  20. "yunion.io/x/onecloud/pkg/util/netutils2"
  21. )
  22. type Vpc struct {
  23. compute_models.SVpc
  24. RouteTable *RouteTable `json:"-"`
  25. Wire *Wire `json:"-"`
  26. Networks Networks `json:"-"`
  27. }
  28. func (el *Vpc) Copy() *Vpc {
  29. return &Vpc{
  30. SVpc: el.SVpc,
  31. }
  32. }
  33. type RouteTable struct {
  34. compute_models.SRouteTable
  35. Vpc *Vpc
  36. }
  37. func (el *RouteTable) Copy() *RouteTable {
  38. return &RouteTable{
  39. SRouteTable: el.SRouteTable,
  40. }
  41. }
  42. type Wire struct {
  43. compute_models.SWire
  44. Vpc *Vpc
  45. }
  46. func (el *Wire) Copy() *Wire {
  47. return &Wire{
  48. SWire: el.SWire,
  49. }
  50. }
  51. type Network struct {
  52. compute_models.SNetwork
  53. Vpc *Vpc `json:"-"`
  54. Wire *Wire `json:"-"`
  55. Guestnetworks Guestnetworks `json:"-"`
  56. Groupnetworks Groupnetworks `json:"-"`
  57. LoadbalancerNetworks LoadbalancerNetworks `json:"-"`
  58. Elasticips Elasticips `json:"-"`
  59. }
  60. func (el *Network) Copy() *Network {
  61. return &Network{
  62. SNetwork: el.SNetwork,
  63. }
  64. }
  65. type Guestnetwork struct {
  66. compute_models.SGuestnetwork
  67. // Guest could be nil for when the guest is pending_deleted
  68. Guest *Guest `json:"-"`
  69. Network *Network `json:"-"`
  70. Elasticip *Elasticip `json:"-"`
  71. SubIPs NetworkAddresses `json:"-"`
  72. // guest nic level secgroup
  73. Guestnetworksecgroups Guestnetworksecgroups `json:"-"`
  74. }
  75. func (el *Guestnetwork) Copy() *Guestnetwork {
  76. return &Guestnetwork{
  77. SGuestnetwork: el.SGuestnetwork,
  78. }
  79. }
  80. type NetworkAddress struct {
  81. compute_models.SNetworkAddress
  82. Guestnetwork *Guestnetwork `json:"-"`
  83. Network *Network `json:"-"`
  84. }
  85. func (el *NetworkAddress) Copy() *NetworkAddress {
  86. return &NetworkAddress{
  87. SNetworkAddress: el.SNetworkAddress,
  88. }
  89. }
  90. type Guestnetworksecgroup struct {
  91. compute_models.SGuestnetworksecgroup
  92. SecurityGroup *SecurityGroup `json:"-"`
  93. }
  94. func (el *Guestnetworksecgroup) ModelSetKey() string {
  95. return fmt.Sprintf("%s/%d", el.GuestId, el.NetworkIndex)
  96. }
  97. func (el *Guestnetworksecgroup) Copy() *Guestnetworksecgroup {
  98. return &Guestnetworksecgroup{
  99. SGuestnetworksecgroup: el.SGuestnetworksecgroup,
  100. }
  101. }
  102. type Guest struct {
  103. compute_models.SGuest
  104. Host *Host `json:"-"`
  105. AdminSecurityGroup *SecurityGroup `json:"-"`
  106. SecurityGroups SecurityGroups `json:"-"`
  107. Guestnetworks Guestnetworks `json:"-"`
  108. Groups Groups `json:"-"`
  109. }
  110. func (el *Guest) Copy() *Guest {
  111. return &Guest{
  112. SGuest: el.SGuest,
  113. }
  114. }
  115. func (el *Guest) GetVips() []string {
  116. ret := make([]string, 0)
  117. for _, g := range el.Groups {
  118. for _, gn := range g.Groupnetworks {
  119. ret = append(ret, fmt.Sprintf("%s/%d", gn.IpAddr, gn.Network.GuestIpMask))
  120. }
  121. }
  122. return ret
  123. }
  124. type GuestnetworkList []*Guestnetwork
  125. func (a GuestnetworkList) Len() int { return len(a) }
  126. func (a GuestnetworkList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  127. func (a GuestnetworkList) Less(i, j int) bool { return a[i].Index < a[j].Index }
  128. func (el *Guest) FixIsDefaults() {
  129. gns := make([]*Guestnetwork, 0, len(el.Guestnetworks))
  130. for _, v := range el.Guestnetworks {
  131. gns = append(gns, v)
  132. }
  133. sort.Sort(GuestnetworkList(gns))
  134. defaultCnt := 0
  135. nics := netutils2.SNicInfoList{}
  136. for _, gn := range gns {
  137. if gn.Network == nil {
  138. log.Debugf("guest %s %s", gn.GuestId, gn.NetworkId)
  139. continue
  140. }
  141. if gn.IsDefault {
  142. defaultCnt++
  143. }
  144. nics = nics.Add(gn.MacAddr, gn.IpAddr, gn.Network.GuestGateway, gn.Ip6Addr, gn.Network.GuestGateway6, gn.IsDefault)
  145. }
  146. if defaultCnt != 1 {
  147. gwMac, _ := nics.FindDefaultNicMac()
  148. for _, gn := range gns {
  149. if gn.MacAddr == gwMac {
  150. gn.IsDefault = true
  151. } else {
  152. gn.IsDefault = false
  153. }
  154. }
  155. }
  156. }
  157. type Host struct {
  158. compute_models.SHost
  159. }
  160. func (el *Host) Copy() *Host {
  161. return &Host{
  162. SHost: el.SHost,
  163. }
  164. }
  165. type Guestsecgroup struct {
  166. compute_models.SGuestsecgroup
  167. Guest *Guest `json:"-"`
  168. SecurityGroup *SecurityGroup `json:"-"`
  169. }
  170. func (el *Guestsecgroup) ModelSetKey() string {
  171. return el.GuestId + "/" + el.SecgroupId
  172. }
  173. func (el *Guestsecgroup) Copy() *Guestsecgroup {
  174. return &Guestsecgroup{
  175. SGuestsecgroup: el.SGuestsecgroup,
  176. }
  177. }
  178. type SecurityGroup struct {
  179. compute_models.SSecurityGroup
  180. SecurityGroupRules SecurityGroupRules `json:"-"`
  181. }
  182. func (el *SecurityGroup) Copy() *SecurityGroup {
  183. return &SecurityGroup{
  184. SSecurityGroup: el.SSecurityGroup,
  185. }
  186. }
  187. type SecurityGroupRule struct {
  188. compute_models.SSecurityGroupRule
  189. SecurityGroup *SecurityGroup `json:"-"`
  190. }
  191. func (el *SecurityGroupRule) Copy() *SecurityGroupRule {
  192. return &SecurityGroupRule{
  193. SSecurityGroupRule: el.SSecurityGroupRule,
  194. }
  195. }
  196. type Elasticip struct {
  197. compute_models.SElasticip
  198. Network *Network `json:"-"`
  199. Guestnetwork *Guestnetwork `json:"-"`
  200. Groupnetwork *Groupnetwork `json:"-"`
  201. LoadbalancerNetwork *LoadbalancerNetwork `json:"-"`
  202. }
  203. func (el *Elasticip) Copy() *Elasticip {
  204. return &Elasticip{
  205. SElasticip: el.SElasticip,
  206. }
  207. }
  208. type DnsRecord struct {
  209. compute_models.SDnsRecord
  210. DnsZone *DnsZone
  211. }
  212. func (el *DnsRecord) Copy() *DnsRecord {
  213. return &DnsRecord{
  214. SDnsRecord: el.SDnsRecord,
  215. }
  216. }
  217. type DnsZone struct {
  218. compute_models.SDnsZone
  219. Records DnsRecords
  220. }
  221. func (el *DnsZone) Copy() *DnsZone {
  222. return &DnsZone{
  223. SDnsZone: el.SDnsZone,
  224. }
  225. }
  226. type Groupguest struct {
  227. compute_models.SGroupguest
  228. Group *Group `json:"-"`
  229. Guest *Guest `json:"-"`
  230. }
  231. func (el *Groupguest) Copy() *Groupguest {
  232. return &Groupguest{
  233. SGroupguest: el.SGroupguest,
  234. }
  235. }
  236. type Groupnetwork struct {
  237. compute_models.SGroupnetwork
  238. Network *Network `json:"-"`
  239. Group *Group `json:"-"`
  240. Elasticip *Elasticip `json:"-"`
  241. }
  242. func (el *Groupnetwork) Copy() *Groupnetwork {
  243. return &Groupnetwork{
  244. SGroupnetwork: el.SGroupnetwork,
  245. }
  246. }
  247. func (el *Groupnetwork) GetGuestNetworks() []*Guestnetwork {
  248. ret := make([]*Guestnetwork, 0)
  249. if el.Group == nil {
  250. log.Errorf("Nil group for groupnetwork %s %s", el.GroupId, el.NetworkId)
  251. } else if el.Group.Groupguests == nil {
  252. log.Errorf("Nil groupguests for group %s", el.GroupId)
  253. } else {
  254. for _, gg := range el.Group.Groupguests {
  255. if gg.Guest == nil {
  256. log.Errorf("Nil guest for groupguest %s %s", gg.GroupId, gg.GuestId)
  257. } else if gg.Guest.Guestnetworks == nil {
  258. log.Errorf("Nil guestnetworks for guest %s", gg.GuestId)
  259. } else {
  260. for _, gn := range gg.Guest.Guestnetworks {
  261. ret = append(ret, gn)
  262. }
  263. }
  264. }
  265. }
  266. return ret
  267. }
  268. type Group struct {
  269. compute_models.SGroup
  270. Groupguests Groupguests `json:"-"`
  271. Groupnetworks Groupnetworks `json:"-"`
  272. }
  273. func (el *Group) Copy() *Group {
  274. return &Group{
  275. SGroup: el.SGroup,
  276. }
  277. }
  278. type LoadbalancerNetwork struct {
  279. compute_models.SLoadbalancerNetwork
  280. Network *Network `json:"-"`
  281. Elasticip *Elasticip `json:"-"`
  282. LoadbalancerListeners LoadbalancerListeners `json:"-"`
  283. }
  284. func (el *LoadbalancerNetwork) Copy() *LoadbalancerNetwork {
  285. return &LoadbalancerNetwork{
  286. SLoadbalancerNetwork: el.SLoadbalancerNetwork,
  287. }
  288. }
  289. func (el *LoadbalancerNetwork) OrderedLoadbalancerListeners() []*LoadbalancerListener {
  290. lblisteners := make([]*LoadbalancerListener, 0, len(el.LoadbalancerListeners))
  291. for _, lblistener := range el.LoadbalancerListeners {
  292. lblisteners = append(lblisteners, lblistener)
  293. }
  294. sort.Slice(lblisteners, func(i, j int) bool {
  295. return lblisteners[i].Id < lblisteners[j].Id
  296. })
  297. return lblisteners
  298. }
  299. type LoadbalancerListener struct {
  300. compute_models.SLoadbalancerListener
  301. LoadbalancerAcl *LoadbalancerAcl `json:"-"`
  302. }
  303. func (el *LoadbalancerListener) Copy() *LoadbalancerListener {
  304. return &LoadbalancerListener{
  305. SLoadbalancerListener: el.SLoadbalancerListener,
  306. }
  307. }
  308. type LoadbalancerAcl struct {
  309. compute_models.SLoadbalancerAcl
  310. }
  311. func (el *LoadbalancerAcl) Copy() *LoadbalancerAcl {
  312. return &LoadbalancerAcl{
  313. SLoadbalancerAcl: el.SLoadbalancerAcl,
  314. }
  315. }