|
| 1 | +package prismacloudcompute |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/paloaltonetworks/prisma-cloud-compute-go/pcc" |
| 8 | + "github.com/paloaltonetworks/prisma-cloud-compute-go/rule" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceCustomRule() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Description: "Use this data source to retrieve ID of a custom rule.", |
| 14 | + Read: dataSourceCustomRuleRead, |
| 15 | + |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "id": { |
| 18 | + Description: "ID of the custom rule.", |
| 19 | + Type: schema.TypeString, |
| 20 | + Computed: true, |
| 21 | + }, |
| 22 | + "prisma_id": { |
| 23 | + Description: "Prisma Cloud Compute ID of the custom rule.", |
| 24 | + Type: schema.TypeInt, |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + "description": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Optional: true, |
| 30 | + Description: "Free-form text description of the custom rule.", |
| 31 | + }, |
| 32 | + "message": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + Description: "Message to display for a custom rule event.", |
| 36 | + }, |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + Description: "Unique custom rule name.", |
| 41 | + }, |
| 42 | + "script": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "Custom rule expression.", |
| 46 | + }, |
| 47 | + "type": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Custom rule type. Can be set to 'processes', 'filesystem', 'network-outgoing', 'kubernetes-audit', 'waas-request', or 'waas-response'.", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func dataSourceCustomRuleRead(d *schema.ResourceData, meta interface{}) error { |
| 57 | + client := meta.(*pcc.Client) |
| 58 | + |
| 59 | + if name := d.Get("name").(string); name != "" { |
| 60 | + retrievedCustomRule, err := rule.GetCustomRuleByName(*client, name) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 63 | + } |
| 64 | + if err := d.Set("description", retrievedCustomRule.Description); err != nil { |
| 65 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 66 | + } |
| 67 | + if err := d.Set("prisma_id", retrievedCustomRule.Id); err != nil { |
| 68 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 69 | + } |
| 70 | + if err := d.Set("message", retrievedCustomRule.Message); err != nil { |
| 71 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 72 | + } |
| 73 | + if err := d.Set("name", retrievedCustomRule.Name); err != nil { |
| 74 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 75 | + } |
| 76 | + if err := d.Set("script", retrievedCustomRule.Script); err != nil { |
| 77 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 78 | + } |
| 79 | + if err := d.Set("type", retrievedCustomRule.Type); err != nil { |
| 80 | + return fmt.Errorf("error reading custom rule: %s", err) |
| 81 | + } |
| 82 | + d.SetId(retrievedCustomRule.Name) |
| 83 | + |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + return fmt.Errorf("Missing name parameter") |
| 88 | +} |
0 commit comments