Creating Db2 site rules

Db2® site rules specify how developers should change object definitions in application instances that were provisioned from Db2 applications.

About this task

You can create simple and complex site rules. Simple rules are built using three segments: object, attribute, and verification type. For example, you can create a site rule that specifies that table (object) names (attribute) must start with (verification type) the string "TAB". When applied to an application or environment, developers creating or editing a table in a provisioned application instance are notified if they violate the rule.

Complex site rules are more flexible and powerful. You define them using a small domain-specific programming language that is composed of a subset of the Python syntax. Every valid complex site rule is a Python expression that is evaluated and resolved to either True or False according to normal Python rules. For example, an empty string is False, and the number 7 is True. Expressions can use the following features:

  • Python ternary expressions (x if y else z)
  • Boolean and/or/not
  • Parentheses
  • <
  • >
  • <=
  • >=
  • ==
  • !=
  • +
  • -
  • String subscription (stride not supported)
  • The functions: len, startswith, and endswith

Supported data types are integers, strings (delimited by double or single quotation marks), and Boolean operators.

Complex site rules have one non-standard Python feature: a predefined variable for every object attribute. For example, to specify that “table names must start with the letter T", you would select Table from Object to create the following site rule:
name.startswith(“T”)

The predefined string variable name holds the value of the table name. The rest is standard Python syntax. Here is another valid way to write that rule:

name[:1] == “T”

For a list of objects and their attribute variables, see Complex Db2 site rule variables.

Rules can be combined to make standard Python expressions. For example, here is one way to create the rule “if a table space name is between 4 and 6 characters, tables in that table space must end their names with the last four characters of their table space”:

name.endswith(TSNAME[-4:]) if 4 <= len(TSNAME) <= 6 else True
Note: Python ternary expression is different from ternary expressions in C/C++/Java™ and is different from a Python if/else block.
Important: Values must be uppercase. The rule log=="LOGGED" works; the rule log=="logged" does not work.

The objects and attributes supported by complex site rules are the same as those supported by simple site rules.

To create and apply a site rule, complete the following steps:

Procedure

  1. On the navigation menu, click Manage DevOps > Site rules.
    The Site rules page opens. This page shows a list of the site rules that have been created.
  2. Click Create rule (Db2).
  3. Complete either of the following steps:
    • To create a simple rule, follow these steps:
      1. In the Rule name field, enter the name of the rule.
        Notes:
        • The name can include alphanumeric (A-Z, a-z, 0-9) and certain special characters. It cannot have leading or trailing spaces, or the following special characters: <, >, /, or \.
        • The rule name is case-insensitive; that is, lowercase and uppercase letters are not distinguished.
      2. From the Object field, select the type of object for which you want to specify a rule. For example, select Database to specify a rule on database objects.
      3. From the Attribute field, select the attribute to which you want this rule to be applied. The attributes are different for each object.
      4. From the Constraint field, select a constraint.
      5. In the Value field, either type values or select an item from the drop-down list, depending on the condition that you specified in previous steps. The rule that you specified will be shown at the bottom of this dialog.
      6. Click Create.
    • To create a complex rule, follow these steps:
      1. Click the Simple rule switch to turn it into Complex rule.
      2. In the Rule name field, enter the name of this rule.
      3. From the Object field, select an object type.
      4. Enter the rule in the editor pane. Make sure that values are uppercase.
      5. Click Create.
  4. To apply a rule, select it and click Assign to applications or Assign to environments.
  5. Find the applications or environments to apply the rule to, and then click Update.
  6. To edit, delete, or duplicate a rule, or to assign it to different applications or environments, click the overflow menu for the rule and select an appropriate menu item.