-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modelarts): add resource to operate devserver
- Loading branch information
1 parent
2f59e85
commit ccd318b
Showing
4 changed files
with
227 additions
and
4 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,44 @@ | ||
--- | ||
subcategory: "AI Development Platform (ModelArts)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_modelarts_devserver_action" | ||
description: |- | ||
Use this resource to operate ModelArts DevServer within HuaweiCloud. | ||
--- | ||
# huaweicloud_modelarts_devserver_action | ||
|
||
Use this resource to operate ModelArts DevServer within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "devserver_id" {} | ||
resource "huaweicloud_modelarts_devserver_action" "test" { | ||
devserver_id = var.devserver_id | ||
action = "start" | ||
} | ||
``` | ||
|
||
## 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. | ||
|
||
* `devserver_id` - (Required, String, ForceNew) Specifies the ID of the DevServer. | ||
Changing this creates a new resource. | ||
|
||
* `action` - (Required, String, ForceNew) Specifies the action type of the DevServer. | ||
Changing this creates a new resource. | ||
The valid values are as follows: | ||
+ **start**: The DevServer can be started only when the DevServer is stopped, stop failure, or start failure. | ||
+ **stop**: The DevServer can be stopped only when it is running or stop failure. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID. |
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
135 changes: 135 additions & 0 deletions
135
huaweicloud/services/modelarts/resource_huaweicloud_modelarts_devserver_action.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,135 @@ | ||
package modelarts | ||
|
||
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/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API ModelArts PUT /v1/{project_id}/dev-servers/{id}/start | ||
// @API ModelArts PUT /v1/{project_id}/dev-servers/{id}/stop | ||
// @API ModelArts GET /v1/{project_id}/dev-servers/{id} | ||
func ResourceDevServerAction() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceDevServerActionCreate, | ||
ReadContext: resourceDevServerActionRead, | ||
DeleteContext: resourceDevServerActionDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"devserver_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: `The ID of the DevServer.`, | ||
}, | ||
"action": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: `The action type of the DevServer.`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDevServerActionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
cfg = meta.(*config.Config) | ||
region = cfg.GetRegion(d) | ||
httpUrl = "v1/{project_id}/dev-servers/{id}/{action}" | ||
devServerId = d.Get("devserver_id").(string) | ||
action = d.Get("action").(string) | ||
actionCompletedMap = map[string]string{ | ||
"start": "RUNNING", | ||
"stop": "STOPPED", | ||
} | ||
) | ||
|
||
client, err := cfg.NewServiceClient("modelarts", region) | ||
if err != nil { | ||
return diag.Errorf("error creating ModelArts client: %s", err) | ||
} | ||
|
||
actionPath := client.Endpoint + httpUrl | ||
actionPath = strings.ReplaceAll(actionPath, "{project_id}", client.ProjectID) | ||
actionPath = strings.ReplaceAll(actionPath, "{id}", devServerId) | ||
actionPath = strings.ReplaceAll(actionPath, "{action}", action) | ||
actionOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
} | ||
if action == "start" { | ||
actionOpt.JSONBody = map[string]interface{}{} | ||
} | ||
|
||
_, err = client.Request("PUT", actionPath, &actionOpt) | ||
if err != nil { | ||
return diag.Errorf("unable to %s DevServer (%s): %s", action, devServerId, err) | ||
} | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"PENDING"}, | ||
Target: []string{"COMPLETED"}, | ||
Refresh: refreshDevServerActionStatusFunc(client, devServerId, actionCompletedMap[action]), | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
Delay: 10 * time.Second, | ||
PollInterval: 10 * time.Second, | ||
} | ||
_, err = stateConf.WaitForStateContext(ctx) | ||
if err != nil { | ||
return diag.Errorf("error waiting for DevServer (%s) to %s completed: %s", devServerId, action, err) | ||
} | ||
|
||
d.SetId(devServerId) | ||
|
||
return nil | ||
} | ||
|
||
func refreshDevServerActionStatusFunc(client *golangsdk.ServiceClient, devServerId string, target string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
respBody, err := GetDevServerById(client, devServerId) | ||
if err != nil { | ||
return nil, "ERROR", err | ||
} | ||
|
||
status := utils.PathSearch("status", respBody, "").(string) | ||
if utils.StrSliceContains([]string{"START_FAILED", "ERROR", "STOP_FAILED"}, status) { | ||
return respBody, "ERROR", fmt.Errorf("unexpected status (%s)", status) | ||
} | ||
|
||
if status == target { | ||
return respBody, "COMPLETED", nil | ||
} | ||
return "continue", "PENDING", nil | ||
} | ||
} | ||
|
||
func resourceDevServerActionRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourceDevServerActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
errorMsg := `This resource is only a one-time action resource for operating the DevServer. Deleting this resource will | ||
not clear the corresponding request record, but will only remove the resource information from the tfstate file.` | ||
return diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: errorMsg, | ||
}, | ||
} | ||
} |