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_security_group_rules.go
157 lines (141 loc) · 4.45 KB
/
data_source_baiducloud_security_group_rules.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
/*
Use this data source to query Security Group list.
Example Usage
```hcl
data "baiducloud_security_group_rules" "default" {}
output "security_group_rules" {
value = "${data.baiducloud_security_group_rules.default.rules}"
}
```
*/
package baiducloud
import (
"github.com/baidubce/bce-sdk-go/services/bcc/api"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)
func dataSourceBaiduCloudSecurityGroupRules() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudSecurityGroupRulesRead,
Schema: map[string]*schema.Schema{
"security_group_id": {
Type: schema.TypeString,
Description: "Security Group ID",
Required: true,
},
"instance_id": {
Type: schema.TypeString,
Description: "Security Group attached instance ID",
Optional: true,
},
"vpc_id": {
Type: schema.TypeString,
Description: "Security Group attached vpc id",
Optional: true,
},
"output_file": {
Type: schema.TypeString,
Description: "Security Group search result output file",
Optional: true,
ForceNew: true,
},
"filter": dataSourceFiltersSchema(),
"rules": {
Type: schema.TypeList,
Description: "Security Group rules",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"remark": {
Type: schema.TypeString,
Description: "SecurityGroup rule's remark",
Computed: true,
},
"direction": {
Type: schema.TypeString,
Description: "SecurityGroup rule's direction",
Computed: true,
},
"ether_type": {
Type: schema.TypeString,
Description: "SecurityGroup rule's ether type",
Computed: true,
},
"port_range": {
Type: schema.TypeString,
Description: "SecurityGroup rule's port range",
Computed: true,
},
"protocol": {
Type: schema.TypeString,
Description: "SecurityGroup rule's protocol",
Computed: true,
},
"source_group_id": {
Type: schema.TypeString,
Description: "SecurityGroup rule's source group id",
Computed: true,
},
"source_ip": {
Type: schema.TypeString,
Description: "SecurityGroup rule's source ip",
Computed: true,
},
"dest_group_id": {
Type: schema.TypeString,
Description: "SecurityGroup rule's destination group id",
Computed: true,
},
"dest_ip": {
Type: schema.TypeString,
Description: "SecurityGroup rule's destination ip",
Computed: true,
},
"security_group_id": {
Type: schema.TypeString,
Description: "SecurityGroup rule's security group id",
Computed: true,
},
},
},
},
},
}
}
func dataSourceBaiduCloudSecurityGroupRulesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
bccService := BccService{client}
listSgArgs := &api.ListSecurityGroupArgs{}
if v, ok := d.GetOk("instance_id"); ok && v.(string) != "" {
listSgArgs.InstanceId = v.(string)
}
if v, ok := d.GetOk("vpc_id"); ok && v.(string) != "" {
listSgArgs.VpcId = v.(string)
}
sgId := d.Get("security_group_id").(string)
action := "Data Source Query All Security Group " + sgId + " rules"
sgList, err := bccService.ListAllSecurityGroups(listSgArgs)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_security_group_rules", action, BCESDKGoERROR)
}
var ruleMap []map[string]interface{}
for _, sg := range sgList {
if sg.Id == sgId {
ruleMap = bccService.FlattenSecurityGroupRuleModelsToMap(sg.Rules)
FilterDataSourceResult(d, &ruleMap)
if err := d.Set("rules", ruleMap); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_security_group_rules", action, BCESDKGoERROR)
}
d.SetId(resource.UniqueId())
if v, ok := d.GetOk("output_file"); ok && v.(string) != "" {
if err := writeToFile(v.(string), ruleMap); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_security_group_rules", action, BCESDKGoERROR)
}
}
return nil
}
}
// no such security group
return WrapErrorf(Error("No such Security Group"), DefaultErrorMsg, "baiducloud_security_group_rules", action, BCESDKGoERROR)
}