codegen

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2023 License: MIT Imports: 13 Imported by: 0

README

MikroTik code generation

This tool allows generating MikroTik resources for API client and Terraform resources based on Mikrotik struct definition.

MikroTik client resource

To generate new MikroTik resource definition, simply run

$ go run ./cmd/mikrotik-codegen mikrotik -name BridgeVlan -commandBase "/interface/bridge/vlan"

where

name - a name of MikroTik resource to generate.

commandBase - base path to craft commands for CRUD operations.

Terraform resource

Just add a codegen tag key to struct fields:

type MikrotikResource struct{
	Id             string   `mikrotik:".id"             codegen:"id,mikrotikID,deleteID"`
	Name           string   `mikrotik:"name"            codegen:"name,required,terraformID"`
	Enabled        bool     `mikrotik:"enabled"         codegen:"enabled"`
	Items          []string `mikrotik:"items"           codegen:"items,elemType=string"`
	UpdatedAt      string   `mikrotik:"updated_at"      codegen:"updated_at,computed"`
	Unused         int      `mikrotik:"unused"          codegen:"-"`
	NotImplemented int      `mikrotik:"not_implemented" codegen:"not_implemented,omit"`
	Comment        string   `mikrotik:"comment"         codegen:"comment"`
}

and run:

$ go run ./cmd/mikrotik-codegen terraform -src client/resource.go -struct MikrotikResource > mikrotik/resource_new.go

Supported options

Name Description
terraformID Use this field during Read and Import resource
mikrotikID This field is MikroTik ID field, usually .id
deleteID Terraform resource will use this field to delete resource
required Mark field as required in resource schema
optional Mark field as optional in resource schema
computed Mark field as computed in resource schema
elemType Explicitly set element type for List or Set attributes. Usage elemType=int
omit Skip this field from code generation process

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateMikrotikResource added in v0.13.0

func GenerateMikrotikResource(resourceName, commandBasePath string, w io.Writer) error

func GenerateResource

func GenerateResource(s *Struct, w io.Writer) error

GenerateResource generates Terraform resource and writes it to specified output

func SourceFormatHook

func SourceFormatHook(p []byte) ([]byte, error)

SourceFormatHook formats code using Go's formatter

Types

type Field

type Field struct {
	// OriginalName is an original field name without chnages.
	OriginalName string

	// Name is a field name defined by struct tag.
	Name string

	// Required marks field as `required` in Terraform definition.
	Required bool

	// Optional marks field as `optional` in Terraform definition.
	Optional bool

	// Computed marks field as `computed` in Terraform definition.
	Computed bool

	// Type holds a field type.
	Type string

	// ElemType holds an element type if field type is List or Set
	ElemType string
}

Field holds information about particular field in parsed struct.

type SourceWriteHookFunc

type SourceWriteHookFunc func([]byte) ([]byte, error)

SourceWriteHookFunc defines a hook func to mutate source before writing to destination

type Struct

type Struct struct {
	// Name is a of parsed struct.
	Name string

	// MikrotikIDField is a field name which holds MikroTik resource ID.
	MikrotikIDField string

	// TerraformIDField holds a field name which will be used as Terraform resource ID.
	TerraformIDField string

	// DeleteField holds a field name to use when deleting resource on MikroTik system.
	DeleteField string

	// Fields is a collection of field definitions in the parsed struct.
	Fields []*Field
}

Struct holds information about parsed struct.

func ParseFile

func ParseFile(filename string, startLine int, structName string) (*Struct, error)

ParseFile parses a .go file with struct declaration.

This functions searches for struct definition `structName` and parses it. If `structName` is empty, function stops at first struct definition in the file right after `startLine`.

type Type

type Type interface {
	// Type returns a type name as string.
	// It must be stable for the same type.
	Name() string

	// Is checks whether two types are the same.
	Is(Type) bool
}

Type represents Terraform field type to use for particular MikroTik field.

var (
	StringType  Type = basetype{/* contains filtered or unexported fields */}
	Int64Type   Type = basetype{/* contains filtered or unexported fields */}
	ListType    Type = basetype{/* contains filtered or unexported fields */}
	SetType     Type = basetype{/* contains filtered or unexported fields */}
	BoolType    Type = basetype{/* contains filtered or unexported fields */}
	UnknownType Type = basetype{/* contains filtered or unexported fields */}
)

Jump to

Keyboard shortcuts

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