-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(as): support lifecycle hook callback resource (#5053)
- Loading branch information
1 parent
a49c4d4
commit 01cf612
Showing
11 changed files
with
318 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
subcategory: "Auto Scaling" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_as_lifecycle_hook_callback" | ||
description: |- | ||
Manages an AS lifecycle hook callback resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_as_lifecycle_hook_callback | ||
|
||
Manages an AS lifecycle hook callback resource within HuaweiCloud. | ||
|
||
-> 1. Callback action can only be performed when the instance lifecycle hook state is **HANGING**. | ||
<br/>2. The lifecycle hook callback is a one-time action. | ||
<br/>3. Destroying resources does not change the current state of the instance lifecycle hook. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "as_group_id" {} | ||
variable "lifecycle_action_result" {} | ||
variable "instance_id" {} | ||
variable "hook_name" {} | ||
resource "huaweicloud_as_lifecycle_hook_callback" "test" { | ||
scaling_group_id = var.as_group_id | ||
lifecycle_action_result = var.lifecycle_action_result | ||
instance_id = var.instance_id | ||
lifecycle_hook_name = var.hook_name | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the AS lifecycle hook callback. | ||
If omitted, the provider-level region will be used. Changing this will create a new resource. | ||
|
||
* `scaling_group_id` - (Required, String, ForceNew) Specifies the ID of the AS group. | ||
Changing this will create a new resource. | ||
|
||
* `lifecycle_action_result` - (Required, String, ForceNew) Specifies the lifecycle hook callback operation. | ||
The valid values are as follows: | ||
+ **ABANDON** | ||
+ **CONTINUE** | ||
+ **EXTEND**: Extend the timeout by `1` hour each time. | ||
|
||
Changing this will create a new resource. | ||
|
||
* `lifecycle_action_key` - (Optional, String, ForceNew) Specifies the lifecycle hook callback operation token. | ||
Changing this will create a new resource. | ||
|
||
* `instance_id` - (Optional, String, ForceNew) Specifies the instance ID for the lifecycle callback. | ||
Changing this will create a new resource. | ||
|
||
* `lifecycle_hook_name` - (Optional, String, ForceNew) Specifies the lifecycle hook name. | ||
Changing this will create a new resource. | ||
|
||
-> The parameters `instance_id` and `lifecycle_hook_name` must be used together, and they are mutually exclusive with | ||
the parameter `lifecycle_action_key`. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID in UUID format. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
huaweicloud/services/acceptance/as/resource_huaweicloud_as_lifecycle_hook_callback_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package as | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
// Because an instance with a status of **HANGING** cannot be obtained through resources and datasource. | ||
// So using environment variables to inject parameters for testing. | ||
func TestAccLifecycleHookCallBack_basic(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckASLifecycleActionKey(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLifecycleHookCallBack_withActionKey("ABANDON"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccLifecycleHookCallBack_withActionKey(action string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_as_lifecycle_hook_callback" "test" { | ||
scaling_group_id = "%[1]s" | ||
lifecycle_action_result = "%[2]s" | ||
lifecycle_action_key = "%[3]s" | ||
} | ||
`, acceptance.HW_AS_SCALING_GROUP_ID, action, acceptance.HW_AS_LIFECYCLE_ACTION_KEY) | ||
} | ||
|
||
func TestAccLifecycleHookCallBack_withInstanceIdAndHookName(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckASINSTANCEID(t) | ||
acceptance.TestAccPreCheckASLifecycleHookName(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccLifecycleHookCallBack_withInstanceIdAndHookName("CONTINUE"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccLifecycleHookCallBack_withInstanceIdAndHookName(action string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_as_lifecycle_hook_callback" "test" { | ||
scaling_group_id = "%[1]s" | ||
lifecycle_action_result = "%[2]s" | ||
instance_id = "%[3]s" | ||
lifecycle_hook_name = "%[4]s" | ||
} | ||
`, acceptance.HW_AS_SCALING_GROUP_ID, action, acceptance.HW_AS_INSTANCE_ID, acceptance.HW_AS_LIFECYCLE_HOOK_NAME) | ||
} |
98 changes: 98 additions & 0 deletions
98
huaweicloud/services/as/resource_huaweicloud_as_lifecycle_hook_callback.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package as | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk/openstack/autoscaling/v1/lifecyclehooks" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
) | ||
|
||
// @API AS PUT /autoscaling-api/v1/{project_id}/scaling_instance_hook/{scaling_group_id}/callback | ||
func ResourceLifecycleHookCallBack() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceLifecycleHookCallBackCreate, | ||
ReadContext: resourceLifecycleHookCallBackRead, | ||
DeleteContext: resourceLifecycleHookCallBackDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"scaling_group_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"lifecycle_action_result": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"lifecycle_action_key": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"lifecycle_action_key"}, | ||
}, | ||
"lifecycle_hook_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"lifecycle_action_key"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceLifecycleHookCallBackCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
conf = meta.(*config.Config) | ||
region = conf.GetRegion(d) | ||
) | ||
client, err := conf.AutoscalingV1Client(region) | ||
if err != nil { | ||
return diag.Errorf("error creating autoscaling client: %s", err) | ||
} | ||
|
||
callBackOpts := lifecyclehooks.CallBackOpts{ | ||
LifecycleActionResult: d.Get("lifecycle_action_result").(string), | ||
LifecycleActionKey: d.Get("lifecycle_action_key").(string), | ||
InstanceId: d.Get("instance_id").(string), | ||
LifecycleHookName: d.Get("lifecycle_hook_name").(string), | ||
} | ||
|
||
err = lifecyclehooks.CallBack(client, callBackOpts, d.Get("scaling_group_id").(string)).ExtractErr() | ||
if err != nil { | ||
return diag.Errorf("callback the AS lifecycle hook failed: %s", err) | ||
} | ||
|
||
uuId, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.Errorf("unable to generate ID: %s", err) | ||
} | ||
|
||
d.SetId(uuId) | ||
|
||
return resourceLifecycleHookCallBackRead(ctx, d, meta) | ||
} | ||
|
||
func resourceLifecycleHookCallBackRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourceLifecycleHookCallBackDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
} |
41 changes: 40 additions & 1 deletion
41
vendor/github.com/chnsz/golangsdk/openstack/autoscaling/v1/lifecyclehooks/requests.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
vendor/github.com/chnsz/golangsdk/openstack/autoscaling/v1/lifecyclehooks/results.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
vendor/github.com/chnsz/golangsdk/openstack/autoscaling/v1/lifecyclehooks/urls.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters