fix: openstack eip dissociate optimized

This commit is contained in:
Qu Xuan
2020-01-04 18:42:45 +08:00
parent 8b546df667
commit 41820a8d4d
5 changed files with 58 additions and 1 deletions
+12
View File
@@ -875,6 +875,18 @@ func (self *SElasticip) PerformAssociate(ctx context.Context, userCred mcclient.
return nil, httperrors.NewInvalidStatusError("cannot associate server in status %s", server.Status)
}
if len(self.NetworkId) > 0 {
gns, err := server.GetNetworks("")
if err != nil {
return nil, httperrors.NewGeneralError(errors.Wrap(err, "GetNetworks"))
}
for _, gn := range gns {
if gn.NetworkId == self.NetworkId {
return nil, httperrors.NewInputParameterError("cannot associate eip with same network")
}
}
}
serverRegion := server.getRegion()
if serverRegion == nil {
return nil, httperrors.NewInputParameterError("server region is not found???")
+12
View File
@@ -2808,6 +2808,18 @@ func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.T
return nil, httperrors.NewInputParameterError("cannot associate eip and instance in different region")
}
if len(eip.NetworkId) > 0 {
gns, err := self.GetNetworks("")
if err != nil {
return nil, httperrors.NewGeneralError(errors.Wrap(err, "GetNetworks"))
}
for _, gn := range gns {
if gn.NetworkId == eip.NetworkId {
return nil, httperrors.NewInputParameterError("cannot associate eip with same network")
}
}
}
eipZone := eip.GetZone()
if eipZone != nil {
insZone := self.getZone()
+12 -1
View File
@@ -179,7 +179,18 @@ func (eip *SEipAddress) GetAssociationType() string {
}
func (eip *SEipAddress) GetAssociationExternalId() string {
return eip.PortDetails.DeviceId
if len(eip.PortDetails.DeviceId) > 0 {
return eip.PortDetails.DeviceId
}
if len(eip.PortId) > 0 {
port, err := eip.region.GetPort(eip.PortId)
if err != nil {
log.Errorf("failed to get eip port %s info", port.DeviceID)
return ""
}
return port.DeviceID
}
return ""
}
func (eip *SEipAddress) GetBillingType() string {
+9
View File
@@ -161,6 +161,15 @@ func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInt
return ret, nil
}
func (region *SRegion) GetPort(portId string) (*SPort, error) {
_, resp, err := region.Get("network", "/v2.0/ports/"+portId, "", nil)
if err != nil {
return nil, err
}
port := &SPort{}
return port, resp.Unmarshal(port, "port")
}
func (region *SRegion) GetPorts(macAddress string) ([]SPort, error) {
base := fmt.Sprintf("/v2.0/ports")
params := url.Values{}
+13
View File
@@ -32,4 +32,17 @@ func init() {
return nil
})
type PortIdOptions struct {
ID string
}
shellutils.R(&PortIdOptions{}, "port-show", "Show port", func(cli *openstack.SRegion, args *PortIdOptions) error {
port, err := cli.GetPort(args.ID)
if err != nil {
return err
}
printObject(port)
return nil
})
}