Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #286 by forcing new a saved search resource if the name changes… #287

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ $ $GOPATH/bin/terraform-provider-prismacloud
...
```

Since [vendoring](https://go.dev/ref/mod#vendoring) is used, make sure to execute `go mod vendor` as needed. If the `vendor` directory is not up-to-date, the `make build` command may return error messages containing `import lookup disabled by -mod=vendor`.

To test the provider, you can run `make test`.

```sh
Expand Down
8 changes: 6 additions & 2 deletions docs/resources/policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ resource "prismacloud_policy" "example" {
rule_type = "Config"
parameters = {
savedSearch = true
withIac = true
withIac = false
}
criteria = file("policies/aks/aks001.rql")
# Since search_id is not computed, make sure to use the id attribute so that terraform will track the dependency
criteria = prismacloud_saved_search.example.id
}
}

Expand All @@ -104,6 +105,9 @@ resource "prismacloud_saved_search" "example" {
amount = prismacloud_rql_search.example.time_range.0.relative.0.amount
}
}
lifecycle {
create_before_destroy = true
}
}

resource "prismacloud_rql_search" "example" {
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/rql_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ The following attributes are supported:

`exceptions` supports the following attributes:

* `message_code` - Message code.
* `message_code` - Message code.
6 changes: 5 additions & 1 deletion docs/resources/saved_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ resource "prismacloud_saved_search" "example" {
amount = prismacloud_rql_search.x.time_range.0.relative.0.amount
}
}
lifecycle {
# Dependent resources need to be updated before destroy happens
create_before_destroy = true
}
}

resource "prismacloud_rql_search" "x" {
Expand Down Expand Up @@ -82,4 +86,4 @@ Resources can be imported using the saved-search ID:

```
$ terraform import prismacloud_saved_search.example 11111111-2222-3333-4444-555555555555
```
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ require (

//replace github.com/paloaltonetworks/prisma-cloud-go => ../prisma-cloud-go

go 1.17
go 1.18
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 h1:Y7NOhdqIOU8kYI7BxsgL38d0ot0raxvcW+EMQU2QrT4=
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
4 changes: 2 additions & 2 deletions prismacloud/data_source_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ func dataSourcePolicy() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"operation": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
},
"payload": {
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions prismacloud/resource_login_ip_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func updateLoginIpStatus(ctx context.Context, d *schema.ResourceData, meta inter
return readLoginIpStatus(ctx, d, meta)
}

//done
// done
func readLoginIpStatus(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*pc.Client)

Expand All @@ -85,7 +85,7 @@ func readLoginIpStatus(ctx context.Context, d *schema.ResourceData, meta interfa
return nil
}

//done
// done
func deleteLoginIpStatus(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
12 changes: 6 additions & 6 deletions prismacloud/resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ func resourcePolicy() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"operation": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
},
"payload": {
Type: schema.TypeString,
Expand Down Expand Up @@ -532,8 +532,8 @@ func parsePolicy(d *schema.ResourceData, id string) policy.Policy {
actions := make([]policy.Action, 0, len(rems["actions"].([]interface{})))
for _, action := range rems["actions"].([]interface{}) {
actions = append(actions, policy.Action{
Operation: action.(map[string]interface{})["operation"].(string),
Payload: action.(map[string]interface{})["payload"].(string),
Operation: action.(map[string]interface{})["operation"].(string),
Payload: action.(map[string]interface{})["payload"].(string),
})
}
ans.Remediation.Actions = actions
Expand Down Expand Up @@ -678,7 +678,7 @@ func savePolicy(d *schema.ResourceData, obj policy.Policy) {
csjs = string(b)
}
var actions []map[string]string
if obj.Remediation.Actions != nil && len(obj.Remediation.Actions) > 0 {
if obj.Remediation.Actions != nil && len(obj.Remediation.Actions) > 0 {
actions = make([]map[string]string, len(obj.Remediation.Actions))
for i, action := range obj.Remediation.Actions {
actions[i] = map[string]string{
Expand All @@ -694,7 +694,7 @@ func savePolicy(d *schema.ResourceData, obj policy.Policy) {
"actions": actions,
"cli_script_json_schema_string": csjs,
}
if err := d.Set("remediation", []interface{}{rem} ); err != nil {
if err := d.Set("remediation", []interface{}{rem}); err != nil {
log.Printf("[WARN] Error setting 'remediation' for %q: %s", d.Id(), err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion prismacloud/resource_rql_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func createUpdateRqlSearch(ctx context.Context, d *schema.ResourceData, meta int
func readRqlSearch(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*pc.Client)
_, _, searchId := parseRqlSearchId(d.Id())
query := d.Get("query").(string)
query := d.Get("query").(string)
searchType := d.Get("search_type").(string)
limit := d.Get("limit").(int)
tr := ParseTimeRange(ResourceDataInterfaceMap(d, "time_range"))
Expand Down
19 changes: 10 additions & 9 deletions prismacloud/resource_saved_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func resourceSavedSearch() *schema.Resource {
Type: schema.TypeString,
Required: true,
Description: "Saved search name",
// Cannot be updated since the API copies the search and does not delete the original.
// ForceNew avoids cyclical issues and behaves more predictably for users (copy & delete).
// Assumes user sets lifecycle create_before_destroy to true
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -92,21 +96,18 @@ func createSavedSearch(ctx context.Context, d *schema.ResourceData, meta interfa
resp1 = resp2
return err
})

d.SetId(resp1.Id)

return readSavedSearch(ctx, d, meta)
}

func updateSavedSearch(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*pc.Client)
old, new := d.GetChange("name")
if old.(string) != new.(string) {
return diag.Errorf("saved search name is immutable")
}

// Note, search_id is ignored for updates
id := d.Id()
req := history.SavedSearch{
Id: d.Get("search_id").(string),
Id: id,
Saved: true,
Name: d.Get("name").(string),
Query: d.Get("query").(string),
Description: d.Get("description").(string),
Expand All @@ -125,8 +126,8 @@ func updateSavedSearch(ctx context.Context, d *schema.ResourceData, meta interfa
resp1 = resp2
return err
})

d.SetId(resp1.Id)
// Any changes that result in a new Id, such as updating name, should force new
d.SetId(resp1.Id) // noop

return readSavedSearch(ctx, d, meta)
}
Expand Down