Skip to content

Commit

Permalink
fixup resource_splunk_admin_saml_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
micahkemp-splunk committed Jun 23, 2022
1 parent c7942e3 commit ab80dd7
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions splunk/resource_splunk_admin_saml_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"regexp"

"github.com/splunk/go-splunk-client/pkg/client"
"github.com/splunk/go-splunk-client/pkg/entry"
"github.com/splunk/terraform-provider-splunk/client/models"
"github.com/splunk/terraform-provider-splunk/internal/sync"
Expand All @@ -31,11 +32,11 @@ func adminSAMLGroups() *schema.Resource {
},
Description: "Required. List of internal roles assigned to group.",
},
"use_legacy_client": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Set to explicitly specify which client to use for this resource. Leave unset to use the provider's default. " +
"use_client": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateClientValueFunc(true),
Description: "Set to explicitly specify which client to use for this resource. Leave unset to use the provider's default. Permitted non-empty values are legacy and external. " +
"The legacy client is being replaced by a standalone Splunk client with improved error and drift handling. The legacy client will be deprecated in a future version.",
},
},
Expand All @@ -44,7 +45,7 @@ func adminSAMLGroups() *schema.Resource {
Delete: deleteFunc(samlGroupSync, adminSAMLGroupsDelete),
Update: updateFunc(samlGroupSync, adminSAMLGroupsUpdate),
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: importStateSAMLGroups,
},
}
}
Expand All @@ -54,8 +55,7 @@ func samlGroupSync() sync.SyncGetter {
var group entry.SAMLGroup

samlGroupSync := sync.ComposeSync(
sync.NewClientID(&group.ID),
sync.NewDirectField("name", &group.ID.Title),
sync.NewClientID(&group.ID, "name"),
sync.NewDirectListField("roles", &group.Content.Roles),
)

Expand All @@ -78,7 +78,15 @@ func adminSAMLGroupsCreate(d *schema.ResourceData, meta interface{}) error {

func adminSAMLGroupsRead(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)
name := d.Get("name").(string)

// name defaults to the stored Id, unless it's a parseable client.ID
name := d.Id()
if id, err := client.ParseID(name); err == nil {
// if d.Id is a parseable client.ID, this was most recently using the non-legacy client
// restore the internal workings back to the legacy client format
name = id.Title
d.SetId(name)
}

// Read the SAML group
resp, err := (*provider.Client).ReadAdminSAMLGroups(name)
Expand All @@ -97,8 +105,6 @@ func adminSAMLGroupsRead(d *schema.ResourceData, meta interface{}) error {
if entry == nil {
d.SetId("")
return nil
} else {
d.SetId(name)
}

if err = d.Set("name", entry.Name); err != nil {
Expand Down Expand Up @@ -183,3 +189,17 @@ func getAdminSAMLGroupsByName(name string, httpResponse *http.Response) (AdminSA

return nil, errors.New(response.Messages[0].Text)
}

// importStateSAMLGroups calls schema.ImportStatePassthrough after setting use_client based on the ID's
// ability to be parsed into a client.ID.
func importStateSAMLGroups(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
if _, err := client.ParseID(d.Id()); err == nil {
if err := d.Set("use_client", useClientExternal); err != nil {
return nil, err
}
} else {
d.Set("use_client", useClientLegacy)
}

return schema.ImportStatePassthrough(d, m)
}

0 comments on commit ab80dd7

Please sign in to comment.