|
| 1 | +package postgresql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudPostgresqlDbVersions() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudPostgresqlDbVersionsRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "db_version": { |
| 19 | + Optional: true, |
| 20 | + Type: schema.TypeString, |
| 21 | + Description: "Version of the postgresql database engine.", |
| 22 | + }, |
| 23 | + "db_major_version": { |
| 24 | + Optional: true, |
| 25 | + Type: schema.TypeString, |
| 26 | + Description: "PostgreSQL major version number.", |
| 27 | + }, |
| 28 | + "db_kernel_version": { |
| 29 | + Optional: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "PostgreSQL kernel version number.", |
| 32 | + }, |
| 33 | + // computed |
| 34 | + "version_set": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Computed: true, |
| 37 | + Description: "List of database versions.", |
| 38 | + Elem: &schema.Resource{ |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "db_engine": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "Database engines. Valid values:\n1. `postgresql` (TencentDB for PostgreSQL)\n2. `mssql_compatible` (MSSQL compatible-TencentDB for PostgreSQL).", |
| 44 | + }, |
| 45 | + "db_version": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "Database version, such as 12.4.", |
| 49 | + }, |
| 50 | + "db_major_version": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Database major version, such as 12.", |
| 54 | + }, |
| 55 | + "db_kernel_version": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Database kernel version, such as v12.4_r1.3.", |
| 59 | + }, |
| 60 | + "supported_feature_names": { |
| 61 | + Type: schema.TypeSet, |
| 62 | + Computed: true, |
| 63 | + Description: "List of features supported by the database kernel, such as:\nTDE: Supports data encryption.", |
| 64 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 65 | + }, |
| 66 | + "status": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Database version status. Valid values:\n`AVAILABLE`.\n`DEPRECATED`.", |
| 70 | + }, |
| 71 | + "available_upgrade_target": { |
| 72 | + Type: schema.TypeSet, |
| 73 | + Computed: true, |
| 74 | + Description: "List of versions to which this database version (`DBKernelVersion`) can be upgraded, including minor and major version numbers available for upgrade (complete kernel version format example: v15.1_v1.6).", |
| 75 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + |
| 81 | + "result_output_file": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Optional: true, |
| 84 | + Description: "Used to save results.", |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func dataSourceTencentCloudPostgresqlDbVersionsRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer tccommon.LogElapsed("data_source.tencentcloud_postgresql_db_versions.read")() |
| 92 | + defer tccommon.InconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + var ( |
| 95 | + logId = tccommon.GetLogId(nil) |
| 96 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 97 | + service = PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 98 | + ) |
| 99 | + |
| 100 | + paramMap := make(map[string]interface{}) |
| 101 | + if v, ok := d.GetOk("db_version"); ok { |
| 102 | + paramMap["DBVersion"] = v.(string) |
| 103 | + } |
| 104 | + |
| 105 | + if v, ok := d.GetOk("db_major_version"); ok { |
| 106 | + paramMap["DBMajorVersion"] = v.(string) |
| 107 | + } |
| 108 | + |
| 109 | + if v, ok := d.GetOk("db_kernel_version"); ok { |
| 110 | + paramMap["DBKernelVersion"] = v.(string) |
| 111 | + } |
| 112 | + |
| 113 | + var respData []*postgresql.Version |
| 114 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 115 | + result, e := service.DescribePostgresqlDbVersionsByFilter(ctx, paramMap) |
| 116 | + if e != nil { |
| 117 | + return tccommon.RetryError(e) |
| 118 | + } |
| 119 | + |
| 120 | + respData = result |
| 121 | + return nil |
| 122 | + }) |
| 123 | + |
| 124 | + if reqErr != nil { |
| 125 | + return reqErr |
| 126 | + } |
| 127 | + |
| 128 | + ids := make([]string, 0, len(respData)) |
| 129 | + versionSetList := make([]map[string]interface{}, 0, len(respData)) |
| 130 | + if respData != nil { |
| 131 | + for _, versionSet := range respData { |
| 132 | + versionSetMap := map[string]interface{}{} |
| 133 | + if versionSet.DBEngine != nil { |
| 134 | + versionSetMap["db_engine"] = versionSet.DBEngine |
| 135 | + } |
| 136 | + |
| 137 | + if versionSet.DBVersion != nil { |
| 138 | + versionSetMap["db_version"] = versionSet.DBVersion |
| 139 | + } |
| 140 | + |
| 141 | + if versionSet.DBMajorVersion != nil { |
| 142 | + versionSetMap["db_major_version"] = versionSet.DBMajorVersion |
| 143 | + } |
| 144 | + |
| 145 | + if versionSet.DBKernelVersion != nil { |
| 146 | + versionSetMap["db_kernel_version"] = versionSet.DBKernelVersion |
| 147 | + } |
| 148 | + |
| 149 | + if versionSet.SupportedFeatureNames != nil { |
| 150 | + versionSetMap["supported_feature_names"] = versionSet.SupportedFeatureNames |
| 151 | + } |
| 152 | + |
| 153 | + if versionSet.Status != nil { |
| 154 | + versionSetMap["status"] = versionSet.Status |
| 155 | + } |
| 156 | + |
| 157 | + if versionSet.AvailableUpgradeTarget != nil { |
| 158 | + versionSetMap["available_upgrade_target"] = versionSet.AvailableUpgradeTarget |
| 159 | + } |
| 160 | + |
| 161 | + ids = append(ids, *versionSet.DBEngine) |
| 162 | + versionSetList = append(versionSetList, versionSetMap) |
| 163 | + } |
| 164 | + |
| 165 | + _ = d.Set("version_set", versionSetList) |
| 166 | + } |
| 167 | + |
| 168 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 169 | + output, ok := d.GetOk("result_output_file") |
| 170 | + if ok && output.(string) != "" { |
| 171 | + if e := tccommon.WriteToFile(output.(string), versionSetList); e != nil { |
| 172 | + return e |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
0 commit comments