client

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package client implements a client to the Splunk REST API.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ACL

type ACL struct {
	Permissions Permissions                 `json:"perms"   values:"perms"`
	Owner       attributes.Explicit[string] `json:"owner"   values:"owner,omitzero"`
	Sharing     Sharing                     `json:"sharing" values:"sharing,omitzero"`
}

ACL represents the ACL of a Splunk object.

type Authenticator

type Authenticator interface {
	AuthenticateRequest(*Client, *http.Request) error
}

Authenticators are capable of adding authentication to requests.

type Client

type Client struct {
	// URL is the URL to the Splunk REST API. It should include the scheme and port number.
	//
	// Example:
	//   https://localhost:8089
	URL string

	// Authenticator defines which authentication method and credentials to use.
	//
	// Example:
	//   authenticators.Password{Username: "admin", Password: "changeme"}
	Authenticator

	// Set TLSInsecureSkipVerify to true to skip TLS verification.
	TLSInsecureSkipVerify bool

	// Timeout configures the timeout of requests. If unspecified, defaults to 5 minutes.
	Timeout time.Duration
	// contains filtered or unexported fields
}

Client defines connectivity and authentication to a Splunk REST API.

Example (PasswordAuth)
package main

import (
	"github.com/splunk/go-splunk-client/pkg/authenticators"
	"github.com/splunk/go-splunk-client/pkg/client"
)

func main() {
	_ = client.Client{
		URL: "https://splunk.example.com:8089",
		Authenticator: &authenticators.Password{
			Username: "admin",
			Password: "changeme",
		},
	}
}
Output:

Example (SessionKeyAuth)
package main

import (
	"github.com/splunk/go-splunk-client/pkg/authenticators"
	"github.com/splunk/go-splunk-client/pkg/client"
)

func main() {
	_ = client.Client{
		URL: "https://splunk.example.com:8089",
		Authenticator: &authenticators.SessionKey{
			SessionKey: "my_session_key",
		},
		TLSInsecureSkipVerify: false,
	}
}
Output:

func (*Client) Create

func (client *Client) Create(entry interface{}) error

Create performs a Create action for the given Entry.

func (*Client) Delete

func (client *Client) Delete(entry interface{}) error

Delete performs a Delete action for the given Entry.

func (*Client) EntryACLURL

func (c *Client) EntryACLURL(e interface{}) (*url.URL, error)

func (*Client) EntryURL

func (c *Client) EntryURL(e interface{}) (*url.URL, error)

EntryURL returns a url.URL for an Entry, relative to the Client's URL.

func (*Client) List

func (client *Client) List(entries interface{}) error

ListNamespace populates entries in place without any ID or Namespace context.

func (*Client) ListID

func (client *Client) ListID(entries interface{}, id ID) error

ListNamespace populates entries in place for an ID.

func (*Client) ListNamespace

func (client *Client) ListNamespace(entries interface{}, ns Namespace) error

ListNamespace populates entries in place for a Namespace.

func (*Client) Read

func (client *Client) Read(entry interface{}) error

Read performs a Read action for the given Entry. It modifies entry in-place, so entry must be a pointer.

func (*Client) ReadACL

func (client *Client) ReadACL(entry interface{}, acl *ACL) error

ReadACL performs a ReadACL action for the given Entry. It modifies acl in-place, so acl must be a pointer.

func (*Client) RequestAndHandle

func (c *Client) RequestAndHandle(builder RequestBuilder, handler ResponseHandler) error

RequestAndHandle creates a new http.Request from the given RequestBuilder, performs the request, and handles the http.Response with the given ResponseHandler.

func (*Client) ServiceURL

func (c *Client) ServiceURL(s interface{}) (*url.URL, error)

ServiceURL returns a url.URL for a Service, relative to the Client's URL.

func (*Client) Update

func (client *Client) Update(entry interface{}) error

Update performs an Update action for the given Entry.

func (*Client) UpdateACL

func (client *Client) UpdateACL(entry interface{}, acl ACL) error

UpdateACL performs an UpdateACL action for the given Entry.

type ConfID

type ConfID struct {
	Namespace Namespace

	File   string
	Stanza string
}

ConfID represents the ID of configs/conf-<file> resources.

func (ConfID) GetEntryPath

func (confID ConfID) GetEntryPath(path string) (string, error)

GetEntryPath implements custom GetEntryPath encoding.

func (ConfID) GetServicePath

func (confID ConfID) GetServicePath(path string) (string, error)

GetServicePath implements custom GetServicePath encoding.

func (*ConfID) Parse

func (confID *ConfID) Parse(idURL string) error

Parse sets the ID's value to match what is parsed from the given ID URL.

func (ConfID) SetURLValues

func (confID ConfID) SetURLValues(key string, v *url.Values) error

SetURLValues implements custom url.Query encoding of ConfID. It adds a field "name" for the ConfID's Stanza. If the Title value is empty, it returns an error, as there are no scenarios where a ConfID object is expected to be POSTed with an empty Stanza.

func (*ConfID) UnmarshalJSON

func (confID *ConfID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for ConfID.

type Error

type Error struct {
	Code       ErrorCode
	Message    string
	Wrapped    error
	StackTrace string
}

Error represents an encountered error. It adheres to the "error" interface, so will be returned as a standard error.

Returned errors can be handled as this Error type:

  if err := c.RequestAndHandle(...); err != nil {
	  if clientErr, ok := err.(client.Error) {
		  // check clientErr.Code to determine appropriate action
	  }
  }

func (Error) Error

func (err Error) Error() string

Error returns the Error's message.

type ErrorCode

type ErrorCode int

ErrorCode identifies the type of error that was encountered.

const (
	// ErrorUndefined is the zero-value, indicating that the error type
	// has not been defined.
	ErrorUndefined ErrorCode = iota

	// ErrorNamespace indicates an error with the Namespace.
	ErrorNamespace

	// ErrorEndpoint indicates an error with the Endpoint configuration.
	ErrorEndpoint

	// ErrorValues indicates an error was encountered while trying to encode
	// to url.Values.
	ErrorValues

	// ErrorNilValue indicates an attempt to perform an action against a nil value,
	// such as attempting to set RawQuery on http.Request with a nil URL.
	ErrorNilValue

	// ErrorOverwriteValue indicates an attempt to overwrite an existing value,
	// such as attempting to set RawQuery multiple times on a URL.
	ErrorOverwriteValue

	// ErrorMissingTitle indicates an operation that required a non-empty Title was
	// attempted with an empty Title.
	ErrorMissingTitle

	// ErrorMissingURL indicates the Client's URL value is missing.
	ErrorMissingURL

	// ErrorHTTPClient indicates an error related to the http.Client was encountered.
	ErrorHTTPClient

	// ErrorResponseBody indicates an error encountered while trying to parse the Body
	// from an http.Response.
	ErrorResponseBody

	// ErrorSplunkMessage indicates the Splunk REST API returned an error message.
	ErrorSplunkMessage

	// ErrorUnauthorized indicates a request was unauthorized.
	ErrorUnauthorized

	// ErrorNotFound indicates an attempt was made against an object that count not be found.
	ErrorNotFound

	// ErrorPtr indicates an operation requiring a pointer was passed a non-pointer.
	ErrorPtr

	// ErrorSlice indicates an operation requiring a slice was passed a non-slice, or a slice
	// of the wrong type.
	ErrorSlice

	// ErrorID indicates an error was encountered related to an object's ID.
	ErrorID

	// ErrorSharing indicates an error was encountered related to a Sharing value.
	ErrorSharing
)

type ID

type ID struct {
	Namespace Namespace

	// Title is the ID's title component. It is the name of the Splunk object.
	Title string
	// contains filtered or unexported fields
}

ID represents a Splunk object ID URL for a specific object.

func ParseID

func ParseID(idURL string) (ID, error)

ParseID returns a new ID by parsing the ID URL string.

func (ID) GetEntryPath

func (id ID) GetEntryPath(path string) (string, error)

GetEntryPath implements custom GetEntryPath encoding. It returns the url-encoded value of the ID's Title with the service path preceding it.

func (ID) GetServicePath

func (id ID) GetServicePath(path string) (string, error)

GetServicePath implements custom GetServicePath encoding. It returns its Namespace's service path.

func (*ID) Parse

func (id *ID) Parse(idURL string) error

Parse sets the ID's value to match what is parsed from the given ID URL.

func (ID) SetURLValues

func (id ID) SetURLValues(key string, v *url.Values) error

SetURLValues implements custom url.Query encoding of ID. It adds a field "name" for the ID's Title. If the Title value is empty, it returns an error, as there are no scenarios where an ID object is expected to be POSTed with an empty Title.

func (ID) URL

func (id ID) URL() (string, error)

URL returns the URL for ID. An error is returned if URL() is run on ID that has no set URL, or if the stored URL doesn't match the ID's fields.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for IDFields.

type Namespace

type Namespace struct {
	User string
	App  string
}

Namespace is a Splunk object's namespace, consisting of a User and App. It is valid only if both User and App are set, or both are unset.

func (Namespace) GetServicePath

func (ns Namespace) GetServicePath(path string) (string, error)

GetServicePath implements custom GetServicePath encoding. It returns the given path back, which has the effect of using the ID field's struct tag as its GetServicePath.

type Permissions

type Permissions struct {
	Read  []string `json:"read"  values:"read,omitzero,fillempty"`
	Write []string `json:"write" values:"write,omitzero,fillempty"`
}

Permissions represents the read/write permissions of a Splunk object.

type RequestBuilder

type RequestBuilder func(*http.Request) error

RequestBuilder defines a function that performs an operation on an http.Request.

func BuildRequestAuthenticate

func BuildRequestAuthenticate(c *Client) RequestBuilder

BuildRequestAuthenticate returns a RequestBuilder that authenticates a request for a given Client.

func BuildRequestBodyValues

func BuildRequestBodyValues(i interface{}) RequestBuilder

BuildRequestBodyValues returns a RequestBuilder that sets the Body to the encoded url.Values for a given interface.

func BuildRequestBodyValuesSelective

func BuildRequestBodyValuesSelective(c interface{}, tag string) RequestBuilder

BuildRequestBodyValuesSelective returns a RequestBuilder that sets the Body to the encoded url.Values for a given interface and selective tag.

func BuildRequestEntryACLURL

func BuildRequestEntryACLURL(c *Client, entry interface{}, acl ACL) RequestBuilder

BuildRequestEntryACLURL returns a RequestBuilder that sets the URL to the ACL URL for a given Entry.

func BuildRequestEntryURL

func BuildRequestEntryURL(c *Client, entry interface{}) RequestBuilder

BuildRequestCollectionURL returns a RequestBuilder that sets the URL to the EntryURL for a given Entry.

func BuildRequestGetServiceStatusCodes

func BuildRequestGetServiceStatusCodes(entry interface{}, codes *service.StatusCodes) RequestBuilder

BuildRequestGetServiceStatusCodes updates codes for the given entry. It returns a RequestBuilder that returns the error (if any) returned by service.ServiceStatusCodes.

func BuildRequestMethod

func BuildRequestMethod(method string) RequestBuilder

BuildRequestMethod returns a RequestBuilder that sets the given method.

func BuildRequestOutputModeJSON

func BuildRequestOutputModeJSON() RequestBuilder

BuildRequestOutputModeJSON returns a RequestBuilder that sets the URL's RawQuery to output_mode=json. It checks that the URL is already set, so it must be applied after setting the URL. It overwrites any existing RawQuery Values.

func BuildRequestServiceURL

func BuildRequestServiceURL(c *Client, service interface{}) RequestBuilder

BuildRequestServiceURL returns a RequestBuilder that sets the URL to the ServiceURL for a given Service.

func ComposeRequestBuilder

func ComposeRequestBuilder(builders ...RequestBuilder) RequestBuilder

ComposeRequestBuilder creates a new RequestBuilder that performs each RequestBuilder provided as an argument, returning the first error encountered, if any.

type ResponseHandler

type ResponseHandler func(*http.Response) error

ResponseHandler defines a function that performs an action on an http.Response.

func ComposeResponseHandler

func ComposeResponseHandler(handlers ...ResponseHandler) ResponseHandler

ComposeResponseHandler creates a new ResponseHandler that runs each ResponseHandler provided as an argument.

func HandleResponseCode

func HandleResponseCode(code int, errorResponseHandler ResponseHandler) ResponseHandler

HandleResponseCode returns a ResponseHandler that calls errorResponseHandler if an http.Response's StatusCode is equal to the provided code.

func HandleResponseEntries

func HandleResponseEntries(entries interface{}) ResponseHandler

HandleResponseEntries returns a ResponseHandler that parses the http.Response Body into the list of Entry reference provided.

func HandleResponseEntry

func HandleResponseEntry(entry interface{}) ResponseHandler

HandleResponseEntry returns a responseHaResponseHandlerndler that parses the http.Response Body into the given Entry.

func HandleResponseJSON

func HandleResponseJSON(i interface{}) ResponseHandler

HandleResponseJSON returns a ResponseHandler that decodes an http.Response's Body as JSON to the given interface.

func HandleResponseJSONMessagesCustomError

func HandleResponseJSONMessagesCustomError(code ErrorCode) ResponseHandler

HandleResponseJSONMessagesCustomError returns a ResponseHandler that decodes an http.Response's Body as JSON document of Messages and returns the Messages as an error with the given ErrorCode.

func HandleResponseJSONMessagesError

func HandleResponseJSONMessagesError() ResponseHandler

HandleResponseJSONMessagesError returns a ResponseHandler that decode's an http.Response's Body as a JSON document of Messages and returns the Messages as an error with the Code ErrorSplunkMessage.

func HandleResponseRequireCode

func HandleResponseRequireCode(code int, errorResponseHandler ResponseHandler) ResponseHandler

HandleResponseRequireCode returns a ResponseHandler that checks for a given StatusCode. If the http.Response has a different StatusCode, the provided ResponseHandler will be called to return the appopriate error message.

func HandleResponseXML

func HandleResponseXML(i interface{}) ResponseHandler

HandleResponseXML returns a ResponseHandler that decodes an http.Response's Body as XML to the given interface.

func HandleResponseXMLMessagesCustomError

func HandleResponseXMLMessagesCustomError(code ErrorCode) ResponseHandler

HandleResponseXMLMessagesCustomError returns a ResponseHandler that decode's an http.Response's Body as an XML document of Messages and returns the Messages as an error with the given ErrorCode.

func HandleResponseXMLMessagesError

func HandleResponseXMLMessagesError() ResponseHandler

HandleResponseXMLMessagesError returns a ResponseHandler that decodes an http.Response's Body as an XML document of Messages and returns the Messages as an error.

type Sharing

type Sharing string

Sharing represents the level of sharing of a Splunk object.

const (
	SharingUndefined Sharing = ""
	SharingGlobal    Sharing = "global"
	SharingUser      Sharing = "user"
	SharingApp       Sharing = "app"
)

func (Sharing) MarshalJSON

func (sharing Sharing) MarshalJSON() ([]byte, error)

MarshalJSON implements custom marshaling.

type Titler

type Titler interface {
	// Title returns the string representation of the object's Title.
	Title() string
}

Titler defines methods that a type must implement to be a titled object.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL