codegen

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Boolean string = "boolean"
	Integer string = "integer"
	Number  string = "number"
	String  string = "string"
	Array   string = "array"
	Object  string = "object"
)
View Source
const DefaultName = "crds"

DefaultName specifies the default value for the package name

View Source
const DotNet string = "dotnet"
View Source
const Go string = "go"
View Source
const NodeJS string = "nodejs"
View Source
const PulumiToolName = "crd2pulumi"

PulumiToolName is a symbol that identifies to Pulumi the name of this program.

View Source
const Python string = "python"

Variables

View Source
var SupportedLanguages = []string{
	DotNet,
	Go,
	NodeJS,
	Python,
}
View Source
var UnneededGoFiles = codegen.NewStringSet(

	"doc.go",
	"init.go",
	"provider.go",

	"meta/v1/pulumiTypes.go",

	"utilities/pulumiUtilities.go",
	"utilities/pulumiVersion.go",
)

Functions

func AddType

func AddType(schema map[string]any, name string, types map[string]pschema.ComplexTypeSpec)

AddType converts the given OpenAPI `schema` to a ObjectTypeSpec and adds it to the `types` map under the given `name`. Recursively converts and adds all nested schemas as well.

func CombineSchemas

func CombineSchemas(combineRequired bool, schemas ...map[string]any) map[string]any

CombineSchemas combines the `properties` fields of the given sub-schemas into a single schema. Returns nil if no schemas are given. Returns the schema if only 1 schema is given. If combineRequired == true, then each sub-schema's `required` fields are also combined. In this case the combined schema's `required` field is of type []any, not []string.

func Generate

func Generate(cs *CodegenSettings, yamls []io.ReadCloser) error

Generate performs the entire CRD codegen process, reading YAML content from the given readers.

func GenerateDotNet

func GenerateDotNet(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error)

func GenerateFromFiles

func GenerateFromFiles(cs *CodegenSettings, yamlPaths []string) error

GenerateFromFiles performs the entire CRD codegen process. The yamlPaths argument can contain both file paths and URLs.

func GenerateGo

func GenerateGo(pg *PackageGenerator, name string) (buffers map[string]*bytes.Buffer, err error)

func GenerateNodeJS

func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error)

func GeneratePython

func GeneratePython(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error)

func GetTypeSpec

func GetTypeSpec(schema map[string]any, name string, types map[string]pschema.ComplexTypeSpec) pschema.TypeSpec

GetTypeSpec returns the corresponding pschema.TypeSpec for a OpenAPI v3 schema. Handles nested pschema.TypeSpecs in case the schema type is an array, object, or "combined schema" (oneOf, allOf, anyOf). Also recursively converts and adds all schemas of type object to the types map.

Types

type CodegenSettings

type CodegenSettings struct {
	Language       string
	OutputDir      string
	PackageName    string
	PackageVersion string
	Overwrite      bool
	ShouldGenerate bool
}

func (*CodegenSettings) Path

func (cs *CodegenSettings) Path() string

type CustomResourceGenerator

type CustomResourceGenerator struct {
	// CustomResourceDefinition contains the unmarshalled CRD YAML
	CustomResourceDefinition unstructured.Unstructured
	// Schemas represents a mapping from each version in the `spec.versions`
	// list to its corresponding `openAPIV3Schema` field in the CRD YAML
	Schemas map[string]map[string]any
	// ApiVersion represents the `apiVersion` field in the CRD YAML
	APIVersion string
	// Kind represents the `spec.names.kind` field in the CRD YAML
	Kind string
	// Plural represents the `spec.names.plural` field in the CRD YAML
	Plural string
	// Group represents the `spec.group` field in the CRD YAML
	Group string
	// Versions is a slice of names of each version supported by this CRD
	Versions []string
	// GroupVersions is a slice of names of each version, in the format
	// <group>/<version>.
	GroupVersions []string
	// ResourceTokens is a slice of the token types of every versioned
	// CustomResource
	ResourceTokens []string
}

CustomResourceGenerator generates a Pulumi schema for a single CustomResource

func (*CustomResourceGenerator) HasSchemas

func (crg *CustomResourceGenerator) HasSchemas() bool

HasSchemas returns true if the CustomResource specifies at least some schema, and false otherwise.

type GenerateFunc

type GenerateFunc func(pg *PackageGenerator, name string) (mapFileNameToData map[string]*bytes.Buffer, err error)

GenerateFunc is the function that is called by the generator to generate the code. It returns a mapping of filename to the contents of said file and any error that may have occurred.

type PackageGenerator

type PackageGenerator struct {
	// CustomResourceGenerators contains a slice of all CustomResourceGenerators
	CustomResourceGenerators []CustomResourceGenerator
	// ResourceTokens is a slice of the token types of every CustomResource
	ResourceTokens []string
	// GroupVersions is a slice of the names of every CustomResource's versions,
	// in the format <group>/<version>
	GroupVersions []string
	// Types is a mapping from every type's token name to its ComplexTypeSpec
	Types map[string]pschema.ComplexTypeSpec
	// Version is the semver that will be stamped into the generated package
	Version string
	// contains filtered or unexported fields
}

PackageGenerator generates code for multiple CustomResources

func ReadPackagesFromSource

func ReadPackagesFromSource(version string, yamlSources []io.ReadCloser) (*PackageGenerator, error)

ReadPackagesFromSource reads one or more documents and returns a PackageGenerator that can be used to generate Pulumi code. Calling this function will fully read and close each document.

func (*PackageGenerator) GetTypes

func (pg *PackageGenerator) GetTypes() map[string]pschema.ComplexTypeSpec

func (*PackageGenerator) HasSchemas

func (pg *PackageGenerator) HasSchemas() bool

HasSchemas returns true if there exists at least one CustomResource with a schema in this package.

func (*PackageGenerator) ModuleToPackage

func (pg *PackageGenerator) ModuleToPackage() (map[string]string, error)

Returns language-specific 'ModuleToPackage' map. Creates a mapping from every groupVersion string <group>/<version> to <groupPrefix>/<version>.

func (*PackageGenerator) SchemaPackage

func (pg *PackageGenerator) SchemaPackage() *pschema.Package

SchemaPackage returns the Pulumi schema package with no ObjectMeta type. This is only necessary for NodeJS and Python.

func (*PackageGenerator) SchemaPackageWithObjectMetaType

func (pg *PackageGenerator) SchemaPackageWithObjectMetaType() *pschema.Package

SchemaPackageWithObjectMetaType returns the Pulumi schema package with an ObjectMeta type. This is only necessary for Go and .NET.

Jump to

Keyboard shortcuts

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