|
| 1 | +/* |
| 2 | +Use this data source to query SQL Server basic instances |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | +resource "tencentcloud_sqlserver_basic_instance" "test" { |
| 9 | + name = "tf_sqlserver_basic_instance" |
| 10 | + availability_zone = var.availability_zone |
| 11 | + charge_type = "POSTPAID_BY_HOUR" |
| 12 | + vpc_id = "vpc-26w7r56z" |
| 13 | + subnet_id = "subnet-lvlr6eeu" |
| 14 | + machine_type = "CLOUD_PREMIUM" |
| 15 | + project_id = 0 |
| 16 | + memory = 2 |
| 17 | + storage = 10 |
| 18 | + cpu = 1 |
| 19 | + security_groups = ["sg-nltpbqg1"] |
| 20 | +
|
| 21 | + tags = { |
| 22 | + "test" = "test" |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | +*/ |
| 27 | +package tencentcloud |
| 28 | + |
| 29 | +import ( |
| 30 | + "context" |
| 31 | + "log" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 34 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 35 | +) |
| 36 | + |
| 37 | +func dataSourceTencentCloudSqlserverBasicInstances() *schema.Resource { |
| 38 | + return &schema.Resource{ |
| 39 | + Read: dataSourceTencentCloudSqlserverBasicInstanceRead, |
| 40 | + Schema: map[string]*schema.Schema{ |
| 41 | + "id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + Description: "ID of the SQL Server basic instance to be query.", |
| 45 | + }, |
| 46 | + "project_id": { |
| 47 | + Type: schema.TypeInt, |
| 48 | + Optional: true, |
| 49 | + Description: "Project ID of the SQL Server basic instance to be query.", |
| 50 | + }, |
| 51 | + "vpc_id": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Description: "Vpc ID of the SQL Server basic instance to be query.", |
| 55 | + }, |
| 56 | + "subnet_id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + Description: "Subnet ID of the SQL Server basic instance to be query.", |
| 60 | + }, |
| 61 | + "result_output_file": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + Description: "Used to save results.", |
| 65 | + }, |
| 66 | + "instance_list": { |
| 67 | + Type: schema.TypeList, |
| 68 | + Computed: true, |
| 69 | + Description: "A list of SQL Server basic instances. Each element contains the following attributes.", |
| 70 | + Elem: &schema.Resource{ |
| 71 | + Schema: map[string]*schema.Schema{ |
| 72 | + "id": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "ID of the SQL Server basic instance.", |
| 76 | + }, |
| 77 | + "name": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Name of the SQL Server basic instance.", |
| 81 | + }, |
| 82 | + "charge_type": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Pay type of the SQL Server basic instance. For now, only `POSTPAID_BY_HOUR` is valid.", |
| 86 | + }, |
| 87 | + "engine_version": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Version of the SQL Server basic database engine. Allowed values are `2008R2`(SQL Server 2008 Enerprise), `2012SP3`(SQL Server 2012 Enterprise), `2016SP1` (SQL Server 2016 Enterprise), `201602`(SQL Server 2016 Standard) and `2017`(SQL Server 2017 Enterprise). Default is `2008R2`.", |
| 91 | + }, |
| 92 | + "vpc_id": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "ID of VPC.", |
| 96 | + }, |
| 97 | + "subnet_id": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "ID of subnet.", |
| 101 | + }, |
| 102 | + "storage": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + Description: "Disk size (in GB). Allowed value must be a multiple of 10. The storage must be set with the limit of `storage_min` and `storage_max` which data source `tencentcloud_sqlserver_specinfos` provides.", |
| 106 | + }, |
| 107 | + "memory": { |
| 108 | + Type: schema.TypeInt, |
| 109 | + Computed: true, |
| 110 | + Description: "Memory size (in GB). Allowed value must be larger than `memory` that data source `tencentcloud_sqlserver_specinfos` provides.", |
| 111 | + }, |
| 112 | + "cpu": { |
| 113 | + Type: schema.TypeInt, |
| 114 | + Computed: true, |
| 115 | + Description: "The CPU number of the SQL Server basic instance.", |
| 116 | + }, |
| 117 | + "project_id": { |
| 118 | + Type: schema.TypeInt, |
| 119 | + Computed: true, |
| 120 | + Description: "Project ID, default value is 0.", |
| 121 | + }, |
| 122 | + "availability_zone": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + Description: "Availability zone.", |
| 126 | + }, |
| 127 | + "used_storage": { |
| 128 | + Type: schema.TypeInt, |
| 129 | + Computed: true, |
| 130 | + Description: "Used storage.", |
| 131 | + }, |
| 132 | + "vip": { |
| 133 | + Type: schema.TypeString, |
| 134 | + Computed: true, |
| 135 | + Description: "IP for private access.", |
| 136 | + }, |
| 137 | + "vport": { |
| 138 | + Type: schema.TypeInt, |
| 139 | + Computed: true, |
| 140 | + Description: "Port for private access.", |
| 141 | + }, |
| 142 | + "create_time": { |
| 143 | + Type: schema.TypeString, |
| 144 | + Computed: true, |
| 145 | + Description: "Create time of the SQL Server basic instance.", |
| 146 | + }, |
| 147 | + "status": { |
| 148 | + Type: schema.TypeInt, |
| 149 | + Computed: true, |
| 150 | + Description: "Status of the SQL Server basic instance. 1 for applying, 2 for running, 3 for running with limit, 4 for isolated, 5 for recycling, 6 for recycled, 7 for running with task, 8 for off-line, 9 for expanding, 10 for migrating, 11 for readonly, 12 for rebooting.", |
| 151 | + }, |
| 152 | + "tags": { |
| 153 | + Type: schema.TypeMap, |
| 154 | + Computed: true, |
| 155 | + Description: "Tags of the SQL Server basic instance.", |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func dataSourceTencentCloudSqlserverBasicInstanceRead(d *schema.ResourceData, meta interface{}) error { |
| 165 | + defer logElapsed("data_source.tencentcloud_sqlserver_basic_instances.read")() |
| 166 | + |
| 167 | + var ( |
| 168 | + logId = getLogId(contextNil) |
| 169 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 170 | + tcClient = meta.(*TencentCloudClient).apiV3Conn |
| 171 | + tagService = &TagService{client: tcClient} |
| 172 | + service = SqlserverService{client: tcClient} |
| 173 | + id = d.Get("id").(string) |
| 174 | + projectId = -1 |
| 175 | + vpcId string |
| 176 | + subnetId string |
| 177 | + ) |
| 178 | + if v, ok := d.GetOk("project_id"); ok { |
| 179 | + projectId = v.(int) |
| 180 | + } |
| 181 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 182 | + vpcId = v.(string) |
| 183 | + } |
| 184 | + if v, ok := d.GetOk("subnet_id"); ok { |
| 185 | + subnetId = v.(string) |
| 186 | + } |
| 187 | + instanceList, err := service.DescribeSqlserverInstances(ctx, id, projectId, vpcId, subnetId, 1) |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + |
| 192 | + ids := make([]string, 0, len(instanceList)) |
| 193 | + list := make([]map[string]interface{}, 0, len(instanceList)) |
| 194 | + for _, v := range instanceList { |
| 195 | + listItem := make(map[string]interface{}) |
| 196 | + listItem["id"] = v.InstanceId |
| 197 | + listItem["name"] = v.Name |
| 198 | + listItem["project_id"] = v.ProjectId |
| 199 | + listItem["storage"] = v.Storage |
| 200 | + listItem["memory"] = v.Memory |
| 201 | + listItem["availability_zone"] = v.Zone |
| 202 | + listItem["create_time"] = v.CreateTime |
| 203 | + listItem["vpc_id"] = v.UniqVpcId |
| 204 | + listItem["subnet_id"] = v.UniqSubnetId |
| 205 | + listItem["engine_version"] = v.Version |
| 206 | + listItem["vip"] = v.Vip |
| 207 | + listItem["vport"] = v.Vport |
| 208 | + listItem["used_storage"] = v.UsedStorage |
| 209 | + listItem["status"] = v.Status |
| 210 | + listItem["cpu"] = v.Cpu |
| 211 | + |
| 212 | + if *v.PayMode == 1 { |
| 213 | + listItem["charge_type"] = COMMON_PAYTYPE_PREPAID |
| 214 | + } else { |
| 215 | + listItem["charge_type"] = COMMON_PAYTYPE_POSTPAID |
| 216 | + } |
| 217 | + tagList, err := tagService.DescribeResourceTags(ctx, "sqlserver", "instance", tcClient.Region, *v.InstanceId) |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + |
| 222 | + listItem["tags"] = tagList |
| 223 | + list = append(list, listItem) |
| 224 | + ids = append(ids, *v.InstanceId) |
| 225 | + } |
| 226 | + |
| 227 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 228 | + if e := d.Set("instance_list", list); e != nil { |
| 229 | + log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error()) |
| 230 | + return e |
| 231 | + } |
| 232 | + output, ok := d.GetOk("result_output_file") |
| 233 | + if ok && output.(string) != "" { |
| 234 | + return writeToFile(output.(string), list) |
| 235 | + } |
| 236 | + |
| 237 | + return nil |
| 238 | + |
| 239 | +} |
0 commit comments