|
| 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/neptune" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceAwsNeptuneEngineVersion() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceAwsNeptuneEngineVersionRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "engine": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Optional: true, |
| 19 | + Default: "neptune", |
| 20 | + }, |
| 21 | + |
| 22 | + "engine_description": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + |
| 27 | + "exportable_log_types": { |
| 28 | + Type: schema.TypeList, |
| 29 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + |
| 33 | + "parameter_group_name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Optional: true, |
| 37 | + }, |
| 38 | + |
| 39 | + "preferred_versions": { |
| 40 | + Type: schema.TypeList, |
| 41 | + Optional: true, |
| 42 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 43 | + ConflictsWith: []string{"version"}, |
| 44 | + }, |
| 45 | + |
| 46 | + "supported_timezones": { |
| 47 | + Type: schema.TypeSet, |
| 48 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 49 | + Computed: true, |
| 50 | + Set: schema.HashString, |
| 51 | + }, |
| 52 | + |
| 53 | + "supports_log_exports_to_cloudwatch": { |
| 54 | + Type: schema.TypeBool, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + |
| 58 | + "supports_read_replica": { |
| 59 | + Type: schema.TypeBool, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + |
| 63 | + "valid_upgrade_targets": { |
| 64 | + Type: schema.TypeSet, |
| 65 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 66 | + Computed: true, |
| 67 | + Set: schema.HashString, |
| 68 | + }, |
| 69 | + |
| 70 | + "version": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Optional: true, |
| 74 | + ConflictsWith: []string{"preferred_versions"}, |
| 75 | + }, |
| 76 | + |
| 77 | + "version_description": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dataSourceAwsNeptuneEngineVersionRead(d *schema.ResourceData, meta interface{}) error { |
| 86 | + conn := meta.(*AWSClient).neptuneconn |
| 87 | + |
| 88 | + input := &neptune.DescribeDBEngineVersionsInput{} |
| 89 | + |
| 90 | + if v, ok := d.GetOk("engine"); ok { |
| 91 | + input.Engine = aws.String(v.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + if v, ok := d.GetOk("parameter_group_name"); ok { |
| 95 | + input.DBParameterGroupFamily = aws.String(v.(string)) |
| 96 | + } |
| 97 | + |
| 98 | + if v, ok := d.GetOk("version"); ok { |
| 99 | + input.EngineVersion = aws.String(v.(string)) |
| 100 | + } |
| 101 | + |
| 102 | + if _, ok := d.GetOk("version"); !ok { |
| 103 | + if _, ok := d.GetOk("preferred_versions"); !ok { |
| 104 | + input.DefaultOnly = aws.Bool(true) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + log.Printf("[DEBUG] Reading Neptune engine versions: %v", input) |
| 109 | + var engineVersions []*neptune.DBEngineVersion |
| 110 | + |
| 111 | + err := conn.DescribeDBEngineVersionsPages(input, func(resp *neptune.DescribeDBEngineVersionsOutput, lastPage bool) bool { |
| 112 | + for _, engineVersion := range resp.DBEngineVersions { |
| 113 | + if engineVersion == nil { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + engineVersions = append(engineVersions, engineVersion) |
| 118 | + } |
| 119 | + return !lastPage |
| 120 | + }) |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("error reading Neptune engine versions: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + if len(engineVersions) == 0 { |
| 127 | + return fmt.Errorf("no Neptune engine versions found") |
| 128 | + } |
| 129 | + |
| 130 | + // preferred versions |
| 131 | + var found *neptune.DBEngineVersion |
| 132 | + if l := d.Get("preferred_versions").([]interface{}); len(l) > 0 { |
| 133 | + for _, elem := range l { |
| 134 | + preferredVersion, ok := elem.(string) |
| 135 | + |
| 136 | + if !ok { |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + for _, engineVersion := range engineVersions { |
| 141 | + if preferredVersion == aws.StringValue(engineVersion.EngineVersion) { |
| 142 | + found = engineVersion |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if found != nil { |
| 148 | + break |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if found == nil && len(engineVersions) > 1 { |
| 154 | + return fmt.Errorf("multiple Neptune engine versions (%v) match the criteria", engineVersions) |
| 155 | + } |
| 156 | + |
| 157 | + if found == nil && len(engineVersions) == 1 { |
| 158 | + found = engineVersions[0] |
| 159 | + } |
| 160 | + |
| 161 | + if found == nil { |
| 162 | + return fmt.Errorf("no Neptune engine versions match the criteria") |
| 163 | + } |
| 164 | + |
| 165 | + d.SetId(aws.StringValue(found.EngineVersion)) |
| 166 | + |
| 167 | + d.Set("engine", found.Engine) |
| 168 | + d.Set("engine_description", found.DBEngineDescription) |
| 169 | + d.Set("exportable_log_types", found.ExportableLogTypes) |
| 170 | + d.Set("parameter_group_name", found.DBParameterGroupFamily) |
| 171 | + |
| 172 | + var timezones []string |
| 173 | + for _, tz := range found.SupportedTimezones { |
| 174 | + timezones = append(timezones, aws.StringValue(tz.TimezoneName)) |
| 175 | + } |
| 176 | + d.Set("supported_timezones", timezones) |
| 177 | + |
| 178 | + d.Set("supports_log_exports_to_cloudwatch", found.SupportsLogExportsToCloudwatchLogs) |
| 179 | + d.Set("supports_read_replica", found.SupportsReadReplica) |
| 180 | + |
| 181 | + var upgradeTargets []string |
| 182 | + for _, ut := range found.ValidUpgradeTarget { |
| 183 | + upgradeTargets = append(upgradeTargets, aws.StringValue(ut.EngineVersion)) |
| 184 | + } |
| 185 | + d.Set("valid_upgrade_targets", upgradeTargets) |
| 186 | + |
| 187 | + d.Set("version", found.EngineVersion) |
| 188 | + d.Set("version_description", found.DBEngineVersionDescription) |
| 189 | + |
| 190 | + return nil |
| 191 | +} |
0 commit comments