Skip to content

Commit

Permalink
resource/alicloud_kvstore_instance: fix bug for creating status polling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Jun 3, 2024
1 parent d18aca0 commit 7089ffa
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion alicloud/resource_alicloud_kvstore_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func resourceAliCloudKvstoreInstanceCreate(d *schema.ResourceData, meta interfac

d.SetId(fmt.Sprintf("%v", response.InstanceId))

stateConf := BuildStateConf([]string{}, []string{"Normal"}, d.Timeout(schema.TimeoutCreate), 180*time.Second, r_kvstoreService.KvstoreInstanceStateRefreshFunc(d.Id(), []string{}))
stateConf := BuildStateConf([]string{}, []string{"Normal"}, d.Timeout(schema.TimeoutCreate), 180*time.Second, r_kvstoreService.KvstoreInstancesStateRefreshFunc(d.Id(), []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
Expand Down
10 changes: 5 additions & 5 deletions alicloud/resource_alicloud_kvstore_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ func TestAccAliCloudKVStoreRedisInstance_vpctest(t *testing.T) {
"config": map[string]string{
"appendonly": "no",
"lazyfree-lazy-eviction": "no",
"EvictionPolicy": "volatile-lru",
"maxmemory-policy": "volatile-lru",
},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"config.%": "3",
"config.appendonly": "no",
"config.lazyfree-lazy-eviction": "no",
"config.EvictionPolicy": "volatile-lru",
"config.maxmemory-policy": "volatile-lru",
}),
),
},
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestAccAliCloudKVStoreRedisInstance_vpctest(t *testing.T) {
"config": map[string]string{
"appendonly": "yes",
"lazyfree-lazy-eviction": "yes",
"EvictionPolicy": "volatile-lru",
"maxmemory-policy": "volatile-lru",
},
"tags": map[string]string{
"Created": "TF",
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestAccAliCloudKVStoreRedisInstance_vpctest(t *testing.T) {
"config.%": "3",
"config.appendonly": "yes",
"config.lazyfree-lazy-eviction": "yes",
"config.EvictionPolicy": "volatile-lru",
"config.maxmemory-policy": "volatile-lru",
"tags.%": "2",
"tags.Created": "TF",
"tags.For": "acceptance test",
Expand Down Expand Up @@ -2402,7 +2402,7 @@ func KvstoreInstanceVpcTestdependence(name string) string {
instance_charge_type = "PostPaid"
}
data "alicloud_vpcs" "default" {
name_regex = "^default-NODELETING$"
name_regex = "^default-NODELETING-Redis$"
cidr_block = "192.168.0.0/16"
}
data "alicloud_vswitches" "default" {
Expand Down
64 changes: 64 additions & 0 deletions alicloud/service_alicloud_r_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,70 @@ func (s *R_kvstoreService) KvstoreInstanceAttributeRefreshFunc(id, attribute str
}
}

func (s *R_kvstoreService) DescribeKvstoreInstances(id string) (object map[string]interface{}, err error) {
var response map[string]interface{}
conn, err := s.client.NewRedisaClient()
if err != nil {
return nil, WrapError(err)
}
action := "DescribeInstances"
request := map[string]interface{}{
"RegionId": s.client.RegionId,
"InstanceIds": id,
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, request, &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 {
if IsExpectedErrors(err, []string{"InvalidInstanceId.NotFound"}) {
err = WrapErrorf(Error(GetNotFoundMessage("KvstoreInstance", id)), NotFoundMsg, ProviderERROR)
return object, err
}
err = WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
return object, err
}
v, err := jsonpath.Get("$.Instances.KVStoreInstance", response)
if err != nil {
return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.Instances", response)
}
if v == nil || len(v.([]interface{})) < 1 || fmt.Sprint(v.([]interface{})[0].(map[string]interface{})["InstanceId"]) != id {
return object, WrapErrorf(Error(GetNotFoundMessage("Redis", id)), NotFoundWithResponse, response)
}
return v.([]interface{})[0].(map[string]interface{}), nil
}

func (s *R_kvstoreService) KvstoreInstancesStateRefreshFunc(id string, failStates []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
object, err := s.DescribeKvstoreInstances(id)
if err != nil {
if NotFoundError(err) {
// Set this to nil as if we didn't find anything.
return nil, "", nil
}
return nil, "", WrapError(err)
}

for _, failState := range failStates {
if object["InstanceStatus"] == failState {
return object, fmt.Sprint(object["InstanceStatus"]), WrapError(Error(FailedToReachTargetStatus, fmt.Sprint(object["InstanceStatus"])))
}
}
return object, fmt.Sprint(object["InstanceStatus"]), nil
}
}

func (s *R_kvstoreService) DescribeKvstoreInstanceDeleted(id string) (object map[string]interface{}, err error) {
var response map[string]interface{}
conn, err := s.client.NewRedisaClient()
Expand Down

0 comments on commit 7089ffa

Please sign in to comment.