Skip to content

Commit

Permalink
New Resource: alicloud_pai_workspace_default_workspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Nov 28, 2024
1 parent 3c4b46d commit 14e3994
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 0 deletions.
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(),
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_pai_workspace_default_workspace": resourceAliCloudPaiWorkspaceDefaultWorkspace(),
"alicloud_pai_workspace_run": resourceAliCloudPaiWorkspaceRun(),
"alicloud_pai_workspace_datasetversion": resourceAliCloudPaiWorkspaceDatasetversion(),
"alicloud_pai_workspace_experiment": resourceAliCloudPaiWorkspaceExperiment(),
Expand Down
190 changes: 190 additions & 0 deletions alicloud/resource_alicloud_pai_workspace_default_workspace.go
Original file line number Diff line number Diff line change
@@ -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
}
105 changes: 105 additions & 0 deletions alicloud/resource_alicloud_pai_workspace_default_workspace_test.go
Original file line number Diff line number Diff line change
@@ -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": "it",
"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.changeWorkspace.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 = "dataset_pop_test_535"
display_name = "DatasetResouceTest_16"
workspace_name = var.name
env_types = ["prod"]
}
resource "alicloud_pai_workspace_workspace" "changeWorkspace" {
description = "dataset_pop_test_185"
display_name = "DatasetResouceTest_146"
workspace_name = var.name
env_types = ["prod"]
}
`, name)
}

// Test PaiWorkspace DefaultWorkspace. <<< Resource test cases, automatically generated.
69 changes: 69 additions & 0 deletions alicloud/service_alicloud_pai_workspace_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,72 @@ 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.
Loading

0 comments on commit 14e3994

Please sign in to comment.