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

Fix error checking in ACL resources #167

Merged
Merged
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
10 changes: 7 additions & 3 deletions consul/resource_consul_acl_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"fmt"
"log"
"strings"

consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -84,9 +85,12 @@ func resourceConsulACLPolicyRead(d *schema.ResourceData, meta interface{}) error

aclPolicy, _, err := client.ACL().PolicyRead(id, nil)
if err != nil {
log.Printf("[WARN] ACL policy not found, removing from state")
d.SetId("")
return nil
if strings.Contains(err.Error(), "ACL not found") {
log.Printf("[INFO] ACL policy not found, removing from state")
d.SetId("")
return nil
}
return fmt.Errorf("Failed to read policy '%s': %v", id, err)
}

log.Printf("[DEBUG] Read ACL policy %q", id)
Expand Down
10 changes: 7 additions & 3 deletions consul/resource_consul_acl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"fmt"
"log"
"strings"

consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -93,9 +94,12 @@ func resourceConsulACLTokenRead(d *schema.ResourceData, meta interface{}) error

aclToken, _, err := client.ACL().TokenRead(id, nil)
if err != nil {
log.Printf("[WARN] ACL token not found, removing from state")
d.SetId("")
return nil
if strings.Contains(err.Error(), "ACL not found") {
log.Printf("[WARN] ACL token not found, removing from state")
d.SetId("")
return nil
}
return fmt.Errorf("Failed to read token '%s': %v", id, err)
}

log.Printf("[DEBUG] Read ACL token %q", id)
Expand Down
9 changes: 6 additions & 3 deletions consul/resource_consul_acl_token_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ func resourceConsulACLTokenPolicyAttachmentRead(d *schema.ResourceData, meta int

aclToken, _, err := client.ACL().TokenRead(tokenID, nil)
if err != nil {
log.Printf("[WARN] ACL token not found, removing from state")
d.SetId("")
return nil
if strings.Contains(err.Error(), "ACL not found") {
log.Printf("[WARN] ACL token not found, removing from state")
d.SetId("")
return nil
}
return fmt.Errorf("Failed to read token '%s': %v", id, err)
}

log.Printf("[DEBUG] Read ACL token %q", tokenID)
Expand Down