-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix issue #214 * Add see also note for cft_template data source * Comment out most changes in splunk integration * Check if this makes CI build pass * Run pre-commit * Add unit tests for integrations_data * Remove useless constant in unit tests * Make getValue exported func * Fix docs Co-authored-by: Victor Moraes <[email protected]>
- Loading branch information
Showing
6 changed files
with
153 additions
and
41 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,56 @@ | ||
package cyral | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
integrationTypeSplunk = "splunk" | ||
) | ||
|
||
type IntegrationsData struct { | ||
Id string `json:"id"` | ||
Type string `json:"type"` | ||
Name string `json:"name"` | ||
Label string `json:"label"` | ||
Value interface{} `json:"value"` | ||
} | ||
|
||
func NewDefaultIntegrationsData() *IntegrationsData { | ||
return &IntegrationsData{ | ||
Id: "id", | ||
Type: "default", | ||
Name: "default", | ||
Label: "default", | ||
Value: "default", | ||
} | ||
} | ||
|
||
func (isd *IntegrationsData) GetValue() (string, error) { | ||
var err error | ||
defer func() { | ||
if err != nil { | ||
err = fmt.Errorf("unable to get integration value for "+ | ||
"type '%s': %w", isd.Type, err) | ||
} | ||
}() | ||
switch isd.Type { | ||
case integrationTypeSplunk: | ||
if value, ok := isd.Value.(SplunkIntegration); ok { | ||
bytesval, jsonErr := json.Marshal(value) | ||
if jsonErr == nil { | ||
return string(bytesval), nil | ||
} | ||
err = fmt.Errorf("error marshalling splunk "+ | ||
"integration: %w", jsonErr) | ||
} | ||
default: | ||
if value, ok := isd.Value.(string); ok { | ||
return value, nil | ||
} | ||
err = errors.New("value is not of string type") | ||
} | ||
return "", err | ||
} |
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,54 @@ | ||
package cyral | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func sampleSplunkIntegrationsData() *IntegrationsData { | ||
return &IntegrationsData{ | ||
Id: "id1", | ||
Type: "splunk", | ||
Name: "name1", | ||
Label: "label1", | ||
Value: SplunkIntegration{ | ||
Name: "name1", | ||
AccessToken: "accessToken1", | ||
Port: 0, | ||
Host: "host1", | ||
Index: "index1", | ||
UseTLS: false, | ||
CyralActivityLogsEnabled: false, | ||
}, | ||
} | ||
} | ||
|
||
func TestIntegrationsData_GetValue_Default(t *testing.T) { | ||
integrationsData := NewDefaultIntegrationsData() | ||
expected := NewDefaultIntegrationsData().Value.(string) | ||
actual, err := integrationsData.GetValue() | ||
require.NoError(t, err) | ||
require.Equal(t, expected, actual) | ||
} | ||
|
||
func TestIntegrationsData_GetValue_Splunk(t *testing.T) { | ||
splunkIntegrationsData := sampleSplunkIntegrationsData() | ||
|
||
expectedBytes, err := json.Marshal(SplunkIntegration{ | ||
Name: "name1", | ||
AccessToken: "accessToken1", | ||
Port: 0, | ||
Host: "host1", | ||
Index: "index1", | ||
UseTLS: false, | ||
CyralActivityLogsEnabled: false, | ||
}) | ||
require.NoError(t, err) | ||
expected := string(expectedBytes) | ||
actual, err := splunkIntegrationsData.GetValue() | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, expected, actual) | ||
} |
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
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