feature: allow network update wire_id (#16124)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2023-03-09 11:08:39 +08:00
committed by GitHub
co-authored by Qiu Jian
parent 78e96c7dc6
commit 1c32ce78a6
5 changed files with 93 additions and 24 deletions
+6 -6
View File
@@ -6,17 +6,17 @@ import (
"yunion.io/x/onecloud/cmd/climc/shell"
"yunion.io/x/onecloud/pkg/mcclient"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/mcclient/options"
compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
)
func init() {
cmd := shell.NewResourceCmd(&modules.NetworkIpMacs)
cmd.List(&options.NetworkIpMacListOptions{})
cmd.Update(&options.NetworkIpMacUpdateOptions{})
cmd.Show(&options.NetworkIpMacIdOptions{})
cmd.Delete(&options.NetworkIpMacIdOptions{})
cmd.Create(&options.NetworkIpMacCreateOptions{})
cmd.List(&compute_options.NetworkIpMacListOptions{})
cmd.Update(&compute_options.NetworkIpMacUpdateOptions{})
cmd.Show(&compute_options.NetworkIpMacIdOptions{})
cmd.Delete(&compute_options.NetworkIpMacIdOptions{})
cmd.Create(&compute_options.NetworkIpMacCreateOptions{})
type NetworkIpMacBatchCreateOptions struct {
NETWORK string `help:"network id" json:"network_id"`
IpMac map[string]string `help:"ip mac map" json:"ip_mac"`
+12 -10
View File
@@ -25,22 +25,24 @@ import (
"yunion.io/x/onecloud/pkg/mcclient"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/mcclient/options"
compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
)
func init() {
cmd := shell.NewResourceCmd(&modules.Networks).WithContextManager(&modules.Wires)
cmd.List(&options.NetworkListOptions{})
cmd.Update(&options.NetworkUpdateOptions{})
cmd.Show(&options.NetworkIdOptions{})
cmd.Delete(&options.NetworkIdOptions{})
cmd.GetMetadata(&options.NetworkIdOptions{})
cmd.Perform("private", &options.NetworkIdOptions{})
cmd.Perform("syncstatus", &options.NetworkIdOptions{})
cmd.Perform("sync", &options.NetworkIdOptions{})
cmd.Perform("purge", &options.NetworkIdOptions{})
cmd.Get("change-owner-candidate-domains", &options.NetworkIdOptions{})
cmd.List(&compute_options.NetworkListOptions{})
cmd.Update(&compute_options.NetworkUpdateOptions{})
cmd.Show(&compute_options.NetworkIdOptions{})
cmd.Delete(&compute_options.NetworkIdOptions{})
cmd.GetMetadata(&compute_options.NetworkIdOptions{})
cmd.Perform("private", &compute_options.NetworkIdOptions{})
cmd.Perform("syncstatus", &compute_options.NetworkIdOptions{})
cmd.Perform("sync", &compute_options.NetworkIdOptions{})
cmd.Perform("purge", &compute_options.NetworkIdOptions{})
cmd.Get("change-owner-candidate-domains", &compute_options.NetworkIdOptions{})
cmd.Perform("set-class-metadata", &options.ResourceMetadataOptions{})
cmd.Perform("switch-wire", &compute_options.NetworkSwitchWireOptions{})
type NetworkShareOptions struct {
ID string `help:"ID or Name of the zone to show"`
+9
View File
@@ -428,3 +428,12 @@ type NetworkIpMacBatchCreateInput struct {
NetworkId string `json:"network_id"`
IpMac map[string]string `json:"ip_mac"`
}
type NetworkSwitchWireInput struct {
apis.Meta
// description: new wire Id or name
// required: true
// example: bcast0
WireId string `json:"wire_id"`
}
+42
View File
@@ -2970,6 +2970,7 @@ func (self *SNetwork) PerformSetBgpType(ctx context.Context, userCred mcclient.T
}); err != nil {
return nil, err
} else {
logclient.AddActionLogWithContext(ctx, self, logclient.ACT_UPDATE, diff, userCred, true)
db.OpsLog.LogEvent(self, db.ACT_UPDATE, diff, userCred)
}
return nil, nil
@@ -2982,3 +2983,44 @@ func (net *SNetwork) IsClassic() bool {
}
return false
}
func (net *SNetwork) PerformSwitchWire(
ctx context.Context,
userCred mcclient.TokenCredential,
query jsonutils.JSONObject,
input *api.NetworkSwitchWireInput,
) (jsonutils.JSONObject, error) {
err := net.ValidateDeleteCondition(ctx, nil)
if err != nil {
return nil, errors.Wrap(httperrors.ErrResourceBusy, "network in use")
}
wireObj, err := WireManager.FetchByIdOrName(userCred, input.WireId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, httperrors.NewResourceNotFoundError2(WireManager.Keyword(), input.WireId)
} else {
return nil, errors.Wrapf(err, "WireManager.FetchByIdOrName %s", input.WireId)
}
}
wire := wireObj.(*SWire)
if net.WireId == wire.Id {
return nil, nil
}
oldWire, _ := net.GetWire()
if oldWire.VpcId != wire.VpcId {
return nil, errors.Wrapf(httperrors.ErrConflict, "cannot switch wires of other vpc")
}
diff, err := db.Update(net, func() error {
net.WireId = wire.Id
return nil
})
if err != nil {
return nil, errors.Wrap(err, "update wire_id")
}
logclient.AddActionLogWithContext(ctx, net, logclient.ACT_UPDATE, diff, userCred, true)
db.OpsLog.LogEvent(net, db.ACT_UPDATE, diff, userCred)
return nil, nil
}
@@ -12,17 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package options
package compute
import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/cmd/climc/shell"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
type NetworkListOptions struct {
BaseListOptions
options.BaseListOptions
Ip string `help:"search networks that contain this IP"`
ZoneIds []string `help:"search networks in zones"`
@@ -55,11 +57,11 @@ func (opts *NetworkListOptions) GetContextId() string {
}
func (opts *NetworkListOptions) Params() (jsonutils.JSONObject, error) {
return ListStructToParams(opts)
return options.ListStructToParams(opts)
}
type NetworkUpdateOptions struct {
BaseUpdateOptions
options.BaseUpdateOptions
StartIp string `help:"Start ip"`
EndIp string `help:"end ip"`
@@ -166,7 +168,7 @@ func (opts *NetworkIpMacIdOptions) Params() (jsonutils.JSONObject, error) {
}
type NetworkIpMacListOptions struct {
BaseListOptions
options.BaseListOptions
Network string `help:"search networks" json:"network_id"`
MacAddr []string `help:"search by mac addr"`
@@ -174,7 +176,7 @@ type NetworkIpMacListOptions struct {
}
func (opts *NetworkIpMacListOptions) Params() (jsonutils.JSONObject, error) {
return ListStructToParams(opts)
return options.ListStructToParams(opts)
}
type NetworkIpMacUpdateOptions struct {
@@ -189,7 +191,7 @@ func (opts *NetworkIpMacUpdateOptions) GetId() string {
}
func (opts *NetworkIpMacUpdateOptions) Params() (jsonutils.JSONObject, error) {
return ListStructToParams(opts)
return options.ListStructToParams(opts)
}
type NetworkIpMacCreateOptions struct {
@@ -208,5 +210,19 @@ func (opts *NetworkIpMacCreateOptions) Params() (jsonutils.JSONObject, error) {
if opts.IPADDR == "" {
return nil, errors.Errorf("missing ip_addr params")
}
return ListStructToParams(opts)
return options.ListStructToParams(opts)
}
type NetworkSwitchWireOptions struct {
ID string `help:"ID or Name of resource to update"`
api.NetworkSwitchWireInput
}
func (opts *NetworkSwitchWireOptions) GetId() string {
return opts.ID
}
func (opts *NetworkSwitchWireOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(opts), nil
}