diff --git a/alicloud/provider.go b/alicloud/provider.go index 683e0ab8f427..78b9e9217a55 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -873,6 +873,7 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ "alicloud_pai_workspace_code_source": resourceAliCloudPaiWorkspaceCodeSource(), + "alicloud_pai_workspace_default_workspace": resourceAliCloudPaiWorkspaceDefaultWorkspace(), "alicloud_pai_workspace_run": resourceAliCloudPaiWorkspaceRun(), "alicloud_pai_workspace_datasetversion": resourceAliCloudPaiWorkspaceDatasetversion(), "alicloud_pai_workspace_experiment": resourceAliCloudPaiWorkspaceExperiment(), diff --git a/alicloud/resource_alicloud_pai_workspace_default_workspace.go b/alicloud/resource_alicloud_pai_workspace_default_workspace.go new file mode 100644 index 000000000000..5c45396ce8f4 --- /dev/null +++ b/alicloud/resource_alicloud_pai_workspace_default_workspace.go @@ -0,0 +1,190 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "time" + + util "github.com/alibabacloud-go/tea-utils/service" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceAliCloudPaiWorkspaceDefaultWorkspace() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudPaiWorkspaceDefaultWorkspaceCreate, + Read: resourceAliCloudPaiWorkspaceDefaultWorkspaceRead, + Update: resourceAliCloudPaiWorkspaceDefaultWorkspaceUpdate, + Delete: resourceAliCloudPaiWorkspaceDefaultWorkspaceDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "description": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "env_types": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "workspace_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func resourceAliCloudPaiWorkspaceDefaultWorkspaceCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := fmt.Sprintf("/api/v1/defaultWorkspaces") + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]*string) + body := make(map[string]interface{}) + conn, err := client.NewPaiworkspaceClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + + request["Description"] = d.Get("description") + if v, ok := d.GetOk("env_types"); ok { + envTypesMapsArray := v.([]interface{}) + request["EnvTypes"] = envTypesMapsArray + } + + body = request + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer("2021-02-04"), nil, StringPointer("POST"), StringPointer("AK"), StringPointer(action), query, nil, body, &runtime) + if err != nil { + if IsExpectedErrors(err, []string{"100400009"}) { + return nil + } + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "alicloud_pai_workspace_default_workspace", action, AlibabaCloudSdkGoERROR) + } + + accountId, err := client.AccountId() + d.SetId(accountId) + + return resourceAliCloudPaiWorkspaceDefaultWorkspaceUpdate(d, meta) +} + +func resourceAliCloudPaiWorkspaceDefaultWorkspaceRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + paiWorkspaceServiceV2 := PaiWorkspaceServiceV2{client} + + objectRaw, err := paiWorkspaceServiceV2.DescribePaiWorkspaceDefaultWorkspace(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_pai_workspace_default_workspace DescribePaiWorkspaceDefaultWorkspace Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["Description"] != nil { + d.Set("description", objectRaw["Description"]) + } + if objectRaw["Status"] != nil { + d.Set("status", objectRaw["Status"]) + } + if objectRaw["WorkspaceId"] != nil { + d.Set("workspace_id", objectRaw["WorkspaceId"]) + } + + envTypes1Raw := make([]interface{}, 0) + if objectRaw["EnvTypes"] != nil { + envTypes1Raw = objectRaw["EnvTypes"].([]interface{}) + } + + d.Set("env_types", envTypes1Raw) + + return nil +} + +func resourceAliCloudPaiWorkspaceDefaultWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + var request map[string]interface{} + var response map[string]interface{} + var query map[string]*string + var body map[string]interface{} + update := false + + action := fmt.Sprintf("/api/v1/defaultWorkspaces") + conn, err := client.NewPaiworkspaceClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]*string) + body = make(map[string]interface{}) + + if d.HasChange("workspace_id") { + update = true + } + if v, ok := d.GetOk("workspace_id"); ok || d.HasChange("workspace_id") { + request["WorkspaceId"] = v + } + body = request + if update { + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer("2021-02-04"), nil, StringPointer("PUT"), StringPointer("AK"), StringPointer(action), query, nil, body, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + + return resourceAliCloudPaiWorkspaceDefaultWorkspaceRead(d, meta) +} + +func resourceAliCloudPaiWorkspaceDefaultWorkspaceDelete(d *schema.ResourceData, meta interface{}) error { + log.Printf("[WARN] Cannot destroy resource AliCloud Resource Default Workspace. Terraform will remove this resource from the state file, however resources may remain.") + return nil +} diff --git a/alicloud/resource_alicloud_pai_workspace_default_workspace_test.go b/alicloud/resource_alicloud_pai_workspace_default_workspace_test.go new file mode 100644 index 000000000000..e21355a47683 --- /dev/null +++ b/alicloud/resource_alicloud_pai_workspace_default_workspace_test.go @@ -0,0 +1,105 @@ +package alicloud + +import ( + "fmt" + "testing" + + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +// Test PaiWorkspace DefaultWorkspace. >>> Resource test cases, automatically generated. +// Case DefaultWorkspace 用例测试01 6819 +func TestAccAliCloudPaiWorkspaceDefaultWorkspace_basic6819(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_pai_workspace_default_workspace.default" + ra := resourceAttrInit(resourceId, AlicloudPaiWorkspaceDefaultWorkspaceMap6819) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &PaiWorkspaceServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribePaiWorkspaceDefaultWorkspace") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(1, 999) + name := fmt.Sprintf("tf_testacc%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudPaiWorkspaceDefaultWorkspaceBasicDependence6819) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "description": "defaultWorkspace", + "env_types": []string{ + "prod"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "description": "defaultWorkspace", + "env_types.#": "1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "workspace_id": "${alicloud_pai_workspace_workspace.defaultWorkspace.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "workspace_id": CHECKSET, + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "workspace_id": "${alicloud_pai_workspace_workspace.changeWorkspace.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "workspace_id": CHECKSET, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudPaiWorkspaceDefaultWorkspaceMap6819 = map[string]string{ + "status": CHECKSET, +} + +func AlicloudPaiWorkspaceDefaultWorkspaceBasicDependence6819(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +resource "alicloud_pai_workspace_workspace" "defaultWorkspace" { + description = "defaultWorkspace" + display_name = "DatasetResouceTest_16" + workspace_name = "DatasetResouceTest_16" + env_types = ["prod"] +} + +resource "alicloud_pai_workspace_workspace" "changeWorkspace" { + description = "defaultWorkspaced'f'c" + display_name = "DatasetResouceTest_146" + workspace_name = "DatasetResouceTest_146" + env_types = ["prod"] +} + +`, name) +} + +// Test PaiWorkspace DefaultWorkspace. <<< Resource test cases, automatically generated. diff --git a/alicloud/service_alicloud_pai_workspace_v2.go b/alicloud/service_alicloud_pai_workspace_v2.go index b054a1582f8e..eb16b7974b16 100644 --- a/alicloud/service_alicloud_pai_workspace_v2.go +++ b/alicloud/service_alicloud_pai_workspace_v2.go @@ -384,6 +384,75 @@ func (s *PaiWorkspaceServiceV2) PaiWorkspaceRunStateRefreshFunc(id string, field } // DescribePaiWorkspaceRun >>> Encapsulated. +// DescribePaiWorkspaceDefaultWorkspace <<< Encapsulated get interface for PaiWorkspace DefaultWorkspace. + +func (s *PaiWorkspaceServiceV2) DescribePaiWorkspaceDefaultWorkspace(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]*string + action := fmt.Sprintf("/api/v1/defaultWorkspaces") + conn, err := client.NewPaiworkspaceClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]*string) + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer("2021-02-04"), nil, StringPointer("GET"), StringPointer("AK"), StringPointer(action), query, nil, nil, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + response = response["body"].(map[string]interface{}) + + return response, nil +} + +func (s *PaiWorkspaceServiceV2) PaiWorkspaceDefaultWorkspaceStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribePaiWorkspaceDefaultWorkspace(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribePaiWorkspaceDefaultWorkspace >>> Encapsulated. // DescribePaiWorkspaceCodeSource <<< Encapsulated get interface for PaiWorkspace CodeSource. diff --git a/website/docs/r/pai_workspace_default_workspace.html.markdown b/website/docs/r/pai_workspace_default_workspace.html.markdown new file mode 100644 index 000000000000..5f0576e82408 --- /dev/null +++ b/website/docs/r/pai_workspace_default_workspace.html.markdown @@ -0,0 +1,68 @@ +--- +subcategory: "PAI Workspace" +layout: "alicloud" +page_title: "Alicloud: alicloud_pai_workspace_default_workspace" +description: |- + Provides a Alicloud PAI Workspace Default Workspace resource. +--- + +# alicloud_pai_workspace_default_workspace + +Provides a PAI Workspace Default Workspace resource. + +Default Workspace Resources. + +For information about PAI Workspace Default Workspace and how to use it, see [What is Default Workspace](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.236.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform_example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + + +resource "alicloud_pai_workspace_default_workspace" "default" { + description = "defaultWorkspace" + env_types = ["prod"] +} +``` + +### Deleting `alicloud_pai_workspace_default_workspace` or removing it from your configuration + +Terraform cannot destroy resource `alicloud_pai_workspace_default_workspace`. Terraform will remove this resource from the state file, however resources may remain. + +## Argument Reference + +The following arguments are supported: +* `description` - (Required, ForceNew) Description of resource description +* `env_types` - (Required, ForceNew, List) Resource description for environment type list +* `workspace_id` - (Optional, Computed) Resource Description of the workspace ID. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as ``. +* `status` - Resource description of workspace state + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Default Workspace. +* `update` - (Defaults to 5 mins) Used when update the Default Workspace. + +## Import + +PAI Workspace Default Workspace can be imported using the id, e.g. + +```shell +$ terraform import alicloud_pai_workspace_default_workspace.example +``` \ No newline at end of file