|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/service/rds" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceAwsRdsEngineVersion() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceAwsRdsEngineVersionRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "default_character_set": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Computed: true, |
| 19 | + }, |
| 20 | + |
| 21 | + "engine": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + |
| 26 | + "engine_description": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + |
| 31 | + "exportable_log_types": { |
| 32 | + Type: schema.TypeSet, |
| 33 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 34 | + Computed: true, |
| 35 | + Set: schema.HashString, |
| 36 | + }, |
| 37 | + |
| 38 | + "parameter_group_family": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Optional: true, |
| 42 | + }, |
| 43 | + |
| 44 | + "preferred_versions": { |
| 45 | + Type: schema.TypeList, |
| 46 | + Optional: true, |
| 47 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 48 | + ConflictsWith: []string{"version"}, |
| 49 | + }, |
| 50 | + |
| 51 | + "status": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + |
| 56 | + "supported_character_sets": { |
| 57 | + Type: schema.TypeSet, |
| 58 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 59 | + Computed: true, |
| 60 | + Set: schema.HashString, |
| 61 | + }, |
| 62 | + |
| 63 | + "supported_feature_names": { |
| 64 | + Type: schema.TypeSet, |
| 65 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 66 | + Computed: true, |
| 67 | + Set: schema.HashString, |
| 68 | + }, |
| 69 | + |
| 70 | + "supported_modes": { |
| 71 | + Type: schema.TypeSet, |
| 72 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 73 | + Computed: true, |
| 74 | + Set: schema.HashString, |
| 75 | + }, |
| 76 | + |
| 77 | + "supported_timezones": { |
| 78 | + Type: schema.TypeSet, |
| 79 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 80 | + Computed: true, |
| 81 | + Set: schema.HashString, |
| 82 | + }, |
| 83 | + |
| 84 | + "supports_global_databases": { |
| 85 | + Type: schema.TypeBool, |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + |
| 89 | + "supports_log_exports_to_cloudwatch": { |
| 90 | + Type: schema.TypeBool, |
| 91 | + Computed: true, |
| 92 | + }, |
| 93 | + |
| 94 | + "supports_parallel_query": { |
| 95 | + Type: schema.TypeBool, |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + |
| 99 | + "supports_read_replica": { |
| 100 | + Type: schema.TypeBool, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + |
| 104 | + "valid_upgrade_targets": { |
| 105 | + Type: schema.TypeSet, |
| 106 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 107 | + Computed: true, |
| 108 | + Set: schema.HashString, |
| 109 | + }, |
| 110 | + |
| 111 | + "version": { |
| 112 | + Type: schema.TypeString, |
| 113 | + Computed: true, |
| 114 | + Optional: true, |
| 115 | + ConflictsWith: []string{"preferred_versions"}, |
| 116 | + }, |
| 117 | + |
| 118 | + "version_description": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Computed: true, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func dataSourceAwsRdsEngineVersionRead(d *schema.ResourceData, meta interface{}) error { |
| 127 | + conn := meta.(*AWSClient).rdsconn |
| 128 | + |
| 129 | + input := &rds.DescribeDBEngineVersionsInput{ |
| 130 | + ListSupportedCharacterSets: aws.Bool(true), |
| 131 | + ListSupportedTimezones: aws.Bool(true), |
| 132 | + } |
| 133 | + |
| 134 | + if v, ok := d.GetOk("engine"); ok { |
| 135 | + input.Engine = aws.String(v.(string)) |
| 136 | + } |
| 137 | + |
| 138 | + if v, ok := d.GetOk("parameter_group_family"); ok { |
| 139 | + input.DBParameterGroupFamily = aws.String(v.(string)) |
| 140 | + } |
| 141 | + |
| 142 | + if v, ok := d.GetOk("version"); ok { |
| 143 | + input.EngineVersion = aws.String(v.(string)) |
| 144 | + } |
| 145 | + |
| 146 | + if _, ok := d.GetOk("version"); !ok { |
| 147 | + if _, ok := d.GetOk("preferred_versions"); !ok { |
| 148 | + input.DefaultOnly = aws.Bool(true) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + log.Printf("[DEBUG] Reading RDS engine versions: %v", input) |
| 153 | + var engineVersions []*rds.DBEngineVersion |
| 154 | + |
| 155 | + err := conn.DescribeDBEngineVersionsPages(input, func(resp *rds.DescribeDBEngineVersionsOutput, lastPage bool) bool { |
| 156 | + for _, engineVersion := range resp.DBEngineVersions { |
| 157 | + if engineVersion == nil { |
| 158 | + continue |
| 159 | + } |
| 160 | + |
| 161 | + engineVersions = append(engineVersions, engineVersion) |
| 162 | + } |
| 163 | + return !lastPage |
| 164 | + }) |
| 165 | + |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("error reading RDS engine versions: %w", err) |
| 168 | + } |
| 169 | + |
| 170 | + if len(engineVersions) == 0 { |
| 171 | + return fmt.Errorf("no RDS engine versions found") |
| 172 | + } |
| 173 | + |
| 174 | + // preferred versions |
| 175 | + var found *rds.DBEngineVersion |
| 176 | + if l := d.Get("preferred_versions").([]interface{}); len(l) > 0 { |
| 177 | + for _, elem := range l { |
| 178 | + preferredVersion, ok := elem.(string) |
| 179 | + |
| 180 | + if !ok { |
| 181 | + continue |
| 182 | + } |
| 183 | + |
| 184 | + for _, engineVersion := range engineVersions { |
| 185 | + if preferredVersion == aws.StringValue(engineVersion.EngineVersion) { |
| 186 | + found = engineVersion |
| 187 | + break |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if found != nil { |
| 192 | + break |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if found == nil && len(engineVersions) > 1 { |
| 198 | + return fmt.Errorf("multiple RDS engine versions (%v) match the criteria", engineVersions) |
| 199 | + } |
| 200 | + |
| 201 | + if found == nil && len(engineVersions) == 1 { |
| 202 | + found = engineVersions[0] |
| 203 | + } |
| 204 | + |
| 205 | + if found == nil { |
| 206 | + return fmt.Errorf("no RDS engine versions match the criteria") |
| 207 | + } |
| 208 | + |
| 209 | + d.SetId(aws.StringValue(found.EngineVersion)) |
| 210 | + |
| 211 | + if found.DefaultCharacterSet != nil { |
| 212 | + d.Set("default_character_set", found.DefaultCharacterSet.CharacterSetName) |
| 213 | + } |
| 214 | + |
| 215 | + d.Set("engine", found.Engine) |
| 216 | + d.Set("engine_description", found.DBEngineDescription) |
| 217 | + d.Set("exportable_log_types", found.ExportableLogTypes) |
| 218 | + d.Set("parameter_group_family", found.DBParameterGroupFamily) |
| 219 | + d.Set("status", found.Status) |
| 220 | + |
| 221 | + var characterSets []string |
| 222 | + for _, cs := range found.SupportedCharacterSets { |
| 223 | + characterSets = append(characterSets, aws.StringValue(cs.CharacterSetName)) |
| 224 | + } |
| 225 | + d.Set("supported_character_sets", characterSets) |
| 226 | + |
| 227 | + d.Set("supported_feature_names", found.SupportedFeatureNames) |
| 228 | + d.Set("supported_modes", found.SupportedEngineModes) |
| 229 | + |
| 230 | + var timezones []string |
| 231 | + for _, tz := range found.SupportedTimezones { |
| 232 | + timezones = append(timezones, aws.StringValue(tz.TimezoneName)) |
| 233 | + } |
| 234 | + d.Set("supported_timezones", timezones) |
| 235 | + |
| 236 | + d.Set("supports_global_databases", found.SupportsGlobalDatabases) |
| 237 | + d.Set("supports_log_exports_to_cloudwatch", found.SupportsLogExportsToCloudwatchLogs) |
| 238 | + d.Set("supports_parallel_query", found.SupportsParallelQuery) |
| 239 | + d.Set("supports_read_replica", found.SupportsReadReplica) |
| 240 | + |
| 241 | + var upgradeTargets []string |
| 242 | + for _, ut := range found.ValidUpgradeTarget { |
| 243 | + upgradeTargets = append(upgradeTargets, aws.StringValue(ut.EngineVersion)) |
| 244 | + } |
| 245 | + d.Set("valid_upgrade_targets", upgradeTargets) |
| 246 | + |
| 247 | + d.Set("version", found.EngineVersion) |
| 248 | + d.Set("version_description", found.DBEngineVersionDescription) |
| 249 | + |
| 250 | + return nil |
| 251 | +} |
0 commit comments