vpc.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 nutanix
  15. import (
  16. "fmt"
  17. "net/url"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type DhcpOptions struct {
  28. }
  29. type SPool struct {
  30. Range string `json:"range"`
  31. }
  32. type IPConfig struct {
  33. NetworkAddress string `json:"network_address"`
  34. PrefixLength int `json:"prefix_length"`
  35. DefaultGateway string `json:"default_gateway"`
  36. DhcpOptions DhcpOptions `json:"dhcp_options"`
  37. Pool []SPool `json:"pool"`
  38. DhcpServerAddress string `json:"dhcp_server_address"`
  39. }
  40. type SVpc struct {
  41. multicloud.SVpc
  42. multicloud.STagBase
  43. region *SRegion
  44. LogicalTimestamp int `json:"logical_timestamp"`
  45. VlanID int `json:"vlan_id"`
  46. UUID string `json:"uuid"`
  47. Name string `json:"name"`
  48. IPConfig IPConfig `json:"ip_config,omitempty"`
  49. }
  50. func (self *SVpc) GetName() string {
  51. return self.Name
  52. }
  53. func (self *SVpc) GetId() string {
  54. return self.UUID
  55. }
  56. func (self *SVpc) GetGlobalId() string {
  57. return self.UUID
  58. }
  59. func (self *SVpc) Delete() error {
  60. return self.region.DeleteVpc(self.UUID)
  61. }
  62. func (self *SVpc) GetCidrBlock() string {
  63. if len(self.IPConfig.NetworkAddress) > 0 {
  64. return fmt.Sprintf("%s/%d", self.IPConfig.NetworkAddress, self.IPConfig.PrefixLength)
  65. }
  66. return ""
  67. }
  68. func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  69. return []cloudprovider.ICloudRouteTable{}, nil
  70. }
  71. func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  72. return nil, cloudprovider.ErrNotFound
  73. }
  74. func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  75. return []cloudprovider.ICloudSecurityGroup{}, nil
  76. }
  77. func (self *SRegion) GetVpcs() ([]SVpc, error) {
  78. vpcs := []SVpc{}
  79. _, err := self.list("networks", url.Values{}, &vpcs)
  80. return vpcs, err
  81. }
  82. func (self *SVpc) getWire() *SWire {
  83. return &SWire{vpc: self}
  84. }
  85. func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  86. wire := self.getWire()
  87. return []cloudprovider.ICloudWire{wire}, nil
  88. }
  89. func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  90. wires, err := self.GetIWires()
  91. if err != nil {
  92. return nil, err
  93. }
  94. for i := range wires {
  95. if wires[i].GetGlobalId() == wireId {
  96. return wires[i], nil
  97. }
  98. }
  99. return nil, cloudprovider.ErrNotFound
  100. }
  101. func (self *SVpc) GetIsDefault() bool {
  102. return len(self.GetCidrBlock()) > 0
  103. }
  104. func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
  105. return self.region
  106. }
  107. func (self *SVpc) GetStatus() string {
  108. return api.VPC_STATUS_AVAILABLE
  109. }
  110. func (self *SRegion) GetVpc(id string) (*SVpc, error) {
  111. vpc := &SVpc{region: self}
  112. return vpc, self.get("networks", id, url.Values{}, vpc)
  113. }
  114. func (self *SRegion) CreateVpc(opts *cloudprovider.VpcCreateOptions) (*SVpc, error) {
  115. ipConfig := map[string]interface{}{}
  116. if len(opts.CIDR) > 0 {
  117. if addrs := strings.Split(opts.CIDR, "/"); len(addrs) == 2 {
  118. ipConfig["network_address"] = addrs[0]
  119. ipConfig["prefix_length"], _ = strconv.Atoi(addrs[1])
  120. }
  121. }
  122. vpcs, err := self.GetVpcs()
  123. if err != nil {
  124. return nil, errors.Wrapf(err, "GetVpcs")
  125. }
  126. vlanId, vlanIds := -1, []int{}
  127. for i := range vpcs {
  128. vlanIds = append(vlanIds, vpcs[i].VlanID)
  129. }
  130. sort.Ints(vlanIds)
  131. for _, vlan := range vlanIds {
  132. if vlan == vlanId+1 {
  133. vlanId = vlan
  134. }
  135. }
  136. params := map[string]interface{}{
  137. "name": opts.NAME,
  138. "annotation": opts.Desc,
  139. "ip_config": ipConfig,
  140. "vlan_id": vlanId + 1,
  141. }
  142. ret := struct {
  143. NetworkUUID string
  144. }{}
  145. err = self.post("networks", jsonutils.Marshal(params), &ret)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return self.GetVpc(ret.NetworkUUID)
  150. }
  151. func (self *SRegion) DeleteVpc(id string) error {
  152. return self.delete("networks", id)
  153. }