Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security rules with service group and address groups #384

32 changes: 32 additions & 0 deletions nutanix/data_source_nutanix_network_security_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func dataSourceNutanixNetworkSecurityRule() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -529,6 +531,8 @@ func dataSourceNutanixNetworkSecurityRule() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -703,6 +707,8 @@ func dataSourceNutanixNetworkSecurityRule() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -849,6 +855,8 @@ func dataSourceNutanixNetworkSecurityRule() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -1016,6 +1024,14 @@ func dataSourceNutanixNetworkSecurityRuleRead(ctx context.Context, d *schema.Res
qroaItem["udp_port_range_list"] = udpprList
}

if v.AddressGroupInclusionList != nil {
qroaItem["address_group_inclusion_list"] = flattenArrayReferenceValues(v.AddressGroupInclusionList)
}

if v.ServiceGroupList != nil {
qroaItem["service_group_list"] = flattenArrayReferenceValues(v.ServiceGroupList)
}

if v.Filter != nil {
qroaItem["filter_kind_list"] = utils.StringValueSlice(v.Filter.KindList)
qroaItem["filter_type"] = utils.StringValue(v.Filter.Type)
Expand Down Expand Up @@ -1118,6 +1134,14 @@ func dataSourceNutanixNetworkSecurityRuleRead(ctx context.Context, d *schema.Res
qriaItem["udp_port_range_list"] = udpprList
}

if v.AddressGroupInclusionList != nil {
qriaItem["address_group_inclusion_list"] = flattenArrayReferenceValues(v.AddressGroupInclusionList)
}

if v.ServiceGroupList != nil {
qriaItem["service_group_list"] = flattenArrayReferenceValues(v.ServiceGroupList)
}

if v.Filter != nil {
if v.Filter.KindList != nil {
fkl := v.Filter.KindList
Expand Down Expand Up @@ -1351,6 +1375,8 @@ func resourceNutanixDatasourceNetworkSecurityRuleResourceV0() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -1497,6 +1523,8 @@ func resourceNutanixDatasourceNetworkSecurityRuleResourceV0() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -1612,6 +1640,8 @@ func resourceNutanixDatasourceNetworkSecurityRuleResourceV0() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -1758,6 +1788,8 @@ func resourceNutanixDatasourceNetworkSecurityRuleResourceV0() *schema.Resource {
},
},
},
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Computed: true,
Expand Down
108 changes: 95 additions & 13 deletions nutanix/resource_nutanix_network_security_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"github.com/spf13/cast"

"github.com/terraform-providers/terraform-provider-nutanix/utils"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -118,8 +120,10 @@ func resourceNutanixNetworkSecurityRule() *schema.Resource {
Optional: true,
Computed: true,
},
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -250,8 +254,10 @@ func resourceNutanixNetworkSecurityRule() *schema.Resource {
Optional: true,
Computed: true,
},
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -414,8 +420,10 @@ func resourceNutanixNetworkSecurityRule() *schema.Resource {
Optional: true,
Computed: true,
},
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -546,8 +554,10 @@ func resourceNutanixNetworkSecurityRule() *schema.Resource {
Optional: true,
Computed: true,
},
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1129,6 +1139,7 @@ func expandNetworkRule(prefix string, d *schema.ResourceData) *v3.NetworkSecurit
for k, v := range oal {
nr := v.(map[string]interface{})
nrItem := &v3.NetworkRule{}
setIPSubnet := false
iPSubnet := &v3.IPSubnet{}
filter := &v3.CategoryFilter{}

Expand All @@ -1138,14 +1149,20 @@ func expandNetworkRule(prefix string, d *schema.ResourceData) *v3.NetworkSecurit

if ip, ipok := nr["ip_subnet"]; ipok && ip.(string) != "" {
iPSubnet.IP = utils.StringPtr(ip.(string))
setIPSubnet = true
}

if ippl, ipok := nr["ip_subnet_prefix_length"]; ipok && ippl.(string) != "" {
if i, err := strconv.Atoi(ippl.(string)); err == nil {
iPSubnet.PrefixLength = utils.Int64Ptr(int64(i))
setIPSubnet = true
}
}

if setIPSubnet {
nrItem.IPSubnet = iPSubnet
}

if t, tcpok := nr["tcp_port_range_list"]; tcpok {
nrItem.TCPPortRangeList = expandPortRangeList(t)
}
Expand All @@ -1154,6 +1171,14 @@ func expandNetworkRule(prefix string, d *schema.ResourceData) *v3.NetworkSecurit
nrItem.UDPPortRangeList = expandPortRangeList(u)
}

if a, agok := nr["address_group_inclusion_list"]; agok {
nrItem.AddressGroupInclusionList = expandReferencesList(a)
}

if s, sgok := nr["service_group_list"]; sgok {
nrItem.ServiceGroupList = expandReferencesList(s)
}

if f, fok := nr["filter_kind_list"]; fok && len(f.([]interface{})) > 0 {
filter.KindList = expandStringList(f.([]interface{}))
}
Expand Down Expand Up @@ -1203,7 +1228,6 @@ func expandNetworkRule(prefix string, d *schema.ResourceData) *v3.NetworkSecurit
nrItem.IcmpTypeCodeList = expandIcmpTypeCodeList(icmp)
}

nrItem.IPSubnet = iPSubnet
if !reflect.DeepEqual(*filter, v3.CategoryFilter{}) {
nrItem.Filter = filter
}
Expand Down Expand Up @@ -1284,6 +1308,14 @@ func expandNetworkRule(prefix string, d *schema.ResourceData) *v3.NetworkSecurit
nrItem.UDPPortRangeList = expandPortRangeList(u)
}

if a, agok := nr["address_group_inclusion_list"]; agok {
nrItem.AddressGroupInclusionList = expandReferencesList(a)
}

if s, sgok := nr["service_group_list"]; sgok {
nrItem.ServiceGroupList = expandReferencesList(s)
}

if f, fok := nr["filter_kind_list"]; fok && len(f.([]interface{})) > 0 {
filter.KindList = expandStringList(f.([]interface{}))
}
Expand Down Expand Up @@ -1353,6 +1385,18 @@ func expandNetworkRule(prefix string, d *schema.ResourceData) *v3.NetworkSecurit
return appRule
}

func expandReferencesList(rl interface{}) []*v3.Reference {
refList := rl.([]interface{})
refs := make([]*v3.Reference, len(refList))

for i, r := range refList {
reff := r.(map[string]interface{})
refs[i] = expandReference(reff)
}

return refs
}

func expandFilterParams(fp map[string][]string) []map[string]interface{} {
fpList := make([]map[string]interface{}, 0)
if len(fp) > 0 {
Expand Down Expand Up @@ -1449,6 +1493,14 @@ func flattenNetworkRuleList(networkRules []*v3.NetworkRule) []map[string]interfa
ruleItem["udp_port_range_list"] = udpprList
}

if v.AddressGroupInclusionList != nil {
ruleItem["address_group_inclusion_list"] = flattenArrayReferenceValues(v.AddressGroupInclusionList)
}

if v.ServiceGroupList != nil {
ruleItem["service_group_list"] = flattenArrayReferenceValues(v.ServiceGroupList)
}

if v.Filter != nil {
ruleItem["filter_kind_list"] = utils.StringValueSlice(v.Filter.KindList)
ruleItem["filter_type"] = utils.StringValue(v.Filter.Type)
Expand Down Expand Up @@ -1483,6 +1535,32 @@ func flattenNetworkRuleList(networkRules []*v3.NetworkRule) []map[string]interfa
return ruleList
}

func referenceListSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kind": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"uuid": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
}

func portRangeSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Expand Down Expand Up @@ -1585,8 +1663,10 @@ func resourceNutanixSecurityRuleInstanceResourceV0() *schema.Resource {
Optional: true,
Computed: true,
},
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1717,8 +1797,10 @@ func resourceNutanixSecurityRuleInstanceResourceV0() *schema.Resource {
Optional: true,
Computed: true,
},
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"tcp_port_range_list": portRangeSchema(),
"udp_port_range_list": portRangeSchema(),
"service_group_list": referenceListSchema(),
"address_group_inclusion_list": referenceListSchema(),
"filter_kind_list": {
Type: schema.TypeList,
Optional: true,
Expand Down
Loading