client

package
v0.0.0-...-80377ec Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 13 Imported by: 21

Documentation

Overview

Package client provides an ability to create a http.RoundTripper OpenStack client with extended options, including the JSON requests and responses log capabilities.

Example usage with the default logger:

package example

import (
	"net/http"
	"os"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/openstack"
	"github.com/gophercloud/utils/client"
	"github.com/gophercloud/utils/openstack/clientconfig"
)

func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
	ao, err := clientconfig.AuthOptions(nil)
	if err != nil {
		return nil, err
	}

	provider, err := openstack.NewClient(ao.IdentityEndpoint)
	if err != nil {
		return nil, err
	}

	if os.Getenv("OS_DEBUG") != "" {
		provider.HTTPClient = http.Client{
			Transport: &client.RoundTripper{
				Rt:     &http.Transport{},
				Logger: &client.DefaultLogger{},
			},
		}
	}

	err = openstack.Authenticate(provider, *ao)
	if err != nil {
		return nil, err
	}

	return openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
}

Example usage with the custom logger:

package example

import (
	"net/http"
	"os"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/openstack"
	"github.com/gophercloud/utils/client"
	"github.com/gophercloud/utils/openstack/clientconfig"
	log "github.com/sirupsen/logrus"
)

type myLogger struct {
	Prefix string
}

func (l myLogger) Printf(format string, args ...interface{}) {
	log.Debugf("%s [DEBUG] "+format, append([]interface{}{l.Prefix}, args...)...)
}

func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
	ao, err := clientconfig.AuthOptions(nil)
	if err != nil {
		return nil, err
	}

	provider, err := openstack.NewClient(ao.IdentityEndpoint)
	if err != nil {
		return nil, err
	}

	if os.Getenv("OS_DEBUG") != "" {
		provider.HTTPClient = http.Client{
			Transport: &client.RoundTripper{
				Rt:     &http.Transport{},
				Logger: &myLogger{Prefix: "myApp"},
			},
		}
	}

	err = openstack.Authenticate(provider, *ao)
	if err != nil {
		return nil, err
	}

	return openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
}

Example usage with additinal headers:

package example

import (
	"net/http"
	"os"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/openstack"
	"github.com/gophercloud/utils/client"
	"github.com/gophercloud/utils/openstack/clientconfig"
)

func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
	ao, err := clientconfig.AuthOptions(nil)
	if err != nil {
		return nil, err
	}

	provider, err := openstack.NewClient(ao.IdentityEndpoint)
	if err != nil {
		return nil, err
	}

	provider.HTTPClient = http.Client{
		Transport: &client.RoundTripper{
			Rt:     &http.Transport{},
		},
	}

	provider.HTTPClient.Transport.(*client.RoundTripper).SetHeaders(map[string][]string{"Cache-Control": {"no-cache"}}})

	err = openstack.Authenticate(provider, *ao)
	if err != nil {
		return nil, err
	}

	return openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatJSON

func FormatJSON(raw []byte) (string, error)

FormatJSON is a default function to pretty-format a JSON body. It will also mask known fields which contain sensitive information.

func GetDefaultSensitiveHeaders

func GetDefaultSensitiveHeaders() []string

GetDefaultSensitiveHeaders returns the default list of headers to be masked

func RetryBackoffFunc

func RetryBackoffFunc(logger Logger) gophercloud.RetryBackoffFunc

Types

type DefaultLogger

type DefaultLogger struct{}

DefaultLogger is a default struct, which satisfies the Logger interface

func (DefaultLogger) Printf

func (DefaultLogger) Printf(format string, args ...interface{})

Printf is a default Printf method

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger is an interface representing the Logger struct

type RoundTripper

type RoundTripper struct {
	// Default http.RoundTripper
	Rt http.RoundTripper

	// A custom function to format and mask JSON requests and responses
	FormatJSON func([]byte) (string, error)
	// How many times HTTP connection should be retried until giving up
	MaxRetries int
	// If Logger is not nil, then RoundTrip method will debug the JSON
	// requests and responses
	Logger Logger
	// contains filtered or unexported fields
}

RoundTripper satisfies the http.RoundTripper interface and is used to customize the default http client RoundTripper

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip performs a round-trip HTTP request and logs relevant information about it.

func (*RoundTripper) SetHeaders

func (rt *RoundTripper) SetHeaders(headers http.Header)

SetHeaders sets request headers to be set (not appended) in all client requests

func (*RoundTripper) SetSensitiveHeaders

func (rt *RoundTripper) SetSensitiveHeaders(headers []string)

SetSensitiveHeaders sets the list of case insensitive headers to be masked in debug log

Jump to

Keyboard shortcuts

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