<commit_before>package resources

import (
	"github.com/hashicorp/terraform/helper/schema"
	"code.cloudfoundry.org/cli/cf/models"
	"github.com/orange-cloudfoundry/terraform-provider-cloudfoundry/cf_client"
	"log"
	"reflect"
	"fmt"
	"bytes"
	"github.com/hashicorp/terraform/helper/hashcode"
	"strconv"
	"github.com/orange-cloudfoundry/terraform-provider-cloudfoundry/resources/caching"
	"errors"
	"strings"
	"regexp"
)

var validProtocoles []string = []string{"icmp", "tcp", "udp", "all"}

type CfSecurityGroupResource struct {
	CfResource
}

func NewCfSecurityGroupResource() CfResource {
	return &CfSecurityGroupResource{}
}

func (c CfSecurityGroupResource) resourceObject(d *schema.ResourceData) models.SecurityGroupFields {
	rulesSchema := d.Get("rules").(*schema.Set)
	rules := make([]map[string]interface{}, 0)
	for _, rule := range rulesSchema.List() {
		rules = append(rules, c.sanitizeRule(rule.(map[string]interface{})))
	}
	return models.SecurityGroupFields{
		GUID: d.Id(),
		Name: d.Get("name").(string),
		Rules: rules,
	}
}
func (c CfSecurityGroupResource) unSanitizeRule(rule map[string]interface{}) map[string]interface{} {
	unSanitizedRule := make(map[string]interface{})
	if _, ok := rule["code"]; !ok {
		unSanitizedRule["code"] = -1
	}
	if _, ok := rule["log"]; !ok {
		unSanitizedRule["log"] = false
	}
	if _, ok := rule["type"]; !ok {
		unSanitizedRule["type"] = -1
	}
	if _, ok := rule["ports"]; !ok {
		unSanitizedRule["ports"] = ""
	}
	if _, ok := rule["destination"]; !ok {
		unSanitizedRule["destination"] = ""
	}
	if _, ok := rule["description"]; !ok {
		unSanitizedRule["description"] = ""
	}
	for index, content := range rule {
		unSanitizedRule[index] = content
	}
	return unSanitizedRule
}
func (c CfSecurityGroupResource) sanitizeRule(rule map[string]interface{}) map[string]interface{} {
	sanitizedRule := make(map[string]interface{})

	for index, content := range rule {
		if index == "code" && content.(int) == -1 {
			continue
		}
		if index == "log" && content.(bool) == false {
			continue
		}
		if index == "type" && content.(int) == -1 {
			continue
		}
		if index == "ports" && content.(string) == "" {
			continue
		}
		if index == "destination" && content.(string) == "" {
			continue
		}<commit_msg>Fix icmp rule in security group when reading<commit_after>package resources

import (
	"github.com/hashicorp/terraform/helper/schema"
	"code.cloudfoundry.org/cli/cf/models"
	"github.com/orange-cloudfoundry/terraform-provider-cloudfoundry/cf_client"
	"log"
	"reflect"
	"fmt"
	"bytes"
	"github.com/hashicorp/terraform/helper/hashcode"
	"strconv"
	"github.com/orange-cloudfoundry/terraform-provider-cloudfoundry/resources/caching"
	"errors"
	"strings"
	"regexp"
)

var validProtocoles []string = []string{"icmp", "tcp", "udp", "all"}

type CfSecurityGroupResource struct {
	CfResource
}

func NewCfSecurityGroupResource() CfResource {
	return &CfSecurityGroupResource{}
}

func (c CfSecurityGroupResource) resourceObject(d *schema.ResourceData) models.SecurityGroupFields {
	rulesSchema := d.Get("rules").(*schema.Set)
	rules := make([]map[string]interface{}, 0)
	for _, rule := range rulesSchema.List() {
		rules = append(rules, c.sanitizeRule(rule.(map[string]interface{})))
	}
	return models.SecurityGroupFields{
		GUID: d.Id(),
		Name: d.Get("name").(string),
		Rules: rules,
	}
}
func (c CfSecurityGroupResource) unSanitizeRule(rule map[string]interface{}) map[string]interface{} {
	unSanitizedRule := make(map[string]interface{})
	if _, ok := rule["code"]; !ok {
		unSanitizedRule["code"] = -1
	} else {
		rule["code"] = c.convertRuleParamFloatToInt(rule["code"])
	}
	if _, ok := rule["log"]; !ok {
		unSanitizedRule["log"] = false
	}
	if _, ok := rule["type"]; !ok {
		unSanitizedRule["type"] = -1
	} else {
		rule["type"] = c.convertRuleParamFloatToInt(rule["type"])
	}
	if _, ok := rule["ports"]; !ok {
		unSanitizedRule["ports"] = ""
	}
	if _, ok := rule["destination"]; !ok {
		unSanitizedRule["destination"] = ""
	}
	if _, ok := rule["description"]; !ok {
		unSanitizedRule["description"] = ""
	}
	for index, content := range rule {
		unSanitizedRule[index] = content
	}
	return unSanitizedRule
}
func (c CfSecurityGroupResource) convertRuleParamFloatToInt(param interface{}) int {
	kindParam := reflect.TypeOf(param).Kind()
	if kindParam == reflect.Float32 {
		return int(param.(float32))
	}
	if kindParam == reflect.Float64 {
		return int(param.(float64))
	}
	return param.(int)
}
func (c CfSecurityGroupResource) sanitizeRule(rule map[string]interface{}) map[string]interface{} {
	sanitizedRule := make(map[string]interface{})

	for index, content := range rule {
		if index == "code" && content.(int) == -1 {
			continue
		}
		if index == "log" && content.(bool) == false {
			continue
		}
		if index == "type" && content.(int) == -1 {
			continue
		}
		if index == "ports" && content.(string) == "" {
			continue
		}
		if index == "destination" && content.(string) == "" {
			continue
		}
{'fraction_non_alphanumeric': 0.1720234485546796, 'fraction_numerical': 0.0036385688295936932, 'mean_word_length': 4.807511737089202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3281930', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1725, 'n_words': 331}