Rego Snippets
The Rego programming language is declarative rather than imperative, and might be a programming style developers are not used to. The Styra VS Code Tools extension provides various Rego snippets to reduce your learning curve:
General Snippets
Open any Rego file and type rego
to see the list of available snippets shown in the following screenshot.
The first snippet, rego__help
provides a brief introduction to using Rego snippets and Styra DAS snippets. If you select any other Rego snippet from the list above, you will see the elements shown in the following screenshot.
- The trigger phrase.
- The short description.
- The long description.
- Inserted description for the current item, which is pasted into the editor.
- The documentation link.
- Package/import header that allows the code block to execute if you copy/paste it to the Rego playground.
- Sample data, surrounded by START/END comments for clarity.
- Code, with annotations as necessary.
A new Rego developer needs to learn how to apply a set of conditions by either conjunction or disjunction. Those are the first two in the list of snippets: rego_ex_CONDITION_AND
and rego_ex_CONDITION_OR
. Each example provides runnable code. Insert the snippet into an empty Rego document, save it, and run OPA: Evaluate Package. With these snippets at your fingertips, you can reduce the number of times you have to reference the Rego documentation.
Policy Snippets
Styra VS Code Tools also provides policy snippets (only for available for the Kubernetes System type).
Policy snippets are installed in a project when you connect to Styra DAS through the following:
Styra Link: Initialize
command in VS Codestyra link init
from the Styra CLI
When you update the Styra extension at a later date and it contains any snippet updates, those will be automatically updated in your project.
Styra provides many Kubernetes policy snippets. As you type, VS Code Intellisense displays a filtered list of snippets, as shown in the following screenshot.