Skip to content

Commit

Permalink
Handle missing SAML groups (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahkemp-splunk authored Sep 29, 2021
1 parent fcb01ce commit d8d7f3d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func (c *Client) DoRequest(method string, requestURL url.URL, body interface{})
if err != nil {
return nil, err
}
return utils.ParseHTTPStatusCodeInResponse(response)

return response, nil
}

func (c *Client) Login() (e error) {
Expand Down
24 changes: 17 additions & 7 deletions splunk/resource_splunk_admin_saml_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package splunk
import (
"encoding/json"
"errors"
"fmt"
"github.com/splunk/terraform-provider-splunk/client/models"
"net/http"
"regexp"
Expand Down Expand Up @@ -70,8 +69,11 @@ func adminSAMLGroupsRead(d *schema.ResourceData, meta interface{}) error {
return err
}

// an empty entry (with no error) means the resource wasn't found
// mark it as such so it can be re-created
if entry == nil {
return fmt.Errorf("Unable to find resource: %v", name)
d.SetId("")
return nil
}

if err = d.Set("name", entry.Name); err != nil {
Expand Down Expand Up @@ -130,21 +132,29 @@ func getAdminSAMLGroupsConfig(d *schema.ResourceData) (adminSAMLGroupsObject *mo

func getAdminSAMLGroupsByName(name string, httpResponse *http.Response) (AdminSAMLGroupsEntry *models.AdminSAMLGroupsEntry, err error) {
response := &models.AdminSAMLGroupsResponse{}
_ = json.NewDecoder(httpResponse.Body).Decode(response)

switch httpResponse.StatusCode {
case 200, 201:
_ = json.NewDecoder(httpResponse.Body).Decode(&response)
re := regexp.MustCompile(`(.*)`)
for _, entry := range response.Entry {
if name == re.FindStringSubmatch(entry.Name)[1] {
return &entry, nil
}
}

default:
_ = json.NewDecoder(httpResponse.Body).Decode(response)
case 400:
// Splunk returns a 400 when a SAML group mapping is not found
// try to catch that here
re := regexp.MustCompile("Unable to find a role mapping for")
if re.MatchString(response.Messages[0].Text) {
return nil, nil
}

// but if the error didn't match, don't assume the 400 status was just a missing resource
err := errors.New(response.Messages[0].Text)
return AdminSAMLGroupsEntry, err
return nil, err
}

return AdminSAMLGroupsEntry, nil
return nil, errors.New(response.Messages[0].Text)
}
7 changes: 7 additions & 0 deletions splunk/resource_splunk_admin_saml_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func TestAccSplunkAdminSAMLGroups(t *testing.T) {
),
},
{
// to test re-creation of remotely deleted or missing resources, delete the new saml group before updating it
PreConfig: func() {
client, _ := newTestClient()
if _, err := client.DeleteAdminSAMLGroups("new-saml-group"); err != nil {
t.Error("PreConfig deletion of new-saml-group failed")
}
},
Config: updateAdminSAMLGroupsInput,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "new-saml-group"),
Expand Down

0 comments on commit d8d7f3d

Please sign in to comment.