From 1c32ce78a66824e7ee6331a32607086088f2b608 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Thu, 9 Mar 2023 11:08:39 +0800 Subject: [PATCH] feature: allow network update wire_id (#16124) Co-authored-by: Qiu Jian --- cmd/climc/shell/compute/network_ip_mac.go | 12 +++--- cmd/climc/shell/compute/networks.go | 22 +++++----- pkg/apis/compute/network.go | 9 ++++ pkg/compute/models/networks.go | 42 +++++++++++++++++++ pkg/mcclient/options/{ => compute}/network.go | 32 ++++++++++---- 5 files changed, 93 insertions(+), 24 deletions(-) rename pkg/mcclient/options/{ => compute}/network.go (90%) diff --git a/cmd/climc/shell/compute/network_ip_mac.go b/cmd/climc/shell/compute/network_ip_mac.go index 3cda210db3..db9b841180 100644 --- a/cmd/climc/shell/compute/network_ip_mac.go +++ b/cmd/climc/shell/compute/network_ip_mac.go @@ -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"` diff --git a/cmd/climc/shell/compute/networks.go b/cmd/climc/shell/compute/networks.go index 0bb6754b20..5765d18db7 100644 --- a/cmd/climc/shell/compute/networks.go +++ b/cmd/climc/shell/compute/networks.go @@ -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"` diff --git a/pkg/apis/compute/network.go b/pkg/apis/compute/network.go index 9aa1255913..57f71afa02 100644 --- a/pkg/apis/compute/network.go +++ b/pkg/apis/compute/network.go @@ -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"` +} diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index e989999ed7..45478d0e59 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -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 +} diff --git a/pkg/mcclient/options/network.go b/pkg/mcclient/options/compute/network.go similarity index 90% rename from pkg/mcclient/options/network.go rename to pkg/mcclient/options/compute/network.go index 95c273ab83..277ae46aa4 100644 --- a/pkg/mcclient/options/network.go +++ b/pkg/mcclient/options/compute/network.go @@ -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 }