From 7acbbacde8af646e2172d2dfad8cfebcae25e81e Mon Sep 17 00:00:00 2001 From: He Guimin Date: Mon, 8 Jan 2018 11:51:16 +0800 Subject: [PATCH] fix creating rds Prepaid instance failed error --- CHANGELOG.md | 5 +++++ alicloud/resource_alicloud_db_instance.go | 20 +++++++++++++++++--- alicloud/resource_alicloud_instance.go | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10ee1c3972d8..c471cb8ce846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## 1.6.0 (Unreleased) +## 1.5.2 (January 8, 2018) + +BUG FIXES: + * fix creating rds 'Prepaid' instance failed error ([#70](https://github.com/alibaba/terraform-provider/pull/70)) + ## 1.5.1 (January 5, 2018) BUG FIXES: diff --git a/alicloud/resource_alicloud_db_instance.go b/alicloud/resource_alicloud_db_instance.go index 687c019ce6a6..b8cbdd7ec89d 100644 --- a/alicloud/resource_alicloud_db_instance.go +++ b/alicloud/resource_alicloud_db_instance.go @@ -333,6 +333,16 @@ func resourceAlicloudDBInstanceRead(d *schema.ResourceData, meta interface{}) er func resourceAlicloudDBInstanceDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*AliyunClient) + instance, err := client.DescribeDBInstanceById(d.Id()) + if err != nil { + if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceNameNotFound) { + return nil + } + return fmt.Errorf("Error Describe DB InstanceAttribute: %#v", err) + } + if instance.PayType == rds.Prepaid { + return fmt.Errorf("At present, 'Prepaid' instance cannot be deleted and must wait it to be expired and release it automatically.") + } return resource.Retry(5*time.Minute, func() *resource.RetryError { err := client.rdsconn.DeleteInstance(d.Id()) @@ -428,14 +438,18 @@ func buildDBCreateOrderArgs(d *schema.ResourceData, meta interface{}) (*rds.Crea args.PayType = rds.DBPayType(d.Get("instance_charge_type").(string)) // if charge type is postpaid, the commodity code must set to bards - args.CommodityCode = rds.Rds - if args.PayType == rds.Postpaid { - args.CommodityCode = rds.Bards + args.CommodityCode = rds.Bards + // At present, API supports two charge options about 'Prepaid'. + // 'Month': valid period ranges [1-9]; 'Year': valid period range [1-3] + // This resource only supports to input Month period [1-9, 12, 24, 36] and the values need to be converted before using them. + if args.PayType == rds.Prepaid { + args.CommodityCode = rds.Rds period := d.Get("period").(int) args.UsedTime = period args.TimeType = common.Month if period > 9 { + args.UsedTime = period / 12 args.TimeType = common.Year } } diff --git a/alicloud/resource_alicloud_instance.go b/alicloud/resource_alicloud_instance.go index 4883cecf974c..2c15c2f375a0 100644 --- a/alicloud/resource_alicloud_instance.go +++ b/alicloud/resource_alicloud_instance.go @@ -578,7 +578,7 @@ func resourceAliyunInstanceDelete(d *schema.ResourceData, meta interface{}) erro client := meta.(*AliyunClient) conn := client.ecsconn if common.InstanceChargeType(d.Get("instance_charge_type").(string)) == common.PrePaid { - return fmt.Errorf("At present, 'PrePaid' instance cannot be deleted and must wait it to be outdated and release it automatically.") + return fmt.Errorf("At present, 'PrePaid' instance cannot be deleted and must wait it to be expired and release it automatically.") } return resource.Retry(5*time.Minute, func() *resource.RetryError { instance, err := client.QueryInstancesById(d.Id())