-
Notifications
You must be signed in to change notification settings - Fork 92
/
datasource.go
58 lines (48 loc) · 1.57 KB
/
datasource.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
package firewall
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/linode/helper"
)
func dataSourceFirewallRules() *schema.Resource {
return &schema.Resource{
Schema: dataSourceRuleSchema,
}
}
func DataSource() *schema.Resource {
return &schema.Resource{
Schema: dataSourceSchema,
ReadContext: readDataSource,
}
}
func readDataSource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*helper.ProviderMeta).Client
id := d.Get("id").(int)
firewall, err := client.GetFirewall(ctx, id)
if err != nil {
return diag.Errorf("failed to get firewall %d: %s", id, err)
}
rules, err := client.GetFirewallRules(ctx, id)
if err != nil {
return diag.Errorf("failed to get firewall rules %d: %s", id, err)
}
devices, err := client.ListFirewallDevices(ctx, id, nil)
if err != nil {
return diag.Errorf("failed to get firewall devices %d: %s", id, err)
}
d.SetId(strconv.Itoa(id))
d.Set("label", firewall.Label)
d.Set("tags", firewall.Tags)
d.Set("disabled", firewall.Status == linodego.FirewallDisabled)
d.Set("inbound", flattenFirewallRules(rules.Inbound))
d.Set("inbound_policy", rules.InboundPolicy)
d.Set("outbound", flattenFirewallRules(rules.Outbound))
d.Set("outbound_policy", rules.OutboundPolicy)
d.Set("status", firewall.Status)
d.Set("linodes", flattenFirewallLinodes(devices))
d.Set("devices", flattenFirewallDevices(devices))
return nil
}