forked from hashicorp/terraform-provider-jdcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
executable file
·389 lines (352 loc) · 14.9 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Before everything starts you have to provide [access key and secret key]
# What are these keys -> https://docs.jdcloud.com/cn/account-management/accesskey-management
provider "jdcloud" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
# -------------------------------------------------------------DISK
################################################
# 1. Create a disk
################################################
# You can choose to attach a disk to an instance after creating it.
# Alternatively, you can create a disk while creating an instance
# -> See it instance part
# [WARN] If (charge_mode == prepaid_by_duration)
# You can not delete it before they expired. "postpaid_by_usage" is recommended
resource "jdcloud_disk" "disk_test_1" {
az = "cn-north-1a"
name = "test_disk"
disk_type = "premium-hdd"
disk_size_gb = 60
charge_mode = "postpaid_by_usage"
}
################################################
# 2. Attach a disk to an instance
################################################
# There are two ways to specify an resource, you can either
# specify its ID number, like following:
resource "jdcloud_disk_attachment" "disk-attachment-TEST-1"{
instance_id = "i-exampleid"
disk_id = "vol-exampleid"
auto_delete = false
}
# Or you can reference it with its resource name, like following:
resource "jdcloud_disk_attachment" "disk-attachment-TEST-2"{
instance_id = "${jdcloud_instance.vm-1.id}"
disk_id = "${jdcloud_disk.disk_test_1.id}"
auto_delete = false
}
# You'll probably interested in REQUIRED and OPTIONAL parameters
# See in -> github.com/jdclouddevelopers/terraform-provider-jdcloud/blob/master/website/docs
# ------------------------------------------------------------- VPC
resource "jdcloud_vpc" "jd-vpc-1" {
vpc_name = "my-vpc-1"
cidr_block = "172.16.0.0/20"
description = "vpc1"
}
# ------------------------------------------------------------- SUBNET
# CIDR-Block of subnet must be a subset of VPC it belongs to
# e.g : [jd-subnet-1] < [jd-vpc-1] ,thus we have :
# [172.16.0.0/26] < [172.16.0.0/20]
# Besides,there is no overlap among subnet CIDR under same VPC
resource "jdcloud_subnet" "jd-subnet-1" {
vpc_id = "${jdcloud_vpc.jd-vpc-1.id}"
cidr_block = "172.16.0.0/26"
subnet_name = "subnet_example"
description = "testing"
}
# ---------------------------------------------------------- ROUTE-TABLE
################################################
# 1. Create a Route Table
################################################
resource "jdcloud_route_table" "jd-route-table-1" {
vpc_id = "${jdcloud_vpc.jd-vpc-1.id}"
route_table_name = "example_route_table"
description = "DevOps2018"
}
################################################
# 2. Associate a Route Table to a Subnet
################################################
resource "jdcloud_route_table_association" "rt-association-1" {
subnet_id = "${jdcloud_subnet.jd-subnet-1.id}"
route_table_id = "${jdcloud_route_table.jd-route-table-1.id}"
}
################################################
# 3. Add rules to this Route Table
################################################
# Candidates for "next_hop_type" : instance/internet/vpc_peering/bgw
# "address_prefix" : if (next_hop_type == "internet") then there's
# no overlap between address prefixes. Default priority for a rule is 100
resource "jdcloud_route_table_rules" "rule-example"{
route_table_id = "rtb-example"
rule_specs = [{
next_hop_type = "internet"
next_hop_id = "internet"
address_prefix= "10.0.0.0/16"
priority = 100
}]
}
# ---------------------------------------------------------- SECURITY-GROUP
################################################
# 1. Create a Security Group
################################################
resource "jdcloud_network_security_group" "sg-1" {
network_security_group_name = "sg-1"
vpc_id = "${jdcloud_vpc.jd-vpc-1.id}"
}
################################################
# 2. Create Security Group Rules
################################################
# Unlike route table rules, SG rules are defined under same resource.
# Detailed candidates, together with their explaination, Can be found here:
# github.com/jdclouddevelopers/terraform-provider-jdcloud/blob/master/website/docs/jdcloud_network_security_group_rules.html.markdown
resource "jdcloud_network_security_group_rules" "sg-r-1" {
security_group_id = "${jdcloud_network_security_group.sg-1.id}"
security_group_rules = [
{
address_prefix = "0.0.0.0/0"
direction = "0"
from_port = "8000"
protocol = "300"
to_port = "8900"
},
{
address_prefix = "0.0.0.0/0"
direction = "1"
from_port = "8000"
protocol = "300"
to_port = "8900"
},
]
}
# ---------------------------------------------------------- NETWORK-INTERFACE
################################################
# 1. Create a Network Interface
################################################
# It can be confusing on parameter [secondary_ip_addresses] & [secondary_ip_count]
# The first one represents you would like to specify some address for this network
# interface, while the second one represents you want more addresses, but not caring what they actually are
# For example, for a following config, you'll get in total 3 addresses on this NI
# The first one is 10.0.3.0, and the remaining two may be 10.0.4.0 and 10.0.3.1
resource "jdcloud_network_interface" "ineterface-TEST-1" {
subnet_id = "subnet-example"
description = "test"
az = "cn-north-1"
network_interface_name = "TerraformTest"
secondary_ip_addresses = ["10.0.3.0",]
secondary_ip_count = "2"
security_groups = ["sg-example"]
}
################################################
# 2. Associate a Network Interface to Instance
################################################
# You can create a NI and attach it to an Instance, Alternatively
# You can still create a NI while creating an Instance -> See instance part
resource "jdcloud_network_interface_attachment" "attachment-TEST-1"{
instance_id = "i-example"
network_interface_id = "port-example"
auto_delete = "true"
}
# ---------------------------------------------------------- ELASTIC-IP
################################################
# 1. Create a Elastic-IP
################################################
# "eip_provider" = bgp/no_bgp, selected according to your region
# cn-north-1:bgp;cn-south-1:[bgp,no_bgp];cn-east-1:[bgp,no_bgp];cn-east-2:bgp
resource "jdcloud_eip" "eip-TEST-1"{
eip_provider = "bgp"
bandwidth_mbps = 1
}
################################################
# 2. Associate an EIP with an Instance
################################################
# Similarly, you can create -> Associate
# Or creating IP while creating instance
resource "jdcloud_eip_association" "eip-association-TEST-1"{
instance_id = "i-p3yh27xd3s"
elastic_ip_id = "fip-e3lfigpewx"
}
# ---------------------------------------------------------- OSS-Bucket
################################################
# 1. Create a OSS-Bucket with certain ACL
################################################
# ACL means privilege control to this bucket
# PRIVATE : only you can write
# public-read: Owner has full control, other people can read from this but no writing is allowed
# public-read-write: Everyone can read/write from this bucket
resource "jdcloud_oss_bucket" "jd-bucket-2" {
bucket_name = "example"
acl = "private"
}
################################################
# 2. Upload file to this bucket
################################################
# When you would like to upload some files to
# to a bucket, buckets are specified by it name
resource "jdcloud_oss_bucket_upload" "devops" {
bucket_name = "example"
file_name = "/home/DevOps/hello.txt"
}
# ---------------------------------------------------------- Instance
# As we can see that although it seems to be pretty long with loads of
# parameter to specify. Most of them are component attaching,NI,EIP,Disk,etc..
# Values in these fields has no difference with above
# [WARN]:
resource "jdcloud_instance" "vm-1" {
################################################
# 1. VM Config
################################################
# Password is a optional field. By missing this
# Field,passwords will be sent by email and SMS
az = "cn-north-1a"
instance_name = "my-vm-1"
instance_type = "c.n1.large"
image_id = "example_image_id"
password = "${var.vm_password}"
key_names = "${jdcloud_key_pairs.key-1.key_name}"
description = "Managed by terraform"
################################################
# 2. Create a Network Interface with it
################################################
subnet_id = "${jdcloud_subnet.jd-subnet-1.id}"
network_interface_name = "veth1"
sanity_check = 1
primary_ip = "172.16.0.13"
secondary_ips = [
"172.16.0.14",
"172.16.0.15"]
security_group_ids = ["${jdcloud_network_security_group.sg-1.id}"]
################################################
# 3. Create an EIP with it
################################################
elastic_ip_bandwidth_mbps = 10
elastic_ip_provider = "bgp"
################################################
# 4. System-Disk(Required)
################################################
# We would recommend local disk as system disk :
# System-Disk ─┬── "disk_category" = "local" ──> Always work ,disk size fixed to 40Gb
# └ "disk_category" = "cloud" ──> Works only when az == cn-east
system_disk = {
disk_category = "local"
auto_delete = true
device_name = "vda"
}
################################################
# 5. Data-Disk(Optional)
################################################
# You can attach multiple data-disk with this instance
# Device name for disk must be unique
data_disk = [
{
disk_category = "local"
auto_delete = true
device_name = "vdb"
},
{
disk_category = "cloud"
auto_delete = true
device_name = "vdc"
az = "cn-north-1a"
disk_name = "vm1-datadisk-1"
description = "test"
disk_type = "ssd"
disk_size_gb = 50
}]
}
# ---------------------------------------------------------- RDS
################################################
# 1. Create an Instance
################################################
# [WARN] If (charge_mode == prepaid_by_duration)
# You can not delete it before they expired. "postpaid_by_duration" is recommended
resource "jdcloud_rds_instance" "rds-test"{
instance_name = "test"
engine = "MySQL"
engine_version = "5.7"
instance_class = "db.mysql.s1.micro"
instance_storage_gb = "20"
az = "cn-north-1a"
vpc_id = "vpc-example"
subnet_id = "subnet-example"
charge_mode = "postpaid_by_duration"
}
################################################
# 1. Create accounts on this instance
################################################
resource "jdcloud_rds_account" "rds-test1"{
instance_id = "mysql-example"
username = "DevOps"
password = "JDCloud123"
}
################################################
# 2. Create databases on this accounts
################################################
# [WARN] Currently any modification on Database resource
# is banned. Trying to modify will result in returning errors
resource "jdcloud_rds_database" "db-TEST"{
instance_id = "mysql-g0afoqpl6y"
db_name = "cloudb1"
character_set = "utf8"
}
resource "jdcloud_rds_database" "db-TEST-2"{
instance_id = "mysql-g0afoqpl6y"
db_name = "cloudb2"
character_set = "utf8"
}
################################################
# 3. Grant privilege for user accounts
################################################
resource "jdcloud_rds_privilege" "pri-test" {
instance_id = "mysql-g0afoqpl6y"
username = "DevOps"
account_privilege = [
{db_name = "cloudb1",privilege = "ro"},
{db_name = "cloudb2",privilege = "rw"},
]
}
# ---------------------------------------------------------- INSTANCE-TEMPLATE
####################################################################################################
# Full parameters ->
#
# + instance_type : g.n2.medium/g.n2.large...Just different type of instance , by default its g.n2.medium
# + password : Optional, if you leave it blank. password will be sent to you by email and SMS.
# + image_id : If you would like to start your instance from an image , fill in here, by default its Ubuntu:16.04
# + bandwidth: Optional. if you leave it blank, no public IP will be assigned to this instance
# + ip_service_provider : [BGP/nonBGP] , by default it's set to BGP
# + charge_mode : [bandwith/flow] , by default it's set to bandwith
# + subnet_id : Each instance is supposed to exists under a subnet, fill its id here
# + security_group_ids : You can make your instance exists under multuple SGs, fill them here as an array
# + local_disk/data_disk
# - disk_category : [local/cloud] : For system disk, local disk is recommended
# - disk_type : [premium-hdd/ssd] : For system disk, we would recommend premium-hdd
# For data disk , ssd, since premium-hdd ususally out of stock
# - auto_delete: [true/false] : Disks will be deleted automatically once it's been detached from instance
# - disk_size : [20~1000] - Must be multiples of 10, that's 20,30,40... by default it is set to 40GB
# - device_name : [vda/vdb/vdc....] - Attach point of disk, points must be unique among all disks
# we would recommend to leave it blank if you're not farmiliar with it
# - snapshot_id : If you would like to build a template from snapshot, fill in its id here
####################################################################################################
# ---------------------------------------------------------- AVAILABILITY-GROUP
# Parameters and candidates
# 1. [AZ] is an array which, candidates lists as follows ["cn-north-1a","cn-north-1b","cn-east-1b","cn-east-1a","cn-south-1a"]
# 2. [AG_TYPE] is a string, candidates are : "kvm", "docker"
# [Ag_name] and [Description] are updatable while the remaining not.
#
resource "jdcloud_availability_group" "ag_01" {
availability_group_name = "example_ag_name"
az = ["cn-north-1a","cn-north-1b"]
instance_template_id = "example_template_id"
description = "This is an example description"
ag_type = "docker"
}
# ---------------------------------------------------------- KEY-PAIRS
resource "jdcloud_key_pairs" "key-1" {
key_name = "JDCLODU-123312FMK"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAm3c0aR7zI0Xkrm1MD3zDrazC+fR+DV6p/xdzQJWviqPSFGfsatptY3Bc6gYF/qY+Jjccmrm6SKrtW0xPicCw5/uGAuIyhzBnG1Ix0fITdJkeBzyBpxdu/oxnJuvu5P8BLfoFH80ovUqysnttC/7hHBp+uIctkt/g14Kqd7kuPc0Gp4cx7tntNWpmzHJI9i+ayF95nJyFGIjF/s57b9pcKnnv2LXkMDNxsnzgWwPpi2hqGpQSz//ji8GgSED08u34zSjVbPc0TYJy4GO+uD8hozGnf9Evlpqx4OSB0D+4AuRcIniPgCOotYpOdp3Lj7CQRwzkiFZ6YpOxj1qMD4fnjQ== rsa-key-jddevelop"
}
resource "jdcloud_key_pairs" "keypairs_2" {
key_name = "JDCLODU-123312FMF"
key_file = "private.ppk"
}