|
| 1 | +package aquasec |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/aquasecurity/terraform-provider-aquasec/client" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceSuppressionRule() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: "", |
| 16 | + ReadContext: dataSourceSuppressionRuleRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "order_by": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Description: "Specify the parameter by which to sort the results" + |
| 21 | + "Available values : name, -name, created, -created, updated, -updated, enforce, -enforce," + |
| 22 | + "description, -description, created_by, -created_by, enforce_date, -enforce_date, enable, -enable," + |
| 23 | + "updated_by, -updated_by, policy_type, -policy_type, scope, -scope, application_scopes, -application_scopes", |
| 24 | + Optional: true, |
| 25 | + Default: "name", |
| 26 | + }, |
| 27 | + "page": { |
| 28 | + Type: schema.TypeInt, |
| 29 | + Description: "Specify the starting page for the results", |
| 30 | + Optional: true, |
| 31 | + Default: 1, |
| 32 | + }, |
| 33 | + "page_size": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Description: "Specify the number of results per page", |
| 36 | + Optional: true, |
| 37 | + Default: 20, |
| 38 | + }, |
| 39 | + "current_page": { |
| 40 | + Type: schema.TypeInt, |
| 41 | + Description: "The current page number (starting from 1)", |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "next_page": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Description: "The next page number (or zero if not relevant)", |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "returned_count": { |
| 50 | + Type: schema.TypeInt, |
| 51 | + Description: "The number of records returned on the current page", |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "total_count": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Description: "The total number of records across all pages", |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "data": { |
| 60 | + Type: schema.TypeList, |
| 61 | + Computed: true, |
| 62 | + Elem: &schema.Resource{ |
| 63 | + Schema: map[string]*schema.Schema{ |
| 64 | + "policy_id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "name": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "description": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "enable": { |
| 77 | + Type: schema.TypeBool, |
| 78 | + Computed: true, |
| 79 | + }, |
| 80 | + "created": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "updated": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "created_by": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + "updated_by": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "enforce": { |
| 97 | + Type: schema.TypeBool, |
| 98 | + Computed: true, |
| 99 | + }, |
| 100 | + "fail_build": { |
| 101 | + Type: schema.TypeBool, |
| 102 | + Computed: true, |
| 103 | + }, |
| 104 | + "fail_pr": { |
| 105 | + Type: schema.TypeBool, |
| 106 | + Computed: true, |
| 107 | + }, |
| 108 | + "enforcement_schedule": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + }, |
| 112 | + "clear_schedule": { |
| 113 | + Type: schema.TypeBool, |
| 114 | + Computed: true, |
| 115 | + }, |
| 116 | + "policy_type": { |
| 117 | + Type: schema.TypeList, |
| 118 | + Computed: true, |
| 119 | + Elem: &schema.Schema{ |
| 120 | + Type: schema.TypeString, |
| 121 | + }, |
| 122 | + }, |
| 123 | + "controls": { |
| 124 | + Type: schema.TypeList, |
| 125 | + Computed: true, |
| 126 | + Elem: &schema.Resource{ |
| 127 | + Schema: map[string]*schema.Schema{ |
| 128 | + "type": { |
| 129 | + Type: schema.TypeString, |
| 130 | + Computed: true, |
| 131 | + }, |
| 132 | + "scan_type": { |
| 133 | + Type: schema.TypeString, |
| 134 | + Computed: true, |
| 135 | + }, |
| 136 | + "provider": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Computed: true, |
| 139 | + }, |
| 140 | + "service": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Computed: true, |
| 143 | + }, |
| 144 | + "dependency_name": { |
| 145 | + Type: schema.TypeString, |
| 146 | + Computed: true, |
| 147 | + }, |
| 148 | + "version": { |
| 149 | + Type: schema.TypeString, |
| 150 | + Computed: true, |
| 151 | + }, |
| 152 | + "dependency_source": { |
| 153 | + Type: schema.TypeString, |
| 154 | + Computed: true, |
| 155 | + }, |
| 156 | + "operator": { |
| 157 | + Type: schema.TypeString, |
| 158 | + Computed: true, |
| 159 | + }, |
| 160 | + "severity": { |
| 161 | + Type: schema.TypeString, |
| 162 | + Computed: true, |
| 163 | + }, |
| 164 | + "vendorfix": { |
| 165 | + Type: schema.TypeBool, |
| 166 | + Computed: true, |
| 167 | + }, |
| 168 | + "direct_only": { |
| 169 | + Type: schema.TypeBool, |
| 170 | + Computed: true, |
| 171 | + }, |
| 172 | + "reachable_only": { |
| 173 | + Type: schema.TypeBool, |
| 174 | + Computed: true, |
| 175 | + }, |
| 176 | + "cve_ids": { |
| 177 | + Type: schema.TypeList, |
| 178 | + Optional: true, |
| 179 | + Elem: &schema.Schema{ |
| 180 | + Type: schema.TypeString, |
| 181 | + }, |
| 182 | + }, |
| 183 | + "avd_ids": { |
| 184 | + Type: schema.TypeList, |
| 185 | + Computed: true, |
| 186 | + Elem: &schema.Schema{ |
| 187 | + Type: schema.TypeString, |
| 188 | + }, |
| 189 | + }, |
| 190 | + "dependency_ids": { |
| 191 | + Type: schema.TypeList, |
| 192 | + Computed: true, |
| 193 | + Elem: &schema.Schema{ |
| 194 | + Type: schema.TypeString, |
| 195 | + }, |
| 196 | + }, |
| 197 | + "ids": { |
| 198 | + Type: schema.TypeList, |
| 199 | + Computed: true, |
| 200 | + Elem: &schema.Schema{ |
| 201 | + Type: schema.TypeString, |
| 202 | + }, |
| 203 | + }, |
| 204 | + "checks": { |
| 205 | + Type: schema.TypeList, |
| 206 | + Computed: true, |
| 207 | + Elem: &schema.Resource{ |
| 208 | + Schema: map[string]*schema.Schema{ |
| 209 | + "provider_name": { |
| 210 | + Type: schema.TypeString, |
| 211 | + Computed: true, |
| 212 | + }, |
| 213 | + "service_name": { |
| 214 | + Type: schema.TypeString, |
| 215 | + Computed: true, |
| 216 | + }, |
| 217 | + "scan_type": { |
| 218 | + Type: schema.TypeString, |
| 219 | + Computed: true, |
| 220 | + }, |
| 221 | + "check_id": { |
| 222 | + Type: schema.TypeString, |
| 223 | + Computed: true, |
| 224 | + }, |
| 225 | + "check_name": { |
| 226 | + Type: schema.TypeString, |
| 227 | + Computed: true, |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + "patterns": { |
| 233 | + Type: schema.TypeList, |
| 234 | + Computed: true, |
| 235 | + Elem: &schema.Schema{ |
| 236 | + Type: schema.TypeString, |
| 237 | + }, |
| 238 | + }, |
| 239 | + "ports": { |
| 240 | + Type: schema.TypeList, |
| 241 | + Computed: true, |
| 242 | + Elem: &schema.Schema{ |
| 243 | + Type: schema.TypeInt, |
| 244 | + }, |
| 245 | + }, |
| 246 | + "file_changes": { |
| 247 | + Type: schema.TypeList, |
| 248 | + Computed: true, |
| 249 | + Elem: &schema.Resource{ |
| 250 | + Schema: map[string]*schema.Schema{ |
| 251 | + "pattern": { |
| 252 | + Type: schema.TypeString, |
| 253 | + Computed: true, |
| 254 | + }, |
| 255 | + "changes": { |
| 256 | + Type: schema.TypeList, |
| 257 | + Computed: true, |
| 258 | + Elem: &schema.Schema{ |
| 259 | + Type: schema.TypeString, |
| 260 | + }, |
| 261 | + }, |
| 262 | + }, |
| 263 | + }, |
| 264 | + }, |
| 265 | + "target_file": { |
| 266 | + Type: schema.TypeString, |
| 267 | + Computed: true, |
| 268 | + }, |
| 269 | + "target_line": { |
| 270 | + Type: schema.TypeInt, |
| 271 | + Computed: true, |
| 272 | + }, |
| 273 | + "fingerprint": { |
| 274 | + Type: schema.TypeString, |
| 275 | + Computed: true, |
| 276 | + }, |
| 277 | + "file_globs": { |
| 278 | + Type: schema.TypeList, |
| 279 | + Computed: true, |
| 280 | + Elem: &schema.Schema{ |
| 281 | + Type: schema.TypeString, |
| 282 | + }, |
| 283 | + }, |
| 284 | + "published_date_filter": { |
| 285 | + Type: schema.TypeList, |
| 286 | + Computed: true, |
| 287 | + Elem: &schema.Resource{ |
| 288 | + Schema: map[string]*schema.Schema{ |
| 289 | + "days": { |
| 290 | + Type: schema.TypeInt, |
| 291 | + Computed: true, |
| 292 | + }, |
| 293 | + "enabled": { |
| 294 | + Type: schema.TypeBool, |
| 295 | + Computed: true, |
| 296 | + }, |
| 297 | + }, |
| 298 | + }, |
| 299 | + }, |
| 300 | + }, |
| 301 | + }, |
| 302 | + }, |
| 303 | + "scope": { |
| 304 | + Type: schema.TypeList, |
| 305 | + Computed: true, |
| 306 | + Elem: &schema.Resource{ |
| 307 | + Schema: map[string]*schema.Schema{ |
| 308 | + "expression": { |
| 309 | + Type: schema.TypeString, |
| 310 | + Computed: true, |
| 311 | + }, |
| 312 | + "variables": { |
| 313 | + Type: schema.TypeList, |
| 314 | + Computed: true, |
| 315 | + Elem: &schema.Resource{ |
| 316 | + Schema: map[string]*schema.Schema{ |
| 317 | + "attribute": { |
| 318 | + Type: schema.TypeString, |
| 319 | + Computed: true, |
| 320 | + }, |
| 321 | + "value": { |
| 322 | + Type: schema.TypeString, |
| 323 | + Computed: true, |
| 324 | + }, |
| 325 | + }, |
| 326 | + }, |
| 327 | + }, |
| 328 | + }, |
| 329 | + }, |
| 330 | + }, |
| 331 | + "application_scopes": { |
| 332 | + Type: schema.TypeList, |
| 333 | + Elem: &schema.Schema{ |
| 334 | + Type: schema.TypeString, |
| 335 | + }, |
| 336 | + Computed: true, |
| 337 | + }, |
| 338 | + }, |
| 339 | + }, |
| 340 | + }, |
| 341 | + }, |
| 342 | + } |
| 343 | +} |
| 344 | + |
| 345 | +func dataSourceSuppressionRuleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 346 | + ac := m.(*client.Client) |
| 347 | + query := client.SuppressionRuleQuery{ |
| 348 | + OrderBy: d.Get("order_by").(string), |
| 349 | + Page: d.Get("page").(int), |
| 350 | + PageSize: d.Get("page_size").(int), |
| 351 | + } |
| 352 | + result, err := ac.GetSuppressionRules(query) |
| 353 | + if err != nil { |
| 354 | + if strings.Contains(fmt.Sprintf("%s", err), "not found") { |
| 355 | + d.SetId("") |
| 356 | + return nil |
| 357 | + } |
| 358 | + return diag.FromErr(err) |
| 359 | + } |
| 360 | + |
| 361 | + rules, id := flattenSuppressionRules(&result) |
| 362 | + d.SetId(id) |
| 363 | + if err := d.Set("data", rules); err != nil { |
| 364 | + return diag.FromErr(err) |
| 365 | + } |
| 366 | + |
| 367 | + return nil |
| 368 | +} |
0 commit comments