endpoints

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package endpoints provides the types and functionality for defining regions and endpoints, as well as querying those definitions.

The SDK's Regions and Endpoints metadata is code generated into the endpoints package, and is accessible via the DefaultResolver function. This function returns a endpoint Resolver will search the metadata and build an associated endpoint if one is found. The default resolver will search all partitions known by the SDK. e.g AWS Standard (aws), AWS China (aws-cn), and AWS GovCloud (US) (aws-us-gov). .

Enumerating Regions and Endpoint Metadata

Casting the Resolver returned by DefaultResolver to a EnumPartitions interface will allow you to get access to the list of underlying Partitions with the Partitions method. This is helpful if you want to limit the SDK's endpoint resolving to a single partition, or enumerate regions, services, and endpoints in the partition.

resolver := endpoints.DefaultResolver()
partitions := resolver.(endpoints.EnumPartitions).Partitions()

for _, p := range partitions {
    fmt.Println("Regions for", p.ID())
    for id, _ := range p.Regions() {
        fmt.Println("*", id)
    }

    fmt.Println("Services for", p.ID())
    for id, _ := range p.Services() {
        fmt.Println("*", id)
    }
}

Using Custom Endpoints

The endpoints package also gives you the ability to use your own logic how endpoints are resolved. This is a great way to define a custom endpoint for select services, without passing that logic down through your code.

If a type implements the Resolver interface it can be used to resolve endpoints. To use this with the SDK's Session and Config set the value of the type to the EndpointsResolver field of nifcloud.Config when initializing the session, or service client.

In addition the ResolverFunc is a wrapper for a func matching the signature of Resolver.EndpointFor, converting it to a type that satisfies the Resolver interface.

myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
    if service == endpoints.S3ServiceID {
        return endpoints.ResolvedEndpoint{
            URL:           "s3.custom.endpoint.com",
            SigningRegion: "custom-signing-region",
        }, nil
    }

    return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
}

sess := session.Must(session.NewSession(&nifcloud.Config{
    Region:           nifcloud.String("us-west-2"),
    EndpointResolver: endpoints.ResolverFunc(myCustomResolver),
}))

Index

Examples

Constants

View Source
const (
	JpEast1RegionID = "jp-east-1" // jp-east-1.
	JpEast2RegionID = "jp-east-2" // jp-east-2.
	JpEast3RegionID = "jp-east-3" // jp-east-3.
	JpEast4RegionID = "jp-east-4" // jp-east-4.
	JpWest1RegionID = "jp-west-1" // jp-west-1.
	UsEast1RegionID = "us-east-1" // us-east-1.
)

NIFCLOUD Standard partition's regions.

View Source
const (
	ComputingServiceID   = "computing"   // Computing.
	Ec2metadataServiceID = "ec2metadata" // Ec2metadata.
	NasServiceID         = "nas"         // Nas.
	RdbServiceID         = "rdb"         // Rdb.
	ScriptServiceID      = "script"      // Script.
)

Service identifiers

View Source
const (
	NifcloudPartitionID = "nifcloud" // NIFCLOUD Standard partition.
)

Partition identifiers

Variables

This section is empty.

Functions

func AddScheme

func AddScheme(endpoint string, disableSSL bool) string

AddScheme adds the HTTP or HTTPS schemes to a endpoint URL if there is no scheme. If disableSSL is true HTTP will set HTTP instead of the default HTTPS.

If disableSSL is set, it will only set the URL's scheme if the URL does not contain a scheme.

func DisableSSLOption

func DisableSSLOption(o *Options)

DisableSSLOption sets the DisableSSL options. Can be used as a functional option when resolving endpoints.

func RegionsForService

func RegionsForService(ps []Partition, partitionID, serviceID string) (map[string]Region, bool)

RegionsForService returns a map of regions for the partition and service. If either the partition or service does not exist false will be returned as the second parameter.

This example shows how to get the regions for DynamoDB in the AWS partition.

rs, exists := endpoints.RegionsForService(endpoints.DefaultPartitions(), endpoints.AwsPartitionID, endpoints.DynamodbServiceID)

This is equivalent to using the partition directly.

rs := endpoints.AwsPartition().Services()[endpoints.DynamodbServiceID].Regions()

func ResolveUnknownServiceOption

func ResolveUnknownServiceOption(o *Options)

ResolveUnknownServiceOption sets the ResolveUnknownService option. Can be used as a functional option when resolving endpoints.

func StrictMatchingOption

func StrictMatchingOption(o *Options)

StrictMatchingOption sets the StrictMatching option. Can be used as a functional option when resolving endpoints.

func UseDualStackOption

func UseDualStackOption(o *Options)

UseDualStackOption sets the UseDualStack option. Can be used as a functional option when resolving endpoints.

Types

type DecodeModelOptions

type DecodeModelOptions struct {
	SkipCustomizations bool
}

A DecodeModelOptions are the options for how the endpoints model definition are decoded.

func (*DecodeModelOptions) Set

func (d *DecodeModelOptions) Set(optFns ...func(*DecodeModelOptions))

Set combines all of the option functions together.

type Endpoint

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

A Endpoint provides information about endpoints, and provides the ability to resolve that endpoint for the service, and the region the endpoint represents.

func (Endpoint) ID

func (e Endpoint) ID() string

ID returns the identifier for an endpoint.

func (Endpoint) ResolveEndpoint

func (e Endpoint) ResolveEndpoint(opts ...func(*Options)) (ResolvedEndpoint, error)

ResolveEndpoint resolves an endpoint from the context of a service and region the endpoint represents. See Partition.EndpointFor for usage and errors that can be returned.

func (Endpoint) ServiceID

func (e Endpoint) ServiceID() string

ServiceID returns the identifier the endpoint belongs to.

type EndpointNotFoundError

type EndpointNotFoundError struct {
	Partition string
	Service   string
	Region    string
	// contains filtered or unexported fields
}

A EndpointNotFoundError is returned when in StrictMatching mode, and the endpoint for the service and region cannot be found in any of the partitions.

type EnumPartitions

type EnumPartitions interface {
	Partitions() []Partition
}

EnumPartitions a provides a way to retrieve the underlying partitions that make up the SDK's default Resolver, or any resolver decoded from a model file.

Use this interface with DefaultResolver and DecodeModels to get the list of Partitions.

Example
package main

import (
	"fmt"

	"github.com/shztki/nifcloud-sdk-go/nifcloud/endpoints"
)

func main() {
	resolver := endpoints.DefaultResolver()
	partitions := resolver.(endpoints.EnumPartitions).Partitions()

	for _, p := range partitions {
		fmt.Println("Regions for", p.ID())
		for id := range p.Regions() {
			fmt.Println("*", id)
		}

		fmt.Println("Services for", p.ID())
		for id := range p.Services() {
			fmt.Println("*", id)
		}
	}
}
Output:

type Options

type Options struct {
	// DisableSSL forces the endpoint to be resolved as HTTP.
	// instead of HTTPS if the service supports it.
	DisableSSL bool

	// Sets the resolver to resolve the endpoint as a dualstack endpoint
	// for the service. If dualstack support for a service is not known and
	// StrictMatching is not enabled a dualstack endpoint for the service will
	// be returned. This endpoint may not be valid. If StrictMatching is
	// enabled only services that are known to support dualstack will return
	// dualstack endpoints.
	UseDualStack bool

	// Enables strict matching of services and regions resolved endpoints.
	// If the partition doesn't enumerate the exact service and region an
	// error will be returned. This option will prevent returning endpoints
	// that look valid, but may not resolve to any real endpoint.
	StrictMatching bool

	// Enables resolving a service endpoint based on the region provided if the
	// service does not exist. The service endpoint ID will be used as the service
	// domain name prefix. By default the endpoint resolver requires the service
	// to be known when resolving endpoints.
	//
	// If resolving an endpoint on the partition list the provided region will
	// be used to determine which partition's domain name pattern to the service
	// endpoint ID with. If both the service and region are unkonwn and resolving
	// the endpoint on partition list an UnknownEndpointError error will be returned.
	//
	// If resolving and endpoint on a partition specific resolver that partition's
	// domain name pattern will be used with the service endpoint ID. If both
	// region and service do not exist when resolving an endpoint on a specific
	// partition the partition's domain pattern will be used to combine the
	// endpoint and region together.
	//
	// This option is ignored if StrictMatching is enabled.
	ResolveUnknownService bool
}

Options provide the configuration needed to direct how the endpoints will be resolved.

func (*Options) Set

func (o *Options) Set(optFns ...func(*Options))

Set combines all of the option functions together.

type Partition

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

A Partition provides the ability to enumerate the partition's regions and services.

func DefaultPartitions

func DefaultPartitions() []Partition

DefaultPartitions returns a list of the partitions the SDK is bundled with. The available partitions are: NIFCLOUD Standard.

partitions := endpoints.DefaultPartitions
for _, p := range partitions {
    // ... inspect partitions
}

func NifcloudPartition

func NifcloudPartition() Partition

NifcloudPartition returns the Resolver for NIFCLOUD Standard.

func PartitionForRegion

func PartitionForRegion(ps []Partition, regionID string) (Partition, bool)

PartitionForRegion returns the first partition which includes the region passed in. This includes both known regions and regions which match a pattern supported by the partition which may include regions that are not explicitly known by the partition. Use the Regions method of the returned Partition if explicit support is needed.

func (Partition) EndpointFor

func (p Partition) EndpointFor(service, region string, opts ...func(*Options)) (ResolvedEndpoint, error)

EndpointFor attempts to resolve the endpoint based on service and region. See Options for information on configuring how the endpoint is resolved.

If the service cannot be found in the metadata the UnknownServiceError error will be returned. This validation will occur regardless if StrictMatching is enabled. To enable resolving unknown services set the "ResolveUnknownService" option to true. When StrictMatching is disabled this option allows the partition resolver to resolve a endpoint based on the service endpoint ID provided.

When resolving endpoints you can choose to enable StrictMatching. This will require the provided service and region to be known by the partition. If the endpoint cannot be strictly resolved an error will be returned. This mode is useful to ensure the endpoint resolved is valid. Without StrictMatching enabled the endpoint returned my look valid but may not work. StrictMatching requires the SDK to be updated if you want to take advantage of new regions and services expansions.

Errors that can be returned.

  • UnknownServiceError
  • UnknownEndpointError

func (Partition) ID

func (p Partition) ID() string

ID returns the identifier of the partition.

func (Partition) Regions

func (p Partition) Regions() map[string]Region

Regions returns a map of Regions indexed by their ID. This is useful for enumerating over the regions in a partition.

func (Partition) Services

func (p Partition) Services() map[string]Service

Services returns a map of Service indexed by their ID. This is useful for enumerating over the services in a partition.

type Region

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

A Region provides information about a region, and ability to resolve an endpoint from the context of a region, given a service.

func (Region) Description

func (r Region) Description() string

Description returns the region's description. The region description is free text, it can be empty, and it may change between SDK releases.

func (Region) ID

func (r Region) ID() string

ID returns the region's identifier.

func (Region) ResolveEndpoint

func (r Region) ResolveEndpoint(service string, opts ...func(*Options)) (ResolvedEndpoint, error)

ResolveEndpoint resolves an endpoint from the context of the region given a service. See Partition.EndpointFor for usage and errors that can be returned.

func (Region) Services

func (r Region) Services() map[string]Service

Services returns a list of all services that are known to be in this region.

type ResolvedEndpoint

type ResolvedEndpoint struct {
	// The endpoint URL
	URL string

	// The region that should be used for signing requests.
	SigningRegion string

	// The service name that should be used for signing requests.
	SigningName string

	// States that the signing name for this endpoint was derived from metadata
	// passed in, but was not explicitly modeled.
	SigningNameDerived bool

	// The signing method that should be used for signing requests.
	SigningMethod string
}

A ResolvedEndpoint is an endpoint that has been resolved based on a partition service, and region.

type Resolver

type Resolver interface {
	EndpointFor(service, region string, opts ...func(*Options)) (ResolvedEndpoint, error)
}

A Resolver provides the interface for functionality to resolve endpoints. The build in Partition and DefaultResolver return value satisfy this interface.

func DecodeModel

func DecodeModel(r io.Reader, optFns ...func(*DecodeModelOptions)) (Resolver, error)

DecodeModel unmarshals a Regions and Endpoint model definition file into a endpoint Resolver. If the file format is not supported, or an error occurs when unmarshaling the model an error will be returned.

Casting the return value of this func to a EnumPartitions will allow you to get a list of the partitions in the order the endpoints will be resolved in.

resolver, err := endpoints.DecodeModel(reader)

partitions := resolver.(endpoints.EnumPartitions).Partitions()
for _, p := range partitions {
    // ... inspect partitions
}

func DefaultResolver

func DefaultResolver() Resolver

DefaultResolver returns an Endpoint resolver that will be able to resolve endpoints for: NIFCLOUD Standard.

Use DefaultPartitions() to get the list of the default partitions.

type ResolverFunc

type ResolverFunc func(service, region string, opts ...func(*Options)) (ResolvedEndpoint, error)

ResolverFunc is a helper utility that wraps a function so it satisfies the Resolver interface. This is useful when you want to add additional endpoint resolving logic, or stub out specific endpoints with custom values.

Example
package main

import (
	"github.com/shztki/nifcloud-sdk-go/nifcloud"
	"github.com/shztki/nifcloud-sdk-go/nifcloud/endpoints"
	"github.com/shztki/nifcloud-sdk-go/nifcloud/session"
	"github.com/shztki/nifcloud-sdk-go/service/s3"
	"github.com/shztki/nifcloud-sdk-go/service/sqs"
)

func main() {
	myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
		if service == endpoints.S3ServiceID {
			return endpoints.ResolvedEndpoint{
				URL:           "s3.custom.endpoint.com",
				SigningRegion: "custom-signing-region",
			}, nil
		}

		return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
	}

	sess := session.Must(session.NewSession(&nifcloud.Config{
		Region:           nifcloud.String("us-west-2"),
		EndpointResolver: endpoints.ResolverFunc(myCustomResolver),
	}))

	// Create the S3 service client with the shared session. This will
	// automatically use the S3 custom endpoint configured in the custom
	// endpoint resolver wrapping the default endpoint resolver.
	s3Svc := s3.New(sess)
	// Operation calls will be made to the custom endpoint.
	s3Svc.GetObject(&s3.GetObjectInput{
		Bucket: nifcloud.String("myBucket"),
		Key:    nifcloud.String("myObjectKey"),
	})

	// Create the SQS service client with the shared session. This will
	// fallback to the default endpoint resolver because the customization
	// passes any non S3 service endpoint resolve to the default resolver.
	sqsSvc := sqs.New(sess)
	// Operation calls will be made to the default endpoint for SQS for the
	// region configured.
	sqsSvc.ReceiveMessage(&sqs.ReceiveMessageInput{
		QueueUrl: nifcloud.String("my-queue-url"),
	})
}
Output:

func (ResolverFunc) EndpointFor

func (fn ResolverFunc) EndpointFor(service, region string, opts ...func(*Options)) (ResolvedEndpoint, error)

EndpointFor wraps the ResolverFunc function to satisfy the Resolver interface.

type Service

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

A Service provides information about a service, and ability to resolve an endpoint from the context of a service, given a region.

func (Service) Endpoints

func (s Service) Endpoints() map[string]Endpoint

Endpoints returns a map of Endpoints indexed by their ID for all known endpoints for a service.

A region is the AWS region the service exists in. Whereas a Endpoint is an URL that can be resolved to a instance of a service.

func (Service) ID

func (s Service) ID() string

ID returns the identifier for the service.

func (Service) Regions

func (s Service) Regions() map[string]Region

Regions returns a map of Regions that the service is present in.

A region is the AWS region the service exists in. Whereas a Endpoint is an URL that can be resolved to a instance of a service.

func (Service) ResolveEndpoint

func (s Service) ResolveEndpoint(region string, opts ...func(*Options)) (ResolvedEndpoint, error)

ResolveEndpoint resolves an endpoint from the context of a service given a region. See Partition.EndpointFor for usage and errors that can be returned.

type UnknownEndpointError

type UnknownEndpointError struct {
	Partition string
	Service   string
	Region    string
	Known     []string
	// contains filtered or unexported fields
}

A UnknownEndpointError is returned when in StrictMatching mode and the service is valid, but the region does not resolve to an endpoint. Includes a list of all known endpoints for the service.

func NewUnknownEndpointError

func NewUnknownEndpointError(p, s, r string, known []string) UnknownEndpointError

NewUnknownEndpointError builds and returns UnknownEndpointError.

func (UnknownEndpointError) Error

func (e UnknownEndpointError) Error() string

String returns the string representation of the error.

func (UnknownEndpointError) String

func (e UnknownEndpointError) String() string

String returns the string representation of the error.

type UnknownServiceError

type UnknownServiceError struct {
	Partition string
	Service   string
	Known     []string
	// contains filtered or unexported fields
}

A UnknownServiceError is returned when the service does not resolve to an endpoint. Includes a list of all known services for the partition. Returned when a partition does not support the service.

func NewUnknownServiceError

func NewUnknownServiceError(p, s string, known []string) UnknownServiceError

NewUnknownServiceError builds and returns UnknownServiceError.

func (UnknownServiceError) Error

func (e UnknownServiceError) Error() string

String returns the string representation of the error.

func (UnknownServiceError) String

func (e UnknownServiceError) String() string

String returns the string representation of the error.

Jump to

Keyboard shortcuts

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