|
| 1 | +/* |
| 2 | +Use this data source to query VPC Network ACL information. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_vpc_instances" "foo" { |
| 8 | +} |
| 9 | +
|
| 10 | +resource "tencentcloud_vpc_acl" "main" { |
| 11 | + vpc_id = data.tencentcloud_vpc_instances.foo.instance_list.0.vpc_id |
| 12 | +} |
| 13 | +
|
| 14 | +resource "tencentcloud_vpc_acl" "main" { |
| 15 | + name = "test_acl" |
| 16 | +} |
| 17 | +
|
| 18 | +``` |
| 19 | +*/ |
| 20 | +package tencentcloud |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "log" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 27 | + "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 28 | +) |
| 29 | + |
| 30 | +func dataSourceTencentCloudVpcAcls() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Read: dataSourceTencentCloudVpcACLRead, |
| 33 | + |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "vpc_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + ValidateFunc: validateNotEmpty, |
| 39 | + Description: "ID of the VPC instance.", |
| 40 | + }, |
| 41 | + "name": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + ValidateFunc: validateStringLengthInRange(0, 60), |
| 45 | + Description: "Name of the network ACL.", |
| 46 | + }, |
| 47 | + "id": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + ValidateFunc: validateNotEmpty, |
| 51 | + Description: "`ID` of the network ACL instance.", |
| 52 | + }, |
| 53 | + "result_output_file": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + Description: "Used to save results.", |
| 57 | + }, |
| 58 | + "acl_list": { |
| 59 | + Type: schema.TypeList, |
| 60 | + Computed: true, |
| 61 | + Description: "The information list of the VPC. Each element contains the following attributes:", |
| 62 | + Elem: &schema.Resource{ |
| 63 | + Schema: map[string]*schema.Schema{ |
| 64 | + "vpc_id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "`ID` of the VPC instance.", |
| 68 | + }, |
| 69 | + "id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "`ID` of the network ACL instance.", |
| 73 | + }, |
| 74 | + "name": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "Name of the network ACL.", |
| 78 | + }, |
| 79 | + "create_time": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: "Creation time.", |
| 83 | + }, |
| 84 | + "subnets": { |
| 85 | + Type: schema.TypeList, |
| 86 | + Computed: true, |
| 87 | + Description: "", |
| 88 | + Elem: &schema.Resource{ |
| 89 | + Schema: map[string]*schema.Schema{ |
| 90 | + "vpc_id": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "ID of the VPC instance.", |
| 94 | + }, |
| 95 | + "subnet_id": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + Description: "Subnet instance `ID`", |
| 99 | + }, |
| 100 | + "subnet_name": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + Description: "Subnet name.", |
| 104 | + }, |
| 105 | + "cidr_block": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Computed: true, |
| 108 | + Description: "The `IPv4` `CIDR` of the subnet.", |
| 109 | + }, |
| 110 | + "tags": { |
| 111 | + Type: schema.TypeMap, |
| 112 | + Computed: true, |
| 113 | + Description: "Tags of the subnet.", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "ingress": { |
| 119 | + Type: schema.TypeList, |
| 120 | + Computed: true, |
| 121 | + Description: "", |
| 122 | + Elem: &schema.Resource{ |
| 123 | + Schema: map[string]*schema.Schema{ |
| 124 | + "protocol": { |
| 125 | + Type: schema.TypeString, |
| 126 | + Computed: true, |
| 127 | + Description: "Type of ip protocol. ", |
| 128 | + }, |
| 129 | + "port": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + Description: "Range of the port.", |
| 133 | + }, |
| 134 | + "policy": { |
| 135 | + Type: schema.TypeString, |
| 136 | + Computed: true, |
| 137 | + Description: "Rule policy of.", |
| 138 | + }, |
| 139 | + "cidr_block": { |
| 140 | + Type: schema.TypeString, |
| 141 | + Computed: true, |
| 142 | + Description: "An IP address network or segment.", |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + "egress": { |
| 148 | + Type: schema.TypeList, |
| 149 | + Computed: true, |
| 150 | + Description: "", |
| 151 | + Elem: &schema.Resource{ |
| 152 | + Schema: map[string]*schema.Schema{ |
| 153 | + "protocol": { |
| 154 | + Type: schema.TypeString, |
| 155 | + Computed: true, |
| 156 | + Description: "Type of ip protocol. ", |
| 157 | + }, |
| 158 | + "port": { |
| 159 | + Type: schema.TypeString, |
| 160 | + Computed: true, |
| 161 | + Description: "Range of the port.", |
| 162 | + }, |
| 163 | + "policy": { |
| 164 | + Type: schema.TypeString, |
| 165 | + Computed: true, |
| 166 | + Description: "Rule policy of.", |
| 167 | + }, |
| 168 | + "cidr_block": { |
| 169 | + Type: schema.TypeString, |
| 170 | + Computed: true, |
| 171 | + Description: "An IP address network or segment.", |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func dataSourceTencentCloudVpcACLRead(d *schema.ResourceData, meta interface{}) error { |
| 184 | + defer logElapsed("data_source.tencentcloud_vpc_acl.read")() |
| 185 | + var ( |
| 186 | + logId = getLogId(contextNil) |
| 187 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 188 | + service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 189 | + |
| 190 | + vpcID string |
| 191 | + name string |
| 192 | + id string |
| 193 | + ) |
| 194 | + |
| 195 | + if temp, ok := d.GetOk("vpc_id"); ok { |
| 196 | + tempStr := temp.(string) |
| 197 | + if tempStr != "" { |
| 198 | + vpcID = tempStr |
| 199 | + } |
| 200 | + } |
| 201 | + if temp, ok := d.GetOk("name"); ok { |
| 202 | + tempStr := temp.(string) |
| 203 | + if tempStr != "" { |
| 204 | + name = tempStr |
| 205 | + } |
| 206 | + } |
| 207 | + if temp, ok := d.GetOk("id"); ok { |
| 208 | + tempStr := temp.(string) |
| 209 | + if tempStr != "" { |
| 210 | + id = tempStr |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + networkAcls, err := service.DescribeNetWorkAcls(ctx, id, vpcID, name) |
| 215 | + if err != nil { |
| 216 | + return err |
| 217 | + } |
| 218 | + |
| 219 | + aclList := make([]map[string]interface{}, 0, len(networkAcls)) |
| 220 | + ids := make([]string, 0, len(networkAcls)) |
| 221 | + |
| 222 | + for _, info := range networkAcls { |
| 223 | + subnetInfo := info.SubnetSet |
| 224 | + subnets := make([]map[string]interface{}, 0, len(subnetInfo)) |
| 225 | + for i := range subnetInfo { |
| 226 | + v := subnetInfo[i] |
| 227 | + subnet := make(map[string]interface{}, 5) |
| 228 | + subnet["vpc_id"] = *v.VpcId |
| 229 | + subnet["subnet_id"] = *v.SubnetId |
| 230 | + subnet["subnet_name"] = *v.SubnetName |
| 231 | + subnet["cidr_block"] = *v.CidrBlock |
| 232 | + |
| 233 | + tag := make(map[string]string, len(v.TagSet)) |
| 234 | + for t := range v.TagSet { |
| 235 | + tagValue := v.TagSet[t] |
| 236 | + tag[*tagValue.Key] = *tagValue.Value |
| 237 | + } |
| 238 | + subnet["tags"] = tag |
| 239 | + |
| 240 | + subnets = append(subnets, subnet) |
| 241 | + } |
| 242 | + |
| 243 | + ingressInfo := info.IngressEntries |
| 244 | + ingress := make([]map[string]interface{}, 0, len(ingressInfo)) |
| 245 | + for i := range ingressInfo { |
| 246 | + v := ingressInfo[i] |
| 247 | + egressMap := make(map[string]interface{}, 4) |
| 248 | + egressMap["protocol"] = *v.Protocol |
| 249 | + egressMap["port"] = *v.Port |
| 250 | + egressMap["cidr_block"] = *v.CidrBlock |
| 251 | + egressMap["policy"] = *v.Action |
| 252 | + egressMap["description"] = *v.Description |
| 253 | + |
| 254 | + ingress = append(ingress, egressMap) |
| 255 | + } |
| 256 | + |
| 257 | + egressInfo := info.EgressEntries |
| 258 | + egress := make([]map[string]interface{}, 0, len(egressInfo)) |
| 259 | + for i := range egressInfo { |
| 260 | + v := egressInfo[i] |
| 261 | + egressMap := make(map[string]interface{}, 4) |
| 262 | + egressMap["protocol"] = *v.Protocol |
| 263 | + egressMap["port"] = *v.Port |
| 264 | + egressMap["cidr_block"] = *v.CidrBlock |
| 265 | + egressMap["policy"] = *v.Action |
| 266 | + egressMap["description"] = *v.Description |
| 267 | + |
| 268 | + egress = append(egress, egressMap) |
| 269 | + } |
| 270 | + |
| 271 | + aclResult := map[string]interface{}{ |
| 272 | + "vpc_id": info.VpcId, |
| 273 | + "id": info.NetworkAclId, |
| 274 | + "name": info.NetworkAclName, |
| 275 | + "create_time": info.CreatedTime, |
| 276 | + "subnets": subnets, |
| 277 | + "ingress": ingress, |
| 278 | + "egress": egress, |
| 279 | + } |
| 280 | + aclList = append(aclList, aclResult) |
| 281 | + ids = append(ids, *info.NetworkAclId) |
| 282 | + } |
| 283 | + |
| 284 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 285 | + err = d.Set("acl_list", aclList) |
| 286 | + if err != nil { |
| 287 | + log.Printf("[CRITAL]%s provider set acl list fail, reason:%v \n ", logId, err) |
| 288 | + return err |
| 289 | + } |
| 290 | + |
| 291 | + output, ok := d.GetOk("result_output_file") |
| 292 | + if ok && output.(string) != "" { |
| 293 | + if err := writeToFile(output.(string), aclList); err != nil { |
| 294 | + return err |
| 295 | + } |
| 296 | + } |
| 297 | + return nil |
| 298 | +} |
0 commit comments