Skip to main content

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.

Styra VS Code Tools Snippet ListStyra VS Code Tools Snippet List

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.

  1. The trigger phrase.
  2. The short description.
  3. The long description.
  4. Inserted description for the current item, which is pasted into the editor.
  5. The documentation link.
  6. Package/import header that allows the code block to execute if you copy/paste it to the Rego playground.
  7. Sample data, surrounded by START/END comments for clarity.
  8. Code, with annotations as necessary.
Styra VS Code Tools Snippet DescriptionsStyra VS Code Tools Snippet Descriptions

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 Code
  • styra link init from the Styra CLI
note

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.

Styra VS Code Tools Kubernetes ExampleStyra VS Code Tools Kubernetes Example