|
| 1 | +/* |
| 2 | +Use this data source to query postgresql instances |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_postgresql_instances" "name"{ |
| 8 | + name = "test" |
| 9 | +} |
| 10 | +
|
| 11 | +data "tencentcloud_postgresql_instances" "project"{ |
| 12 | + project_id = 0 |
| 13 | +} |
| 14 | +
|
| 15 | +data "tencentcloud_postgresql_instances" "id"{ |
| 16 | + id = "postgres-h9t4fde1" |
| 17 | +} |
| 18 | +``` |
| 19 | +*/ |
| 20 | +package tencentcloud |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "log" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 27 | + postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 28 | + "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 29 | +) |
| 30 | + |
| 31 | +func dataSourceTencentCloudPostgresqlInstances() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Read: dataSourceTencentCloudPostgresqlInstanceRead, |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "name": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Description: "Name of the postgresql instance to be query.", |
| 39 | + }, |
| 40 | + "id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: "ID of the postgresql instance to be query.", |
| 44 | + }, |
| 45 | + "project_id": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Optional: true, |
| 48 | + Description: "Project id of the postgresql instance to be query.", |
| 49 | + }, |
| 50 | + "result_output_file": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: "Used to save results.", |
| 54 | + }, |
| 55 | + "instance_list": { |
| 56 | + Type: schema.TypeList, |
| 57 | + Computed: true, |
| 58 | + Description: "A list of postgresql instances. Each element contains the following attributes.", |
| 59 | + Elem: &schema.Resource{ |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "ID of the postgresql instance.", |
| 65 | + }, |
| 66 | + "name": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Name of the postgresql instance.", |
| 70 | + }, |
| 71 | + "charge_type": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Pay type of the postgresql instance.", |
| 75 | + }, |
| 76 | + "auto_renew_flag": { |
| 77 | + Type: schema.TypeInt, |
| 78 | + Computed: true, |
| 79 | + Description: "Auto renew flag.", |
| 80 | + }, |
| 81 | + "engine_version": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "Version of the postgresql database engine.", |
| 85 | + }, |
| 86 | + "vpc_id": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "ID of VPC.", |
| 90 | + }, |
| 91 | + "subnet_id": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "ID of subnet.", |
| 95 | + }, |
| 96 | + "storage": { |
| 97 | + Type: schema.TypeInt, |
| 98 | + Computed: true, |
| 99 | + Description: "Disk size (in GB).", |
| 100 | + }, |
| 101 | + "memory": { |
| 102 | + Type: schema.TypeInt, |
| 103 | + Computed: true, |
| 104 | + Description: "Memory size (in MB).", |
| 105 | + }, |
| 106 | + "project_id": { |
| 107 | + Type: schema.TypeInt, |
| 108 | + Computed: true, |
| 109 | + Description: "Project ID, default value is 0.", |
| 110 | + }, |
| 111 | + "availability_zone": { |
| 112 | + Type: schema.TypeString, |
| 113 | + Computed: true, |
| 114 | + Description: "Availability zone.", |
| 115 | + }, |
| 116 | + "public_access_switch": { |
| 117 | + Type: schema.TypeBool, |
| 118 | + Computed: true, |
| 119 | + Description: "Indicates whether to enable the access to an instance from public network or not.", |
| 120 | + }, |
| 121 | + "public_access_host": { |
| 122 | + Type: schema.TypeString, |
| 123 | + Computed: true, |
| 124 | + Description: "Host for public access.", |
| 125 | + }, |
| 126 | + "public_access_port": { |
| 127 | + Type: schema.TypeInt, |
| 128 | + Computed: true, |
| 129 | + Description: "Port for public access.", |
| 130 | + }, |
| 131 | + "private_access_ip": { |
| 132 | + Type: schema.TypeString, |
| 133 | + Computed: true, |
| 134 | + Description: "Ip address for private access.", |
| 135 | + }, |
| 136 | + "private_access_port": { |
| 137 | + Type: schema.TypeInt, |
| 138 | + Computed: true, |
| 139 | + Description: "Port for private access.", |
| 140 | + }, |
| 141 | + "charset": { |
| 142 | + Type: schema.TypeString, |
| 143 | + Computed: true, |
| 144 | + Description: "Charset of the postgresql instance.", |
| 145 | + }, |
| 146 | + "create_time": { |
| 147 | + Type: schema.TypeString, |
| 148 | + Computed: true, |
| 149 | + Description: "Create time of the postgresql instance.", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func dataSourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta interface{}) error { |
| 159 | + defer logElapsed("data_source.tencentcloud_postgresql_instances.read")() |
| 160 | + |
| 161 | + logId := getLogId(contextNil) |
| 162 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 163 | + |
| 164 | + service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 165 | + |
| 166 | + filter := make([]*postgresql.Filter, 0) |
| 167 | + if v, ok := d.GetOk("name"); ok { |
| 168 | + filter = append(filter, &postgresql.Filter{Name: helper.String("db-instance-name"), Values: []*string{helper.String(v.(string))}}) |
| 169 | + } |
| 170 | + if v, ok := d.GetOk("id"); ok { |
| 171 | + filter = append(filter, &postgresql.Filter{Name: helper.String("db-instance-id"), Values: []*string{helper.String(v.(string))}}) |
| 172 | + } |
| 173 | + if v, ok := d.GetOk("project_id"); ok { |
| 174 | + filter = append(filter, &postgresql.Filter{Name: helper.String("db-project-id"), Values: []*string{helper.String(v.(string))}}) |
| 175 | + } |
| 176 | + |
| 177 | + instanceList, err := service.DescribePostgresqlInstances(ctx, filter) |
| 178 | + |
| 179 | + if err != nil { |
| 180 | + instanceList, err = service.DescribePostgresqlInstances(ctx, filter) |
| 181 | + } |
| 182 | + |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + ids := make([]string, 0, len(instanceList)) |
| 188 | + list := make([]map[string]interface{}, 0, len(instanceList)) |
| 189 | + |
| 190 | + for _, v := range instanceList { |
| 191 | + listItem := make(map[string]interface{}) |
| 192 | + listItem["id"] = v.DBInstanceId |
| 193 | + listItem["name"] = v.DBInstanceName |
| 194 | + listItem["auto_renew_flag"] = v.AutoRenew |
| 195 | + listItem["project_id"] = v.ProjectId |
| 196 | + listItem["storage"] = v.DBInstanceStorage |
| 197 | + listItem["memory"] = v.DBInstanceMemory |
| 198 | + listItem["availability_zone"] = v.Zone |
| 199 | + listItem["create_time"] = v.CreateTime |
| 200 | + listItem["vpc_id"] = v.VpcId |
| 201 | + listItem["subnet_id"] = v.SubnetId |
| 202 | + listItem["engine_version"] = v.DBVersion |
| 203 | + listItem["public_access_switch"] = false |
| 204 | + listItem["charset"] = v.DBCharset |
| 205 | + listItem["public_access_host"] = "" |
| 206 | + |
| 207 | + for _, netInfo := range v.DBInstanceNetInfo { |
| 208 | + if *netInfo.NetType == "public" { |
| 209 | + if *netInfo.Status == "opened" || *netInfo.Status == "1" { |
| 210 | + listItem["public_access_switch"] = true |
| 211 | + } |
| 212 | + listItem["public_access_host"] = netInfo.Address |
| 213 | + listItem["public_access_port"] = netInfo.Port |
| 214 | + } |
| 215 | + if (*netInfo.NetType == "private" || *netInfo.NetType == "inner") && *netInfo.Ip != "" { |
| 216 | + listItem["private_access_ip"] = netInfo.Ip |
| 217 | + listItem["private_access_port"] = netInfo.Port |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + if *v.PayType == POSTGRESQL_PAYTYPE_PREPAID || *v.PayType == COMMON_PAYTYPE_PREPAID { |
| 222 | + listItem["charge_type"] = COMMON_PAYTYPE_PREPAID |
| 223 | + } else { |
| 224 | + listItem["charge_type"] = COMMON_PAYTYPE_POSTPAID |
| 225 | + } |
| 226 | + list = append(list, listItem) |
| 227 | + ids = append(ids, *v.DBInstanceId) |
| 228 | + } |
| 229 | + |
| 230 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 231 | + if e := d.Set("instance_list", list); e != nil { |
| 232 | + log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error()) |
| 233 | + return e |
| 234 | + } |
| 235 | + output, ok := d.GetOk("result_output_file") |
| 236 | + if ok && output.(string) != "" { |
| 237 | + return writeToFile(output.(string), list) |
| 238 | + } |
| 239 | + return nil |
| 240 | +} |
0 commit comments