Skip to content

Commit 383c23a

Browse files
author
hhermanwang
committed
add sqlserver basic instance resource and data_source file
1 parent 1748115 commit 383c23a

15 files changed

+1583
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.48.0 (Unreleased)
2+
3+
FEATURES:
4+
5+
* **New Resource**: `tencentcloud_sqlserver_basic_instance`
6+
* **New Data Source**: `tencentcloud_sqlserver_basic_instances`
7+
18
## 1.47.0 (Unreleased)
29

310
ENHANCEMENTS:

examples/tencentcloud-sqlserver/main.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
data "tencentcloud_sqlserver_zone_config" "foo" {
22
}
33

4+
resource "tencentcloud_vpc" "sqlserver_vpc" {
5+
name = "tf-sqlserver-vpc"
6+
cidr_block = "10.0.0.0/16"
7+
}
8+
9+
resource "tencentcloud_subnet" "sqlserver_subnet" {
10+
availability_zone = "ap-guangzhou-3"
11+
name = "tf-sqlserver-subnet"
12+
vpc_id = tencentcloud_vpc.sqlserver_vpc.id
13+
cidr_block = "10.0.0.0/16"
14+
is_multicast = false
15+
}
16+
417
resource "tencentcloud_vpc" "foo" {
518
name = "example"
619
cidr_block = "10.0.0.0/16"
@@ -29,6 +42,28 @@ resource "tencentcloud_sqlserver_instance" "example" {
2942
}
3043
}
3144

45+
resource "tencentcloud_sqlserver_basic_instance" "test" {
46+
name = "tf_sqlserver_basic_instance"
47+
availability_zone = "ap-guangzhou-3"
48+
charge_type = "POSTPAID_BY_HOUR"
49+
vpc_id = tencentcloud_vpc.sqlserver_vpc.id
50+
subnet_id = tencentcloud_subnet.sqlserver_subnet.id
51+
machine_type ="CLOUD_PREMIUM"
52+
project_id = 0
53+
memory = 2
54+
storage = 20
55+
cpu = 1
56+
security_groups = ["sg-nltpbqg1"]
57+
goods_num = 1
58+
maintenance_week_set = [1,2,3]
59+
maintenance_start_time = "09:00"
60+
maintenance_time_span = 3
61+
62+
tags = {
63+
"test" = "test"
64+
}
65+
}
66+
3267
resource "tencentcloud_sqlserver_db" "example" {
3368
instance_id = tencentcloud_sqlserver_instance.example.id
3469
name = "example"
@@ -118,3 +153,6 @@ data "tencentcloud_sqlserver_publish_subscribes" "publish_subscribes" {
118153
publish_subscribe_name = tencentcloud_sqlserver_publish_subscribe.example.publish_subscribe_name
119154
}
120155

156+
data "tencentcloud_sqlserver_basic_instances" "id_test"{
157+
id = tencentcloud_sqlserver_basic_instance.test.id
158+
}

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,6 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
441441
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
442442
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2 h1:Xr9gkxfOP0KQWXKNqmwe8vEeSUiUj4Rlee9CMVX2ZUQ=
443443
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
444-
github.com/tencentcloud/tencentcloud-sdk-go v1.0.37 h1:zNJfC6+5sj/OloSq6GzmdSC9lwlZbgQ438zz3qH+v4k=
445-
github.com/tencentcloud/tencentcloud-sdk-go v1.0.37/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
446444
github.com/tencentcloud/tencentcloud-sdk-go v1.0.50 h1:KAIRXHVSVMvRX3Dl5gvrcVqCxBv7fDLvHahU0F3GNsc=
447445
github.com/tencentcloud/tencentcloud-sdk-go v1.0.50/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
448446
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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

Comments
 (0)