|
| 1 | +package cph |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | + |
| 14 | + "github.com/chnsz/golangsdk" |
| 15 | + |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 17 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 18 | +) |
| 19 | + |
| 20 | +var phoneDataRestoreNonUpdatableParams = []string{ |
| 21 | + "phone_id", |
| 22 | + "bucket_name", |
| 23 | + "object_path", |
| 24 | +} |
| 25 | + |
| 26 | +// @API CPH POST /v1/{project_id}/cloud-phone/phones/batch-restore |
| 27 | +// @API CPH GET /v1/{project_id}/cloud-phone/phones/{phone_id} |
| 28 | +func ResourcePhoneDataRestore() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + CreateContext: resourcePhoneDataRestoreCreate, |
| 31 | + UpdateContext: resourcePhoneDataRestoreUpdate, |
| 32 | + ReadContext: resourcePhoneDataRestoreRead, |
| 33 | + DeleteContext: resourcePhoneDataRestoreDelete, |
| 34 | + |
| 35 | + Timeouts: &schema.ResourceTimeout{ |
| 36 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 37 | + }, |
| 38 | + |
| 39 | + CustomizeDiff: config.FlexibleForceNew(phoneDataRestoreNonUpdatableParams), |
| 40 | + |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "region": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Computed: true, |
| 46 | + ForceNew: true, |
| 47 | + }, |
| 48 | + "phone_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + Description: `Specifies the phone ID.`, |
| 52 | + }, |
| 53 | + "bucket_name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + Description: `Specifies the bucket name of OBS.`, |
| 57 | + }, |
| 58 | + "object_path": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Required: true, |
| 61 | + Description: `Specifies the object path of OBS.`, |
| 62 | + }, |
| 63 | + "enable_force_new": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 67 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func resourcePhoneDataRestoreCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 74 | + cfg := meta.(*config.Config) |
| 75 | + region := cfg.GetRegion(d) |
| 76 | + |
| 77 | + client, err := cfg.NewServiceClient("cph", region) |
| 78 | + if err != nil { |
| 79 | + return diag.Errorf("error creating CPH client: %s", err) |
| 80 | + } |
| 81 | + |
| 82 | + // createPhoneDataRestore: create CPH phone data restore |
| 83 | + createPhoneDataRestoreHttpUrl := "v1/{project_id}/cloud-phone/phones/batch-restore" |
| 84 | + createPhoneDataRestorePath := client.Endpoint + createPhoneDataRestoreHttpUrl |
| 85 | + createPhoneDataRestorePath = strings.ReplaceAll(createPhoneDataRestorePath, "{project_id}", client.ProjectID) |
| 86 | + |
| 87 | + createPhoneDataRestoreOpt := golangsdk.RequestOpts{ |
| 88 | + KeepResponseBody: true, |
| 89 | + } |
| 90 | + |
| 91 | + createPhoneDataRestoreOpt.JSONBody = map[string]interface{}{ |
| 92 | + "restore_infos": []map[string]interface{}{ |
| 93 | + { |
| 94 | + "phone_id": d.Get("phone_id"), |
| 95 | + "bucket_name": d.Get("bucket_name"), |
| 96 | + "object_path": d.Get("object_path"), |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + createPhoneDataRestoreResp, err := client.Request("POST", createPhoneDataRestorePath, &createPhoneDataRestoreOpt) |
| 101 | + if err != nil { |
| 102 | + return diag.Errorf("error creating CPH phone data restore: %s", err) |
| 103 | + } |
| 104 | + |
| 105 | + resp, err := utils.FlattenResponse(createPhoneDataRestoreResp) |
| 106 | + if err != nil { |
| 107 | + return diag.FromErr(err) |
| 108 | + } |
| 109 | + id := utils.PathSearch("jobs|[0].phone_id", resp, "").(string) |
| 110 | + if id == "" { |
| 111 | + return diag.Errorf("unable to find the phone ID from the API response") |
| 112 | + } |
| 113 | + d.SetId(id) |
| 114 | + |
| 115 | + errorCode := utils.PathSearch("jobs|[0].error_code", resp, "").(string) |
| 116 | + if errorCode != "" { |
| 117 | + errorMsg := utils.PathSearch("jobs|[0].error_msg", resp, "").(string) |
| 118 | + return diag.Errorf("failed to restore CPH phone (phone_id: %s) data, error_code: %s, error_msg: %s", id, errorCode, errorMsg) |
| 119 | + } |
| 120 | + |
| 121 | + err = checkPhoneDataRestoreJobStatus(ctx, client, d.Id(), d.Timeout(schema.TimeoutCreate)) |
| 122 | + if err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func resourcePhoneDataRestoreRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func resourcePhoneDataRestoreUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func resourcePhoneDataRestoreDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 138 | + errorMsg := "Deleting CPH phone data restore resource is not supported. The resource is only removed from the state." |
| 139 | + return diag.Diagnostics{ |
| 140 | + diag.Diagnostic{ |
| 141 | + Severity: diag.Warning, |
| 142 | + Summary: errorMsg, |
| 143 | + }, |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func checkPhoneDataRestoreJobStatus(ctx context.Context, client *golangsdk.ServiceClient, id string, timeout time.Duration) error { |
| 148 | + stateConf := &resource.StateChangeConf{ |
| 149 | + Pending: []string{"PENDING"}, |
| 150 | + Target: []string{"COMPLETED"}, |
| 151 | + Refresh: jobStatusRefreshFunc(client, id), |
| 152 | + Timeout: timeout, |
| 153 | + PollInterval: 10 * timeout, |
| 154 | + Delay: 10 * time.Second, |
| 155 | + } |
| 156 | + _, err := stateConf.WaitForStateContext(ctx) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("error waiting for CPH phone data restore to be completed: %s", err) |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
0 commit comments