client

package
v0.0.1-aggregation Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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/abramad-labs/gophercloud-utils-x/client"
	"github.com/abramad-labs/gophercloud-utils-x/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/abramad-labs/gophercloud-utils-x/client"
	"github.com/abramad-labs/gophercloud-utils-x/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/abramad-labs/gophercloud-utils-x/client"
	"github.com/abramad-labs/gophercloud-utils-x/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