resourcename

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OrganizationTypePart is the type of HCP organization when used as a part of
	// a Resource Name.
	OrganizationTypePart = "organization"

	// ProjectTypePart is the type of HCP project when used as a part of
	// a Resource Name.
	ProjectTypePart = "project"

	// GeoTypePart is the type of HCP geography when used as a part of
	// a Resource Name.
	GeoTypePart = "geo"
)

Variables

View Source
var (
	// IsNamePart returns ozzo validator that verifies if given string is valid resource name part.
	// Name parts are case-sensitive and may contain lower-case alphanumeric
	// characters as well as dashes (-), dots (.) and underscores (_)
	//
	// For example:
	//   - e1e04e11-d590-41cf-b818-1535bc4b4889
	//   - my_cluster
	IsNamePart = validation.By(func(value interface{}) error {
		s, ok := value.(string)
		if !ok {
			return fmt.Errorf("invalid resource name part type: %T", value)
		}

		return validateNamePart(s)
	})

	// IsResourceName is a validation rule that evaluates whether the provided Resource Name satisfies the
	// requirements. Resource Name must have the following format:
	//
	//	<namespace>/<type a>/<name a>/<type b>/<name b>/…/<type z>/<name z>.
	//
	// Where
	//   - namespace - is expected to be based on the service name and may contain
	//     lower-case alphabetic characters as well as dashes (-)
	//   - type part - may contain lower-case alphabetic characters as well as
	//     dashes (-)
	//   - name part - is case-sensitive and may contain mixed-case alphanumeric
	//     characters as well as dashes (-), dots (.) and underscores (_)
	//
	// For example:
	//   - vault/project/e1e04e11-d590-41cf-b818-1535bc4b4889/cluster/my-cluster
	//   - vagrant/organization/ubuntu/box/lunar64/version/v20230130.0.0
	//
	// See [RFC-344] for more information.
	//
	// [RFC-344]: https://docs.google.com/document/d/1VY5pkYqKQ9-uQgQUIEBqgfVCKYTI99zchyMcwySgpdU/edit
	IsResourceName = validation.By(func(value interface{}) error {
		s, ok := value.(string)
		if !ok {
			return fmt.Errorf("invalid resource name type: %T", value)
		}

		if s == "" {
			return nil
		}

		return Validate(s)
	})
)

Functions

func ExtractGeo

func ExtractGeo(resourceName string) (string, error)

ExtractGeo extracts the geography set in the resource name or returns an error if the resource name is either invalid or doesn't contain a geography.

func ExtractOrganizationID

func ExtractOrganizationID(resourceName string) (string, error)

ExtractOrganizationID extracts the organization ID set in the resource name or returns an error if the resource name is either invalid or doesn't contain an organization as its first part.

func ExtractOrganizationOrProjectID

func ExtractOrganizationOrProjectID(resourceName string) (organizationID, projectID string, err error)

ExtractOrganizationOrProjectID extracts either a project ID or organization ID from the resource name. It returns an error if the resource name is invalid or doesn't contain either a project or organization ID in its first type part.

func ExtractProjectID

func ExtractProjectID(resourceName string) (string, error)

ExtractProjectID extracts the project ID set in the resource name or returns an error if the resource name is either invalid or doesn't contain a project as its first part.

func Generate

func Generate(namespace string, parts ...Part) (string, error)

Generate generates a Resource Name from a namespace and a list of parts.

func HasResourceNamePattern

func HasResourceNamePattern(pattern string) *hasResourceNamePattern

HasResourceNamePattern is a validation rule that evaluates whether the provided Resource Name is a valid resource name and that it matches the passed resource name pattern. See Validate for details on what consistitutes a valid resource name.

The passed pattern should be the format of resource name that is expected, with the values replaced with a '*'. The namespace, and part types are checked against the resource name being validated but the values are ignored. The pattern can exclude the namespace for organization and project resource names.

See the following examples where validation passes:

HasResourceNamePattern("organization/*").
  Validate("organization/e1e04e11-d590-41cf-b818-1535bc4b4889")

HasResourceNamePattern("project/*").
  Validate("project/e1e04e11-d590-41cf-b818-1535bc4b4889")

HasResourceNamePattern("iam/project/*/service-principal/*").
  Validate("iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/prod-sp")

See the following examples where validation fails:

HasResourceNamePattern("iam/project/*/service-principal/*").
  Validate("project/e1e04e11-d590-41cf-b818-1535bc4b4889")

HasResourceNamePattern("iam/organization/*/group/*").
  Validate("iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/prod-sp")

HasResourceNamePattern("iam/organization/*/group/*").
  Validate("network/project/e1e04e11-d590-41cf-b818-1535bc4b4889/hvn/aws")

func MustExtractGeo

func MustExtractGeo(resourceName string) string

MustExtractGeo extracts the geography from the resource name or panics if the passed resource name is invalid or doesn't contain a geography. This is intended to be a helper during tests only.

func MustExtractOrganizationID

func MustExtractOrganizationID(resourceName string) string

MustExtractOrganizationID extracts the organization ID from the resource name or panics if the passed resource name is invalid or doesn't contain an organization ID in its first part. This is intended to be a helper during tests only.

func MustExtractOrganizationOrProjectID

func MustExtractOrganizationOrProjectID(resourceName string) (organizationID, projectID string)

MustExtractOrganizationOrProjectID extracts either a project ID or organization ID from the resource name. It panics if the resource name is invalid or doesn't contain either a project or organization ID in its first type part. This is intended to be a helper during tests only.

func MustExtractProjectID

func MustExtractProjectID(resourceName string) string

MustExtractProjectID extracts the project ID from the resource name or panics if the passed resource name is invalid or doesn't contain a project ID in its first part. This is intended to be a helper during tests only.

func Pop

func Pop(resourceName string, numParts int) (string, error)

Pop pops the requested number of parts off of a resource name. See the following examples for behavior:

Pop("iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/foo/key/bar", 1)
  returns "iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/foo"

Pop("iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/foo/key/bar", 2)
  returns "project/e1e04e11-d590-41cf-b818-1535bc4b4889"

func Push

func Push(resourceName string, part Part) (string, error)

Push pushes the passed part onto an existing resource name. See the example:

 Push("iam/project/example/service-principal/foo", Part{Type: "key", Name: "bar"})
	 returns "iam/project/example/service-principal/foo/key/bar"

func Validate

func Validate(resourceName string) error

Validate checks that the provided Resource Name satisfies the requirements. Resource Name must have the following format:

<namespace>/<type a>/<name a>/<type b>/<name b>/…/<type z>/<name z>.

Where

  • namespace - is expected to be based on the service name and may contain lower-case alphabetic characters as well as dashes (-)
  • type part - may contain lower-case alphabetic characters as well as dashes (-)
  • name part - is case-sensitive and may contain lower-case alphanumeric characters as well as dashes (-), dots (.) and underscores (_)

For example:

  • vault/project/e1e04e11-d590-41cf-b818-1535bc4b4889/cluster/my-cluster
  • vagrant/organization/ubuntu/box/lunar64/version/v20230130.0.0

Projects and organizations adopt shorter resource names that exclude the <namespace> part.

For example:

  • organization/863063b6-c485-4cf4-8df3-6991a2ffd4b4
  • project/e1e04e11-d590-41cf-b818-1535bc4b4889
  • project/gTmD2KMMRt9DWhJz

See RFC-344 for more information.

func ValidatePattern

func ValidatePattern(resourceName, resourceNamePattern string) error

ValidatePattern validates whether the provided Resource Name is a valid resource name and that it matches the passed resource name pattern. See Validate for details on what consistitutes a valid resource name.

The passed pattern should be the format of resource name that is expected, with the values replaced with a '*'. The namespace, and part types are checked against the resource name being validated but the values are ignored. The pattern can exclude the namespace for organization and project resource names.

See the following examples where validation passes:

ValidatePattern("organization/e1e04e11-d590-41cf-b818-1535bc4b4889", "organization/*")

ValidatePattern("project/e1e04e11-d590-41cf-b818-1535bc4b4889", "project/*")

ValidatePattern("iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/prod-sp", "iam/project/*/service-principal/*")

See the following examples where validation fails:

ValidatePattern("project/e1e04e11-d590-41cf-b818-1535bc4b4889", "iam/project/*/service-principal/*")

ValidatePattern("iam/project/e1e04e11-d590-41cf-b818-1535bc4b4889/service-principal/prod-sp", "iam/organization/*/group/*")

ValidatePattern("network/project/e1e04e11-d590-41cf-b818-1535bc4b4889/hvn/aws", "iam/organization/*/group/*")

Types

type Part

type Part struct {
	Type string
	Name string
}

Part is a tuple representing a resource level in a Resource Name as described in RFC-344. It contains a resource type and a resource name.

For example, in a Resource Name "namespace/type1/name1/type2/name2", Parts are

type1:name1 and type2:name2

func GeoPart

func GeoPart(geo string) Part

GeoPart is a helper method that returns a Part based on the provided geography.

For example:

name, err := resourcename.Generate(
  "namespace",
  resources.ProjectPart("projectID"),
  resources.GeoPart("us"),
)

Will return "namespace/geo/geography

func OrganizationPart

func OrganizationPart(organizationID string) Part

OrganizationPart is a helper method that returns a Part based on the provided organization ID.

For example:

name, err := resourcename.Generate(
  "namespace",
  resources.OrganizationPart("organizationID"),
)

Will return "namespace/organization/organizationID"

func Parse

func Parse(resourceName string) (namespace string, parts []Part, error error)

Parse parses the provided Resource Name and returns individual components. If the Resource Name doesn't satisfy the requirements, the function returns a validation error. Resource Name must have the following format:

<namespace>/<type a>/<name a>/<type b>/<name b>/…/<type z>/<name z>.

Where

  • namespace - is expected to be based on the service name and may contain lower-case alphabetic characters as well as dashes (-)
  • type part - may contain lower-case alphabetic characters as well as dashes (-)
  • name part - is case-sensitive and may contain lower-case alphanumeric characters as well as dashes (-), dots (.) and underscores (_)

For example:

  • vault/project/e1e04e11-d590-41cf-b818-1535bc4b4889/cluster/my-cluster
  • vagrant/organization/ubuntu/box/lunar64/version/v20230130.0.0

See RFC-344 for more information.

func ProjectPart

func ProjectPart(projectID string) Part

ProjectPart is a helper method that returns a Part based on the provided project ID.

For example:

name, err := resourcename.Generate(
  "namespace",
  resources.ProjectPart("projectID"),
)

Will return "namespace/project/projectID

func (Part) String

func (p Part) String() string

String returns the string representation of an individual Part.

Jump to

Keyboard shortcuts

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