-
Notifications
You must be signed in to change notification settings - Fork 165
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
feat(cph): add resource cph phone data restore #6244
Open
luoping-12345
wants to merge
1
commit into
huaweicloud:master
Choose a base branch
from
luoping-12345:cph_phone_data_restore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,53 @@ | ||
--- | ||
subcategory: "Cloud Phone (CPH)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_cph_phone_data_restore" | ||
description: |- | ||
Manages a CPH phone data restore resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_cph_phone_data_restore | ||
|
||
Manages a CPH phone data restore resource within HuaweiCloud. | ||
|
||
-> The current resource is a one-time resource, and destroying this resource is only removed from the state. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "phone_id" {} | ||
variable "bucket_name" {} | ||
variable "object_path" {} | ||
|
||
resource "huaweicloud_cph_phone_data_restore" "test" { | ||
phone_id = var.phone_id | ||
bucket_name = var.bucket_name | ||
object_path = var.object_path | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource. | ||
If omitted, the provider-level region will be used. | ||
Changing this creates a new resource. | ||
|
||
* `phone_id` - (Required, String) Specifies the phone ID. | ||
|
||
* `bucket_name` - (Required, String) Specifies the bucket name of OBS. | ||
|
||
* `object_path` - (Required, String) Specifies the object path of OBS. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID. | ||
|
||
## Timeouts | ||
|
||
This resource provides the following timeouts configuration options: | ||
|
||
* `create` - Default is 30 minutes. |
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
51 changes: 51 additions & 0 deletions
51
huaweicloud/services/acceptance/cph/resource_huaweicloud_cph_phone_data_restore_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,51 @@ | ||
package cph | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccCphPhoneDataRestore_basic(t *testing.T) { | ||
name := acceptance.RandomAccResourceName() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPrecheckCphObsBucketName(t) | ||
acceptance.TestAccPrecheckCphAdbObjectPath(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testCphPhoneDataRestore_basic(name), | ||
}, | ||
{ | ||
Config: testCphServerBase(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
waitForDeletionCooldownComplete(), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCphPhoneDataRestore_basic(name string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
|
||
data "huaweicloud_cph_phones" "test" { | ||
server_id = huaweicloud_cph_server.test.id | ||
} | ||
|
||
resource "huaweicloud_cph_phone_data_restore" "test" { | ||
phone_id = data.huaweicloud_cph_phones.test.phones[0].phone_id | ||
bucket_name = "%[2]s" | ||
object_path = "%[3]s" | ||
} | ||
`, testCphServer_basic(name), acceptance.HW_CPH_OBS_BUCKET_NAME, acceptance.HW_CPH_OBS_OBJECT_PATH) | ||
} |
161 changes: 161 additions & 0 deletions
161
huaweicloud/services/cph/resource_huaweicloud_cph_phone_data_restore.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,161 @@ | ||
package cph | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"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" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
var phoneDataRestoreNonUpdatableParams = []string{ | ||
"phone_id", | ||
"bucket_name", | ||
"object_path", | ||
} | ||
|
||
// @API CPH POST /v1/{project_id}/cloud-phone/phones/batch-restore | ||
// @API CPH GET /v1/{project_id}/cloud-phone/phones/{phone_id} | ||
func ResourcePhoneDataRestore() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourcePhoneDataRestoreCreate, | ||
UpdateContext: resourcePhoneDataRestoreUpdate, | ||
ReadContext: resourcePhoneDataRestoreRead, | ||
DeleteContext: resourcePhoneDataRestoreDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
CustomizeDiff: config.FlexibleForceNew(phoneDataRestoreNonUpdatableParams), | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"phone_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the phone ID.`, | ||
}, | ||
"bucket_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the bucket name of OBS.`, | ||
}, | ||
"object_path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the object path of OBS.`, | ||
}, | ||
"enable_force_new": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), | ||
Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePhoneDataRestoreCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
|
||
client, err := cfg.NewServiceClient("cph", region) | ||
if err != nil { | ||
return diag.Errorf("error creating CPH client: %s", err) | ||
} | ||
|
||
// createPhoneDataRestore: create CPH phone data restore | ||
createPhoneDataRestoreHttpUrl := "v1/{project_id}/cloud-phone/phones/batch-restore" | ||
createPhoneDataRestorePath := client.Endpoint + createPhoneDataRestoreHttpUrl | ||
createPhoneDataRestorePath = strings.ReplaceAll(createPhoneDataRestorePath, "{project_id}", client.ProjectID) | ||
|
||
createPhoneDataRestoreOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
} | ||
|
||
createPhoneDataRestoreOpt.JSONBody = map[string]interface{}{ | ||
"restore_infos": []map[string]interface{}{ | ||
{ | ||
"phone_id": d.Get("phone_id"), | ||
"bucket_name": d.Get("bucket_name"), | ||
"object_path": d.Get("object_path"), | ||
}, | ||
}, | ||
} | ||
createPhoneDataRestoreResp, err := client.Request("POST", createPhoneDataRestorePath, &createPhoneDataRestoreOpt) | ||
if err != nil { | ||
return diag.Errorf("error creating CPH phone data restore: %s", err) | ||
} | ||
|
||
resp, err := utils.FlattenResponse(createPhoneDataRestoreResp) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
id := utils.PathSearch("jobs|[0].phone_id", resp, "").(string) | ||
if id == "" { | ||
return diag.Errorf("unable to find the phone ID from the API response") | ||
} | ||
d.SetId(id) | ||
|
||
errorCode := utils.PathSearch("jobs|[0].error_code", resp, "").(string) | ||
if errorCode != "" { | ||
errorMsg := utils.PathSearch("jobs|[0].error_msg", resp, "").(string) | ||
return diag.Errorf("failed to restore CPH phone (phone_id: %s) data, error_code: %s, error_msg: %s", id, errorCode, errorMsg) | ||
} | ||
|
||
err = checkPhoneDataRestoreJobStatus(ctx, client, d.Id(), d.Timeout(schema.TimeoutCreate)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourcePhoneDataRestoreRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourcePhoneDataRestoreUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourcePhoneDataRestoreDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
errorMsg := "Deleting CPH phone data restore resource is not supported. The resource is only removed from the state." | ||
return diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: errorMsg, | ||
}, | ||
} | ||
} | ||
|
||
func checkPhoneDataRestoreJobStatus(ctx context.Context, client *golangsdk.ServiceClient, id string, timeout time.Duration) error { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"PENDING"}, | ||
Target: []string{"COMPLETED"}, | ||
Refresh: jobStatusRefreshFunc(client, id), | ||
Timeout: timeout, | ||
PollInterval: 10 * timeout, | ||
Delay: 10 * time.Second, | ||
} | ||
_, err := stateConf.WaitForStateContext(ctx) | ||
if err != nil { | ||
return fmt.Errorf("error waiting for CPH phone data restore to be completed: %s", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.