From ab80dd7b2f4a1702b29105ed17fba9f06947f572 Mon Sep 17 00:00:00 2001 From: Micah Kemp Date: Thu, 23 Jun 2022 15:42:34 -0500 Subject: [PATCH] fixup resource_splunk_admin_saml_groups --- splunk/resource_splunk_admin_saml_groups.go | 42 +++++++++++++++------ 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/splunk/resource_splunk_admin_saml_groups.go b/splunk/resource_splunk_admin_saml_groups.go index a6eb947e..12f1c790 100644 --- a/splunk/resource_splunk_admin_saml_groups.go +++ b/splunk/resource_splunk_admin_saml_groups.go @@ -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" @@ -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.", }, }, @@ -44,7 +45,7 @@ func adminSAMLGroups() *schema.Resource { Delete: deleteFunc(samlGroupSync, adminSAMLGroupsDelete), Update: updateFunc(samlGroupSync, adminSAMLGroupsUpdate), Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: importStateSAMLGroups, }, } } @@ -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), ) @@ -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) @@ -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 { @@ -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) +}