fix(region): incloudsphere vm nic with ip (#15209)

This commit is contained in:
屈轩
2022-10-21 17:17:12 +08:00
committed by GitHub
parent 8696c5e504
commit 8888393da1
3 changed files with 69 additions and 25 deletions
+30 -10
View File
@@ -16,6 +16,7 @@ package incloudsphere
import (
"fmt"
"net"
"net/url"
"strings"
@@ -279,6 +280,34 @@ func (self *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudpr
}
}
}
nic := SInstanceNic{
AutoGenerated: true,
DeviceId: opts.ExternalNetworkId,
DeviceType: "NETWORK",
Model: "E1000",
Queues: 1,
}
network, err := self.zone.region.GetNetwork(opts.ExternalNetworkId)
if err != nil {
return nil, errors.Wrapf(err, "GetNetwork")
}
if network.DhcpEnabled {
nic.Dhcp = true
nic.DhcpEnabled = true
nic.DhcpIP = opts.IpAddr
} else if len(opts.IpAddr) > 0 {
nic.IP = opts.IpAddr
mask := net.CIDRMask(int(network.GetIpMask()), 32)
nic.Netmask = fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3])
nic.Gateway = network.GetGateway()
if len(nic.Gateway) == 0 {
info := strings.Split(opts.IpAddr, ".")
if len(info) == 4 {
info[3] = "1"
nic.Gateway = strings.Join(info, ".")
}
}
}
body := map[string]interface{}{
"boot": "HD",
"bootMode": "BIOS",
@@ -311,16 +340,7 @@ func (self *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudpr
"memoryUnit": "GB",
"name": opts.Name,
"nics": []SInstanceNic{
{
AutoGenerated: true,
DeviceId: opts.ExternalNetworkId,
DeviceType: "NETWORK",
Model: "E1000",
Queues: 1,
Dhcp: true,
DhcpEnabled: true,
DhcpIP: opts.IpAddr,
},
nic,
},
"panickPolicy": "NO_ACTION",
"replicate": 1,
+8 -1
View File
@@ -444,7 +444,14 @@ func (self *SInstance) GetProjectId() string {
}
func (self *SInstance) GetVNCInfo(input *cloudprovider.ServerVncInput) (*cloudprovider.ServerVncOutput, error) {
return nil, cloudprovider.ErrNotSupported
ret := &cloudprovider.ServerVncOutput{
Protocol: "vnc",
Host: self.host.Name,
Port: self.VNCPort,
Id: self.Id,
Password: "1q@W3e$R",
}
return ret, nil
}
func (self *SInstance) GetVcpuCount() int {
+31 -14
View File
@@ -17,6 +17,7 @@ package incloudsphere
import (
"fmt"
"net/url"
"strings"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/util/netutils"
@@ -42,11 +43,12 @@ type SNetwork struct {
Type string `json:"type"`
VswitchDto SWire `json:"vswitchDto"`
//PortDtos []string `json:"portDtos"`
VMDtos string `json:"vmDtos"`
VnicDtos string `json:"vnicDtos"`
Vmcount int `json:"vmcount"`
Vniccount int `json:"vniccount"`
ConnectMode string `json:"connectMode"`
VMDtos string `json:"vmDtos"`
VnicDtos string `json:"vnicDtos"`
Vmcount int `json:"vmcount"`
Vniccount int `json:"vniccount"`
ConnectMode string `json:"connectMode"`
// 约定未开启dhcp时,描述信息里面放置<gateway>/<netmask>
Description string `json:"description"`
UplinkRate int `json:"uplinkRate"`
UplinkBurst int `json:"uplinkBurst"`
@@ -65,6 +67,17 @@ type SNetwork struct {
NetworkTopoly bool `json:"networkTopoly"`
}
func (self *SNetwork) GetCidr() string {
if len(self.Cidr) > 0 {
return self.Cidr
}
_, err := netutils.NewIPV4Prefix(self.Description)
if err == nil {
return self.Description
}
return "0.0.0.0/0"
}
func (self *SNetwork) GetName() string {
return self.Name
}
@@ -94,6 +107,16 @@ func (self *SNetwork) GetAllocTimeoutSeconds() int {
}
func (self *SNetwork) GetGateway() string {
if len(self.Gateway) > 0 {
return self.Gateway
}
if len(self.Description) > 0 && strings.Contains(self.Description, "/") {
info := strings.Split(self.Description, "/")
ip, err := netutils.NewIPV4Addr(info[0])
if err == nil {
return ip.String()
}
}
return self.Gateway
}
@@ -102,10 +125,7 @@ func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
}
func (self *SNetwork) GetIpStart() string {
if len(self.Cidr) == 0 {
self.Cidr = "0.0.0.0/0"
}
_range, err := netutils.NewIPV4Prefix(self.Cidr)
_range, err := netutils.NewIPV4Prefix(self.GetCidr())
if err != nil {
return ""
}
@@ -113,10 +133,7 @@ func (self *SNetwork) GetIpStart() string {
}
func (self *SNetwork) GetIpEnd() string {
if len(self.Cidr) == 0 {
self.Cidr = "0.0.0.0/0"
}
_range, err := netutils.NewIPV4Prefix(self.Cidr)
_range, err := netutils.NewIPV4Prefix(self.GetCidr())
if err != nil {
return ""
}
@@ -131,7 +148,7 @@ func (self *SNetwork) Contains(_ip string) bool {
}
func (self *SNetwork) GetIpMask() int8 {
pref, err := netutils.NewIPV4Prefix(self.Cidr)
pref, err := netutils.NewIPV4Prefix(self.GetCidr())
if err != nil {
return 0
}