| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package models
- import (
- "fmt"
- "sort"
- "yunion.io/x/log"
- compute_models "yunion.io/x/onecloud/pkg/compute/models"
- "yunion.io/x/onecloud/pkg/util/netutils2"
- )
- type Vpc struct {
- compute_models.SVpc
- RouteTable *RouteTable `json:"-"`
- Wire *Wire `json:"-"`
- Networks Networks `json:"-"`
- }
- func (el *Vpc) Copy() *Vpc {
- return &Vpc{
- SVpc: el.SVpc,
- }
- }
- type RouteTable struct {
- compute_models.SRouteTable
- Vpc *Vpc
- }
- func (el *RouteTable) Copy() *RouteTable {
- return &RouteTable{
- SRouteTable: el.SRouteTable,
- }
- }
- type Wire struct {
- compute_models.SWire
- Vpc *Vpc
- }
- func (el *Wire) Copy() *Wire {
- return &Wire{
- SWire: el.SWire,
- }
- }
- type Network struct {
- compute_models.SNetwork
- Vpc *Vpc `json:"-"`
- Wire *Wire `json:"-"`
- Guestnetworks Guestnetworks `json:"-"`
- Groupnetworks Groupnetworks `json:"-"`
- LoadbalancerNetworks LoadbalancerNetworks `json:"-"`
- Elasticips Elasticips `json:"-"`
- }
- func (el *Network) Copy() *Network {
- return &Network{
- SNetwork: el.SNetwork,
- }
- }
- type Guestnetwork struct {
- compute_models.SGuestnetwork
- // Guest could be nil for when the guest is pending_deleted
- Guest *Guest `json:"-"`
- Network *Network `json:"-"`
- Elasticip *Elasticip `json:"-"`
- SubIPs NetworkAddresses `json:"-"`
- // guest nic level secgroup
- Guestnetworksecgroups Guestnetworksecgroups `json:"-"`
- }
- func (el *Guestnetwork) Copy() *Guestnetwork {
- return &Guestnetwork{
- SGuestnetwork: el.SGuestnetwork,
- }
- }
- type NetworkAddress struct {
- compute_models.SNetworkAddress
- Guestnetwork *Guestnetwork `json:"-"`
- Network *Network `json:"-"`
- }
- func (el *NetworkAddress) Copy() *NetworkAddress {
- return &NetworkAddress{
- SNetworkAddress: el.SNetworkAddress,
- }
- }
- type Guestnetworksecgroup struct {
- compute_models.SGuestnetworksecgroup
- SecurityGroup *SecurityGroup `json:"-"`
- }
- func (el *Guestnetworksecgroup) ModelSetKey() string {
- return fmt.Sprintf("%s/%d", el.GuestId, el.NetworkIndex)
- }
- func (el *Guestnetworksecgroup) Copy() *Guestnetworksecgroup {
- return &Guestnetworksecgroup{
- SGuestnetworksecgroup: el.SGuestnetworksecgroup,
- }
- }
- type Guest struct {
- compute_models.SGuest
- Host *Host `json:"-"`
- AdminSecurityGroup *SecurityGroup `json:"-"`
- SecurityGroups SecurityGroups `json:"-"`
- Guestnetworks Guestnetworks `json:"-"`
- Groups Groups `json:"-"`
- }
- func (el *Guest) Copy() *Guest {
- return &Guest{
- SGuest: el.SGuest,
- }
- }
- func (el *Guest) GetVips() []string {
- ret := make([]string, 0)
- for _, g := range el.Groups {
- for _, gn := range g.Groupnetworks {
- ret = append(ret, fmt.Sprintf("%s/%d", gn.IpAddr, gn.Network.GuestIpMask))
- }
- }
- return ret
- }
- type GuestnetworkList []*Guestnetwork
- func (a GuestnetworkList) Len() int { return len(a) }
- func (a GuestnetworkList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
- func (a GuestnetworkList) Less(i, j int) bool { return a[i].Index < a[j].Index }
- func (el *Guest) FixIsDefaults() {
- gns := make([]*Guestnetwork, 0, len(el.Guestnetworks))
- for _, v := range el.Guestnetworks {
- gns = append(gns, v)
- }
- sort.Sort(GuestnetworkList(gns))
- defaultCnt := 0
- nics := netutils2.SNicInfoList{}
- for _, gn := range gns {
- if gn.Network == nil {
- log.Debugf("guest %s %s", gn.GuestId, gn.NetworkId)
- continue
- }
- if gn.IsDefault {
- defaultCnt++
- }
- nics = nics.Add(gn.MacAddr, gn.IpAddr, gn.Network.GuestGateway, gn.Ip6Addr, gn.Network.GuestGateway6, gn.IsDefault)
- }
- if defaultCnt != 1 {
- gwMac, _ := nics.FindDefaultNicMac()
- for _, gn := range gns {
- if gn.MacAddr == gwMac {
- gn.IsDefault = true
- } else {
- gn.IsDefault = false
- }
- }
- }
- }
- type Host struct {
- compute_models.SHost
- }
- func (el *Host) Copy() *Host {
- return &Host{
- SHost: el.SHost,
- }
- }
- type Guestsecgroup struct {
- compute_models.SGuestsecgroup
- Guest *Guest `json:"-"`
- SecurityGroup *SecurityGroup `json:"-"`
- }
- func (el *Guestsecgroup) ModelSetKey() string {
- return el.GuestId + "/" + el.SecgroupId
- }
- func (el *Guestsecgroup) Copy() *Guestsecgroup {
- return &Guestsecgroup{
- SGuestsecgroup: el.SGuestsecgroup,
- }
- }
- type SecurityGroup struct {
- compute_models.SSecurityGroup
- SecurityGroupRules SecurityGroupRules `json:"-"`
- }
- func (el *SecurityGroup) Copy() *SecurityGroup {
- return &SecurityGroup{
- SSecurityGroup: el.SSecurityGroup,
- }
- }
- type SecurityGroupRule struct {
- compute_models.SSecurityGroupRule
- SecurityGroup *SecurityGroup `json:"-"`
- }
- func (el *SecurityGroupRule) Copy() *SecurityGroupRule {
- return &SecurityGroupRule{
- SSecurityGroupRule: el.SSecurityGroupRule,
- }
- }
- type Elasticip struct {
- compute_models.SElasticip
- Network *Network `json:"-"`
- Guestnetwork *Guestnetwork `json:"-"`
- Groupnetwork *Groupnetwork `json:"-"`
- LoadbalancerNetwork *LoadbalancerNetwork `json:"-"`
- }
- func (el *Elasticip) Copy() *Elasticip {
- return &Elasticip{
- SElasticip: el.SElasticip,
- }
- }
- type DnsRecord struct {
- compute_models.SDnsRecord
- DnsZone *DnsZone
- }
- func (el *DnsRecord) Copy() *DnsRecord {
- return &DnsRecord{
- SDnsRecord: el.SDnsRecord,
- }
- }
- type DnsZone struct {
- compute_models.SDnsZone
- Records DnsRecords
- }
- func (el *DnsZone) Copy() *DnsZone {
- return &DnsZone{
- SDnsZone: el.SDnsZone,
- }
- }
- type Groupguest struct {
- compute_models.SGroupguest
- Group *Group `json:"-"`
- Guest *Guest `json:"-"`
- }
- func (el *Groupguest) Copy() *Groupguest {
- return &Groupguest{
- SGroupguest: el.SGroupguest,
- }
- }
- type Groupnetwork struct {
- compute_models.SGroupnetwork
- Network *Network `json:"-"`
- Group *Group `json:"-"`
- Elasticip *Elasticip `json:"-"`
- }
- func (el *Groupnetwork) Copy() *Groupnetwork {
- return &Groupnetwork{
- SGroupnetwork: el.SGroupnetwork,
- }
- }
- func (el *Groupnetwork) GetGuestNetworks() []*Guestnetwork {
- ret := make([]*Guestnetwork, 0)
- if el.Group == nil {
- log.Errorf("Nil group for groupnetwork %s %s", el.GroupId, el.NetworkId)
- } else if el.Group.Groupguests == nil {
- log.Errorf("Nil groupguests for group %s", el.GroupId)
- } else {
- for _, gg := range el.Group.Groupguests {
- if gg.Guest == nil {
- log.Errorf("Nil guest for groupguest %s %s", gg.GroupId, gg.GuestId)
- } else if gg.Guest.Guestnetworks == nil {
- log.Errorf("Nil guestnetworks for guest %s", gg.GuestId)
- } else {
- for _, gn := range gg.Guest.Guestnetworks {
- ret = append(ret, gn)
- }
- }
- }
- }
- return ret
- }
- type Group struct {
- compute_models.SGroup
- Groupguests Groupguests `json:"-"`
- Groupnetworks Groupnetworks `json:"-"`
- }
- func (el *Group) Copy() *Group {
- return &Group{
- SGroup: el.SGroup,
- }
- }
- type LoadbalancerNetwork struct {
- compute_models.SLoadbalancerNetwork
- Network *Network `json:"-"`
- Elasticip *Elasticip `json:"-"`
- LoadbalancerListeners LoadbalancerListeners `json:"-"`
- }
- func (el *LoadbalancerNetwork) Copy() *LoadbalancerNetwork {
- return &LoadbalancerNetwork{
- SLoadbalancerNetwork: el.SLoadbalancerNetwork,
- }
- }
- func (el *LoadbalancerNetwork) OrderedLoadbalancerListeners() []*LoadbalancerListener {
- lblisteners := make([]*LoadbalancerListener, 0, len(el.LoadbalancerListeners))
- for _, lblistener := range el.LoadbalancerListeners {
- lblisteners = append(lblisteners, lblistener)
- }
- sort.Slice(lblisteners, func(i, j int) bool {
- return lblisteners[i].Id < lblisteners[j].Id
- })
- return lblisteners
- }
- type LoadbalancerListener struct {
- compute_models.SLoadbalancerListener
- LoadbalancerAcl *LoadbalancerAcl `json:"-"`
- }
- func (el *LoadbalancerListener) Copy() *LoadbalancerListener {
- return &LoadbalancerListener{
- SLoadbalancerListener: el.SLoadbalancerListener,
- }
- }
- type LoadbalancerAcl struct {
- compute_models.SLoadbalancerAcl
- }
- func (el *LoadbalancerAcl) Copy() *LoadbalancerAcl {
- return &LoadbalancerAcl{
- SLoadbalancerAcl: el.SLoadbalancerAcl,
- }
- }
|