|
| 1 | +package convert |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/PaloAltoNetworks/terraform-provider-prismacloudcompute/internal/api/policy" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 6 | +) |
| 7 | + |
| 8 | +func SchemaToVulnerabilityCiCoderepoRules(d *schema.ResourceData) ([]policy.VulnerabilityCoderepoRule, error) { |
| 9 | + parsedRules := make([]policy.VulnerabilityCoderepoRule, 0) |
| 10 | + if rules, ok := d.GetOk("rule"); ok { |
| 11 | + presentRules := rules.([]interface{}) |
| 12 | + for _, val := range presentRules { |
| 13 | + presentRule := val.(map[string]interface{}) |
| 14 | + parsedRule := policy.VulnerabilityCoderepoRule{} |
| 15 | + |
| 16 | + if presentRule["alert_threshold"].([]interface{})[0] != nil { |
| 17 | + presentAlertThreshold := presentRule["alert_threshold"].([]interface{})[0].(map[string]interface{}) |
| 18 | + parsedRule.AlertThreshold = policy.VulnerabilityCoderepoThreshold{ |
| 19 | + Disabled: presentAlertThreshold["disabled"].(bool), |
| 20 | + Value: presentAlertThreshold["value"].(int), |
| 21 | + } |
| 22 | + } else { |
| 23 | + parsedRule.AlertThreshold = policy.VulnerabilityCoderepoThreshold{} |
| 24 | + } |
| 25 | + |
| 26 | + parsedRule.BlockMessage = presentRule["block_message"].(string) |
| 27 | + |
| 28 | + if presentRule["block_threshold"].([]interface{})[0] != nil { |
| 29 | + presentBlockThreshold := presentRule["block_threshold"].([]interface{})[0].(map[string]interface{}) |
| 30 | + parsedRule.BlockThreshold = policy.VulnerabilityCoderepoThreshold{ |
| 31 | + Enabled: presentBlockThreshold["enabled"].(bool), |
| 32 | + Value: presentBlockThreshold["value"].(int), |
| 33 | + } |
| 34 | + } else { |
| 35 | + parsedRule.BlockThreshold = policy.VulnerabilityCoderepoThreshold{} |
| 36 | + } |
| 37 | + |
| 38 | + parsedRule.Collections = PolicySchemaToCollections(presentRule["collections"].([]interface{})) |
| 39 | + |
| 40 | + presentCveRules := presentRule["cve_rule"].([]interface{}) |
| 41 | + parsedCveRules := make([]policy.VulnerabilityCoderepoCveRule, 0, len(presentCveRules)) |
| 42 | + for _, val := range presentCveRules { |
| 43 | + presentCveRule := val.(map[string]interface{}) |
| 44 | + parsedCveRules = append(parsedCveRules, policy.VulnerabilityCoderepoCveRule{ |
| 45 | + Description: presentCveRule["description"].(string), |
| 46 | + Effect: presentCveRule["effect"].(string), |
| 47 | + Expiration: schemaToVulnerabilityCiCoderepoExpiration(presentCveRule["expiration"].([]interface{})), |
| 48 | + Id: presentCveRule["id"].(string), |
| 49 | + }) |
| 50 | + } |
| 51 | + parsedRule.CveRules = parsedCveRules |
| 52 | + |
| 53 | + parsedRule.Disabled = presentRule["disabled"].(bool) |
| 54 | + parsedRule.Effect = presentRule["effect"].(string) |
| 55 | + |
| 56 | + if len(presentRule["grace_days_policy"].([]interface{})) > 0 && presentRule["grace_days_policy"].([]interface{})[0] != nil { |
| 57 | + presentGraceDaysPolicy := presentRule["grace_days_policy"].([]interface{})[0].(map[string]interface{}) |
| 58 | + parsedRule.GraceDaysPolicy = policy.VulnerabilityCoderepoGraceDaysPolicy{ |
| 59 | + Enabled: true, |
| 60 | + Low: presentGraceDaysPolicy["low"].(int), |
| 61 | + Medium: presentGraceDaysPolicy["medium"].(int), |
| 62 | + High: presentGraceDaysPolicy["high"].(int), |
| 63 | + Critical: presentGraceDaysPolicy["critical"].(int), |
| 64 | + } |
| 65 | + parsedRule.GraceDays = 0 |
| 66 | + } else { |
| 67 | + parsedRule.GraceDays = presentRule["grace_days"].(int) |
| 68 | + } |
| 69 | + |
| 70 | + parsedRule.Name = presentRule["name"].(string) |
| 71 | + parsedRule.Notes = presentRule["notes"].(string) |
| 72 | + parsedRule.OnlyFixed = presentRule["only_fixed"].(bool) |
| 73 | + |
| 74 | + presentTagRules := presentRule["tag_rule"].([]interface{}) |
| 75 | + parsedTagRules := make([]policy.VulnerabilityCoderepoTagRule, 0, len(presentTagRules)) |
| 76 | + for _, val := range presentTagRules { |
| 77 | + presentTagRule := val.(map[string]interface{}) |
| 78 | + parsedTagRules = append(parsedTagRules, policy.VulnerabilityCoderepoTagRule{ |
| 79 | + Description: presentTagRule["description"].(string), |
| 80 | + Effect: presentTagRule["effect"].(string), |
| 81 | + Expiration: schemaToVulnerabilityCiCoderepoExpiration(presentTagRule["expiration"].([]interface{})), |
| 82 | + Name: presentTagRule["name"].(string), |
| 83 | + }) |
| 84 | + } |
| 85 | + parsedRule.TagRules = parsedTagRules |
| 86 | + |
| 87 | + parsedRule.Verbose = presentRule["verbose"].(bool) |
| 88 | + |
| 89 | + parsedRules = append(parsedRules, parsedRule) |
| 90 | + } |
| 91 | + } |
| 92 | + return parsedRules, nil |
| 93 | +} |
| 94 | + |
| 95 | +func schemaToVulnerabilityCiCoderepoExpiration(in []interface{}) policy.VulnerabilityCoderepoExpiration { |
| 96 | + parsedExpiration := policy.VulnerabilityCoderepoExpiration{} |
| 97 | + if in[0] == nil { |
| 98 | + return parsedExpiration |
| 99 | + } |
| 100 | + presentExpiration := in[0].(map[string]interface{}) |
| 101 | + parsedExpiration.Date = presentExpiration["date"].(string) |
| 102 | + parsedExpiration.Enabled = presentExpiration["enabled"].(bool) |
| 103 | + return parsedExpiration |
| 104 | +} |
| 105 | + |
| 106 | +func VulnerabilityCiCoderepoRulesToSchema(in []policy.VulnerabilityCoderepoRule) []interface{} { |
| 107 | + ans := make([]interface{}, 0, len(in)) |
| 108 | + for _, val := range in { |
| 109 | + m := make(map[string]interface{}) |
| 110 | + m["alert_threshold"] = vulnerabilityCiCoderepoAlertThresholdToSchema(val.AlertThreshold) |
| 111 | + m["block_message"] = val.BlockMessage |
| 112 | + m["block_threshold"] = vulnerabilityCiCoderepoBlockThresholdToSchema(val.BlockThreshold) |
| 113 | + m["collections"] = CollectionsToPolicySchema(val.Collections) |
| 114 | + m["cve_rule"] = vulnerabilityCiCoderepoCveRulesToSchema(val.CveRules) |
| 115 | + m["disabled"] = val.Disabled |
| 116 | + m["effect"] = val.Effect |
| 117 | + m["grace_days"] = val.GraceDays |
| 118 | + m["grace_days_policy"] = vulnerabilityCiCoderepoGraceDaysPolicyToSchema(val.GraceDaysPolicy) |
| 119 | + m["name"] = val.Name |
| 120 | + m["notes"] = val.Notes |
| 121 | + m["only_fixed"] = val.OnlyFixed |
| 122 | + m["tag_rule"] = vulnerabilityCiCoderepoTagRulesToSchema(val.TagRules) |
| 123 | + m["verbose"] = val.Verbose |
| 124 | + ans = append(ans, m) |
| 125 | + } |
| 126 | + return ans |
| 127 | +} |
| 128 | + |
| 129 | +func vulnerabilityCiCoderepoAlertThresholdToSchema(in policy.VulnerabilityCoderepoThreshold) []interface{} { |
| 130 | + ans := make([]interface{}, 0, 1) |
| 131 | + m := make(map[string]interface{}) |
| 132 | + m["disabled"] = in.Disabled |
| 133 | + m["value"] = in.Value |
| 134 | + ans = append(ans, m) |
| 135 | + return ans |
| 136 | +} |
| 137 | + |
| 138 | +func vulnerabilityCiCoderepoBlockThresholdToSchema(in policy.VulnerabilityCoderepoThreshold) []interface{} { |
| 139 | + ans := make([]interface{}, 0, 1) |
| 140 | + m := make(map[string]interface{}) |
| 141 | + m["enabled"] = in.Enabled |
| 142 | + m["value"] = in.Value |
| 143 | + ans = append(ans, m) |
| 144 | + return ans |
| 145 | +} |
| 146 | + |
| 147 | +func vulnerabilityCiCoderepoCveRulesToSchema(in []policy.VulnerabilityCoderepoCveRule) []interface{} { |
| 148 | + ans := make([]interface{}, 0, len(in)) |
| 149 | + for _, val := range in { |
| 150 | + m := make(map[string]interface{}) |
| 151 | + m["description"] = val.Description |
| 152 | + m["effect"] = val.Effect |
| 153 | + m["expiration"] = vulnerabilityCoderepoExpirationToSchema(val.Expiration) |
| 154 | + m["id"] = val.Id |
| 155 | + ans = append(ans, m) |
| 156 | + } |
| 157 | + return ans |
| 158 | +} |
| 159 | + |
| 160 | +func vulnerabilityCiCoderepoExpirationToSchema(in policy.VulnerabilityCoderepoExpiration) []interface{} { |
| 161 | + ans := make([]interface{}, 0, 1) |
| 162 | + m := make(map[string]interface{}) |
| 163 | + m["date"] = in.Date |
| 164 | + m["enabled"] = in.Enabled |
| 165 | + ans = append(ans, m) |
| 166 | + return ans |
| 167 | +} |
| 168 | + |
| 169 | +func vulnerabilityCiCoderepoTagRulesToSchema(in []policy.VulnerabilityCoderepoTagRule) []interface{} { |
| 170 | + ans := make([]interface{}, 0, len(in)) |
| 171 | + for _, val := range in { |
| 172 | + m := make(map[string]interface{}) |
| 173 | + m["description"] = val.Description |
| 174 | + m["effect"] = val.Effect |
| 175 | + m["expiration"] = vulnerabilityCoderepoExpirationToSchema(val.Expiration) |
| 176 | + m["name"] = val.Name |
| 177 | + ans = append(ans, m) |
| 178 | + } |
| 179 | + return ans |
| 180 | +} |
| 181 | + |
| 182 | +func vulnerabilityCiCoderepoGraceDaysPolicyToSchema(in policy.VulnerabilityCoderepoGraceDaysPolicy) []interface{} { |
| 183 | + ans := make([]interface{}, 0, 1) |
| 184 | + m := make(map[string]interface{}) |
| 185 | + m["low"] = in.Low |
| 186 | + m["medium"] = in.Medium |
| 187 | + m["high"] = in.High |
| 188 | + m["critical"] = in.Critical |
| 189 | + ans = append(ans, m) |
| 190 | + return ans |
| 191 | +} |
0 commit comments