Skip to content

Commit

Permalink
Composable CRUD functions, SAML external client
Browse files Browse the repository at this point in the history
  • Loading branch information
micahkemp-splunk committed Jun 3, 2022
1 parent b15c4fe commit cc01567
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/resources/admin_saml_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ For latest resource argument reference: https://docs.splunk.com/Documentation/Sp
This resource block supports the following arguments:
* `name` - (Required) The name of the external group.
* `roles` - (Required) List of internal roles assigned to the group.
* `use_legacy_client` - (Default true) Set to explicitly specify which client to use for this resource. Leave unset to use the provider's default.
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.

## Attribute Reference
In addition to all arguments above, This resource block exports the following arguments:
Expand Down
162 changes: 162 additions & 0 deletions splunk/entry_crud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2022 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package splunk

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/splunk/go-splunk-client/pkg/client"
"github.com/splunk/terraform-provider-splunk/internal/sync"
)

// useLegacyClient returns:
//
// * the resource's use_legacy_client value, if false or explicitly true
//
// * the provider's use_legacy_client_default value
func useLegacyClient(provider *SplunkProvider, d *schema.ResourceData) bool {
resourceLegacyClientI, ok := d.GetOk("use_legacy_client")
resourceLegacyClient := resourceLegacyClientI.(bool)
// GetOk only returns true if the fetched value is not the zero value for its type,
// so we can only determine if use_legacy_client was explicitly true. but because
// true is our default value, we know that it can only be false if explicitly set.
if ok || !resourceLegacyClient {
return resourceLegacyClient
}

return provider.useLegacyClientDefault
}

// createFunc returns a schema.CreateFunc for the Sync returned by the given function.
//
// During the transition between the legacy and external Splunk clients, it will return legacyFunction
// if the provider configuration or the resource configuration sets use_legacy_client=true.
func createFunc(syncFn func() sync.SyncGetter, legacyFunction schema.CreateFunc) schema.CreateFunc {
return func(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)

if useLegacyClient(provider, d) {
return legacyFunction(d, meta)
}

c := provider.ExternalClient

s := syncFn()

if err := s.SyncObject(d); err != nil {
return err
}

if err := c.Create(s.GetObject()); err != nil {
return err
}

return readFunc(syncFn, legacyFunction)(d, meta)
}
}

// readFunc returns a schema.CreateFunc for the Sync returned by the given function.
//
// During the transition between the legacy and external Splunk clients, it will return legacyFunction
// if the provider configuration or the resource configuration sets use_legacy_client=true.
func readFunc(syncFn func() sync.SyncGetter, legacyFunction schema.CreateFunc) schema.ReadFunc {
return func(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)

if useLegacyClient(provider, d) {
return legacyFunction(d, meta)
}

c := provider.ExternalClient

s := syncFn()

if err := s.SyncObject(d); err != nil {
return err
}

if err := c.Read(s.GetObject()); err != nil {
if clientErr, ok := err.(client.Error); ok {
if clientErr.Code == client.ErrorNotFound {
d.SetId("")

return nil
}
}
return err
}

if err := s.SyncResource(d); err != nil {
return err
}

return nil
}
}

// updateFunc returns a schema.UpdateFunc for the Sync returned by the given function.
//
// During the transition between the legacy and external Splunk clients, it will return legacyFunction
// if the provider configuration or the resource configuration sets use_legacy_client=true.
func updateFunc(syncFn func() sync.SyncGetter, legacyFunction schema.CreateFunc) schema.UpdateFunc {
return func(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)

if useLegacyClient(provider, d) {
return legacyFunction(d, meta)
}

c := provider.ExternalClient

s := syncFn()

if err := s.SyncObject(d); err != nil {
return err
}

if err := c.Update(s.GetObject()); err != nil {
return err
}

return readFunc(syncFn, legacyFunction)(d, meta)
}
}

// deleteFunc returns a schema.DeleteFunc for the Sync returned by the given function.
//
// During the transition between the legacy and external Splunk clients, it will return legacyFunction
// if the provider configuration or the resource configuration sets use_legacy_client=true.
func deleteFunc(syncFn func() sync.SyncGetter, legacyFunction schema.CreateFunc) schema.DeleteFunc {
return func(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)

if useLegacyClient(provider, d) {
return legacyFunction(d, meta)
}

c := provider.ExternalClient

s := syncFn()

if err := s.SyncObject(d); err != nil {
return err
}

if err := c.Delete(s.GetObject()); err != nil {
return err
}

return nil
}
}
41 changes: 33 additions & 8 deletions splunk/resource_splunk_admin_saml_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package splunk
import (
"encoding/json"
"errors"
"github.com/splunk/terraform-provider-splunk/client/models"
"net/http"
"regexp"

"github.com/splunk/go-splunk-client/pkg/entry"
"github.com/splunk/terraform-provider-splunk/client/models"
"github.com/splunk/terraform-provider-splunk/internal/sync"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand All @@ -28,17 +31,37 @@ 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. " +
"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.",
},
},
Read: adminSAMLGroupsRead,
Create: adminSAMLGroupsCreate,
Delete: adminSAMLGroupsDelete,
Update: adminSAMLGroupsUpdate,
Read: readFunc(samlGroupSync, adminSAMLGroupsRead),
Create: createFunc(samlGroupSync, adminSAMLGroupsCreate),
Delete: deleteFunc(samlGroupSync, adminSAMLGroupsDelete),
Update: updateFunc(samlGroupSync, adminSAMLGroupsUpdate),
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

// samlGroupSync returns a SyncGetter that manages entry.SAMLGroup.
func samlGroupSync() sync.SyncGetter {
var group entry.SAMLGroup

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

return sync.NewIndirectObject(&group, samlGroupSync)
}

// Functions
func adminSAMLGroupsCreate(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)
Expand All @@ -55,7 +78,7 @@ func adminSAMLGroupsCreate(d *schema.ResourceData, meta interface{}) error {

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

// Read the SAML group
resp, err := (*provider.Client).ReadAdminSAMLGroups(name)
Expand All @@ -74,6 +97,8 @@ 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 All @@ -90,7 +115,7 @@ func adminSAMLGroupsRead(d *schema.ResourceData, meta interface{}) error {
func adminSAMLGroupsUpdate(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)
adminSAMLGroupsObj := getAdminSAMLGroupsConfig(d)
err := (*provider.Client).UpdateAdminSAMLGroups(d.Id(), adminSAMLGroupsObj)
err := (*provider.Client).UpdateAdminSAMLGroups(d.Get("name").(string), adminSAMLGroupsObj)
if err != nil {
return err
}
Expand All @@ -100,7 +125,7 @@ func adminSAMLGroupsUpdate(d *schema.ResourceData, meta interface{}) error {

func adminSAMLGroupsDelete(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*SplunkProvider)
resp, err := (*provider.Client).DeleteAdminSAMLGroups(d.Id())
resp, err := (*provider.Client).DeleteAdminSAMLGroups(d.Get("name").(string))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cc01567

Please sign in to comment.