network.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 cloudpods
  15. import (
  16. "yunion.io/x/cloudmux/pkg/cloudprovider"
  17. "yunion.io/x/cloudmux/pkg/multicloud"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/rbacscope"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. )
  23. type SNetwork struct {
  24. multicloud.SNetworkBase
  25. CloudpodsTags
  26. wire *SWire
  27. api.NetworkDetails
  28. }
  29. func (self *SNetwork) GetName() string {
  30. return self.Name
  31. }
  32. func (self *SNetwork) GetId() string {
  33. return self.Id
  34. }
  35. func (self *SNetwork) GetGlobalId() string {
  36. return self.Id
  37. }
  38. func (self *SNetwork) GetStatus() string {
  39. return self.Status
  40. }
  41. func (self *SNetwork) Delete() error {
  42. return self.wire.vpc.region.cli.delete(&modules.Networks, self.Id)
  43. }
  44. func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
  45. return self.wire
  46. }
  47. func (net *SNetwork) GetIp6Start() string {
  48. return net.GuestIp6Start
  49. }
  50. func (net *SNetwork) GetIp6End() string {
  51. return net.GuestIp6End
  52. }
  53. func (net *SNetwork) GetIp6Mask() uint8 {
  54. return net.GuestIpMask
  55. }
  56. func (self *SNetwork) GetGateway6() string {
  57. return self.GuestGateway6
  58. }
  59. func (self *SNetwork) GetIpStart() string {
  60. return self.GuestIpStart
  61. }
  62. func (self *SNetwork) GetIpEnd() string {
  63. return self.GuestIpEnd
  64. }
  65. func (self *SNetwork) GetIpMask() int8 {
  66. return int8(self.GuestIpMask)
  67. }
  68. func (self *SNetwork) GetGateway() string {
  69. return self.GuestGateway
  70. }
  71. func (self *SNetwork) GetServerType() string {
  72. return self.ServerType
  73. }
  74. func (self *SNetwork) GetPublicScope() rbacscope.TRbacScope {
  75. return rbacscope.TRbacScope(self.PublicScope)
  76. }
  77. func (self *SNetwork) GetAllocTimeoutSeconds() int {
  78. return self.AllocTimoutSeconds
  79. }
  80. func (self *SNetwork) GetProjectId() string {
  81. return self.TenantId
  82. }
  83. func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
  84. networks, err := self.vpc.region.GetNetworks(self.Id)
  85. if err != nil {
  86. return nil, err
  87. }
  88. ret := []cloudprovider.ICloudNetwork{}
  89. for i := range networks {
  90. networks[i].wire = self
  91. ret = append(ret, &networks[i])
  92. }
  93. return ret, nil
  94. }
  95. func (self *SWire) GetINetworkById(id string) (cloudprovider.ICloudNetwork, error) {
  96. net, err := self.vpc.region.GetNetwork(id)
  97. if err != nil {
  98. return nil, errors.Wrapf(err, "GetNetwork(%s)", id)
  99. }
  100. net.wire = self
  101. return net, nil
  102. }
  103. func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
  104. input := api.NetworkCreateInput{}
  105. input.Name = opts.Name
  106. input.Description = opts.Desc
  107. input.GuestIpPrefix = opts.Cidr
  108. input.WireId = self.Id
  109. input.ProjectId = opts.ProjectId
  110. network := &SNetwork{wire: self}
  111. return network, self.vpc.region.create(&modules.Networks, input, network)
  112. }
  113. func (self *SRegion) GetNetworks(wireId string) ([]SNetwork, error) {
  114. networks := []SNetwork{}
  115. params := map[string]interface{}{}
  116. if len(wireId) > 0 {
  117. params["wire_id"] = wireId
  118. }
  119. return networks, self.list(&modules.Networks, params, &networks)
  120. }
  121. func (self *SRegion) GetNetwork(id string) (*SNetwork, error) {
  122. network := &SNetwork{}
  123. return network, self.cli.get(&modules.Networks, id, nil, network)
  124. }