Skip to content

Commit d852d70

Browse files
committed
feat(as): add a new resource to manage policy execute
1 parent 9bddbce commit d852d70

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

Diff for: docs/resources/as_execute_policy.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Auto Scaling"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_as_execute_policy"
5+
description: |-
6+
Manages an AS execute policy resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_as_execute_policy
10+
11+
Manages an AS execute policy resource within HuaweiCloud.
12+
13+
-> 1. The current resource is a one-time resource, and destroying this resource will not change the current status.
14+
<br/>2. If the policy belongs the AS group policy, before creating the resource, you need to ensure the AS group status
15+
and the AS group policy status is **INSERVICE**.
16+
<br/>3. If the policy belongs the AS bandwidth policy, before creating the resource, you need to ensure the AS
17+
bandwidth policy status is **INSERVICE**.
18+
<br/>4. After creating the resource, you can use the datasource `huaweicloud_as_policy_execute_logs` to querry
19+
the policy execute result and details.
20+
21+
## Example Usage
22+
23+
```hcl
24+
variable "scaling_policy_id" {}
25+
26+
resource "huaweicloud_as_execute_policy" "test" {
27+
scaling_policy_id = var.scaling_policy_id
28+
}
29+
```
30+
31+
## Argument Reference
32+
33+
The following arguments are supported:
34+
35+
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
36+
If omitted, the provider-level region will be used. Changing this creates a new resource.
37+
38+
* `scaling_policy_id` - (Required, String, NonUpdatable) Specifies the AS group policy ID or AS bandwidth policy ID.
39+
40+
## Attribute Reference
41+
42+
In addition to all arguments above, the following attributes are exported:
43+
44+
* `id` - The resource ID.

Diff for: huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,7 @@ func Provider() *schema.Provider {
15101510
"huaweicloud_apig_endpoint_whitelist": apig.ResourceEndpointWhiteList(),
15111511

15121512
"huaweicloud_as_configuration": as.ResourceASConfiguration(),
1513+
"huaweicloud_as_execute_policy": as.ResourceExecutePolicy(),
15131514
"huaweicloud_as_group": as.ResourceASGroup(),
15141515
"huaweicloud_as_lifecycle_hook": as.ResourceASLifecycleHook(),
15151516
"huaweicloud_as_instance_attach": as.ResourceASInstanceAttach(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package as
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccResourceExecutePolicy_basic(t *testing.T) {
13+
// Avoid CheckDestroy because this resource is a one-time action resource and there is nothing in the destroy method.
14+
// lintignore:AT001
15+
resource.ParallelTest(t, resource.TestCase{
16+
PreCheck: func() {
17+
acceptance.TestAccPreCheck(t)
18+
acceptance.TestAccPreCheckASScalingPolicyID(t)
19+
},
20+
ProviderFactories: acceptance.TestAccProviderFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccResourceExecutePolicy_basic(),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccResourceExecutePolicy_basic() string {
30+
return fmt.Sprintf(`
31+
resource "huaweicloud_as_execute_policy" "test" {
32+
scaling_policy_id = "%s"
33+
}
34+
`, acceptance.HW_AS_SCALING_POLICY_ID)
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package as
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
"github.com/chnsz/golangsdk"
11+
12+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
13+
)
14+
15+
// @API AS POST /autoscaling-api/v1/{project_id}/scaling_policy/{scaling_policy_id}/action
16+
var nonUpdatableParams = []string{"scaling_policy_id"}
17+
18+
func ResourceExecutePolicy() *schema.Resource {
19+
return &schema.Resource{
20+
CreateContext: resourceExecutePolicyCreate,
21+
ReadContext: resourceExecutePolicyRead,
22+
UpdateContext: resourceExecutePolicyUpdate,
23+
DeleteContext: resourceExecutePolicyDelete,
24+
25+
CustomizeDiff: config.FlexibleForceNew(nonUpdatableParams),
26+
27+
Schema: map[string]*schema.Schema{
28+
"region": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
Computed: true,
32+
ForceNew: true,
33+
},
34+
"scaling_policy_id": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
Description: `Specifies the AS group policy ID or AS bandwidth policy ID.`,
38+
},
39+
},
40+
}
41+
}
42+
43+
func resourceExecutePolicyCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
44+
var (
45+
cfg = meta.(*config.Config)
46+
region = cfg.GetRegion(d)
47+
httpUrl = "autoscaling-api/v1/{project_id}/scaling_policy/{scaling_policy_id}/action"
48+
policyId = d.Get("scaling_policy_id").(string)
49+
)
50+
51+
client, err := cfg.NewServiceClient("autoscaling", region)
52+
if err != nil {
53+
return diag.Errorf("error creating AS client: %s", err)
54+
}
55+
56+
createPath := client.Endpoint + httpUrl
57+
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
58+
createPath = strings.ReplaceAll(createPath, "{scaling_policy_id}", policyId)
59+
createOpt := golangsdk.RequestOpts{
60+
KeepResponseBody: true,
61+
JSONBody: map[string]interface{}{
62+
"action": "execute",
63+
},
64+
OkCodes: []int{
65+
200, 201, 204,
66+
},
67+
}
68+
69+
_, err = client.Request("POST", createPath, &createOpt)
70+
if err != nil {
71+
return diag.Errorf("error executing the AS policy (%s): %s", policyId, err)
72+
}
73+
74+
d.SetId(policyId)
75+
d.Set("region", region)
76+
77+
return nil
78+
}
79+
80+
func resourceExecutePolicyRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
81+
return nil
82+
}
83+
84+
func resourceExecutePolicyUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
85+
return nil
86+
}
87+
88+
func resourceExecutePolicyDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
89+
return nil
90+
}

0 commit comments

Comments
 (0)