-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_mongodb_zone_config.go
155 lines (143 loc) · 4.33 KB
/
data_source_tc_mongodb_zone_config.go
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
package mongodb
import (
"context"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceTencentCloudMongodbZoneConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudMongodbZoneConfigRead,
Schema: map[string]*schema.Schema{
"available_zone": {
Type: schema.TypeString,
Optional: true,
Description: "The available zone of the Mongodb.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to store results.",
},
"list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of zone config. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"available_zone": {
Type: schema.TypeString,
Computed: true,
Description: "The available zone of the Mongodb.",
},
"cluster_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of Mongodb cluster.",
},
"machine_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of Mongodb instance.",
},
"cpu": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of cpu's core.",
},
"memory": {
Type: schema.TypeInt,
Computed: true,
Description: "Memory size.",
},
"default_storage": {
Type: schema.TypeInt,
Computed: true,
Description: "Default disk size.",
},
"min_storage": {
Type: schema.TypeInt,
Computed: true,
Description: "Minimum sie of the disk.",
},
"max_storage": {
Type: schema.TypeInt,
Computed: true,
Description: "Maximum size of the disk.",
},
"engine_version": {
Type: schema.TypeString,
Computed: true,
Description: "Version of the Mongodb version.",
},
"min_replicate_set_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Minimum replicate set num for sharding cluster.",
},
"max_replicate_set_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Maximum replicate set num for sharding cluster.",
},
},
},
},
},
}
}
func dataSourceTencentCloudMongodbZoneConfigRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_mongodb_zone_config.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
zone := ""
if v, ok := d.GetOk("available_zone"); ok {
zone = v.(string)
}
mongodbService := MongodbService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
infos, err := mongodbService.DescribeSpecInfo(ctx, zone)
if err != nil {
return err
}
configList := make([]map[string]interface{}, 0)
for _, info := range infos {
for _, item := range info.SpecItems {
clusterType := MONGODB_CLUSTER_TYPE_REPLSET
if *item.ClusterType == 1 {
clusterType = MONGODB_CLUSTER_TYPE_SHARD
}
mapping := map[string]interface{}{
"available_zone": info.Zone,
"cluster_type": clusterType,
"machine_type": item.MachineType,
"cpu": item.Cpu,
"memory": item.Memory,
"default_storage": item.DefaultStorage,
"min_storage": item.MinStorage,
"max_storage": item.MaxStorage,
"engine_version": item.Version,
"min_replicate_set_num": item.MinReplicateSetNum,
"max_replicate_set_num": item.MaxReplicateSetNum,
}
configList = append(configList, mapping)
}
}
id := zone
if id == "" {
id = meta.(tccommon.ProviderMeta).GetAPIV3Conn().Region
}
d.SetId(id)
if err = d.Set("list", configList); err != nil {
log.Printf("[CRITAL]%s provider set mongodb zone config list fail, reason:%s\n ", logId, err.Error())
return err
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if err := tccommon.WriteToFile(output.(string), configList); err != nil {
return err
}
}
return nil
}