Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CES] use client from ctx in opentelekomcloud_ces_alarmrule #1847

Merged
merged 2 commits into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -105,7 +105,10 @@ var testCESAlarmRuleBasic = fmt.Sprintf(`
%s

resource "opentelekomcloud_compute_instance_v2" "vm_1" {
name = "instance_1"
name = "instance_1"
image_name = "Standard_Debian_11_latest"
flavor_name = "s3.large.2"

network {
uuid = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
}
Expand Down Expand Up @@ -150,7 +153,9 @@ var testCESAlarmRuleUpdate = fmt.Sprintf(`
%s

resource "opentelekomcloud_compute_instance_v2" "vm_1" {
name = "instance_1"
name = "instance_1"
image_name = "Standard_Debian_11_latest"
flavor_name = "s3.large.2"
network {
uuid = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
}
Expand Down
1 change: 1 addition & 0 deletions opentelekomcloud/services/ces/common.go
Expand Up @@ -2,4 +2,5 @@ package ces

const (
errCreationClient = "error creating CESv1 client: %w"
cesClientV1 = "ces-v1-client"
)
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cloudeyeservice/alarmrule"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
Expand Down Expand Up @@ -318,7 +319,9 @@ func getAlarmAction(d *schema.ResourceData, name string) []alarmrule.ActionOpts

func resourceAlarmRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CesV1Client(config.GetRegion(d))
client, err := common.ClientFromCtx(ctx, cesClientV1, func() (*golangsdk.ServiceClient, error) {
return config.CesV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationClient, err)
}
Expand Down Expand Up @@ -355,12 +358,15 @@ func resourceAlarmRuleCreate(ctx context.Context, d *schema.ResourceData, meta i

d.SetId(r.AlarmID)

return resourceAlarmRuleRead(ctx, d, meta)
clientCtx := common.CtxWithClient(ctx, client, cesClientV1)
return resourceAlarmRuleRead(clientCtx, d, meta)
}

func resourceAlarmRuleRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceAlarmRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CesV1Client(config.GetRegion(d))
client, err := common.ClientFromCtx(ctx, cesClientV1, func() (*golangsdk.ServiceClient, error) {
return config.CesV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationClient, err)
}
Expand Down Expand Up @@ -461,7 +467,9 @@ func resourceAlarmRuleRead(_ context.Context, d *schema.ResourceData, meta inter

func resourceAlarmRuleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CesV1Client(config.GetRegion(d))
client, err := common.ClientFromCtx(ctx, cesClientV1, func() (*golangsdk.ServiceClient, error) {
return config.CesV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationClient, err)
}
Expand All @@ -487,12 +495,15 @@ func resourceAlarmRuleUpdate(ctx context.Context, d *schema.ResourceData, meta i
return fmterr.Errorf("error updating alarm rule %s: %w", alarmRuleID, err)
}

return resourceAlarmRuleRead(ctx, d, meta)
clientCtx := common.CtxWithClient(ctx, client, cesClientV1)
return resourceAlarmRuleRead(clientCtx, d, meta)
}

func resourceAlarmRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := config.CesV1Client(config.GetRegion(d))
client, err := common.ClientFromCtx(ctx, cesClientV1, func() (*golangsdk.ServiceClient, error) {
return config.CesV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationClient, err)
}
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/ces-fix-throttling-c92557430ae94e3a.yaml
@@ -0,0 +1,4 @@
---
enhancements:
- |
**[CES]** Reduce amount of init client requests in ``resource/opentelekomcloud_ces_alarmrule`` (`#1847 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/1847>`_)