|
| 1 | +package aquasec |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/aquasecurity/terraform-provider-aquasec/client" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceScannerGroup() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + ReadContext: dataScannerGroupRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + // Optional input argument |
| 16 | + "name": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Description: "Name of the scanner group (optional). If omitted, all scanner groups are returned.", |
| 19 | + Optional: true, |
| 20 | + }, |
| 21 | + // Single scanner group fields (computed) |
| 22 | + "description": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + "status": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + "tokens": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Computed: true, |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + "os_type": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "type": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "author": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "created_at": { |
| 50 | + Type: schema.TypeInt, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "updated_at": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "registries": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Computed: true, |
| 60 | + Elem: &schema.Schema{ |
| 61 | + Type: schema.TypeString, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "application_scopes": { |
| 65 | + Type: schema.TypeList, |
| 66 | + Computed: true, |
| 67 | + Elem: &schema.Schema{ |
| 68 | + Type: schema.TypeString, |
| 69 | + }, |
| 70 | + }, |
| 71 | + "scanners": { |
| 72 | + Type: schema.TypeList, |
| 73 | + Computed: true, |
| 74 | + Elem: &schema.Resource{ |
| 75 | + Schema: map[string]*schema.Schema{ |
| 76 | + "last_heartbeat": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + }, |
| 80 | + "scanner_name": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "scanner_version": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "os_version": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + "token": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "registered_on": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Computed: true, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + "deploy_command": { |
| 104 | + Type: schema.TypeList, |
| 105 | + Computed: true, |
| 106 | + Elem: &schema.Resource{ |
| 107 | + Schema: map[string]*schema.Schema{ |
| 108 | + "additional_prop1": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + }, |
| 112 | + "additional_prop2": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + }, |
| 116 | + "additional_prop3": { |
| 117 | + Type: schema.TypeString, |
| 118 | + Computed: true, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + |
| 124 | + // For listing all scanner groups (list of objects) |
| 125 | + "scanner_groups": { |
| 126 | + Type: schema.TypeList, |
| 127 | + Computed: true, |
| 128 | + Elem: &schema.Resource{ |
| 129 | + Schema: map[string]*schema.Schema{ |
| 130 | + "name": { |
| 131 | + Type: schema.TypeString, |
| 132 | + Computed: true, |
| 133 | + }, |
| 134 | + "description": { |
| 135 | + Type: schema.TypeString, |
| 136 | + Computed: true, |
| 137 | + }, |
| 138 | + "status": { |
| 139 | + Type: schema.TypeString, |
| 140 | + Computed: true, |
| 141 | + }, |
| 142 | + // Add other fields here same as above... |
| 143 | + "tokens": { |
| 144 | + Type: schema.TypeList, |
| 145 | + Computed: true, |
| 146 | + Elem: &schema.Schema{ |
| 147 | + Type: schema.TypeString, |
| 148 | + }, |
| 149 | + }, |
| 150 | + "os_type": { |
| 151 | + Type: schema.TypeString, |
| 152 | + Computed: true, |
| 153 | + }, |
| 154 | + "type": { |
| 155 | + Type: schema.TypeString, |
| 156 | + Computed: true, |
| 157 | + }, |
| 158 | + "author": { |
| 159 | + Type: schema.TypeString, |
| 160 | + Computed: true, |
| 161 | + }, |
| 162 | + "created_at": { |
| 163 | + Type: schema.TypeInt, |
| 164 | + Computed: true, |
| 165 | + }, |
| 166 | + "updated_at": { |
| 167 | + Type: schema.TypeInt, |
| 168 | + Computed: true, |
| 169 | + }, |
| 170 | + "registries": { |
| 171 | + Type: schema.TypeList, |
| 172 | + Computed: true, |
| 173 | + Elem: &schema.Schema{ |
| 174 | + Type: schema.TypeString, |
| 175 | + }, |
| 176 | + }, |
| 177 | + "application_scopes": { |
| 178 | + Type: schema.TypeList, |
| 179 | + Computed: true, |
| 180 | + Elem: &schema.Schema{ |
| 181 | + Type: schema.TypeString, |
| 182 | + }, |
| 183 | + }, |
| 184 | + "scanners": { |
| 185 | + Type: schema.TypeList, |
| 186 | + Computed: true, |
| 187 | + Elem: &schema.Resource{ |
| 188 | + Schema: map[string]*schema.Schema{ |
| 189 | + "last_heartbeat": { |
| 190 | + Type: schema.TypeString, |
| 191 | + Computed: true, |
| 192 | + }, |
| 193 | + "scanner_name": { |
| 194 | + Type: schema.TypeString, |
| 195 | + Computed: true, |
| 196 | + }, |
| 197 | + "scanner_version": { |
| 198 | + Type: schema.TypeString, |
| 199 | + Computed: true, |
| 200 | + }, |
| 201 | + "os_version": { |
| 202 | + Type: schema.TypeString, |
| 203 | + Computed: true, |
| 204 | + }, |
| 205 | + "token": { |
| 206 | + Type: schema.TypeString, |
| 207 | + Computed: true, |
| 208 | + }, |
| 209 | + "registered_on": { |
| 210 | + Type: schema.TypeString, |
| 211 | + Computed: true, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + "deploy_command": { |
| 217 | + Type: schema.TypeList, |
| 218 | + Computed: true, |
| 219 | + Elem: &schema.Resource{ |
| 220 | + Schema: map[string]*schema.Schema{ |
| 221 | + "additional_prop1": { |
| 222 | + Type: schema.TypeString, |
| 223 | + Computed: true, |
| 224 | + }, |
| 225 | + "additional_prop2": { |
| 226 | + Type: schema.TypeString, |
| 227 | + Computed: true, |
| 228 | + }, |
| 229 | + "additional_prop3": { |
| 230 | + Type: schema.TypeString, |
| 231 | + Computed: true, |
| 232 | + }, |
| 233 | + }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + }, |
| 237 | + }, |
| 238 | + }, |
| 239 | + }, |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +func dataScannerGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 244 | + c := m.(*client.Client) |
| 245 | + |
| 246 | + name, hasName := d.GetOk("name") |
| 247 | + |
| 248 | + if hasName && name.(string) != "" { |
| 249 | + // Single scanner group fetch |
| 250 | + sg, err := c.GetScannerGroup(name.(string)) |
| 251 | + if err != nil { |
| 252 | + return diag.FromErr(err) |
| 253 | + } |
| 254 | + |
| 255 | + d.SetId(sg.Name) |
| 256 | + d.Set("description", sg.Description) |
| 257 | + d.Set("status", sg.Status) |
| 258 | + d.Set("tokens", sg.Tokens) |
| 259 | + d.Set("os_type", sg.OSType) |
| 260 | + d.Set("type", sg.Type) |
| 261 | + d.Set("author", sg.Author) |
| 262 | + d.Set("created_at", sg.CreatedAt) |
| 263 | + d.Set("updated_at", sg.UpdatedAt) |
| 264 | + d.Set("registries", sg.Registries) |
| 265 | + d.Set("application_scopes", sg.ApplicationScopes) |
| 266 | + if err := d.Set("scanners", flattenScanners(sg.Scanners)); err != nil { |
| 267 | + return diag.FromErr(err) |
| 268 | + } |
| 269 | + if err := d.Set("deploy_command", flattenDeployCommand(sg.DeployCommand)); err != nil { |
| 270 | + return diag.FromErr(err) |
| 271 | + } |
| 272 | + } else { |
| 273 | + // List all scanner groups |
| 274 | + groups, err := c.GetScannerGroups() |
| 275 | + if err != nil { |
| 276 | + return diag.FromErr(err) |
| 277 | + } |
| 278 | + |
| 279 | + list := make([]interface{}, 0, len(groups)) |
| 280 | + for _, sg := range groups { |
| 281 | + list = append(list, flattenSG([]client.ScannerGroup{sg})[0].(map[string]interface{})) |
| 282 | + } |
| 283 | + d.SetId("all") |
| 284 | + d.Set("scanner_groups", list) |
| 285 | + } |
| 286 | + |
| 287 | + return nil |
| 288 | +} |
| 289 | + |
| 290 | +func flattenScanners(scanners []client.Scanners) interface{} { |
| 291 | + if scanners == nil { |
| 292 | + return []map[string]interface{}{} |
| 293 | + } |
| 294 | + var scannerList []map[string]interface{} |
| 295 | + for _, scanner := range scanners { |
| 296 | + scannerList = append(scannerList, map[string]interface{}{ |
| 297 | + "last_heartbeat": scanner.LastHeartBeat, |
| 298 | + "scanner_name": scanner.ScannerName, |
| 299 | + "scanner_version": scanner.ScannerVersion, |
| 300 | + "os_version": scanner.OsVersion, |
| 301 | + "registered_on": scanner.RegisteredOn, |
| 302 | + "token": scanner.Token, |
| 303 | + }) |
| 304 | + } |
| 305 | + return scannerList |
| 306 | +} |
| 307 | + |
| 308 | +func flattenDeployCommand(deployCommand client.DeployCommand) []map[string]interface{} { |
| 309 | + return []map[string]interface{}{ |
| 310 | + { |
| 311 | + "additional_prop1": deployCommand.AdditionalProp1, |
| 312 | + "additional_prop2": deployCommand.AdditionalProp2, |
| 313 | + "additional_prop3": deployCommand.AdditionalProp3, |
| 314 | + }, |
| 315 | + } |
| 316 | +} |
| 317 | + |
| 318 | +func flattenSG(sgs []client.ScannerGroup) []interface{} { |
| 319 | + result := make([]interface{}, 0, len(sgs)) |
| 320 | + for _, sg := range sgs { |
| 321 | + m := map[string]interface{}{ |
| 322 | + "name": sg.Name, |
| 323 | + "description": sg.Description, |
| 324 | + "status": sg.Status, |
| 325 | + "tokens": sg.Tokens, |
| 326 | + "os_type": sg.OSType, |
| 327 | + "type": sg.Type, |
| 328 | + "author": sg.Author, |
| 329 | + "created_at": sg.CreatedAt, |
| 330 | + "updated_at": sg.UpdatedAt, |
| 331 | + "registries": sg.Registries, |
| 332 | + "application_scopes": sg.ApplicationScopes, |
| 333 | + "scanners": flattenScanners(sg.Scanners), |
| 334 | + "deploy_command": flattenDeployCommand(sg.DeployCommand), |
| 335 | + } |
| 336 | + result = append(result, m) |
| 337 | + } |
| 338 | + return result |
| 339 | +} |
0 commit comments