diff --git a/cmd/climc/shell/cloudaccounts.go b/cmd/climc/shell/cloudaccounts.go index 3f87b8028e..812fc76a19 100644 --- a/cmd/climc/shell/cloudaccounts.go +++ b/cmd/climc/shell/cloudaccounts.go @@ -103,8 +103,15 @@ func init() { }) R(&options.SAWSCloudAccountCreateOptions{}, "cloud-account-create-aws", "Create an AWS cloud account", func(s *mcclient.ClientSession, args *options.SAWSCloudAccountCreateOptions) error { - params := jsonutils.Marshal(args) - params.(*jsonutils.JSONDict).Add(jsonutils.NewString("Aws"), "provider") + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + options := jsonutils.NewDict() + if len(args.OptionsBillingReportBucket) > 0 { + options.Add(jsonutils.NewString(args.OptionsBillingReportBucket), "billing_report_bucket") + } + if options.Size() > 0 { + params.Add(options, "options") + } + params.Add(jsonutils.NewString("Aws"), "provider") result, err := modules.Cloudaccounts.Create(s, params) if err != nil { return err @@ -147,24 +154,121 @@ func init() { Desc string `help:"Description"` } R(&CloudaccountUpdateOptions{}, "cloud-account-update", "Update a cloud account", func(s *mcclient.ClientSession, args *CloudaccountUpdateOptions) error { - params := jsonutils.NewDict() - if len(args.Name) > 0 { - params.Add(jsonutils.NewString(args.Name), "name") + return fmt.Errorf("obsolete, please try cloud-account-update-xxx, where xxx is vmware, aliyun, azure, qcloud, aws, openstack, huawei etc.") + }) + + R(&options.SVMwareCloudAccountUpdateOptions{}, "cloud-account-update-vmware", "update a vmware cloud account", func(s *mcclient.ClientSession, args *options.SVMwareCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + if params.Size() == 0 { + return InvalidUpdateError() } - if len(args.AccessUrl) > 0 { - params.Add(jsonutils.NewString(args.AccessUrl), "access_url") + result, err := modules.Cloudaccounts.Update(s, args.ID, params) + if err != nil { + return err } - if len(args.BalanceKey) > 0 { - params.Add(jsonutils.NewString(args.BalanceKey), "balance_key") - } else if args.RemoveBalanceKey { - params.Add(jsonutils.NewString(""), "balance_key") + printObject(result) + return nil + }) + + R(&options.SAliyunCloudAccountUpdateOptions{}, "cloud-account-update-aliyun", "update an Aliyun cloud account", func(s *mcclient.ClientSession, args *options.SAliyunCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + if params.Size() == 0 { + return InvalidUpdateError() } - if args.SyncIntervalSeconds > 0 { - params.Add(jsonutils.NewInt(int64(args.SyncIntervalSeconds)), "sync_interval_seconds") + result, err := modules.Cloudaccounts.Update(s, args.ID, params) + if err != nil { + return err } - if len(args.Desc) > 0 { - params.Add(jsonutils.NewString(args.Desc), "description") + printObject(result) + return nil + }) + + R(&options.SAzureCloudAccountUpdateOptions{}, "cloud-account-update-azure", "update an Azure cloud account", func(s *mcclient.ClientSession, args *options.SAzureCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + + options := jsonutils.NewDict() + if len(args.OptionsBalanceKey) > 0 { + options.Add(jsonutils.NewString(args.OptionsBalanceKey), "balance_key") } + if options.Size() > 0 { + params.Add(options, "options") + } + removeOptions := make([]string, 0) + if args.RemoveOptionsBalanceKey { + removeOptions = append(removeOptions, "balance_key") + } + if len(removeOptions) > 0 { + params.Add(jsonutils.NewStringArray(removeOptions), "remove_options") + } + + if params.Size() == 0 { + return InvalidUpdateError() + } + result, err := modules.Cloudaccounts.Update(s, args.ID, params) + if err != nil { + return err + } + printObject(result) + return nil + }) + + R(&options.SQcloudCloudAccountUpdateOptions{}, "cloud-account-update-qcloud", "update a Tencent cloud account", func(s *mcclient.ClientSession, args *options.SQcloudCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + if params.Size() == 0 { + return InvalidUpdateError() + } + result, err := modules.Cloudaccounts.Update(s, args.ID, params) + if err != nil { + return err + } + printObject(result) + return nil + }) + + R(&options.SAWSCloudAccountUpdateOptions{}, "cloud-account-update-aws", "update an AWS cloud account", func(s *mcclient.ClientSession, args *options.SAWSCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + + options := jsonutils.NewDict() + if len(args.OptionsBillingReportBucket) > 0 { + options.Add(jsonutils.NewString(args.OptionsBillingReportBucket), "billing_report_bucket") + } + if options.Size() > 0 { + params.Add(options, "options") + } + removeOptions := make([]string, 0) + if args.RemoveOptionsBillingReportBucket { + removeOptions = append(removeOptions, "billing_report_bucket") + } + if len(removeOptions) > 0 { + params.Add(jsonutils.NewStringArray(removeOptions), "remove_options") + } + + if params.Size() == 0 { + return InvalidUpdateError() + } + result, err := modules.Cloudaccounts.Update(s, args.ID, params) + if err != nil { + return err + } + printObject(result) + return nil + }) + + R(&options.SOpenStackCloudAccountUpdateOptions{}, "cloud-account-update-openstack", "update an AWS cloud account", func(s *mcclient.ClientSession, args *options.SOpenStackCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) + if params.Size() == 0 { + return InvalidUpdateError() + } + result, err := modules.Cloudaccounts.Update(s, args.ID, params) + if err != nil { + return err + } + printObject(result) + return nil + }) + + R(&options.SHuaweiCloudAccountUpdateOptions{}, "cloud-account-update-huawei", "update a Huawei cloud account", func(s *mcclient.ClientSession, args *options.SHuaweiCloudAccountUpdateOptions) error { + params := jsonutils.Marshal(args).(*jsonutils.JSONDict) if params.Size() == 0 { return InvalidUpdateError() } diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index 7f9090168c..8291d5ed29 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -53,7 +53,7 @@ type SCloudaccount struct { Account string `width:"128" charset:"ascii" nullable:"false" list:"admin" create:"admin_required"` // Column(VARCHAR(64, charset='ascii'), nullable=False) Secret string `width:"256" charset:"ascii" nullable:"false" list:"admin" create:"admin_required"` // Column(VARCHAR(256, charset='ascii'), nullable=False) - BalanceKey string `width:"256" charset:"ascii" nullable:"true" list:"admin" update:"admin" create:"admin_optional"` + // BalanceKey string `width:"256" charset:"ascii" nullable:"true" list:"admin" update:"admin" create:"admin_optional"` IsPublicCloud *bool `nullable:"false" get:"user" create:"optional" list:"user" default:"true"` IsOnPremise bool `nullable:"false" get:"user" create:"optional" list:"user" default:"false"` @@ -72,6 +72,8 @@ type SCloudaccount struct { Version string `width:"32" charset:"ascii" nullable:"true" list:"admin"` // Column(VARCHAR(32, charset='ascii'), nullable=True) Sysinfo jsonutils.JSONObject `get:"admin"` // Column(JSONEncodedDict, nullable=True) + + Options *jsonutils.JSONDict `get:"admin" create:"admin_optional" update:"admin"` } func (self *SCloudaccountManager) AllowListItems(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { @@ -179,6 +181,25 @@ func (self *SCloudaccount) ValidateUpdateData(ctx context.Context, userCred mccl } data.Set("sync_interval_seconds", jsonutils.NewInt(syncIntervalSecs)) } + if data.Contains("options") || data.Contains("remove_options") { + toRemoveKeys, _ := data.GetArray("remove_options") + removes := make([]string, 0) + if len(toRemoveKeys) > 0 { + for i := range toRemoveKeys { + key, _ := toRemoveKeys[i].GetString() + removes = append(removes, key) + } + } + var optionsJson *jsonutils.JSONDict + if self.Options != nil { + optionsJson = self.Options.CopyExcludes(removes...) + } else { + optionsJson = jsonutils.NewDict() + } + toUpdate, _ := data.Get("options") + optionsJson.Update(toUpdate) + data.Set("options", optionsJson) + } return self.SEnabledStatusStandaloneResourceBase.ValidateUpdateData(ctx, userCred, query, data) } diff --git a/pkg/mcclient/options/cloudaccounts.go b/pkg/mcclient/options/cloudaccounts.go index 95d29219e6..1e6c285c8e 100644 --- a/pkg/mcclient/options/cloudaccounts.go +++ b/pkg/mcclient/options/cloudaccounts.go @@ -91,6 +91,8 @@ type SQcloudCloudAccountCreateOptions struct { type SAWSCloudAccountCreateOptions struct { SCloudAccountCreateBaseOptions SAccessKeyCredentialWithEnvironment + + OptionsBillingReportBucket string `help:"bucket that stores billing report" json:"-"` } type SOpenStackCloudAccountCreateOptions struct { @@ -143,3 +145,49 @@ type SHuaweiCloudAccountUpdateCredentialOptions struct { SCloudAccountUpdateCredentialBaseOptions SAccessKeyCredential } + +// update + +type SCloudAccountUpdateBaseOptions struct { + ID string `help:"ID or Name of cloud account" json:"-"` + Name string `help:"New name to update"` + + SyncIntervalSeconds int `help:"auto synchornize interval in seconds"` + AutoCreateProject *bool `help:"automatically create local project for new remote project"` + + Desc string `help:"Description" json:"description" token:"desc"` +} + +type SVMwareCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions +} + +type SAliyunCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions +} + +type SAzureCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions + + OptionsBalanceKey string `help:"update cloud balance account key, such as Azure EA key" json:"-"` + RemoveOptionsBalanceKey bool `help:"remove cloud blance account key" json:"-"` +} + +type SQcloudCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions +} + +type SAWSCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions + + OptionsBillingReportBucket string `help:"update AWS S3 bucket that stores account billing report" json:"-"` + RemoveOptionsBillingReportBucket bool `help:"remote AWS S3 bucket that stores account billing report" json:"-"` +} + +type SOpenStackCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions +} + +type SHuaweiCloudAccountUpdateOptions struct { + SCloudAccountUpdateBaseOptions +}