common

package
v0.9.3 Latest Latest
Warning

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

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

Documentation

Overview

Package common contains various things common to all Romana services.

Contains general routines and definitions for a generic back-end storage (currently geared towards RDBMS but not necessarily limited to that).

Index

Constants

View Source
const (
	CredentialUsernamePassword = "userPass"
	CredentialNone             = "none"

	PasswordEnvironmentVariable = "ROMANA_PASSWORD"
)
View Source
const (
	// For passing in Gorilla Mux context the unmarshalled data
	ContextKeyUnmarshalledMap string = "UnmarshalledMap"
	// For passing in Gorilla Mux context path variables
	ContextKeyQueryVariables string = "QueryVars"
	// 	 For passing in Gorilla Mux context the original body data
	ContextKeyOriginalBody string = "OriginalBody"
	ContextKeyMarshaller   string = "Marshaller"
	ContextKeyRoles        string = "Roles"
	// DefaultRestTimeout, in milliseconds.
	DefaultRestTimeout    = 500
	DefaultRestRetries    = 3
	ReadWriteTimeoutDelta = 10

	// Name of the query parameter used for request token
	RequestTokenQueryParameter = "RequestToken"

	HeaderContentType = "content-type"

	Starting ServiceMessage = "Starting."

	// JSON
	TimeoutMessage = "{ \"error\" : \"Timed out\" }"

	// Empty string returned when there is a string return
	// but there is an error so no point in returning any
	// value.
	ErrorNoValue = ""

	// Path for authentication; if this is what is used
	// in the request we will not check the token (because
	// we are attempting to get a token at this point).
	AuthPath = "/auth"

	// Body provided.
	HookExecutableBodyArgument = "body"

	// Max port number for TCP/UDP.
	MaxPortNumber = 65535
	MaxIcmpType   = 255

	// Wildcard
	Wildcard = "any"
	// Name of root service
	ServiceRoot = "root"
)

Constants

View Source
const (
	PolicyDirectionIngress = "ingress"
	PolicyDirectionEgress  = "egress"
)
View Source
const (
	// Flags to store.Find operation
	FindFirst      = "findFirst"
	FindLast       = "findLast"
	FindExactlyOne = "findExactlyOne"
	FindAll        = "findAll"
)
View Source
const (
	MySQLUniqueConstraintErrorCode = 1062
)
View Source
const (
	// 422 (unprocessable entity http://www.restpatterns.org/HTTP_Status_Codes/422_-_Unprocessable_Entity)
	// is not in net/http yet.
	StatusUnprocessableEntity = 422
)

Variables

View Source
var ContentTypeMarshallers map[string]Marshaller = map[string]Marshaller{

	"":                                  jsonMarshaller{},
	"application/json":                  jsonMarshaller{},
	"application/vnd.romana.v1+json":    jsonMarshaller{},
	"application/vnd.romana+json":       jsonMarshaller{},
	"application/x-www-form-urlencoded": formMarshaller{},
}

ContentTypeMarshallers maps MIME type to Marshaller instances

View Source
var SupportedContentTypes = []string{"text/plain", "application/vnd.romana.v1+json", "application/vnd.romana+json", "application/json", "application/x-www-form-urlencoded"}

List of supported content types to return in a 406 response.

View Source
var SupportedContentTypesMessage = struct {
	SupportedContentTypes []string `json:"supported_content_types"`
}{
	SupportedContentTypes,
}

Above list of supported content types wrapped in a struct for converion to JSON.

Functions

func BuildInfo

func BuildInfo() string

BuildInfo return build revision and time string.

func CleanURL added in v0.9.0

func CleanURL(url string) (string, error)

CleanURL is similar to path.Clean() but to work on URLs

func DbToHttpError added in v0.9.3

func DbToHttpError(err error) error

DbToHttpError produces an appropriate HttpError given an error, if it can (for example, producing a 409 CONFLICT in case of a unique or primary key constraint violation). If it cannot, it returns the original error.

func Environ added in v0.8.2

func Environ() map[string]string

Environ is similar to os.Environ() but returning environment as a map instead of an array of strings.

func GetDbErrors added in v0.9.0

func GetDbErrors(db *gorm.DB) error

GetDbErrors creates MultiError on error from DB.

func IPv4ToInt

func IPv4ToInt(ip net.IP) uint64

func In added in v0.9.3

func In(needle string, haystack []string) bool

In returns true if needle is found in haystack.

func InitMap added in v0.9.3

func InitMap(keyValuePairs ...KeyValue) map[string]interface{}

func IntToIPv4

func IntToIPv4(ipInt uint64) net.IP

func IsZeroValue added in v0.9.1

func IsZeroValue(val interface{}) bool

IsZeroValue checks whether the provided value is equal to the zero value for the type. Zero values would be:

  • 0 for numeric types
  • "" for strings
  • uninitialized struct for a struct
  • zero-size for a slice or a map

func MakeMultiError

func MakeMultiError(errors []error) error

MakeMultiError creates a single error object out of an array of errors as follows: 1. If the array is empty or nil, nil is returned 2. If the array has exactly 1 element, that element is returned 3. Otherwise, a MultiError is returned.

func MkMap added in v0.9.3

func MkMap() map[string]interface{}

func MkMapStr added in v0.9.3

func MkMapStr() map[string]string

func MockPortsInConfig

func MockPortsInConfig(fname string) error

MockPortsInConfig will take the config file specified and replace the ports with 0 to use arbitrary ports and write it out to /tmp/romana.yaml

func NewError

func NewError(text string, args ...interface{}) error

NewError constructs an error by formatting text with arguments.

func PressEnterToContinue

func PressEnterToContinue()

func String added in v0.9.0

func String(i interface{}) string

func ToBool

func ToBool(val string) (bool, error)

toBool is a convenience function that's like ParseBool but allows also "on"/"off" values.

func WriteConfig

func WriteConfig(config Config, fname string) error

WriteConfig writes config from file to structure

Types

type Api

type Api struct {
	// Host to listen on.
	Host string `yaml:"host" json:"host"`
	// Port to listen on.
	Port uint64 `yaml:"port" json:"port"`
	// Root service URL
	RootServiceUrl string `json:"root_service_url,omitempty" yaml:"root_service_url,omitempty"`
	// Rest timeout in milliseconds (if omitted, defaults to DefaultRestTimeout)
	RestTimeoutMillis int64 `yaml:"rest_timeout_millis,omitempty" json:"rest_timeout_millis,omitempty"`
	RestRetries       int   `yaml:"rest_retries,omitempty" json:"rest_retries,omitempty"`
	// Location of the public key.
	AuthPublic   string `yaml:"auth_public" json:"auth_public"`
	RestTestMode bool   `yaml:"rest_test_mode,omitempty" json:"rest_test_mode,omitempty"`
	Hooks        []Hook
}

Api part of service configuration (host/port).

func (Api) GetHostPort

func (api Api) GetHostPort() string

type AuthMiddleware

type AuthMiddleware struct {
	PublicKey []byte
}

AuthMiddleware wrapper for auth.

func (AuthMiddleware) ServeHTTP

func (am AuthMiddleware) ServeHTTP(writer http.ResponseWriter, request *http.Request, next http.HandlerFunc)

If the path of request is common.AuthPath, this does nothing, as the request is for authentication in the first place. Otherwise, checks token from request. If the token is not valid, returns a 403 FORBIDDEN status.

type CommonConfig

type CommonConfig struct {
	Api *Api `yaml:"api" json:"api"`
	// Credential is convenient to store here but it is not part of the
	// configuration that is passed around in JSON.
	Credential *Credential `yaml:"-" json:"-"`
	PublicKey  []byte      `yaml:"-" json:"-"`
}

CommonConfig stores configuration that is common to all services. For things such as API information (host/port), DB, etc.

type Config

type Config struct {
	Services map[string]ServiceConfig
}

Config provides the main configuration object

func ReadConfig

func ReadConfig(fname string) (Config, error)

ReadConfig parses the configuration file provided and returns ReadConfig reads config from file to structure

type Credential added in v0.8.2

type Credential struct {
	Type     CredentialType
	Username string
	Password string
}

Container for various credentials. Currently containing Username/Password but keys, certificates, etc. can be used in the future.

func MakeCredentialFromCliArgs added in v0.8.2

func MakeCredentialFromCliArgs(username string, password string) *Credential

MakeCredentialFromCliArgs takes all possible CLI arguments that can be provided and constructs appropriate Credential structure. This is just keeping in one method a common functionality that will be in every command.

type CredentialType added in v0.8.2

type CredentialType string

Represents the type of credential (e.g., certificate, username-password, etc.

type Datacenter

type Datacenter struct {
	Id        uint64 `json:"id",sql:"AUTO_INCREMENT"`
	IpVersion uint   `json:"ip_version,omitempty"`
	// We don't need to store this, but calculate and pass around
	Prefix      uint64 `json:"prefix,omitempty"`
	Cidr        string `json:"cidr,omitempty"`
	PrefixBits  uint   `json:"prefix_bits"`
	PortBits    uint   `json:"port_bits"`
	TenantBits  uint   `json:"tenant_bits"`
	SegmentBits uint   `json:"segment_bits"`
	// We don't need to store this, but calculate and pass around
	EndpointBits      uint   `json:"endpoint_bits"`
	EndpointSpaceBits uint   `json:"endpoint_space_bits"`
	Name              string `json:"name,omitempty"`
}

Datacenter represents the configuration of a datacenter.

func (Datacenter) String added in v0.9.0

func (dc Datacenter) String() string

type DbStore

type DbStore struct {
	ServiceStore ServiceStore
	Config       *StoreConfig
	Db           *gorm.DB
	// contains filtered or unexported fields
}

DbStore is a structure storing information specific to RDBMS-based implementation of Store.

func (*DbStore) Connect

func (dbStore *DbStore) Connect() error

Connect connects to the appropriate DB (mutating dbStore's state with the connection information), or returns an error.

func (*DbStore) CreateSchema

func (dbStore *DbStore) CreateSchema(force bool) error

CreateSchema creates the schema in this DB. If force flag is specified, the schema is dropped and recreated.

func (*DbStore) DbStore

func (dbStore *DbStore) DbStore() DbStore

func (*DbStore) Find added in v0.9.1

func (dbStore *DbStore) Find(query url.Values, entities interface{}, flag FindFlag) (interface{}, error)

Find generically implements Find() of Store interface.

func (*DbStore) GetPasswordFunction

func (dbStore *DbStore) GetPasswordFunction() (string, error)

GetPasswordFunction returns appropriate function to hash password depending on the underlying DB (note that in sqlite it is plain text).

func (*DbStore) SetConfig

func (dbStore *DbStore) SetConfig(configMap map[string]interface{}) error

SetConfig sets the config object from a map.

type Endpoint added in v0.9.0

type Endpoint struct {
	Peer              string  `json:"peer,omitempty"`
	Cidr              string  `json:"cidr,omitempty"`
	Dest              string  `json:"dest,omitempty"`
	TenantID          uint64  `json:"tenant_id,omitempty"`
	TenantName        string  `json:"tenant,omitempty"`
	TenantExternalID  string  `json:"tenant_external_id,omitempty"`
	TenantNetworkID   *uint64 `json:"tenant_network_id,omitempty"`
	SegmentID         uint64  `json:"segment_id,omitempty"`
	SegmentName       string  `json:"segment,omitempty"`
	SegmentExternalID string  `json:"segment_external_id,omitempty"`
	SegmentNetworkID  *uint64 `json:"segment_network_id,omitempty"`
}

Endpoint represents an endpoint - that is, something that has an IP address and routes to/from. It can be a container, a Kubernetes POD, a VM, etc.

func (Endpoint) String added in v0.9.0

func (e Endpoint) String() string

type ExecErrorDetails added in v0.9.0

type ExecErrorDetails struct {
	Error string
}

type FindFlag added in v0.9.1

type FindFlag string

type Hook

type Hook struct {
	// Executable to run
	Executable string `json:"executable"`
	// Pattern to match in common.Route
	Pattern string `json:"pattern"`
	// HTTP method to match
	Method string `json:"method"`
	// Whether to run before or after the handler for the pattern/method
	// pair is invoked
	When string `json:"when"`
	// Where to write output (if omitted, will be just logged)
	Output string `json:"output"`
}

Hook defines an executable to run before or after any Route handler execution.

type Host added in v0.9.1

type Host struct {
	ID        uint64 `sql:"AUTO_INCREMENT" json:"id,omitempty"`
	Name      string `json:"name,omitempty"`
	Ip        string `json:"ip,omitempty"`
	RomanaIp  string `json:"romana_ip,omitempty"`
	AgentPort uint64 `json:"agent_port,omitempty"`
	Links     Links  `json:"links,omitempty" sql:"-"`
}

Host is a structure representing information about the host.

type HttpError

type HttpError struct {
	// HTTP status code
	StatusCode int         `json:"status_code"`
	Details    interface{} `json:"details,omitempty"`
	// ResourceID specifies the relevant resource ID, if applicable
	ResourceID string `json:"resource_id,omitempty"`
	// ResourceType specifies the relevant resource type, if applicable
	ResourceType string `json:"resource_type,omitempty"`
	SeeAlso      string `json:"see_also, omitempty"`
}

HttpError is a structure that represents, well, an HTTP error.

func NewError400

func NewError400(details interface{}) HttpError

NewError400 creates an HttpError with 400 (http.StatusBadRequest) status code.

func NewError404

func NewError404(resourceType string, resourceID string) HttpError

NewError404 creates a 404 NOT FOUND message.

func NewError500

func NewError500(details interface{}) HttpError

NewError500 creates an HttpError with 500 (http.StatusInternalServerError) status code.

func NewErrorConflict added in v0.9.0

func NewErrorConflict(details interface{}) HttpError

NewErrorConflict creates an HttpError with 409 (http.StatusConflict) status code.

func NewHttpError added in v0.9.0

func NewHttpError(code int, details interface{}) HttpError

NewError helps to construct new Error structure.

func NewUnprocessableEntityError added in v0.9.0

func NewUnprocessableEntityError(details interface{}) HttpError

NewUnprocessableEntityError creates an HttpError with 423 (StatusUnprocessableEntity) status code.

func (HttpError) Error

func (httpErr HttpError) Error() string

String returns formatted HTTP error for human consumption.

func (HttpError) StatusText

func (err HttpError) StatusText() string

StatusText returns the string value of the HttpError corresponding to the StatusCode.

type IP

type IP struct {
	// contains filtered or unexported fields
}

func MakeIPv4

func MakeIPv4(a, b, c, d byte) IP

type IndexResponse

type IndexResponse struct {
	ServiceName string `json:"service_name"`
	Links       Links  `json:"links"`
}

Response to /

type KeyValue added in v0.9.3

type KeyValue struct {
	Key   string
	Value interface{}
}

KeyValue represents a key-value pair (similar to Java's Map.Entry)

func KV added in v0.9.3

func KV(key string, value interface{}) KeyValue

KV is a convenience function to create a KeyValue value.

type LinkResponse

type LinkResponse struct {
	Href string `json:"href,omitempty"`
	Rel  string `json:"rel,omitempty"`
}

LinkResponse structure represents the commonly occurring

{
       "href" : "https://<own-addr>",
       "rel"  : "self"
 }

part of the response.

type Links []LinkResponse

func (Links) FindByRel

func (links Links) FindByRel(rel string) string

FindByRel finds the path (href) for a link based on its rel value. This is because in the REST message links are organized as an array of structures containing href and rel, but, of course, in actual usage it is easier to be used as a map.

type MakeMessage

type MakeMessage func() interface{}

MakeMessage is a factory function, which should return a pointer to an instance into which we will unmarshal wire data.

type Marshaller

type Marshaller interface {
	Marshal(v interface{}) ([]byte, error)
	Unmarshal(data []byte, v interface{}) error
}

Marshaller is capable of marshalling and unmarshalling data to/from the wire.

type MultiError

type MultiError struct {
	// contains filtered or unexported fields
}

MultiError is a facility to collect multiple number of errors but present them as a single error interface. For example, GORM does not return errors at every turn. It accumulates them and returns them whenever you feel like calling GetErrors() (https://godoc.org/github.com/jinzhu/gorm#DB.GetErrors). Since this is not consistent with the rest of the code, I prefer to isolate it here and make an adapter.

func NewMultiError

func NewMultiError() *MultiError

NewMultiError creates a new MultiError object to which errors can be added.

func (MultiError) Add

func (me MultiError) Add(err error)

Add adds an error to the MultiError object.

func (*MultiError) Error

func (m *MultiError) Error() string

Error satisfies Error method on error interface and returns a concatenated string of all error messages.

func (*MultiError) GetError

func (m *MultiError) GetError() error

GetError returns nil if there are no underlying errors, the single error if there is only one, and the MultiError object if there is more than one.

func (*MultiError) GetErrors added in v0.9.0

func (m *MultiError) GetErrors() []error

GetErrors returns all errors in this MultiError object.

type NegotiatorMiddleware

type NegotiatorMiddleware struct {
}

func NewNegotiator

func NewNegotiator() *NegotiatorMiddleware

func (NegotiatorMiddleware) ServeHTTP

func (negotiator NegotiatorMiddleware) ServeHTTP(writer http.ResponseWriter, request *http.Request, next http.HandlerFunc)

type Policy added in v0.9.0

type Policy struct {
	// Direction is one of common.PolicyDirectionIngress or common.PolicyDirectionIngress,
	// otherwise common.Validate will return an error.
	Direction string `json:"direction,omitempty" romana:"desc:Direction is one of 'ingress' or egress'."`
	// Description is human-redable description of the policy.
	Description string `json:"description,omitempty"`
	// Name is human-readable name for this policy.
	Name string `json:"name" romana:"desc:Name is human-readable name for this policy."`
	// ID is Romana-generated unique (within Romana deployment) ID of this policy,
	// to be used in REST requests. It will be ignored when set by user.
	ID uint64 `json:"id,omitempty" sql:"AUTO_INCREMENT"`
	// ExternalID is an optional identifier of this policy in an external system working
	// with Romana in this deployment (e.g., Open Stack).
	ExternalID string `json:"external_id,omitempty",sql:"unique"`
	// Datacenter describes a Romana deployment.
	Datacenter *Datacenter `json:"datacenter,omitempty"`
	AppliedTo  []Endpoint  `json:"applied_to,omitempty"`
	Peers      []Endpoint  `json:"peers,omitempty"`
	Rules      []Rule      `json:"rules,omitempty"`
}

Policy describes Romana network security policy. For examples, see: 1. https://github.com/romana/core/blob/master/policy/policy.sample.json 2. https://github.com/romana/core/blob/master/policy/policy.example.agent.json

func (Policy) String added in v0.9.0

func (p Policy) String() string

func (*Policy) Validate added in v0.9.0

func (p *Policy) Validate() error

Validate validates the policy and returns an Unprocessable Entity (422) HttpError if the policy is invalid. The following would lead to errors if they are not specified elsewhere: 1. Rules must be specified. TODO As a reference I'm using https://docs.google.com/spreadsheets/d/1vN-EnZqJnIp8krY1cRf6VzRPkO9_KNYLW0QOfw2k1Mo/edit which we'll remove from this comment once finalized. This also mutates the policy to ensure the following:

  1. If only one of Name, External ID is specified, then the unspecified field is populated from the specified one.

TODO mutation is probably a bad idea for a method called Validate(); however, this behavior is probably a good idea, as it is a sort of DWIM. Maybe keep behavior, rename this to ValidateAndNormalize()?

type PortRange added in v0.9.0

type PortRange [2]uint

func (PortRange) String added in v0.9.0

func (p PortRange) String() string

type PortUpdateMessage

type PortUpdateMessage struct {
	Port uint64 `json:"port"`
}

Message to register with the root service the actual port a service is listening on.

type Raw added in v0.9.0

type Raw struct {
	Body string
}

Raw is a type that can be returned from any service's route and the middleware will not try to marshal it.

type RestClient

type RestClient struct {
	// contains filtered or unexported fields
}

Rest Client for the Romana services. Incorporates facilities to deal with various REST requests.

func NewRestClient

func NewRestClient(config RestClientConfig) (*RestClient, error)

NewRestClient creates a new Romana REST client. It provides convenience methods to make REST calls. When configured with a root URL pointing to Romana root service, it provides some common functionality useful for Romana services (such as ListHosts, GetServiceConfig, etc.) If the root URL does not point to the Romana service, the generic REST operations still work, but Romana-specific functionality does not.

func (*RestClient) Delete

func (rc *RestClient) Delete(url string, data interface{}, result interface{}) error

Delete applies DELETE method to the specified URL, putting the result into the provided interface

func (*RestClient) Find added in v0.9.1

func (rc *RestClient) Find(entity interface{}, flag FindFlag) error

FindOne is a convenience function, which queries the appropriate service and retrieves one entity based on provided structure, and puts the results into the same structure. The provided argument, entity, should be a pointer to the desired structure, e.g., &common.Host{}.

func (*RestClient) Get

func (rc *RestClient) Get(url string, result interface{}) error

Get applies GET method to the specified URL, putting the result into the provided interface

func (*RestClient) GetServiceConfig

func (rc *RestClient) GetServiceConfig(name string) (*ServiceConfig, error)

GetServiceConfig retrieves configuration for the given service from the root service.

func (*RestClient) GetServiceUrl

func (rc *RestClient) GetServiceUrl(name string) (string, error)

GetServiceUrl is a convenience function, which, given the root service URL and name of desired service, returns the URL of that service.

func (*RestClient) GetStatusCode added in v0.9.0

func (rc *RestClient) GetStatusCode() int

GetStatusCode returns status code of last executed request. As stated above, it is not recommended to share RestClient between goroutines. 0 is returned if no previous requests have been yet made, or if the most recent request resulted in some error that was not a 4xx or 5xx HTTP error.

func (*RestClient) ListHosts added in v0.9.0

func (rc *RestClient) ListHosts() ([]Host, error)

ListHost queries the Topology service in order to return a list of currently configured hosts in a Romana cluster.

func (*RestClient) NewUrl

func (rc *RestClient) NewUrl(dest string) error

NewUrl sets the client's new URL (yes, it mutates) to dest. If dest is a relative URL then it will be based on the previous value of the URL that the RestClient had.

func (*RestClient) Post

func (rc *RestClient) Post(url string, data interface{}, result interface{}) error

Post applies POST method to the specified URL, putting the result into the provided interface

func (*RestClient) Put added in v0.9.0

func (rc *RestClient) Put(url string, data interface{}, result interface{}) error

Put applies PUT method to the specified URL, putting the result into the provided interface

type RestClientConfig

type RestClientConfig struct {
	TimeoutMillis int64
	Retries       int
	Credential    *Credential
	TestMode      bool
	RootURL       string
}

RestClientConfig holds configuration for restful client.

func GetDefaultRestClientConfig

func GetDefaultRestClientConfig(rootURL string) RestClientConfig

GetDefaultRestClientConfig gets a RestClientConfig with specified rootURL and other values set to their defaults: DefaultRestTimeout, DefaultRestRetries.

func GetRestClientConfig

func GetRestClientConfig(config ServiceConfig) RestClientConfig

GetRestClientConfig returns a RestClientConfig based on a ServiceConfig. That is, the information provided in the service configuration is used for the client configuration.

type RestContext

type RestContext struct {
	// Path variables as described in https://godoc.org/code.google.com/p/gorilla/mux
	PathVariables map[string]string
	// QueryVariables stores key-value-list map of query variables, see url.Values
	// for more details.
	QueryVariables url.Values
	// Unique identifier for a request.
	RequestToken string
	Roles        []Role
	// Output of the hook if any run before the execution of the handler.
	HookOutput string
}

RestContext contains the context of the REST request other than the body data that has been unmarshaled.

type RestHandler

type RestHandler func(input interface{}, context RestContext) (interface{}, error)

RestHandler specifies type of a function that each Route provides. It takes (for now) an interface as input, and returns any interface. The middleware provided in this file takes care of unmarshalling the data from the wire to the input object (the type of the object created will be determined by the type of the instance provided in Consumes field of Route type, below), and of marshalling the returned object to the wire (the type of which is determined by type of the instance provided in Produces field of Route type, below).

type RestServiceInfo

type RestServiceInfo struct {
	// Address being listened on (as host:port)
	Address string
	// Channel to communicate with the service
	Channel chan ServiceMessage
}

RestServiceInfo describes information about a running Romana service.

func InitializeService

func InitializeService(service Service, config ServiceConfig) (*RestServiceInfo, error)

InitializeService initializes the service with the provided config and starts it. The channel returned allows the caller to wait for a message from the running service. Messages are of type ServiceMessage above. It can be used for launching service from tests, etc.

func ListenAndServe

func ListenAndServe(svr *http.Server) (*RestServiceInfo, error)

ListenAndServe is same as http.ListenAndServe except it returns the address that will be listened on (which is useful when using arbitrary ports). See https://github.com/golang/go/blob/master/src/net/http/server.go

func RunNegroni

func RunNegroni(n *negroni.Negroni, addr string, timeout time.Duration) (*RestServiceInfo, error)

RunNegroni is a convenience function that runs the negroni stack as a provided HTTP server, with the following caveats:

  1. the Handler field of the provided serverConfig should be nil, because the Handler used will be the n Negroni object.

type Role added in v0.8.2

type Role interface {
	Name() string
}

type RomanaHandler

type RomanaHandler struct {
	// contains filtered or unexported fields
}

RomanaHandler interface to comply with http.Handler

func (RomanaHandler) ServeHTTP

func (romanaHandler RomanaHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP is required by https://golang.org/pkg/net/http/#Handler

type RootIndexResponse

type RootIndexResponse struct {
	ServiceName string            `json:"service_name"`
	Links       Links             `json:"links"`
	Services    []ServiceResponse `json:"services"`
}

RootIndexResponse represents a response from the / path specific for root service only.

type Route

type Route struct {
	// REST method
	Method string

	// Pattern (see http://www.gorillatoolkit.org/pkg/mux)
	Pattern string

	// Handler (see documentation above)
	Handler RestHandler

	// This should return a POINTER to an instance which
	// this route expects as an input.
	MakeMessage MakeMessage

	// Whether this route is using a request token. If true, the
	// request token will be parsed out of the request and made
	// available in RestContext. It can then
	// used by the handler to achieve idempotence.
	UseRequestToken bool

	Hook *Hook
}

Route determines an action taken on a URL pattern/HTTP method. Each service can define a route See routes.go and handlers.go in root package for a demonstration of use

type Routes

type Routes []Route

Routes provided by each service.

func CreateFindRoutes added in v0.9.1

func CreateFindRoutes(entities interface{}, store Store) Routes

CreateFindRoute creates Routes for a find functionality given the provided entities. Four routes are created:

  1. /findOne/<entityName>s, which will return a single structure (or an error if more than one entry is found,
  2. /findFirst/<entityName>s, which will return the first entity (in order of their creation).
  3. /findLast/<entityName>s -- similar to above.
  4. /findAll/<entityName>s

Routes will return a 404 if no entries found. Here "entities" *must* be a pointer to an array of entities to find (for example, it has to be &[]Tenant{}, not Tenant{}), which will then create /findOne/tenants (returning Tenant structure} and /findAll/tenants (returning []Tenant array) routes.

type Rule added in v0.9.0

type Rule struct {
	Protocol   string      `json:"protocol,omitempty"`
	Ports      []uint      `json:"ports,omitempty"`
	PortRanges []PortRange `json:"port_ranges,omitempty"`
	// IcmpType only applies if Protocol value is ICMP and
	// is mutually exclusive with Ports or PortRanges
	IcmpType   uint `json:"icmp_type,omitempty"`
	IcmpCode   uint `json:"icmp_code,omitempty"`
	IsStateful bool `json:"is_stateful,omitempty"`
}

Rule describes a rule of the policy. The following requirements apply (the policy would not be validated otherwise): 1. Protocol must be specified. 2. Protocol must be one of those validated by isValidProto(). 3. Ports cannot be negative or greater than 65535. 4. If Protocol specified is "icmp", Ports and PortRanges fields should be blank. 5. If Protocol specified is not "icmp", Icmptype and IcmpCode should be unspecified.

func (Rule) String added in v0.9.0

func (r Rule) String() string

type Rules added in v0.9.0

type Rules []Rule

type Service

type Service interface {
	// SetConfig sets the configuration, validating it if needed
	// and returning an error if not valid.
	SetConfig(config ServiceConfig) error

	// Initializes the service (mostly for error reporting, could be a no-op)
	Initialize() error

	// Returns the routes that this service works with
	Routes() Routes

	// Name returns the name of this service.
	Name() string
}

Service is the interface that microservices implement.

type ServiceConfig

type ServiceConfig struct {
	Common CommonConfig `json:"common" yaml:"common"`
	// TODO I really dislike this name, but there
	// should be some common part that's applicable
	// to all services, and something service-specific
	// that we in common do not need to know about.
	ServiceSpecific map[string]interface{} `json:"config" yaml:"config,omitempty"`
}

ServiceConfig contains common configuration for each service and also a section for service-specific configuration. This may be an overkill but if we have a type system, we should use it instead of just dictionaries.

type ServiceMessage

type ServiceMessage string

Type definitions

type ServiceResponse

type ServiceResponse struct {
	Name  string `json:"name"`
	Links Links  `json:"links"`
}

Service information

type ServiceStore

type ServiceStore interface {
	// Entities returns list of entities (DB tables) this store is managing.
	Entities() []interface{}
	// CreateSchemaPostProcess runs whatever required post-processing after
	// schema creation (perhaps initializing DB with some initial or sample data).
	CreateSchemaPostProcess() error
}

ServiceStore interface is what each service's store needs to implement.

type ServiceUtils

type ServiceUtils struct {
	// ResourceIdToStatus is a maps request ID
	// to status. A request ID can be a RequestToken if required,
	// or a resource ID. In general the idea is that this is used
	// in conjunction with RequestToken.
	// See also
	// - Route.UseRequestToken
	// - RestContext.RequestToken
	RequestIdToStatus map[string]interface{}

	// RequestIdToTimestamp maps request ID (for more information
	// on what that is see RequestIdToStatus) to the timestamp
	// of the original request. It will later be used for things
	// such as possible expiration, etc., but for now it's just a
	// placeholder.
	RequestIdToTimestamp map[string]int64
}

ServiceUtils represents functionality common to various services. One example of such functionality is asynchronous processing -- a service can accept a request for creation of an object and return a 202 ACCEPTED, creating an entry that can be queried for status.

func (ServiceUtils) AddStatus

func (s ServiceUtils) AddStatus(requestId string, value interface{})

AddStatus adds a status of a request

func (ServiceUtils) GetStatus

func (s ServiceUtils) GetStatus(resourceType string, requestId string) (interface{}, error)

GetStatus gets the status of the request or returns an common.HttpError (404) if not found.

type Store

type Store interface {
	// SetConfig sets the configuration
	SetConfig(map[string]interface{}) error
	// Connect connects to the store
	Connect() error
	// Create the schema, dropping existing one if the force flag is specified
	CreateSchema(bool) error
	// Find finds entries in the store based on the query string. The meaning of the
	// flags is as follows:
	// 1. FindFirst - the first entity (as ordered by primary key) is returned.
	// 2. FindLast - tha last entity is returned
	// 3. FindExactlyOne - it is expected that only one result is to be found --
	// multiple results will yield an errror.
	// 4. FindAll - returns all.
	// Here "entities" *must* be a pointer to an array
	// of entities to find (for example, it has to be &[]Tenant{}, not Tenant{}).
	Find(query url.Values, entities interface{}, flag FindFlag) (interface{}, error)
}

Store defines generic store interface that can be used by any service for persistence.

type StoreConfig

type StoreConfig struct {
	Host     string
	Port     uint64
	Username string
	Password string
	Database string
	// Database type, e.g., sqlite3, mysql, etc.
	// TODO add a set of constants for it.
	Type string
}

StoreConfig stores information needed for a DB connection.

func (StoreConfig) String added in v0.9.3

func (sc StoreConfig) String() string

type Tag added in v0.9.0

type Tag struct {
	Key   string `json:"key,omitempty"`
	Value string `json:"value,omitempty"`
}

Metadata attached to entities for various external environments like Open Stack / Kubernetes

type TokenMessage added in v0.8.2

type TokenMessage struct {
	Token string
}

type UnmarshallerMiddleware

type UnmarshallerMiddleware struct {
}

func NewUnmarshaller

func NewUnmarshaller() *UnmarshallerMiddleware

func (UnmarshallerMiddleware) ServeHTTP

Unmarshals request body if needed. If not acceptable, returns an http.StatusNotAcceptable and this ends this request's lifecycle.

type UnwrappedRestHandlerInput

type UnwrappedRestHandlerInput struct {
	ResponseWriter http.ResponseWriter
	Request        *http.Request
}

UnwrappedRestHandlerInput is used to pass in http.Request and http.ResponseWriter, should some service like unfettered access directly to them. In such a case, the service's RestHandler's input will be of this type; and the return value will be ignored.

Jump to

Keyboard shortcuts

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