-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource: alicloud_cloud_phone_instance.
- Loading branch information
1 parent
698aa93
commit 93b7511
Showing
5 changed files
with
410 additions
and
0 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
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,145 @@ | ||
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/PaesslerAG/jsonpath" | ||
"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 resourceAliCloudCloudPhoneInstance() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAliCloudCloudPhoneInstanceCreate, | ||
Read: resourceAliCloudCloudPhoneInstanceRead, | ||
Update: resourceAliCloudCloudPhoneInstanceUpdate, | ||
Delete: resourceAliCloudCloudPhoneInstanceDelete, | ||
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{ | ||
"android_instance_group_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"android_instance_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAliCloudCloudPhoneInstanceCreate(d *schema.ResourceData, meta interface{}) error { | ||
|
||
client := meta.(*connectivity.AliyunClient) | ||
|
||
action := "DescribeAndroidInstances" | ||
var request map[string]interface{} | ||
var response map[string]interface{} | ||
query := make(map[string]interface{}) | ||
var err error | ||
request = make(map[string]interface{}) | ||
|
||
if v, ok := d.GetOk("android_instance_group_id"); ok { | ||
request["InstanceGroupId"] = v | ||
} | ||
wait := incrementalWait(3*time.Second, 5*time.Second) | ||
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { | ||
response, err = client.RpcPost("eds-aic", "2023-09-30", action, query, request, true) | ||
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, "alicloud_cloud_phone_instance", action, AlibabaCloudSdkGoERROR) | ||
} | ||
|
||
id, _ := jsonpath.Get("$.InstanceModel[0].AndroidInstanceId", response) | ||
d.SetId(fmt.Sprint(id)) | ||
|
||
return resourceAliCloudCloudPhoneInstanceUpdate(d, meta) | ||
} | ||
|
||
func resourceAliCloudCloudPhoneInstanceRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
cloudPhoneServiceV2 := CloudPhoneServiceV2{client} | ||
|
||
objectRaw, err := cloudPhoneServiceV2.DescribeCloudPhoneInstance(d.Id()) | ||
if err != nil { | ||
if !d.IsNewResource() && NotFoundError(err) { | ||
log.Printf("[DEBUG] Resource alicloud_cloud_phone_instance DescribeCloudPhoneInstance Failed!!! %s", err) | ||
d.SetId("") | ||
return nil | ||
} | ||
return WrapError(err) | ||
} | ||
|
||
d.Set("android_instance_group_id", objectRaw["AndroidInstanceGroupId"]) | ||
d.Set("android_instance_name", objectRaw["AndroidInstanceName"]) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAliCloudCloudPhoneInstanceUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
var request map[string]interface{} | ||
var response map[string]interface{} | ||
var query map[string]interface{} | ||
update := false | ||
|
||
var err error | ||
action := "ModifyAndroidInstance" | ||
request = make(map[string]interface{}) | ||
query = make(map[string]interface{}) | ||
request["AndroidInstanceId"] = d.Id() | ||
|
||
if d.HasChange("android_instance_name") { | ||
update = true | ||
} | ||
if v, ok := d.GetOk("android_instance_name"); ok || d.HasChange("android_instance_name") { | ||
request["NewAndroidInstanceName"] = v | ||
} | ||
if update { | ||
wait := incrementalWait(3*time.Second, 5*time.Second) | ||
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { | ||
response, err = client.RpcPost("eds-aic", "2023-09-30", action, query, request, true) | ||
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 resourceAliCloudCloudPhoneInstanceRead(d, meta) | ||
} | ||
|
||
func resourceAliCloudCloudPhoneInstanceDelete(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[WARN] Cannot destroy resource AliCloud Resource Instance. Terraform will remove this resource from the state file, however resources may remain.") | ||
return nil | ||
} |
106 changes: 106 additions & 0 deletions
106
alicloud/resource_alicloud_cloud_phone_instance_test.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,106 @@ | ||
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 CloudPhone Instance. >>> Resource test cases, automatically generated. | ||
// Case chuyuan_CreateInstance_prod 9932 | ||
func TestAccAliCloudCloudPhoneInstance_basic9932(t *testing.T) { | ||
var v map[string]interface{} | ||
resourceId := "alicloud_cloud_phone_instance.default" | ||
ra := resourceAttrInit(resourceId, AlicloudCloudPhoneInstanceMap9932) | ||
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { | ||
return &CloudPhoneServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} | ||
}, "DescribeCloudPhoneInstance") | ||
rac := resourceAttrCheckInit(rc, ra) | ||
testAccCheck := rac.resourceAttrMapUpdateSet() | ||
rand := acctest.RandIntRange(10000, 99999) | ||
name := fmt.Sprintf("tfacccloudphone%d", rand) | ||
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudCloudPhoneInstanceBasicDependence9932) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
IDRefreshName: resourceId, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccConfig(map[string]interface{}{ | ||
"android_instance_group_id": "${alicloud_cloud_phone_instance_group.defaultYHMlTO.id}", | ||
"android_instance_name": "CreateInstanceName", | ||
}), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheck(map[string]string{ | ||
"android_instance_group_id": CHECKSET, | ||
"android_instance_name": "CreateInstanceName", | ||
}), | ||
), | ||
}, | ||
{ | ||
Config: testAccConfig(map[string]interface{}{ | ||
"android_instance_name": "AndroidInstanceName", | ||
}), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheck(map[string]string{ | ||
"android_instance_name": "AndroidInstanceName", | ||
}), | ||
), | ||
}, | ||
{ | ||
Config: testAccConfig(map[string]interface{}{ | ||
"android_instance_name": "NewAndroidInstanceName", | ||
}), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheck(map[string]string{ | ||
"android_instance_name": "NewAndroidInstanceName", | ||
}), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceId, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
var AlicloudCloudPhoneInstanceMap9932 = map[string]string{} | ||
|
||
func AlicloudCloudPhoneInstanceBasicDependence9932(name string) string { | ||
return fmt.Sprintf(` | ||
variable "name" { | ||
default = "%s" | ||
} | ||
resource "alicloud_cloud_phone_policy" "defaultjZ1gi0" { | ||
} | ||
resource "alicloud_cloud_phone_instance_group" "defaultYHMlTO" { | ||
biz_region_id = "cn-hangzhou" | ||
instance_group_spec = "acp.basic.small" | ||
policy_group_id = alicloud_cloud_phone_policy.defaultjZ1gi0.id | ||
instance_group_name = "AutoCreateGroupName" | ||
period = "1" | ||
number_of_instances = "1" | ||
charge_type = "PostPaid" | ||
image_id = "imgc-075cllfeuazh03tg9" | ||
period_unit = "Hour" | ||
auto_renew = false | ||
amount = "1" | ||
auto_pay = false | ||
gpu_acceleration = false | ||
} | ||
`, name) | ||
} | ||
|
||
// Test CloudPhone Instance. <<< Resource test cases, automatically generated. |
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
Oops, something went wrong.