Skip to content

Commit 7c64f74

Browse files
committed
feat(modelarts): add new action resource for service
1 parent 27bfa79 commit 7c64f74

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
subcategory: "AI Development Platform (ModelArts)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_modelartsv2_service_action"
5+
description: |-
6+
Use this resource to operate the ModelArts service within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_modelartsv2_service_action
10+
11+
Use this resource to operate the ModelArts service within HuaweiCloud.
12+
13+
-> This resource is only a one-time action resource for operating the service. Deleting this resource will not undo
14+
action that has been performed, but will only remove the resource information from the tfstate file.
15+
16+
## Example Usage
17+
18+
```hcl
19+
variable "service_id" {}
20+
21+
resource "huaweicloud_modelartsv2_service_action" "test" {
22+
service_id = var.service_id
23+
action = "stop"
24+
}
25+
```
26+
27+
## Argument Reference
28+
29+
The following arguments are supported:
30+
31+
* `region` - (Optional, String, ForceNew) Specifies the region where the service is located.
32+
If omitted, the provider-level region will be used.
33+
Changing this creates a new resource.
34+
35+
* `service_id` - (Required, String, NonUpdatable) Specifies the service ID to be operated.
36+
37+
* `action` - (Required, String, NonUpdatable) Specifies the action type.
38+
The valid values are as follows:
39+
+ **start**
40+
+ **stop**
41+
+ **interrupt**
42+
43+
## Attribute Reference
44+
45+
In addition to all arguments above, the following attributes are exported:
46+
47+
* `id` - The resource ID.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,7 @@ func Provider() *schema.Provider {
21242124
"huaweicloud_modelarts_authorization": modelarts.ResourceModelArtsAuthorization(),
21252125
"huaweicloud_modelarts_network": modelarts.ResourceModelartsNetwork(),
21262126
"huaweicloud_modelarts_resource_pool": modelarts.ResourceModelartsResourcePool(),
2127+
"huaweicloud_modelartsv2_service_action": modelarts.ResourceV2ServiceAction(),
21272128

21282129
// DataArts Studio - Management Center
21292130
"huaweicloud_dataarts_studio_data_connection": dataarts.ResourceDataConnection(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package modelarts
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-uuid"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
11+
"github.com/chnsz/golangsdk"
12+
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
14+
)
15+
16+
var v2ServiceActionNonUpdatableParams = []string{
17+
"service_id",
18+
"action",
19+
}
20+
21+
// @API ModelArts POST /v2/{project_id}/services/{service_id}/start
22+
// @API ModelArts POST /v2/{project_id}/services/{service_id}/stop
23+
// @API ModelArts POST /v2/{project_id}/services/{service_id}/interrupt
24+
func ResourceV2ServiceAction() *schema.Resource {
25+
return &schema.Resource{
26+
CreateContext: resourceV2ServiceActionCreate,
27+
ReadContext: resourceV2ServiceActionRead,
28+
UpdateContext: resourceV2ServiceActionUpdate,
29+
DeleteContext: resourceV2ServiceActionDelete,
30+
31+
CustomizeDiff: config.FlexibleForceNew(v2ServiceActionNonUpdatableParams),
32+
33+
Schema: map[string]*schema.Schema{
34+
"region": {
35+
Type: schema.TypeString,
36+
Optional: true,
37+
Computed: true,
38+
ForceNew: true,
39+
Description: `The region where the service is located.`,
40+
},
41+
"service_id": {
42+
Type: schema.TypeString,
43+
Required: true,
44+
Description: `The service ID to be operated.`,
45+
},
46+
"action": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
Description: `The action type.`,
50+
},
51+
},
52+
}
53+
}
54+
55+
func resourceV2ServiceActionCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
56+
var (
57+
cfg = meta.(*config.Config)
58+
region = cfg.GetRegion(d)
59+
httpUrl = "v2/{project_id}/services/{service_id}/{action}"
60+
serviceId = d.Get("service_id").(string)
61+
action = d.Get("action").(string)
62+
)
63+
64+
client, err := cfg.NewServiceClient("modelarts", region)
65+
if err != nil {
66+
return diag.Errorf("error creating ER client: %s", err)
67+
}
68+
69+
actionPath := client.Endpoint + httpUrl
70+
actionPath = strings.ReplaceAll(actionPath, "{project_id}", client.ProjectID)
71+
actionPath = strings.ReplaceAll(actionPath, "{service_id}", serviceId)
72+
actionPath = strings.ReplaceAll(actionPath, "{action}", action)
73+
74+
opt := golangsdk.RequestOpts{
75+
KeepResponseBody: true,
76+
}
77+
_, err = client.Request("POST", actionPath, &opt)
78+
if err != nil {
79+
return diag.Errorf("unable to operate the service (%s), the action is: %s, the error is: %s",
80+
action, serviceId, err)
81+
}
82+
83+
generateUUID, err := uuid.GenerateUUID()
84+
if err != nil {
85+
return diag.Errorf("unable to generate ID: %s", err)
86+
}
87+
d.SetId(generateUUID)
88+
89+
return nil
90+
}
91+
92+
func resourceV2ServiceActionRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
93+
return nil
94+
}
95+
96+
func resourceV2ServiceActionUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
97+
return nil
98+
}
99+
100+
func resourceV2ServiceActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
101+
errorMsg := `This resource is only a one-time action resource for operating the service. Deleting this resource will
102+
not clear the corresponding request record, but will only remove the resource information from the tfstate file.`
103+
return diag.Diagnostics{
104+
diag.Diagnostic{
105+
Severity: diag.Warning,
106+
Summary: errorMsg,
107+
},
108+
}
109+
}

0 commit comments

Comments
 (0)