-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composable CRUD functions, SAML external client
- Loading branch information
1 parent
b15c4fe
commit cc01567
Showing
4 changed files
with
319 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.