Skip to content

Commit

Permalink
modify db_instance and output more useful error message
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Jan 4, 2018
1 parent 5bb12f9 commit 75bc521
Show file tree
Hide file tree
Showing 45 changed files with 1,395 additions and 1,297 deletions.
14 changes: 8 additions & 6 deletions alicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (

// Config of aliyun
type Config struct {
AccessKey string
SecretKey string
Region common.Region
AccessKey string
SecretKey string
Region common.Region
SecurityToken string
}

// AliyunClient of aliyun
Expand Down Expand Up @@ -141,7 +142,7 @@ func (c *Config) validateRegion() error {
}

func (c *Config) ecsConn() (*ecs.Client, error) {
client := ecs.NewECSClient(c.AccessKey, c.SecretKey, c.Region)
client := ecs.NewECSClientWithSecurityToken(c.AccessKey, c.SecretKey, c.SecurityToken, c.Region)
client.SetBusinessInfo(BusinessInfoKey)
client.SetUserAgent(getUserAgent())

Expand All @@ -167,7 +168,7 @@ func (c *Config) slbConn() (*slb.Client, error) {
}

func (c *Config) vpcConn() (*ecs.Client, error) {
client := ecs.NewVPCClient(c.AccessKey, c.SecretKey, c.Region)
client := ecs.NewVPCClientWithSecurityToken(c.AccessKey, c.SecretKey, c.SecurityToken, c.Region)
client.SetBusinessInfo(BusinessInfoKey)
client.SetUserAgent(getUserAgent())
return client, nil
Expand All @@ -182,6 +183,7 @@ func (c *Config) essConn() (*ess.Client, error) {
func (c *Config) ossConn() (*oss.Client, error) {

endpointClient := location.NewClient(c.AccessKey, c.SecretKey)
endpointClient.SetSecurityToken(c.SecurityToken)
args := &location.DescribeEndpointsArgs{
Id: c.Region,
ServiceCode: "oss",
Expand Down Expand Up @@ -221,7 +223,7 @@ func (c *Config) ramConn() (ram.RamClientInterface, error) {
}

func (c *Config) csConn() (*cs.Client, error) {
client := cs.NewClient(c.AccessKey, c.SecretKey)
client := cs.NewClientForAussumeRole(c.AccessKey, c.SecretKey, c.SecurityToken)
client.SetUserAgent(getUserAgent())
return client, nil
}
Expand Down
85 changes: 85 additions & 0 deletions alicloud/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package alicloud

import (
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/dns"
"github.com/denverdino/aliyungo/rds"
"github.com/denverdino/aliyungo/slb"
"github.com/hashicorp/terraform/helper/schema"
"strconv"
)

func httpHttpsDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
Expand Down Expand Up @@ -82,3 +85,85 @@ func dnsPriorityDiffSuppressFunc(k, old, new string, d *schema.ResourceData) boo
}
return true
}

func slbInternetDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if internet, ok := d.GetOk("internet"); ok && internet.(bool) {
return true
}
return false
}

func slbInternetChargeTypeDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
return !slbInternetDiffSuppressFunc(k, old, new, d)
}

func slbBandwidthDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if slbInternetDiffSuppressFunc(k, old, new, d) && slb.InternetChargeType(d.Get("internet_charge_type").(string)) == slb.PayByBandwidth {
return false
}
return true
}

func ecsPrivateIpDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
vswitch := ""
if vsw, ok := d.GetOk("vswitch_id"); ok && vsw.(string) != "" {
vswitch = vsw.(string)
} else if subnet, ok := d.GetOk("subnet_id"); ok && subnet.(string) != "" {
vswitch = subnet.(string)
}

if vswitch != "" {
return false
}
return true
}
func ecsInternetDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if allocate, ok := d.GetOk("allocate_public_ip"); ok && allocate.(bool) {
return false
}
return true
}

func ecsPostPaidDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if common.InstanceChargeType(d.Get("instance_charge_type").(string)) == common.PrePaid {
return false
}
return true
}

func ecsChargeTypeSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if common.InstanceChargeType(old) == common.PrePaid && common.InstanceChargeType(new) == common.PostPaid {
return true
}
return false
}

func zoneIdDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if vsw, ok := d.GetOk("vswitch_id"); ok && vsw.(string) != "" {
return true
} else if vsw, ok := d.GetOk("subnet_id"); ok && vsw.(string) != "" {
return true
} else if multi, ok := d.GetOk("multi_az"); ok && multi.(bool) {
return true
}
return false
}

func logRetentionPeriodDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if d.Get("log_backup").(bool) {
return false
}

if v, err := strconv.Atoi(new); err != nil && v > d.Get("retention_period").(int) {
return false
}

return true
}

func rdsPostPaidDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
if rds.DBPayType(d.Get("instance_charge_type").(string)) == rds.Prepaid {
return false
}
return true
}
13 changes: 13 additions & 0 deletions alicloud/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
InstanceLockedForSecurity = "InstanceLockedForSecurity"
SystemDiskNotFound = "SystemDiskNotFound"
DiskOperationConflict = "OperationConflict"
DiskInternalError = "InternalError"
DiskInvalidOperation = "InvalidOperation.Conflict"
// eip
EipIncorrectStatus = "IncorrectEipStatus"
InstanceIncorrectStatus = "IncorrectInstanceStatus"
Expand All @@ -28,6 +30,7 @@ const (
ListenerAlreadyExists = "ListenerAlreadyExists"
ServiceIsConfiguring = "ServiceIsConfiguring"
BackendServerconfiguring = "BackendServer.configuring"
SystemBusy = "SystemBusy"
// security_group
InvalidInstanceIdAlreadyExists = "InvalidInstanceId.AlreadyExists"
InvalidSecurityGroupIdNotFound = "InvalidSecurityGroupId.NotFound"
Expand All @@ -53,6 +56,16 @@ const (
IncorrectScalingConfigurationLifecycleState = "IncorrectScalingConfigurationLifecycleState"
IncorrectScalingGroupStatus = "IncorrectScalingGroupStatus"

// rds
InvalidDBNameNotFound = "InvalidDBName.NotFound"
InvalidDBInstanceNameNotFound = "InvalidDBInstanceName.NotFound"
InvalidCurrentConnectionStringNotFound = "InvalidCurrentConnectionString.NotFound"
NetTypeExists = "NetTypeExists"
InvalidAccountNameDuplicate = "InvalidAccountName.Duplicate"
InvalidAccountNameNotFound = "InvalidAccountName.NotFound"
OperationDeniedDBInstanceStatus = "OperationDenied.DBInstanceStatus"
InvalidConnectionStringDuplicate = "InvalidConnectionString.Duplicate"
AtLeastOneNetTypeExists = "AtLeastOneNetTypeExists"
// oss
OssBucketNotFound = "NoSuchBucket"
OssBodyNotFound = "404 Not Found"
Expand Down
185 changes: 4 additions & 181 deletions alicloud/import_alicloud_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,7 @@ import (
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAlicloudDBInstance_importBasic(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstanceConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_importVpc(t *testing.T) {
func TestAccAlicloudDBInstance_import(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
Expand All @@ -41,164 +19,9 @@ func TestAccAlicloudDBInstance_importVpc(t *testing.T) {
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
},
},
})
}

func TestAlicloudDBInstance_importPrepaidOrder(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_prepaid_order,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_multiIZ(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_multiAZ,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_importDatabase(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_database,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_importAccount(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_grantDatabasePrivilege2Account,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "master_user_password", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_importAllocatePublicConnection(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_allocatePublicConnection,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "master_user_password", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_importBackupPolicy(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_backup,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
},
},
})
}

func TestAccAlicloudDBInstance_importSecurityIps(t *testing.T) {
resourceName := "alicloud_db_instance.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDBInstance_securityIpsConfig,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allocate_public_connection", "period"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down
Loading

0 comments on commit 75bc521

Please sign in to comment.