util

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2016 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertURLsToStrings

func ConvertURLsToStrings(urls []*url.URL) []string

ConvertURLsToStrings converts a slice of *url.URL to a slice of string.

func IsHTTPURL

func IsHTTPURL(s string) bool

IsHTTPURL returns true if a string is an HTTP URL.

func LogAndReturnError

func LogAndReturnError(handler string, statusCode int, err error, w http.ResponseWriter)

LogAndReturnError logs the given error and status to stderr, and then returns them as the HTTP response.

func LogHandlerEntry

func LogHandlerEntry(handler string, r *http.Request)

LogHandlerEntry logs the start of the given handler handling the given request.

func LogHandlerExit

func LogHandlerExit(handler string, statusCode int, status string, w http.ResponseWriter)

LogHandlerExit logs the response from the given handler with the given results.

func LogHandlerExitWithJSON

func LogHandlerExitWithJSON(handler string, w http.ResponseWriter, v interface{}, statusCode int)

LogHandlerExitWithJSON marshals the given object as JSON, writes it to the response body, returns the given status, and then logs the response.

func LogHandlerExitWithText

func LogHandlerExitWithText(handler string, w http.ResponseWriter, v string, statusCode int)

LogHandlerExitWithText converts the given string to []byte, writes it to the response body, returns the given status, and then logs the response

func LogHandlerExitWithYAML

func LogHandlerExitWithYAML(handler string, w http.ResponseWriter, v interface{}, statusCode int)

LogHandlerExitWithYAML marshals the given object as YAML, writes it to the response body, returns the given status, and then logs the response.

func LogHandlerText

func LogHandlerText(handler string, v string)

func MarshalAndWriteJSON

func MarshalAndWriteJSON(handler string, w http.ResponseWriter, v interface{}, statusCode int) []byte

MarshalAndWriteJSON marshals the given object as JSON, writes it to the response body, and then returns the given status.

func MarshalAndWriteYAML

func MarshalAndWriteYAML(handler string, w http.ResponseWriter, v interface{}, statusCode int) []byte

MarshalAndWriteYAML marshals the given object as YAML, writes it to the response body, and then returns the given status.

func NewTemplateFromArchive

func NewTemplateFromArchive(name string, r io.Reader, importFileNames []string) (*common.Template, error)

NewTemplateFromArchive creates and returns a new template whose content and imported files are read from the supplied archive.

func NewTemplateFromFileNames

func NewTemplateFromFileNames(
	templateFileName string,
	importFileNames []string,
) (*common.Template, error)

NewTemplateFromFileNames creates and returns a new template whose content and imported files are read from the supplied file names.

func NewTemplateFromReader

func NewTemplateFromReader(name string, r io.Reader, importFileNames []string) (*common.Template, error)

NewTemplateFromReader creates and returns a new template whose content is read from the supplied reader.

func NewTemplateFromRootTemplate

func NewTemplateFromRootTemplate(templateFileName string) (*common.Template, error)

NewTemplateFromRootTemplate creates and returns a new template whose content and imported files are constructed from reading the root template, parsing out the imports section and reading the imports from there

func NewTemplateFromType

func NewTemplateFromType(name, typeName string, properties map[string]interface{}) (*common.Template, error)

NewTemplateFromType creates and returns a new template whose content is a YAML marshaled resource assembled from the supplied arguments.

func ParseKubernetesObject

func ParseKubernetesObject(object []byte) (*common.Resource, error)

ParseKubernetesObject parses a Kubernetes API object in YAML format.

func TestHandlerWithURL

func TestHandlerWithURL(handler http.Handler, method, urlString string) (*httptest.ResponseRecorder, error)

TestHandlerWithURL creates a HandlerTester with the given handler, and tests it with the given HTTP method and URL string using HandlerTester.TestWithURL.

func ToJSONOrError

func ToJSONOrError(v interface{}) string

ToJSONOrError marshals the given object to JSON and returns either the resulting YAML or an error message. Useful when marshaling an object for a log entry.

func ToYAMLOrError

func ToYAMLOrError(v interface{}) string

ToYAMLOrError marshals the given object to YAML and returns either the resulting YAML or an error message. Useful when marshaling an object for a log entry.

func TrimURLScheme

func TrimURLScheme(URL string) string

TrimURLScheme removes the scheme, if any, from an URL.

func WriteJSON

func WriteJSON(handler string, w http.ResponseWriter, j []byte, status int)

WriteJSON writes the given bytes to the response body, sets the content type to "application/json; charset=UTF-8", and then returns the given status.

func WriteResponse

func WriteResponse(handler string, w http.ResponseWriter, v []byte, ct string, status int)

WriteResponse writes the given bytes to the response body, sets the content type to the given value, and then returns the given status.

func WriteYAML

func WriteYAML(handler string, w http.ResponseWriter, y []byte, status int)

WriteYAML writes the given bytes to the response body, sets the content type to "application/x-yaml; charset=UTF-8", and then returns the given status.

Types

type HTTPClient

type HTTPClient interface {
	Get(url string) (body string, code int, err error)
}

HTTPClient is a higher level HTTP client which takes a URL and returns the response body as a string, along with the resulting status code and any errors.

func DefaultHTTPClient

func DefaultHTTPClient() HTTPClient

DefaultHTTPClient returns a default http client.

func NewHTTPClient

func NewHTTPClient(retries uint, c HTTPDoer, s Sleeper) HTTPClient

NewHTTPClient returns a new HTTPClient.

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (resp *http.Response, err error)
}

HTTPDoer is an interface for something that can 'Do' an http.Request and return an http.Response and error.

type HandlerTester

type HandlerTester func(method, path, ctype string, reader io.Reader) (*httptest.ResponseRecorder, error)

A HandlerTester is a function that takes an HTTP method, an URL path, and a reader for a request body, creates a request from them, and serves it to the handler to which it was bound and returns a response recorder describing the outcome.

func NewHandlerTester

func NewHandlerTester(handler http.Handler) HandlerTester

NewHandlerTester creates and returns a new HandlerTester for an http.Handler.

func (HandlerTester) TestWithURL

func (h HandlerTester) TestWithURL(method, urlString string) (*httptest.ResponseRecorder, error)

TestWithURL invokes a HandlerTester with the given HTTP method, an URL path parsed from the given URL string, and a string reader on the query parameters parsed from the given URL string.

type Kubernetes

type Kubernetes interface {
	Get(name string, resourceType string) (string, error)
	Create(resource string) (string, error)
	Delete(resource string) (string, error)
	Replace(resource string) (string, error)
}

Kubernetes defines the interface for talking to Kubernetes. Currently the only implementation is through kubectl, but eventually this could be done via direct API calls.

func NewKubernetesKubectl

func NewKubernetesKubectl(config *KubernetesConfig) Kubernetes

NewKubernetesKubectl creates a new Kubernetes kubectl wrapper.

type KubernetesConfig

type KubernetesConfig struct {
	KubePath       string // The path to kubectl binary
	KubeService    string // DNS name of the kubernetes service
	KubeServer     string // The IP address and optional port of the kubernetes master
	KubeInsecure   bool   // Do not check the server's certificate for validity
	KubeConfig     string // Path to a kubeconfig file
	KubeCertAuth   string // Path to a file for the certificate authority
	KubeClientCert string // Path to a client certificate file
	KubeClientKey  string // Path to a client key file
	KubeToken      string // A service account token
	KubeUsername   string // The username to use for basic auth
	KubePassword   string // The password to use for basic auth
}

KubernetesConfig defines the configuration options for talking to Kubernetes master

type KubernetesKubectl

type KubernetesKubectl struct {
	KubePath string
	// Base level arguments to kubectl. User commands/arguments get appended to this.
	Arguments []string
}

KubernetesKubectl implements the interface for talking to Kubernetes by wrapping calls via kubectl.

func (*KubernetesKubectl) Create

func (k *KubernetesKubectl) Create(resource string) (string, error)

Create runs a kubectl create command for a given resource.

func (*KubernetesKubectl) Delete

func (k *KubernetesKubectl) Delete(resource string) (string, error)

Delete runs a kubectl delete command for a given resource.

func (*KubernetesKubectl) Get

func (k *KubernetesKubectl) Get(name string, resourceType string) (string, error)

Get runs a kubectl get command for a named resource of a given type.

func (*KubernetesKubectl) Replace

func (k *KubernetesKubectl) Replace(resource string) (string, error)

Replace runs a kubectl replace command for a given resource.

type ServerTester

type ServerTester func(method, path, ctype string, reader io.Reader) (*http.Response, error)

A ServerTester is a function that takes an HTTP method, an URL path, and a reader for a request body, creates a request from them, and serves it to a test server using the handler to which it was bound and returns the response.

func NewServerTester

func NewServerTester(handler http.Handler) ServerTester

NewServerTester creates and returns a new NewServerTester for an http.Handler.

type Sleeper

type Sleeper interface {
	Sleep(d time.Duration)
}

Sleeper exposes a Sleep func which causes the current goroutine to sleep for the requested duration.

func NewSleeper

func NewSleeper() Sleeper

NewSleeper returns a new Sleeper.

Jump to

Keyboard shortcuts

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