|
| 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 | + serviceId, action, 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