github.com/terraform-modules-krish/terratest@v0.29.0/modules/aws/rds.go (about) 1 package aws 2 3 import ( 4 "database/sql" 5 "fmt" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/rds" 9 _ "github.com/go-sql-driver/mysql" 10 "github.com/terraform-modules-krish/terratest/modules/testing" 11 ) 12 13 // GetAddressOfRdsInstance gets the address of the given RDS Instance in the given region. 14 func GetAddressOfRdsInstance(t testing.TestingT, dbInstanceID string, awsRegion string) string { 15 address, err := GetAddressOfRdsInstanceE(t, dbInstanceID, awsRegion) 16 if err != nil { 17 t.Fatal(err) 18 } 19 return address 20 } 21 22 // GetAddressOfRdsInstanceE gets the address of the given RDS Instance in the given region. 23 func GetAddressOfRdsInstanceE(t testing.TestingT, dbInstanceID string, awsRegion string) (string, error) { 24 dbInstance, err := GetRdsInstanceDetailsE(t, dbInstanceID, awsRegion) 25 if err != nil { 26 return "", err 27 } 28 29 return aws.StringValue(dbInstance.Endpoint.Address), nil 30 } 31 32 // GetPortOfRdsInstance gets the address of the given RDS Instance in the given region. 33 func GetPortOfRdsInstance(t testing.TestingT, dbInstanceID string, awsRegion string) int64 { 34 port, err := GetPortOfRdsInstanceE(t, dbInstanceID, awsRegion) 35 if err != nil { 36 t.Fatal(err) 37 } 38 return port 39 } 40 41 // GetPortOfRdsInstanceE gets the address of the given RDS Instance in the given region. 42 func GetPortOfRdsInstanceE(t testing.TestingT, dbInstanceID string, awsRegion string) (int64, error) { 43 dbInstance, err := GetRdsInstanceDetailsE(t, dbInstanceID, awsRegion) 44 if err != nil { 45 return -1, err 46 } 47 48 return *dbInstance.Endpoint.Port, nil 49 } 50 51 // GetWhetherSchemaExistsInRdsMySqlInstance checks whether the specified schema/table name exists in the RDS instance 52 func GetWhetherSchemaExistsInRdsMySqlInstance(t testing.TestingT, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) bool { 53 output, err := GetWhetherSchemaExistsInRdsMySqlInstanceE(t, dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName) 54 if err != nil { 55 t.Fatal(err) 56 } 57 return output 58 } 59 60 // GetWhetherSchemaExistsInRdsMySqlInstanceE checks whether the specified schema/table name exists in the RDS instance 61 func GetWhetherSchemaExistsInRdsMySqlInstanceE(t testing.TestingT, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) (bool, error) { 62 connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/", dbUsername, dbPassword, dbUrl, dbPort) 63 db, connErr := sql.Open("mysql", connectionString) 64 if connErr != nil { 65 return false, connErr 66 } 67 defer db.Close() 68 var ( 69 schemaName string 70 ) 71 sqlStatement := "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=?;" 72 row := db.QueryRow(sqlStatement, expectedSchemaName) 73 scanErr := row.Scan(&schemaName) 74 if scanErr != nil { 75 return false, scanErr 76 } 77 return true, nil 78 } 79 80 // GetParameterValueForParameterOfRdsInstance gets the value of the parameter name specified for the RDS instance in the given region. 81 func GetParameterValueForParameterOfRdsInstance(t testing.TestingT, parameterName string, dbInstanceID string, awsRegion string) string { 82 parameterValue, err := GetParameterValueForParameterOfRdsInstanceE(t, parameterName, dbInstanceID, awsRegion) 83 if err != nil { 84 t.Fatal(err) 85 } 86 return parameterValue 87 } 88 89 // GetParameterValueForParameterOfRdsInstanceE gets the value of the parameter name specified for the RDS instance in the given region. 90 func GetParameterValueForParameterOfRdsInstanceE(t testing.TestingT, parameterName string, dbInstanceID string, awsRegion string) (string, error) { 91 output := GetAllParametersOfRdsInstance(t, dbInstanceID, awsRegion) 92 for _, parameter := range output { 93 if aws.StringValue(parameter.ParameterName) == parameterName { 94 return aws.StringValue(parameter.ParameterValue), nil 95 } 96 } 97 return "", ParameterForDbInstanceNotFound{ParameterName: parameterName, DbInstanceID: dbInstanceID, AwsRegion: awsRegion} 98 } 99 100 // GetOptionSettingForOfRdsInstance gets the value of the option name in the option group specified for the RDS instance in the given region. 101 func GetOptionSettingForOfRdsInstance(t testing.TestingT, optionName string, optionSettingName string, dbInstanceID, awsRegion string) string { 102 optionValue, err := GetOptionSettingForOfRdsInstanceE(t, optionName, optionSettingName, dbInstanceID, awsRegion) 103 if err != nil { 104 t.Fatal(err) 105 } 106 return optionValue 107 } 108 109 // GetOptionSettingForOfRdsInstanceE gets the value of the option name in the option group specified for the RDS instance in the given region. 110 func GetOptionSettingForOfRdsInstanceE(t testing.TestingT, optionName string, optionSettingName string, dbInstanceID, awsRegion string) (string, error) { 111 optionGroupName := GetOptionGroupNameOfRdsInstance(t, dbInstanceID, awsRegion) 112 options := GetOptionsOfOptionGroup(t, optionGroupName, awsRegion) 113 for _, option := range options { 114 if aws.StringValue(option.OptionName) == optionName { 115 for _, optionSetting := range option.OptionSettings { 116 if aws.StringValue(optionSetting.Name) == optionSettingName { 117 return aws.StringValue(optionSetting.Value), nil 118 } 119 } 120 } 121 } 122 return "", OptionGroupOptionSettingForDbInstanceNotFound{OptionName: optionName, OptionSettingName: optionSettingName, DbInstanceID: dbInstanceID, AwsRegion: awsRegion} 123 } 124 125 // GetOptionGroupNameOfRdsInstance gets the name of the option group associated with the RDS instance 126 func GetOptionGroupNameOfRdsInstance(t testing.TestingT, dbInstanceID string, awsRegion string) string { 127 dbInstance, err := GetOptionGroupNameOfRdsInstanceE(t, dbInstanceID, awsRegion) 128 if err != nil { 129 t.Fatal(err) 130 } 131 return dbInstance 132 } 133 134 // GetOptionGroupNameOfRdsInstanceE gets the name of the option group associated with the RDS instance 135 func GetOptionGroupNameOfRdsInstanceE(t testing.TestingT, dbInstanceID string, awsRegion string) (string, error) { 136 dbInstance, err := GetRdsInstanceDetailsE(t, dbInstanceID, awsRegion) 137 if err != nil { 138 return "", err 139 } 140 return aws.StringValue(dbInstance.OptionGroupMemberships[0].OptionGroupName), nil 141 } 142 143 // GetOptionsOfOptionGroup gets the options of the option group specified 144 func GetOptionsOfOptionGroup(t testing.TestingT, optionGroupName string, awsRegion string) []*rds.Option { 145 output, err := GetOptionsOfOptionGroupE(t, optionGroupName, awsRegion) 146 if err != nil { 147 t.Fatal(err) 148 } 149 return output 150 } 151 152 // GetOptionsOfOptionGroupE gets the options of the option group specified 153 func GetOptionsOfOptionGroupE(t testing.TestingT, optionGroupName string, awsRegion string) ([]*rds.Option, error) { 154 rdsClient := NewRdsClient(t, awsRegion) 155 input := rds.DescribeOptionGroupsInput{OptionGroupName: aws.String(optionGroupName)} 156 output, err := rdsClient.DescribeOptionGroups(&input) 157 if err != nil { 158 return []*rds.Option{}, err 159 } 160 return output.OptionGroupsList[0].Options, nil 161 } 162 163 // GetAllParametersOfRdsInstance gets all the parameters defined in the parameter group for the RDS instance in the given region. 164 func GetAllParametersOfRdsInstance(t testing.TestingT, dbInstanceID string, awsRegion string) []*rds.Parameter { 165 parameters, err := GetAllParametersOfRdsInstanceE(t, dbInstanceID, awsRegion) 166 if err != nil { 167 t.Fatal(err) 168 } 169 return parameters 170 } 171 172 // GetAllParametersOfRdsInstanceE gets all the parameters defined in the parameter group for the RDS instance in the given region. 173 func GetAllParametersOfRdsInstanceE(t testing.TestingT, dbInstanceID string, awsRegion string) ([]*rds.Parameter, error) { 174 dbInstance, dbInstanceErr := GetRdsInstanceDetailsE(t, dbInstanceID, awsRegion) 175 if dbInstanceErr != nil { 176 return []*rds.Parameter{}, dbInstanceErr 177 } 178 parameterGroupName := aws.StringValue(dbInstance.DBParameterGroups[0].DBParameterGroupName) 179 180 rdsClient := NewRdsClient(t, awsRegion) 181 input := rds.DescribeDBParametersInput{DBParameterGroupName: aws.String(parameterGroupName)} 182 output, err := rdsClient.DescribeDBParameters(&input) 183 184 if err != nil { 185 return []*rds.Parameter{}, err 186 } 187 return output.Parameters, nil 188 } 189 190 // GetRdsInstanceDetailsE gets the details of a single DB instance whose identifier is passed. 191 func GetRdsInstanceDetailsE(t testing.TestingT, dbInstanceID string, awsRegion string) (*rds.DBInstance, error) { 192 rdsClient := NewRdsClient(t, awsRegion) 193 input := rds.DescribeDBInstancesInput{DBInstanceIdentifier: aws.String(dbInstanceID)} 194 output, err := rdsClient.DescribeDBInstances(&input) 195 if err != nil { 196 return nil, err 197 } 198 return output.DBInstances[0], nil 199 } 200 201 // NewRdsClient creates an RDS client. 202 func NewRdsClient(t testing.TestingT, region string) *rds.RDS { 203 client, err := NewRdsClientE(t, region) 204 if err != nil { 205 t.Fatal(err) 206 } 207 return client 208 } 209 210 // NewRdsClientE creates an RDS client. 211 func NewRdsClientE(t testing.TestingT, region string) (*rds.RDS, error) { 212 sess, err := NewAuthenticatedSession(region) 213 if err != nil { 214 return nil, err 215 } 216 217 return rds.New(sess), nil 218 } 219 220 // ParameterForDbInstanceNotFound is an error that occurs when the parameter group specified is not found for the DB instance 221 type ParameterForDbInstanceNotFound struct { 222 ParameterName string 223 DbInstanceID string 224 AwsRegion string 225 } 226 227 func (err ParameterForDbInstanceNotFound) Error() string { 228 return fmt.Sprintf("Could not find a parameter %s in parameter group of database %s in %s", err.ParameterName, err.DbInstanceID, err.AwsRegion) 229 } 230 231 // OptionGroupOptionSettingForDbInstanceNotFound is an error that occurs when the option setting specified is not found in the option group of the DB instance 232 type OptionGroupOptionSettingForDbInstanceNotFound struct { 233 OptionName string 234 OptionSettingName string 235 DbInstanceID string 236 AwsRegion string 237 } 238 239 func (err OptionGroupOptionSettingForDbInstanceNotFound) Error() string { 240 return fmt.Sprintf("Could not find a option setting %s in option name %s of database %s in %s", err.OptionName, err.OptionSettingName, err.DbInstanceID, err.AwsRegion) 241 }