|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of postgresql parameter_templates |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_postgresql_parameter_templates" "parameter_templates" { |
| 8 | + filters { |
| 9 | + name = "TemplateName" |
| 10 | + values = ["temp_name"] |
| 11 | + } |
| 12 | + filters { |
| 13 | + name = "DBEngine" |
| 14 | + values = ["postgresql"] |
| 15 | + } |
| 16 | + order_by = "CreateTime" |
| 17 | + order_by_type = "desc" |
| 18 | +} |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | + postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 29 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 30 | +) |
| 31 | + |
| 32 | +func dataSourceTencentCloudPostgresqlParameterTemplates() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Read: dataSourceTencentCloudPostgresqlParameterTemplatesRead, |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "filters": { |
| 37 | + Optional: true, |
| 38 | + Type: schema.TypeList, |
| 39 | + Description: "Filter conditions. Valid values:TemplateName, TemplateId, DBMajorVersion, DBEngine.", |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "name": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Description: "Filter name.", |
| 46 | + }, |
| 47 | + "values": { |
| 48 | + Type: schema.TypeSet, |
| 49 | + Elem: &schema.Schema{ |
| 50 | + Type: schema.TypeString, |
| 51 | + }, |
| 52 | + Optional: true, |
| 53 | + Description: "One or more filter values.", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + |
| 59 | + "order_by": { |
| 60 | + Optional: true, |
| 61 | + Type: schema.TypeString, |
| 62 | + Description: "Sorting metric. Valid values:CreateTime, TemplateName, DBMajorVersion.", |
| 63 | + }, |
| 64 | + |
| 65 | + "order_by_type": { |
| 66 | + Optional: true, |
| 67 | + Type: schema.TypeString, |
| 68 | + Description: "Sorting order. Valid values:asc (ascending order),desc (descending order).", |
| 69 | + }, |
| 70 | + |
| 71 | + "list": { |
| 72 | + Computed: true, |
| 73 | + Type: schema.TypeList, |
| 74 | + Description: "list of parameter templates.", |
| 75 | + Elem: &schema.Resource{ |
| 76 | + Schema: map[string]*schema.Schema{ |
| 77 | + "template_id": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "parameter template ID.", |
| 81 | + }, |
| 82 | + "template_name": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "parameter template name.", |
| 86 | + }, |
| 87 | + "db_major_version": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "the database version to which the parameter template applies.", |
| 91 | + }, |
| 92 | + "db_engine": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "the database engine for which the parameter template applies.", |
| 96 | + }, |
| 97 | + "template_description": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "parameter template description.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + |
| 106 | + "result_output_file": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Optional: true, |
| 109 | + Description: "Used to save results.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func dataSourceTencentCloudPostgresqlParameterTemplatesRead(d *schema.ResourceData, meta interface{}) error { |
| 116 | + defer logElapsed("data_source.tencentcloud_postgresql_parameter_templates.read")() |
| 117 | + defer inconsistentCheck(d, meta)() |
| 118 | + |
| 119 | + logId := getLogId(contextNil) |
| 120 | + |
| 121 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 122 | + |
| 123 | + paramMap := make(map[string]interface{}) |
| 124 | + if v, ok := d.GetOk("filters"); ok { |
| 125 | + filtersSet := v.([]interface{}) |
| 126 | + tmpSet := make([]*postgresql.Filter, 0, len(filtersSet)) |
| 127 | + |
| 128 | + for _, item := range filtersSet { |
| 129 | + filter := postgresql.Filter{} |
| 130 | + filterMap := item.(map[string]interface{}) |
| 131 | + |
| 132 | + if v, ok := filterMap["name"]; ok { |
| 133 | + filter.Name = helper.String(v.(string)) |
| 134 | + } |
| 135 | + if v, ok := filterMap["values"]; ok { |
| 136 | + valuesSet := v.(*schema.Set).List() |
| 137 | + filter.Values = helper.InterfacesStringsPoint(valuesSet) |
| 138 | + } |
| 139 | + tmpSet = append(tmpSet, &filter) |
| 140 | + } |
| 141 | + paramMap["filters"] = tmpSet |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOk("order_by"); ok { |
| 145 | + paramMap["OrderBy"] = helper.String(v.(string)) |
| 146 | + } |
| 147 | + |
| 148 | + if v, ok := d.GetOk("order_by_type"); ok { |
| 149 | + paramMap["OrderByType"] = helper.String(v.(string)) |
| 150 | + } |
| 151 | + |
| 152 | + service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 153 | + |
| 154 | + var parameterTemplateSet []*postgresql.ParameterTemplate |
| 155 | + |
| 156 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 157 | + result, e := service.DescribePostgresqlParameterTemplatesByFilter(ctx, paramMap) |
| 158 | + if e != nil { |
| 159 | + return retryError(e) |
| 160 | + } |
| 161 | + parameterTemplateSet = result |
| 162 | + return nil |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + ids := make([]string, 0, len(parameterTemplateSet)) |
| 168 | + tmpList := make([]map[string]interface{}, 0, len(parameterTemplateSet)) |
| 169 | + |
| 170 | + if parameterTemplateSet != nil { |
| 171 | + for _, parameterTemplate := range parameterTemplateSet { |
| 172 | + parameterTemplateMap := map[string]interface{}{} |
| 173 | + |
| 174 | + if parameterTemplate.TemplateId != nil { |
| 175 | + parameterTemplateMap["template_id"] = parameterTemplate.TemplateId |
| 176 | + } |
| 177 | + |
| 178 | + if parameterTemplate.TemplateName != nil { |
| 179 | + parameterTemplateMap["template_name"] = parameterTemplate.TemplateName |
| 180 | + } |
| 181 | + |
| 182 | + if parameterTemplate.DBMajorVersion != nil { |
| 183 | + parameterTemplateMap["db_major_version"] = parameterTemplate.DBMajorVersion |
| 184 | + } |
| 185 | + |
| 186 | + if parameterTemplate.DBEngine != nil { |
| 187 | + parameterTemplateMap["db_engine"] = parameterTemplate.DBEngine |
| 188 | + } |
| 189 | + |
| 190 | + if parameterTemplate.TemplateDescription != nil { |
| 191 | + parameterTemplateMap["template_description"] = parameterTemplate.TemplateDescription |
| 192 | + } |
| 193 | + |
| 194 | + ids = append(ids, *parameterTemplate.TemplateId) |
| 195 | + tmpList = append(tmpList, parameterTemplateMap) |
| 196 | + } |
| 197 | + |
| 198 | + _ = d.Set("list", tmpList) |
| 199 | + } |
| 200 | + |
| 201 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 202 | + output, ok := d.GetOk("result_output_file") |
| 203 | + if ok && output.(string) != "" { |
| 204 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 205 | + return e |
| 206 | + } |
| 207 | + } |
| 208 | + return nil |
| 209 | +} |
0 commit comments