This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
forked from baidubce/terraform-provider-baiducloud
/
data_source_baiducloud_ccev2_clusterip_cidr.go
203 lines (187 loc) · 5.92 KB
/
data_source_baiducloud_ccev2_clusterip_cidr.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
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
/*
Use this data source to recommend ccev2 cluster IP CIDR.
Example Usage
```hcl
data "baiducloud_ccev2_clusterip_cidr" "default" {
vpc_cidr = var.vpc_cidr
container_cidr = var.container_cidr
cluster_max_service_num = 32
private_net_cidrs = ["172.16.0.0/12",]
ip_version = "ipv4"
output_file = "${path.cwd}/recommendClusterIPCidr.txt"
}
```
*/
package baiducloud
import (
"encoding/json"
"fmt"
ccev2 "github.com/baidubce/bce-sdk-go/services/cce/v2"
"github.com/baidubce/bce-sdk-go/services/cce/v2/types"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)
func dataSourceBaiduCloudCCEv2ClusterIPCidrs() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudCCEv2ClusterIPCidrsRead,
Schema: map[string]*schema.Schema{
//以下是资源的请求参数
"vpc_cidr": {
Type: schema.TypeString,
Description: "VPC CIDR",
Optional: true,
},
"vpc_cidr_ipv6": {
Type: schema.TypeString,
Description: "VPC CIDR IPv6",
Optional: true,
},
"container_cidr": {
Type: schema.TypeString,
Description: "Container CIDR",
Optional: true,
},
"container_cidr_ipv6": {
Type: schema.TypeString,
Description: "Container CIDR IPv6",
Optional: true,
},
"cluster_max_service_num": {
Type: schema.TypeInt,
Description: "Max service number in the cluster",
Optional: true,
},
"private_net_cidrs": {
Type: schema.TypeList,
Description: "Private Net CIDRs",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"private_net_cidrs_ipv6": {
Type: schema.TypeList,
Description: "Private Net CIDRs IPv6",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ip_version": {
Type: schema.TypeString,
Description: "IP Version",
Optional: true,
},
//输出结果写到哪
"output_file": {
Type: schema.TypeString,
Description: "Result output file",
Optional: true,
},
//以下是资源的返回结果
"is_success": {
Type: schema.TypeBool,
Description: "Is the recommendation request success",
Computed: true,
},
"err_msg": {
Type: schema.TypeString,
Description: "Error message if an error occurs",
Computed: true,
},
"request_id": {
Type: schema.TypeString,
Description: "Request ID",
Computed: true,
},
"recommended_clusterip_cidrs": {
Type: schema.TypeList,
Description: "Recommend Cluster IP CIDR List",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"recommended_clusterip_cidrs_ipv6": {
Type: schema.TypeList,
Description: "Recommend Cluster IP CIDR List IPv6",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceBaiduCloudCCEv2ClusterIPCidrsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
args := &ccev2.RecommendClusterIPCIDRArgs{}
//构建请求参数
if v := d.Get("vpc_cidr"); v.(string) != "" {
args.VPCCIDR = v.(string)
}
if v := d.Get("vpc_cidr_ipv6"); v.(string) != "" {
args.VPCCIDRIPv6 = v.(string)
}
if v := d.Get("container_cidr"); v.(string) != "" {
args.ContainerCIDR = v.(string)
}
if v := d.Get("container_cidr_ipv6"); v.(string) != "" {
args.ContainerCIDRIPv6 = v.(string)
}
if v := d.Get("cluster_max_service_num"); v != nil {
args.ClusterMaxServiceNum = v.(int)
}
if v := d.Get("private_net_cidrs"); v != nil {
cidrs := make([]ccev2.PrivateNetString, 0)
for _, cidrRaw := range v.([]interface{}) {
cidrs = append(cidrs, ccev2.PrivateNetString(cidrRaw.(string)))
}
args.PrivateNetCIDRs = cidrs
}
if v := d.Get("private_net_cidrs_ipv6"); v != nil {
cidrsipv6 := make([]ccev2.PrivateNetString, 0)
for _, cidrRaw := range v.([]interface{}) {
cidrsipv6 = append(cidrsipv6, ccev2.PrivateNetString(cidrRaw.(string)))
}
args.PrivateNetCIDRIPv6s = cidrsipv6
}
if v := d.Get("ip_version"); v != nil {
args.IPVersion = types.ContainerNetworkIPType(v.(string))
}
action := "Recommend CCEv2 Cluster IP CIDR in vpc"
raw, err := client.WithCCEv2Client(func(client *ccev2.Client) (i interface{}, e error) {
return client.RecommendClusterIPCIDR(args)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_clusterip_cidr", action, BCESDKGoERROR)
}
addDebug(action, raw)
response := raw.(*ccev2.RecommendClusterIPCIDRResponse)
//设置返回结果
d.SetId(resource.UniqueId())
if err := d.Set("is_success", response.IsSuccess); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_container_cidr", action, BCESDKGoERROR)
}
if err := d.Set("err_msg", response.ErrMsg); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_container_cidr", action, BCESDKGoERROR)
}
if err := d.Set("request_id", response.RequestID); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_container_cidr", action, BCESDKGoERROR)
}
if err := d.Set("recommended_clusterip_cidrs", response.RecommendedClusterIPCIDRs); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_container_cidr", action, BCESDKGoERROR)
}
if err := d.Set("recommended_clusterip_cidrs_ipv6", response.RecommendedClusterIPCIDRIPv6s); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_container_cidr", action, BCESDKGoERROR)
}
if v, ok := d.GetOk("output_file"); ok && v.(string) != "" {
s, _ := json.MarshalIndent(response, "", "\t")
str := fmt.Sprintf("%s", s)
if err := writeStringToFile(v.(string), str); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_ccev2_container_cidr", action, BCESDKGoERROR)
}
}
return nil
}