|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceSysdigSecureRuleNetwork() *schema.Resource { |
| 14 | + timeout := 5 * time.Minute |
| 15 | + |
| 16 | + return &schema.Resource{ |
| 17 | + ReadContext: dataSourceSysdigRuleNetworkRead, |
| 18 | + |
| 19 | + Timeouts: &schema.ResourceTimeout{ |
| 20 | + Read: schema.DefaultTimeout(timeout), |
| 21 | + }, |
| 22 | + |
| 23 | + Schema: createRuleDataSourceSchema(map[string]*schema.Schema{ |
| 24 | + "block_inbound": { |
| 25 | + Type: schema.TypeBool, |
| 26 | + Computed: true, |
| 27 | + }, |
| 28 | + "block_outbound": { |
| 29 | + Type: schema.TypeBool, |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + "tcp": { |
| 33 | + Type: schema.TypeList, |
| 34 | + Computed: true, |
| 35 | + Elem: &schema.Resource{ |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + "matching": { |
| 38 | + Type: schema.TypeBool, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "ports": { |
| 42 | + Type: schema.TypeSet, |
| 43 | + Computed: true, |
| 44 | + Elem: &schema.Schema{ |
| 45 | + Type: schema.TypeInt, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "udp": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Optional: true, |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "matching": { |
| 57 | + Type: schema.TypeBool, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "ports": { |
| 61 | + Type: schema.TypeSet, |
| 62 | + Computed: true, |
| 63 | + Elem: &schema.Schema{ |
| 64 | + Type: schema.TypeInt, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func dataSourceSysdigRuleNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 75 | + return commonDataSourceSysdigRuleRead(ctx, d, meta, v2.RuleTypeNetwork, networkRuleDataSourceToResourceData) |
| 76 | +} |
| 77 | + |
| 78 | +func networkRuleDataSourceToResourceData(rule v2.Rule, d *schema.ResourceData) diag.Diagnostics { |
| 79 | + _ = d.Set("block_inbound", rule.Details.AllInbound) |
| 80 | + _ = d.Set("block_outbound", rule.Details.AllOutbound) |
| 81 | + |
| 82 | + if rule.Details.TCPListenPorts == nil { |
| 83 | + return diag.Errorf("no tcpListenPorts for a network rule") |
| 84 | + } |
| 85 | + |
| 86 | + if rule.Details.UDPListenPorts == nil { |
| 87 | + return diag.Errorf("no udpListenPorts for a network rule") |
| 88 | + } |
| 89 | + |
| 90 | + if len(rule.Details.TCPListenPorts.Items) > 0 { |
| 91 | + tcpPorts := []int{} |
| 92 | + for _, port := range rule.Details.TCPListenPorts.Items { |
| 93 | + intPort, err := strconv.Atoi(port) |
| 94 | + if err != nil { |
| 95 | + return diag.FromErr(err) |
| 96 | + } |
| 97 | + tcpPorts = append(tcpPorts, intPort) |
| 98 | + } |
| 99 | + _ = d.Set("tcp", []map[string]interface{}{{ |
| 100 | + "matching": rule.Details.TCPListenPorts.MatchItems, |
| 101 | + "ports": tcpPorts, |
| 102 | + }}) |
| 103 | + } |
| 104 | + if len(rule.Details.UDPListenPorts.Items) > 0 { |
| 105 | + udpPorts := []int{} |
| 106 | + for _, port := range rule.Details.UDPListenPorts.Items { |
| 107 | + intPort, err := strconv.Atoi(port) |
| 108 | + if err != nil { |
| 109 | + return diag.FromErr(err) |
| 110 | + } |
| 111 | + udpPorts = append(udpPorts, intPort) |
| 112 | + } |
| 113 | + _ = d.Set("udp", []map[string]interface{}{{ |
| 114 | + "matching": rule.Details.UDPListenPorts.MatchItems, |
| 115 | + "ports": udpPorts, |
| 116 | + }}) |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments