Some of the key benefits of using TypeScript are:
- clean and extensible object-oriented code
- increased productivity with auto-completion
- safe restructuring of code using the refactoring feature
- as-you-write error checking due to static typing
In this blog post we will show you how to write vRO code in TypeScript using classes, interfaces and polymorphism. In addition, we will go through writing unit/integration tests and referencing our code from another projects.
The vRO TypeScript support is an extension on the VMware PSCoE toolchain. To better understand the examples, I encourage you to read the Automating at Scale with vRO and vRA blog post.
vRO TypeScript Compiler (vrotsc)
The compiler is written in NodeJS and can be distributed as a single executable for Windows, MacOS, and Linux.
“Hello World” with vRO and TypeScript
Let’s get started with a very simple example. We have created a toolchain project with two TypeScript classes – Animal and Horse. Horse inherit from the abstract class Animal but implement its own version of the move functionality.
You write your code just like you do it in NodeJS. At the same time, you can use all vRO plug-ins and intrinsic APIs. The TypeScript compiler will automatically resolve modules based on the folder structure.
In order to compile the code above you execute the command:
- src is the root folder of the code
–classLib PSCoE – this option tells the compiler to use an external class library when transpiling. The compiler can work without a class library.
- -d types – when specifying this option, the compiler will generate type definitions for your code in the types folder.
When the package is imported in vRO, each file becomes an action.
A real-world vRO TypeScript project
Let’s look at an Infoblox library written in TypeScript. Here, you have a Network class, which extends the InfobloxEntity base class.
Next, we have interfaces for the the relevant Infoblox objects. Interfaces serve for static typing, which gives us better auto-completion and error checking.
Finally, we get to the implementation of the actual Network class.
Just like code, tests are written in TypeScript. To test your code, you can use Jasmine. Bellow is the test for the Infoblox Network class.
Along with the features provided by Jasmine, we have developed a set of mocking libraries, which help you easily mock vRO APIs, such as modules, configurations, resources, REST endpoints, etc.
The following example mocks the Infoblox network API using the REST mocking library.
Referencing an External Project
When referencing an external project, you need the type definitions for that project in order to benefit from the auto-completion and static type checking. We generate the types using the vrotsc command (-d option). Now, let’s see how to reference the Infoblox library in a new TypeScript project.
First, we import the dependencies from the Infoblox project.
Next, we declare a function, which establishes an HTTP connection with the Infoblox REST host and creates a new network using the fluent API of the Network class. We then log the network identifier using built-in vRO logging.
The function transpiles to a vRO action which expect two parameters: a vRO REST host object and a network name.
Now you can enjoy the best of both world – leverage the power of TypeScript in your cloud automation while using the built-in functionality of the VMware vRO platform.
Powered by WPeMatico