From 4bd398db565e47d1594ae11a66431faf663f9008 Mon Sep 17 00:00:00 2001 From: "bistuzx@163.com" Date: Tue, 29 Dec 2020 21:41:26 +0800 Subject: [PATCH 1/5] add meter budget&event&reservation module --- cmd/climc/shell/meter/budgets.go | 30 ++++++ cmd/climc/shell/meter/meter-events.go | 29 ++++++ cmd/climc/shell/meter/reservations.go | 26 ++++++ pkg/mcclient/modules/mod_budgets.go | 32 +++++++ pkg/mcclient/modules/mod_meter_events.go | 28 ++++++ pkg/mcclient/modules/mod_reservations.go | 31 +++++++ pkg/mcclient/options/meter/budgets.go | 103 +++++++++++++++++++++ pkg/mcclient/options/meter/doc.go | 1 + pkg/mcclient/options/meter/reservations.go | 21 +++++ 9 files changed, 301 insertions(+) create mode 100644 cmd/climc/shell/meter/budgets.go create mode 100644 cmd/climc/shell/meter/meter-events.go create mode 100644 cmd/climc/shell/meter/reservations.go create mode 100644 pkg/mcclient/modules/mod_budgets.go create mode 100644 pkg/mcclient/modules/mod_meter_events.go create mode 100644 pkg/mcclient/modules/mod_reservations.go create mode 100644 pkg/mcclient/options/meter/budgets.go create mode 100644 pkg/mcclient/options/meter/doc.go create mode 100644 pkg/mcclient/options/meter/reservations.go diff --git a/cmd/climc/shell/meter/budgets.go b/cmd/climc/shell/meter/budgets.go new file mode 100644 index 0000000000..7a5c55da05 --- /dev/null +++ b/cmd/climc/shell/meter/budgets.go @@ -0,0 +1,30 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package meter + +import ( + "yunion.io/x/onecloud/cmd/climc/shell" + "yunion.io/x/onecloud/pkg/mcclient/modules" + options "yunion.io/x/onecloud/pkg/mcclient/options/meter" +) + +func init() { + cmd := shell.NewResourceCmd(&modules.Budgets).WithKeyword("budget") + cmd.List(new(options.BudgetListOptions)) + cmd.Create(new(options.BudgetCreateOptions)) + cmd.Update(new(options.BudgetUpdateOptions)) + cmd.Delete(new(options.BudgetDeleteOptions)) + cmd.Perform("sync-alerts", new(options.BudgetSyncAlertsOptions)) +} diff --git a/cmd/climc/shell/meter/meter-events.go b/cmd/climc/shell/meter/meter-events.go new file mode 100644 index 0000000000..db1bd258c9 --- /dev/null +++ b/cmd/climc/shell/meter/meter-events.go @@ -0,0 +1,29 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package meter + +import ( + "yunion.io/x/onecloud/cmd/climc/shell/events" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +func doMeterEventList(s *mcclient.ClientSession, args *events.EventListOptions) error { + return events.DoEventList(modules.MeterLogs, s, args) +} + +func init() { + R(&events.EventListOptions{}, "meter-event-show", "Show operation meter event logs", doMeterEventList) +} diff --git a/cmd/climc/shell/meter/reservations.go b/cmd/climc/shell/meter/reservations.go new file mode 100644 index 0000000000..eeb972145a --- /dev/null +++ b/cmd/climc/shell/meter/reservations.go @@ -0,0 +1,26 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package meter + +import ( + "yunion.io/x/onecloud/cmd/climc/shell" + "yunion.io/x/onecloud/pkg/mcclient/modules" + options "yunion.io/x/onecloud/pkg/mcclient/options/meter" +) + +func init() { + cmd := shell.NewResourceCmd(&modules.Reservations).WithKeyword("reservation") + cmd.List(new(options.ReservationListOptions)) +} diff --git a/pkg/mcclient/modules/mod_budgets.go b/pkg/mcclient/modules/mod_budgets.go new file mode 100644 index 0000000000..847a1044bb --- /dev/null +++ b/pkg/mcclient/modules/mod_budgets.go @@ -0,0 +1,32 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package modules + +import "yunion.io/x/onecloud/pkg/mcclient/modulebase" + +var ( + Budgets modulebase.ResourceManager +) + +func init() { + Budgets = NewMeterManager("budget", "budgets", + []string{"period_type", "start_time", "end_time", "brand", "cloudaccount_id", "cloudaccount", + "cloudprovider_id", "cloudprovider_name", "region_id", "region", + "domain_id_filter", "domain_filter", "project_id_filter", "project_filter", + "resource_type", "currency", "amount", "alerts", "availability_status", "budget_scope"}, + []string{}, + ) + register(&Budgets) +} diff --git a/pkg/mcclient/modules/mod_meter_events.go b/pkg/mcclient/modules/mod_meter_events.go new file mode 100644 index 0000000000..4472e80755 --- /dev/null +++ b/pkg/mcclient/modules/mod_meter_events.go @@ -0,0 +1,28 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package modules + +import "yunion.io/x/onecloud/pkg/mcclient/modulebase" + +var ( + MeterLogs modulebase.ResourceManager +) + +func init() { + MeterLogs = NewMeterManager("meter_event", "meter_events", + []string{"id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes"}, + []string{}) + register(&MeterLogs) +} diff --git a/pkg/mcclient/modules/mod_reservations.go b/pkg/mcclient/modules/mod_reservations.go new file mode 100644 index 0000000000..8474a0e9b8 --- /dev/null +++ b/pkg/mcclient/modules/mod_reservations.go @@ -0,0 +1,31 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package modules + +import "yunion.io/x/onecloud/pkg/mcclient/modulebase" + +var ( + Reservations modulebase.ResourceManager +) + +func init() { + Reservations = NewMeterManager("reservation", "reservations", + []string{"cloudaccount_id", "resource_type", "reservation_years", "lookback_days", "payment_option", + "offering_class", "category", "spec", "instance_amount", "monthly_savings_amount", + "monthly_usage_percentage", "monthly_cost", "currency", "details"}, + []string{}, + ) + register(&Reservations) +} diff --git a/pkg/mcclient/options/meter/budgets.go b/pkg/mcclient/options/meter/budgets.go new file mode 100644 index 0000000000..164257b142 --- /dev/null +++ b/pkg/mcclient/options/meter/budgets.go @@ -0,0 +1,103 @@ +package meter + +import ( + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type BudgetListOptions struct { + options.BaseListOptions +} + +func (opt *BudgetListOptions) Params() (jsonutils.JSONObject, error) { + return options.StructToParams(opt) +} + +type BudgetCreateOptions struct { + Name string + PeriodType string `help:"period of budget, example:year/quarter/month/day" json:"period_type"` + StartTime string `help:"start time of budget" json:"start_time"` + EndTime string `help:"end time of budget" json:"end_time"` + + Brand string `help:"brand of budget cost filter" json:"brand"` + CloudaccountId string `help:"cloudaccount id of budget cost filter" json:"cloudaccount_id"` + Cloudaccount string `help:"cloudaccount name of budget cost filter" json:"cloudaccount"` + CloudproviderId string `help:"cloudprovider id of budget cost filter" json:"cloudprovider_id"` + CloudproviderName string `help:"cloudprovider name of budget cost filter" json:"cloudprovider_name"` + RegionId string `help:"region id of budget cost filter" json:"region_id"` + Region string `help:"region name of budget cost filter" json:"region"` + DomainIdFilter string `help:"domain id of budget cost filter" json:"domain_id_filter"` + ProjectIdFilter string `help:"project id of budget cost filter" json:"project_id_filter"` + DomainFilter string `help:"domain of budget cost filter" json:"domain_filter"` + ProjectFilter string `help:"project of budget cost filter" json:"project_filter"` + ResourceType string `help:"resource type of budget cost filter" json:"resource_type"` + Currency string `help:"currency of budget cost filter" json:"currency"` + + Amount float64 `help:"amount of budget cost" json:"amount"` +} + +func (opt *BudgetCreateOptions) Params() (jsonutils.JSONObject, error) { + return options.StructToParams(opt) +} + +type BudgetUpdateOptions struct { + ID string `help:"ID of budget" json:"-"` + + Brand string `help:"brand of budget cost filter" json:"brand"` + CloudaccountId string `help:"cloudaccount id of budget cost filter" json:"cloudaccount_id"` + Cloudaccount string `help:"cloudaccount name of budget cost filter" json:"cloudaccount"` + CloudproviderId string `help:"cloudprovider id of budget cost filter" json:"cloudprovider_id"` + CloudproviderName string `help:"cloudprovider name of budget cost filter" json:"cloudprovider_name"` + RegionId string `help:"region id of budget cost filter" json:"region_id"` + Region string `help:"region name of budget cost filter" json:"region"` + DomainIdFilter string `help:"domain id of budget cost filter" json:"domain_id_filter"` + ProjectIdFilter string `help:"project id of budget cost filter" json:"project_id_filter"` + DomainFilter string `help:"domain of budget cost filter" json:"domain_filter"` + ProjectFilter string `help:"project of budget cost filter" json:"project_filter"` + ResourceType string `help:"resource type of budget cost filter" json:"resource_type"` + Currency string `help:"currency of budget cost filter" json:"currency"` + + Amount float64 `help:"amount of budget cost" json:"amount"` +} + +func (opt *BudgetUpdateOptions) Params() (jsonutils.JSONObject, error) { + return options.StructToParams(opt) +} + +func (opt *BudgetUpdateOptions) GetId() string { + return opt.ID +} + +type BudgetDeleteOptions struct { + ID string `help:"ID of budget" json:"-"` +} + +func (opt *BudgetDeleteOptions) Params() (jsonutils.JSONObject, error) { + return options.StructToParams(opt) +} + +func (opt *BudgetDeleteOptions) GetId() string { + return opt.ID +} + +type BudgetSyncAlertsOptions struct { + ID string `help:"ID of budget" json:"-"` + + Alerts []BudgetAlert `help:"alerts of budget" json:"alerts"` +} + +type BudgetAlert struct { + AlertType string `help:"alert type, example:cost" json:"alert_type"` + + AlertThresholdType string `help:"alert type, example:percert/amount" json:"alert_threshold_type"` + AlertThreshold float64 `help:"alert amount" json:"alert_threshold"` + UserIds []string `help:"id of users" json:"user_ids"` +} + +func (opt *BudgetSyncAlertsOptions) Params() (jsonutils.JSONObject, error) { + return options.StructToParams(opt) +} + +func (opt *BudgetSyncAlertsOptions) GetId() string { + return opt.ID +} diff --git a/pkg/mcclient/options/meter/doc.go b/pkg/mcclient/options/meter/doc.go new file mode 100644 index 0000000000..dba89c9562 --- /dev/null +++ b/pkg/mcclient/options/meter/doc.go @@ -0,0 +1 @@ +package meter // import "yunion.io/x/onecloud/pkg/mcclient/options/meter" diff --git a/pkg/mcclient/options/meter/reservations.go b/pkg/mcclient/options/meter/reservations.go new file mode 100644 index 0000000000..b11b9f9abc --- /dev/null +++ b/pkg/mcclient/options/meter/reservations.go @@ -0,0 +1,21 @@ +package meter + +import ( + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type ReservationListOptions struct { + options.BaseListOptions + + CloudaccountId string `help:"cloudaccount id of reservation" json:"cloudaccount_id"` + ResourceType string `help:"resource type of reservation" json:"resource_type"` + ReservationYears string `help:"number of reservation years" json:"reservation_years"` + LookbackDays string `help:"number of previous days will be consider" json:"lookback_days"` + PaymentOption string `help:"payment option of reservation, example:all_upfront/partial_upfront/no_upfront" json:"payment_option"` + OfferingClass string `help:"offering class of reservation, example:standard/convertible" json:"offering_class"` +} + +func (opt *ReservationListOptions) Params() (jsonutils.JSONObject, error) { + return options.StructToParams(opt) +} From 005ed23fa41f11ae18464be82efb6973f8034147 Mon Sep 17 00:00:00 2001 From: "bistuzx@163.com" Date: Tue, 29 Dec 2020 21:46:07 +0800 Subject: [PATCH 2/5] fix reservation import format --- pkg/mcclient/options/meter/reservations.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/mcclient/options/meter/reservations.go b/pkg/mcclient/options/meter/reservations.go index b11b9f9abc..fe67c6d173 100644 --- a/pkg/mcclient/options/meter/reservations.go +++ b/pkg/mcclient/options/meter/reservations.go @@ -2,6 +2,7 @@ package meter import ( "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient/options" ) From 6af676b93a538d661ce564a99755d2c0b55aafe8 Mon Sep 17 00:00:00 2001 From: "bistuzx@163.com" Date: Tue, 29 Dec 2020 22:02:34 +0800 Subject: [PATCH 3/5] fix budget import format --- pkg/mcclient/options/meter/budgets.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/mcclient/options/meter/budgets.go b/pkg/mcclient/options/meter/budgets.go index 164257b142..69f84471e5 100644 --- a/pkg/mcclient/options/meter/budgets.go +++ b/pkg/mcclient/options/meter/budgets.go @@ -2,6 +2,7 @@ package meter import ( "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient/options" ) From 5f7d848723075985dbd0e4263c6ae402dd162112 Mon Sep 17 00:00:00 2001 From: "bistuzx@163.com" Date: Tue, 29 Dec 2020 22:16:57 +0800 Subject: [PATCH 4/5] add cost&prepaid_cost to reservation --- pkg/mcclient/modules/mod_reservations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/mcclient/modules/mod_reservations.go b/pkg/mcclient/modules/mod_reservations.go index 8474a0e9b8..82dceb5397 100644 --- a/pkg/mcclient/modules/mod_reservations.go +++ b/pkg/mcclient/modules/mod_reservations.go @@ -24,7 +24,7 @@ func init() { Reservations = NewMeterManager("reservation", "reservations", []string{"cloudaccount_id", "resource_type", "reservation_years", "lookback_days", "payment_option", "offering_class", "category", "spec", "instance_amount", "monthly_savings_amount", - "monthly_usage_percentage", "monthly_cost", "currency", "details"}, + "monthly_usage_percentage", "monthly_cost", "prepaid_cost", "cost", "currency", "details"}, []string{}, ) register(&Reservations) From d5d72a977ef540dcf464088e7851b8a221f153d8 Mon Sep 17 00:00:00 2001 From: "bistuzx@163.com" Date: Tue, 29 Dec 2020 22:33:21 +0800 Subject: [PATCH 5/5] fix reservation columns --- pkg/mcclient/modules/mod_reservations.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/mcclient/modules/mod_reservations.go b/pkg/mcclient/modules/mod_reservations.go index 82dceb5397..886ae1859c 100644 --- a/pkg/mcclient/modules/mod_reservations.go +++ b/pkg/mcclient/modules/mod_reservations.go @@ -23,8 +23,9 @@ var ( func init() { Reservations = NewMeterManager("reservation", "reservations", []string{"cloudaccount_id", "resource_type", "reservation_years", "lookback_days", "payment_option", - "offering_class", "category", "spec", "instance_amount", "monthly_savings_amount", - "monthly_usage_percentage", "monthly_cost", "prepaid_cost", "cost", "currency", "details"}, + "offering_class", "category", "spec", "instance_amount", "monthly_savings_amount", "upfront_cost", + "average_utilization", "monthly_cost", "total_savings_amount", "total_savings_percentage", + "currency", "details"}, []string{}, ) register(&Reservations)