github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/service_alicloud_rds.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"github.com/denverdino/aliyungo/common"
     5  	"github.com/denverdino/aliyungo/rds"
     6  	"strings"
     7  )
     8  
     9  //
    10  //       _______________                      _______________                       _______________
    11  //       |              | ______param______\  |              |  _____request_____\  |              |
    12  //       |   Business   |                     |    Service   |                      |    SDK/API   |
    13  //       |              | __________________  |              |  __________________  |              |
    14  //       |______________| \    (obj, err)     |______________|  \ (status, cont)    |______________|
    15  //                           |                                    |
    16  //                           |A. {instance, nil}                  |a. {200, content}
    17  //                           |B. {nil, error}                     |b. {200, nil}
    18  //                      					  |c. {4xx, nil}
    19  //
    20  // The API return 200 for resource not found.
    21  // When getInstance is empty, then throw InstanceNotfound error.
    22  // That the business layer only need to check error.
    23  func (client *AliyunClient) DescribeDBInstanceById(id string) (instance *rds.DBInstanceAttribute, err error) {
    24  	arrtArgs := rds.DescribeDBInstancesArgs{
    25  		DBInstanceId: id,
    26  	}
    27  	resp, err := client.rdsconn.DescribeDBInstanceAttribute(&arrtArgs)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	attr := resp.Items.DBInstanceAttribute
    33  
    34  	if len(attr) <= 0 {
    35  		return nil, GetNotFoundErrorFromString("DB instance not found")
    36  	}
    37  
    38  	return &attr[0], nil
    39  }
    40  
    41  func (client *AliyunClient) CreateAccountByInfo(instanceId, username, pwd string) error {
    42  	conn := client.rdsconn
    43  	args := rds.CreateAccountArgs{
    44  		DBInstanceId:    instanceId,
    45  		AccountName:     username,
    46  		AccountPassword: pwd,
    47  	}
    48  
    49  	if _, err := conn.CreateAccount(&args); err != nil {
    50  		return err
    51  	}
    52  
    53  	if err := conn.WaitForAccount(instanceId, username, rds.Available, 200); err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func (client *AliyunClient) CreateDatabaseByInfo(instanceId, dbName, charset, desp string) error {
    60  	conn := client.rdsconn
    61  	args := rds.CreateDatabaseArgs{
    62  		DBInstanceId:     instanceId,
    63  		DBName:           dbName,
    64  		CharacterSetName: charset,
    65  		DBDescription:    desp,
    66  	}
    67  	_, err := conn.CreateDatabase(&args)
    68  	return err
    69  }
    70  
    71  func (client *AliyunClient) DescribeDatabaseByName(instanceId, dbName string) (ds []rds.Database, err error) {
    72  	conn := client.rdsconn
    73  	args := rds.DescribeDatabasesArgs{
    74  		DBInstanceId: instanceId,
    75  		DBName:       dbName,
    76  	}
    77  
    78  	resp, err := conn.DescribeDatabases(&args)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return resp.Databases.Database, nil
    84  }
    85  
    86  func (client *AliyunClient) GrantDBPrivilege2Account(instanceId, username, dbName string) error {
    87  	conn := client.rdsconn
    88  	pargs := rds.GrantAccountPrivilegeArgs{
    89  		DBInstanceId:     instanceId,
    90  		AccountName:      username,
    91  		DBName:           dbName,
    92  		AccountPrivilege: rds.ReadWrite,
    93  	}
    94  	if _, err := conn.GrantAccountPrivilege(&pargs); err != nil {
    95  		return err
    96  	}
    97  
    98  	if err := conn.WaitForAccountPrivilege(instanceId, username, dbName, rds.ReadWrite, 200); err != nil {
    99  		return err
   100  	}
   101  	return nil
   102  }
   103  
   104  func (client *AliyunClient) AllocateDBPublicConnection(instanceId, port string) error {
   105  	conn := client.rdsconn
   106  	args := rds.AllocateInstancePublicConnectionArgs{
   107  		DBInstanceId:           instanceId,
   108  		ConnectionStringPrefix: instanceId + "o",
   109  		Port: port,
   110  	}
   111  
   112  	if _, err := conn.AllocateInstancePublicConnection(&args); err != nil {
   113  		return err
   114  	}
   115  
   116  	if err := conn.WaitForPublicConnection(instanceId, 600); err != nil {
   117  		return err
   118  	}
   119  	return nil
   120  }
   121  
   122  func (client *AliyunClient) ConfigDBBackup(instanceId, backupTime, backupPeriod string, retentionPeriod int) error {
   123  	bargs := rds.BackupPolicy{
   124  		PreferredBackupTime:   backupTime,
   125  		PreferredBackupPeriod: backupPeriod,
   126  		BackupRetentionPeriod: retentionPeriod,
   127  	}
   128  	args := rds.ModifyBackupPolicyArgs{
   129  		DBInstanceId: instanceId,
   130  		BackupPolicy: bargs,
   131  	}
   132  
   133  	if _, err := client.rdsconn.ModifyBackupPolicy(&args); err != nil {
   134  		return err
   135  	}
   136  
   137  	if err := client.rdsconn.WaitForInstance(instanceId, rds.Running, 600); err != nil {
   138  		return err
   139  	}
   140  	return nil
   141  }
   142  
   143  func (client *AliyunClient) ModifyDBSecurityIps(instanceId, ips string) error {
   144  	sargs := rds.DBInstanceIPArray{
   145  		SecurityIps: ips,
   146  	}
   147  
   148  	args := rds.ModifySecurityIpsArgs{
   149  		DBInstanceId:      instanceId,
   150  		DBInstanceIPArray: sargs,
   151  	}
   152  
   153  	if _, err := client.rdsconn.ModifySecurityIps(&args); err != nil {
   154  		return err
   155  	}
   156  
   157  	if err := client.rdsconn.WaitForInstance(instanceId, rds.Running, 600); err != nil {
   158  		return err
   159  	}
   160  	return nil
   161  }
   162  
   163  func (client *AliyunClient) DescribeDBSecurityIps(instanceId string) (ips []rds.DBInstanceIPList, err error) {
   164  	args := rds.DescribeDBInstanceIPsArgs{
   165  		DBInstanceId: instanceId,
   166  	}
   167  
   168  	resp, err := client.rdsconn.DescribeDBInstanceIPs(&args)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	return resp.Items.DBInstanceIPArray, nil
   173  }
   174  
   175  func (client *AliyunClient) GetSecurityIps(instanceId string) ([]string, error) {
   176  	arr, err := client.DescribeDBSecurityIps(instanceId)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  	var ips, separator string
   181  	for _, ip := range arr {
   182  		ips += separator + ip.SecurityIPList
   183  		separator = COMMA_SEPARATED
   184  	}
   185  	return strings.Split(ips, COMMA_SEPARATED), nil
   186  }
   187  
   188  func (client *AliyunClient) ModifyDBClassStorage(instanceId, class, storage string) error {
   189  	conn := client.rdsconn
   190  	args := rds.ModifyDBInstanceSpecArgs{
   191  		DBInstanceId:      instanceId,
   192  		PayType:           rds.Postpaid,
   193  		DBInstanceClass:   class,
   194  		DBInstanceStorage: storage,
   195  	}
   196  
   197  	if _, err := conn.ModifyDBInstanceSpec(&args); err != nil {
   198  		return err
   199  	}
   200  
   201  	if err := conn.WaitForInstance(instanceId, rds.Running, 600); err != nil {
   202  		return err
   203  	}
   204  	return nil
   205  }
   206  
   207  // turn period to TimeType
   208  func TransformPeriod2Time(period int, chargeType string) (ut int, tt common.TimeType) {
   209  	if chargeType == string(rds.Postpaid) {
   210  		return 1, common.Day
   211  	}
   212  
   213  	if period >= 1 && period <= 9 {
   214  		return period, common.Month
   215  	}
   216  
   217  	if period == 12 {
   218  		return 1, common.Year
   219  	}
   220  
   221  	if period == 24 {
   222  		return 2, common.Year
   223  	}
   224  	return 0, common.Day
   225  
   226  }
   227  
   228  // turn TimeType to Period
   229  func TransformTime2Period(ut int, tt common.TimeType) (period int) {
   230  	if tt == common.Year {
   231  		return 12 * ut
   232  	}
   233  
   234  	return ut
   235  
   236  }
   237  
   238  // Flattens an array of databases into a []map[string]interface{}
   239  func flattenDatabaseMappings(list []rds.Database) []map[string]interface{} {
   240  	result := make([]map[string]interface{}, 0, len(list))
   241  	for _, i := range list {
   242  		l := map[string]interface{}{
   243  			"db_name":            i.DBName,
   244  			"character_set_name": i.CharacterSetName,
   245  			"db_description":     i.DBDescription,
   246  		}
   247  		result = append(result, l)
   248  	}
   249  	return result
   250  }
   251  
   252  func flattenDBBackup(list []rds.BackupPolicy) []map[string]interface{} {
   253  	result := make([]map[string]interface{}, 0, len(list))
   254  	for _, i := range list {
   255  		l := map[string]interface{}{
   256  			"preferred_backup_period": i.PreferredBackupPeriod,
   257  			"preferred_backup_time":   i.PreferredBackupTime,
   258  			"backup_retention_period": i.LogBackupRetentionPeriod,
   259  		}
   260  		result = append(result, l)
   261  	}
   262  	return result
   263  }
   264  
   265  func flattenDBSecurityIPs(list []rds.DBInstanceIPList) []map[string]interface{} {
   266  	result := make([]map[string]interface{}, 0, len(list))
   267  	for _, i := range list {
   268  		l := map[string]interface{}{
   269  			"security_ips": i.SecurityIPList,
   270  		}
   271  		result = append(result, l)
   272  	}
   273  	return result
   274  }
   275  
   276  // Flattens an array of databases connection into a []map[string]interface{}
   277  func flattenDBConnections(list []rds.DBInstanceNetInfo) []map[string]interface{} {
   278  	result := make([]map[string]interface{}, 0, len(list))
   279  	for _, i := range list {
   280  		l := map[string]interface{}{
   281  			"connection_string": i.ConnectionString,
   282  			"ip_type":           i.IPType,
   283  			"ip_address":        i.IPAddress,
   284  		}
   285  		result = append(result, l)
   286  	}
   287  	return result
   288  }