sendgrid

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//ProviderName Standardised name of the SendGrid provider
	ProviderName = "sendgrid"
	//EnvAPIKey Name of the env var to retrieve the SendGrid API key
	EnvAPIKey = "SENDGRID_API_KEY"
	//APIHost SendGrid API default host
	APIHost = "https://api.sendgrid.com"
	//APIRouteSubUsers SendGrid v3 API endpoint for sub user management
	APIRouteSubUsers = "/v3/subusers"
	//APIRouteAPIKeys SendGrid v3 API endpoint for api key management
	APIRouteAPIKeys = "/v3/api_keys"
	//APIRouteIPAddresses SendGrid v3 API endpoint for ip address management
	APIRouteIPAddresses = "/v3/ips"
	//HeaderOnBehalfOf SendGrid v3 header for declaring an action is on behalf of a sub user
	HeaderOnBehalfOf = "on-behalf-of"
	//LogFieldAPIClient Logging field name for a description of the API client
	LogFieldAPIClient = "sendgrid_service_api_client"
	//ConnectionDetailsHost Default SendGrid host
	ConnectionDetailsHost = "smtp.sendgrid.net"
	//ConnectionDetailsPort Default SendGrid port
	ConnectionDetailsPort = 587
	//ConnectionDetailsTLS Default SendGrid TLS setting
	ConnectionDetailsTLS = true
	//ConnectionDetailsUsername Default SendGrid SMTP auth username
	ConnectionDetailsUsername = "apikey"
)

Variables

View Source
var (
	//DefaultAPIKeyScopes The default API scopes given to the generated SendGrid API key
	DefaultAPIKeyScopes = []string{"mail.send"}
)

Functions

func IsAlreadyExistsError

func IsAlreadyExistsError(err error) bool

IsAlreadyExistsError Compare check for AlreadyExistsError

func IsNotExistError

func IsNotExistError(err error) bool

IsNotExistError Compare check for NotExistError

Types

type APIClient

type APIClient interface {
	// ip addresses
	ListIPAddresses() ([]*IPAddress, error)
	// api keys
	GetAPIKeysForSubUser(username string) ([]*APIKey, error)
	CreateAPIKeyForSubUser(username string, scopes []string) (*APIKey, error)
	DeleteAPIKeyForSubUser(id, keyName string) error
	// sub users
	CreateSubUser(id, email, password string, ips []string) (*SubUser, error)
	DeleteSubUser(username string) error
	ListSubUsers(query map[string]string) ([]*SubUser, error)
	GetSubUserByUsername(username string) (*SubUser, error)
}

APIClient SendGrid client with utility functions for interacting with resources

type APIClientMock

type APIClientMock struct {
	// CreateAPIKeyForSubUserFunc mocks the CreateAPIKeyForSubUser method.
	CreateAPIKeyForSubUserFunc func(username string, scopes []string) (*APIKey, error)

	// CreateSubUserFunc mocks the CreateSubUser method.
	CreateSubUserFunc func(id string, email string, password string, ips []string) (*SubUser, error)

	// DeleteAPIKeyForSubUserFunc mocks the DeleteAPIKeyForSubUser method.
	DeleteAPIKeyForSubUserFunc func(id string, keyName string) error

	// DeleteSubUserFunc mocks the DeleteSubUser method.
	DeleteSubUserFunc func(username string) error

	// GetAPIKeysForSubUserFunc mocks the GetAPIKeysForSubUser method.
	GetAPIKeysForSubUserFunc func(username string) ([]*APIKey, error)

	// GetSubUserByUsernameFunc mocks the GetSubUserByUsername method.
	GetSubUserByUsernameFunc func(username string) (*SubUser, error)

	// ListIPAddressesFunc mocks the ListIPAddresses method.
	ListIPAddressesFunc func() ([]*IPAddress, error)

	// ListSubUsersFunc mocks the ListSubUsers method.
	ListSubUsersFunc func(query map[string]string) ([]*SubUser, error)
	// contains filtered or unexported fields
}

APIClientMock is a mock implementation of APIClient.

    func TestSomethingThatUsesAPIClient(t *testing.T) {

        // make and configure a mocked APIClient
        mockedAPIClient := &APIClientMock{
            CreateAPIKeyForSubUserFunc: func(username string, scopes []string) (*APIKey, error) {
	               panic("mock out the CreateAPIKeyForSubUser method")
            },
            CreateSubUserFunc: func(id string, email string, password string, ips []string) (*SubUser, error) {
	               panic("mock out the CreateSubUser method")
            },
            DeleteAPIKeyForSubUserFunc: func(id string, keyName string) error {
	               panic("mock out the DeleteAPIKeyForSubUser method")
            },
            DeleteSubUserFunc: func(username string) error {
	               panic("mock out the DeleteSubUser method")
            },
            GetAPIKeysForSubUserFunc: func(username string) ([]*APIKey, error) {
	               panic("mock out the GetAPIKeysForSubUser method")
            },
            GetSubUserByUsernameFunc: func(username string) (*SubUser, error) {
	               panic("mock out the GetSubUserByUsername method")
            },
            ListIPAddressesFunc: func() ([]*IPAddress, error) {
	               panic("mock out the ListIPAddresses method")
            },
            ListSubUsersFunc: func(query map[string]string) ([]*SubUser, error) {
	               panic("mock out the ListSubUsers method")
            },
        }

        // use mockedAPIClient in code that requires APIClient
        // and then make assertions.

    }

func (*APIClientMock) CreateAPIKeyForSubUser

func (mock *APIClientMock) CreateAPIKeyForSubUser(username string, scopes []string) (*APIKey, error)

CreateAPIKeyForSubUser calls CreateAPIKeyForSubUserFunc.

func (*APIClientMock) CreateAPIKeyForSubUserCalls

func (mock *APIClientMock) CreateAPIKeyForSubUserCalls() []struct {
	Username string
	Scopes   []string
}

CreateAPIKeyForSubUserCalls gets all the calls that were made to CreateAPIKeyForSubUser. Check the length with:

len(mockedAPIClient.CreateAPIKeyForSubUserCalls())

func (*APIClientMock) CreateSubUser

func (mock *APIClientMock) CreateSubUser(id string, email string, password string, ips []string) (*SubUser, error)

CreateSubUser calls CreateSubUserFunc.

func (*APIClientMock) CreateSubUserCalls

func (mock *APIClientMock) CreateSubUserCalls() []struct {
	ID       string
	Email    string
	Password string
	Ips      []string
}

CreateSubUserCalls gets all the calls that were made to CreateSubUser. Check the length with:

len(mockedAPIClient.CreateSubUserCalls())

func (*APIClientMock) DeleteAPIKeyForSubUser

func (mock *APIClientMock) DeleteAPIKeyForSubUser(id string, keyName string) error

DeleteAPIKeyForSubUser calls DeleteAPIKeyForSubUserFunc.

func (*APIClientMock) DeleteAPIKeyForSubUserCalls

func (mock *APIClientMock) DeleteAPIKeyForSubUserCalls() []struct {
	ID      string
	KeyName string
}

DeleteAPIKeyForSubUserCalls gets all the calls that were made to DeleteAPIKeyForSubUser. Check the length with:

len(mockedAPIClient.DeleteAPIKeyForSubUserCalls())

func (*APIClientMock) DeleteSubUser

func (mock *APIClientMock) DeleteSubUser(username string) error

DeleteSubUser calls DeleteSubUserFunc.

func (*APIClientMock) DeleteSubUserCalls

func (mock *APIClientMock) DeleteSubUserCalls() []struct {
	Username string
}

DeleteSubUserCalls gets all the calls that were made to DeleteSubUser. Check the length with:

len(mockedAPIClient.DeleteSubUserCalls())

func (*APIClientMock) GetAPIKeysForSubUser

func (mock *APIClientMock) GetAPIKeysForSubUser(username string) ([]*APIKey, error)

GetAPIKeysForSubUser calls GetAPIKeysForSubUserFunc.

func (*APIClientMock) GetAPIKeysForSubUserCalls

func (mock *APIClientMock) GetAPIKeysForSubUserCalls() []struct {
	Username string
}

GetAPIKeysForSubUserCalls gets all the calls that were made to GetAPIKeysForSubUser. Check the length with:

len(mockedAPIClient.GetAPIKeysForSubUserCalls())

func (*APIClientMock) GetSubUserByUsername

func (mock *APIClientMock) GetSubUserByUsername(username string) (*SubUser, error)

GetSubUserByUsername calls GetSubUserByUsernameFunc.

func (*APIClientMock) GetSubUserByUsernameCalls

func (mock *APIClientMock) GetSubUserByUsernameCalls() []struct {
	Username string
}

GetSubUserByUsernameCalls gets all the calls that were made to GetSubUserByUsername. Check the length with:

len(mockedAPIClient.GetSubUserByUsernameCalls())

func (*APIClientMock) ListIPAddresses

func (mock *APIClientMock) ListIPAddresses() ([]*IPAddress, error)

ListIPAddresses calls ListIPAddressesFunc.

func (*APIClientMock) ListIPAddressesCalls

func (mock *APIClientMock) ListIPAddressesCalls() []struct {
}

ListIPAddressesCalls gets all the calls that were made to ListIPAddresses. Check the length with:

len(mockedAPIClient.ListIPAddressesCalls())

func (*APIClientMock) ListSubUsers

func (mock *APIClientMock) ListSubUsers(query map[string]string) ([]*SubUser, error)

ListSubUsers calls ListSubUsersFunc.

func (*APIClientMock) ListSubUsersCalls

func (mock *APIClientMock) ListSubUsersCalls() []struct {
	Query map[string]string
}

ListSubUsersCalls gets all the calls that were made to ListSubUsers. Check the length with:

len(mockedAPIClient.ListSubUsersCalls())

type APIKey

type APIKey struct {
	ID     string   `json:"api_key_id"`
	Key    string   `json:"api_key"`
	Name   string   `json:"name"`
	Scopes []string `json:"scopes"`
}

APIKey A SendGrid API key, from https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html

func FindAPIKeyByName

func FindAPIKeyByName(apiKeys []*APIKey, keyName string) *APIKey

FindAPIKeyByName checks a list of APIKeys for a key with a given name

type AlreadyExistsError

type AlreadyExistsError struct {
	Message string
}

AlreadyExistsError Error to indicate an API key already exists

func (*AlreadyExistsError) Error

func (e *AlreadyExistsError) Error() string

Error String representation of error

type BackendAPIClient

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

BackendAPIClient Light wrapper around the default SendGrid library to allow for mocking

func NewBackendAPIClient

func NewBackendAPIClient(restClient RESTClient, logger *logrus.Entry) *BackendAPIClient

NewBackendAPIClient Create a new BackendAPIClient with default logger labels

func (*BackendAPIClient) CreateAPIKeyForSubUser

func (c *BackendAPIClient) CreateAPIKeyForSubUser(username string, scopes []string) (*APIKey, error)

CreateAPIKeyForSubUser Create API key on behalf of a sub user

func (*BackendAPIClient) CreateSubUser

func (c *BackendAPIClient) CreateSubUser(id, email, password string, ips []string) (*SubUser, error)

CreateSubUser Create sub user

func (*BackendAPIClient) DeleteAPIKeyForSubUser

func (c *BackendAPIClient) DeleteAPIKeyForSubUser(keyID, keyName string) error

DeleteAPIKeyForSubUser Delete api key of user with supplied username

func (*BackendAPIClient) DeleteSubUser

func (c *BackendAPIClient) DeleteSubUser(username string) error

DeleteSubUser Delete sub user by username

func (*BackendAPIClient) GetAPIKeysForSubUser

func (c *BackendAPIClient) GetAPIKeysForSubUser(username string) ([]*APIKey, error)

GetAPIKeysForSubUser Get API keys on behalf of a sub user

func (*BackendAPIClient) GetSubUserByUsername

func (c *BackendAPIClient) GetSubUserByUsername(username string) (*SubUser, error)

GetSubUserByUsername Get sub user of current authenticated user by username

func (*BackendAPIClient) ListIPAddresses

func (c *BackendAPIClient) ListIPAddresses() ([]*IPAddress, error)

ListIPAddresses List the IP Addresses for the authenticated user

func (*BackendAPIClient) ListSubUsers

func (c *BackendAPIClient) ListSubUsers(query map[string]string) ([]*SubUser, error)

ListSubUsers List all sub users for current authenticated user

type BackendRESTClient

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

BackendRESTClient Thin wrapper around the SendGrid library

func NewBackendRESTClient

func NewBackendRESTClient(apiHost, apiKey string, logger *logrus.Entry) *BackendRESTClient

NewBackendRESTClient Create a new BackendAPIClient with default logger labels

func (*BackendRESTClient) BuildRequest

func (c *BackendRESTClient) BuildRequest(endpoint string, method rest.Method) rest.Request

BuildRequest Create a REST request that can be sent through the SendGrid API

func (*BackendRESTClient) InvokeRequest

func (c *BackendRESTClient) InvokeRequest(request rest.Request) (*rest.Response, error)

InvokeRequest Invoke a REST request against the SendGrid API

type Client

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

Client Client used to generate new API keys for OpenShift clusters, abstracting sub user creation

func NewClient

func NewClient(sendgridClient APIClient, apiKeyScopes []string, passGen smtpdetails.PasswordGenerator, logger *logrus.Entry) (*Client, error)

NewClient Create new Client

func NewDefaultClient

func NewDefaultClient(logger *logrus.Entry) (*Client, error)

NewDefaultClient Create new client using API key from SENDGRID_API_KEY env var and the default SendGrid API host.

func (*Client) Create

func (c *Client) Create(id string) (*smtpdetails.SMTPDetails, error)

Create Generate new SendGrid sub user and API key for a cluster with it's ID

func (*Client) Delete

func (c *Client) Delete(id string) error

Delete Delete the SendGrid sub user associated with a cluster by the cluster ID

func (*Client) Get

func (c *Client) Get(id string) (*smtpdetails.SMTPDetails, error)

Get Retrieve the name of the SendGrid API key associated with an OpenShift cluster by it's ID

func (*Client) Refresh

func (c *Client) Refresh(id string) (*smtpdetails.SMTPDetails, error)

Refresh deletes the API key associated with a subuser and generates a new key

type IPAddress

type IPAddress struct {
	IP        string   `json:"ip"`
	Warmup    bool     `json:"warmup"`
	StartDate int      `json:"start_date"`
	SubUsers  []string `json:"subusers"`
	RDNS      string   `json:"rdns"`
	Pools     []string `json:"pools"`
}

IPAddress A SendGrid IP address, from https://sendgrid.com/docs/API_Reference/Web_API_v3/IP_Management/ip_addresses.html

type NotExistError

type NotExistError struct {
	Message string
}

NotExistError Error to indicate an API key does not exist

func (*NotExistError) Error

func (e *NotExistError) Error() string

Error String representation of error

type RESTClient

type RESTClient interface {
	BuildRequest(endpoint string, method rest.Method) rest.Request
	InvokeRequest(request rest.Request) (*rest.Response, error)
}

RESTClient Thin wrapper around the SendGrid package

type RESTClientMock

type RESTClientMock struct {
	// BuildRequestFunc mocks the BuildRequest method.
	BuildRequestFunc func(endpoint string, method rest.Method) rest.Request

	// InvokeRequestFunc mocks the InvokeRequest method.
	InvokeRequestFunc func(request rest.Request) (*rest.Response, error)
	// contains filtered or unexported fields
}

RESTClientMock is a mock implementation of RESTClient.

    func TestSomethingThatUsesRESTClient(t *testing.T) {

        // make and configure a mocked RESTClient
        mockedRESTClient := &RESTClientMock{
            BuildRequestFunc: func(endpoint string, method rest.Method) rest.Request {
	               panic("mock out the BuildRequest method")
            },
            InvokeRequestFunc: func(request rest.Request) (*rest.Response, error) {
	               panic("mock out the InvokeRequest method")
            },
        }

        // use mockedRESTClient in code that requires RESTClient
        // and then make assertions.

    }

func (*RESTClientMock) BuildRequest

func (mock *RESTClientMock) BuildRequest(endpoint string, method rest.Method) rest.Request

BuildRequest calls BuildRequestFunc.

func (*RESTClientMock) BuildRequestCalls

func (mock *RESTClientMock) BuildRequestCalls() []struct {
	Endpoint string
	Method   rest.Method
}

BuildRequestCalls gets all the calls that were made to BuildRequest. Check the length with:

len(mockedRESTClient.BuildRequestCalls())

func (*RESTClientMock) InvokeRequest

func (mock *RESTClientMock) InvokeRequest(request rest.Request) (*rest.Response, error)

InvokeRequest calls InvokeRequestFunc.

func (*RESTClientMock) InvokeRequestCalls

func (mock *RESTClientMock) InvokeRequestCalls() []struct {
	Request rest.Request
}

InvokeRequestCalls gets all the calls that were made to InvokeRequest. Check the length with:

len(mockedRESTClient.InvokeRequestCalls())

type SubUser

type SubUser struct {
	ID       int    `json:"id"`
	Username string `json:"username"`
	Email    string `json:"email"`
	Disabled bool   `json:"disabled"`
}

SubUser A SendGrid sub user, from https://sendgrid.com/docs/API_Reference/Web_API_v3/subusers.html

Jump to

Keyboard shortcuts

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