Getting started with creating a ROR resource definition
Prerequisites
- Good knowledge about the resource you're planning to implement
- Golang SDK: https://go.dev
Optional
- A clear picture of what you want to implement and how go generalize it
General information
When creating a new resource, editing, or deleting an existing resource for ROR the generator MUST be ran, this is required for amongst other things to update the TypeScript types.
Getting started
Cloning the repository
To start with you need to first setup your repositories ready for development. For how to do that please see here
Creating the base resource files
Create the resource definition
- Creating it at:
ror/pkg/rorresources/rortypes
with the format:
resourcedef_<resourceName>.go
- When creating this file the following rule must be followed:
- The package name is
rortypes - The structs all begin with
Resourceresulting your type to beResource<YourResourceName> - Your main struct should contain a
Specand aStatusfield - The
Specis the desired state, whileStatusis observed state, more about that further down.
- The package name is
An example of a resourcedef:
package rortypes
type ResourceVirtualMachine struct {
Id string `json:"id"`
Name string `json:"name"`
Spec ResourceVirtualMachineSpec `json:"spec"`
Status ResourceVirtualMachineStatus `json:"status"`
}
type ResourceVirtualMachineSpec struct {
Cpu ResourceVirtualMachineCpuSpec `json:"cpu"`
Tags []ResourceVirtualMachineTagSpec `json:"tags"`
Disks []ResourceVirtualMachineDiskSpec `json:"disks"`
Memory ResourceVirtualMachineMemorySpec `json:"memory"`
Networks []ResourceVirtualMachineNetworkSpec `json:"networks"`
OperatingSystem ResourceVirtualMachineOperatingSystemSpec `json:"operatingSystem"`
}
type ResourceVirtualMachineStatus struct {
Cpu ResourceVirtualMachineCpuStatus `json:"cpu"`
Disks []ResourceVirtualMachineDiskStatus `json:"disks"`
Memory ResourceVirtualMachineMemoryStatus `json:"memory"`
Networks []ResourceVirtualMachineNetworkStatus `json:"networks"`
OperatingSystem ResourceVirtualMachineOperatingSystemStatus `json:"operatingSystem"`
}
.
.
.
Spec
Spec will be what we desire the configuration to be for when we wish to change something, like cpu, disk, or memory for the example above. Any parameter within here it is expected we're allowed to change if change from ROR is implmeneted.
If the resource is read-only Spec is necessary to implement.
Status
Status will be what we observe about this resource, like ids, the current cpu, disk, or memory for the example above.
Create the resource_input_filter definition
The resource_input_filter runs on import on that type, it can be used to censor, anonymize, remove, or change data before import.
While it has to be defined on every type, generally we create it and return nil for no action.
- Create it at:
ror/pkg/rorresources/rortypes
with the format:
resouce_input_filter<resourceName>.go
- Which should contain something to this example with
swapped out with your core struct:
package rortypes
// (r *<ResourceName>) ApplyInputFilter Applies the input filter to the resource
func (r *<ResourceName>) ApplyInputFilter(cr *CommonResource) error {
return nil
}
ROR definitions
- Go to:
ror/pkg/rorresources/rordefs
- And edit defs.go
- On the top const definition, add your new agent in the format:
ApiResoureType<Name> ApiReesourceType = "<AgentName>"
- And at the near bottom at at to the Resroucedefs slice:
{
TypeMeta: metav1.TypeMeta{
Kind: "<Name",
APIVersion: "<Version of your choice>",
},
Plural: "<Plural of name>",
Namespaced: false,
Types: []ApiResourceType{<Type>},
},
Running the generator
- Go to the root folder of ROR
- Run the following command:
go run cmd/generator/main.go
- And you're done, commit to the branch and make a pull/merge request.