Schema and Arguments

Develop a Schema

After defining the class in the correct file and inheriting the correct parent class the next step is to develop the schema. The schema of a stage defines what arguments the stage accepts, what arguments are optional or mandatory, and whether an argument is an integer or a string. It also defines the structure of arguments passed using the clean YAML file.

When writing a schema you want to think about how you can drive the stage by using arguments. In the following example of our ChangeBootVariable stage we specified these arguments in the schema:

  • images: the image to use as the boot variable

  • timeout: Execute timeout in seconds

  • config_register: Value to set config-register for reload

  • current_running_image: Flag to set the running image as the boot variable

 1from genie.metaparser.util.schemaengine import Optional
 2from genie.libs.clean import BaseStage
 3
 4class ChangeBootVariable(BaseStage):
 5
 6    # ============
 7    # Stage Schema
 8    # ============
 9    schema = {
10        Optional('images'): list,
11        Optional('timeout'): int,
12        Optional('config_register'): str,
13        Optional('current_running_image'): bool,
14    }
15
16    # =================
17    # Argument Defaults
18    # =================
19
20    # ==============================
21    # Execution order of Stage steps
22    # ==============================
23    exec_order = [
24
25    ]

Set Default Values for Optional Arguments

In most cases we want to provide a default value for optional arguments. In this case we will skip the images argument because it can be nothing if current_running_image is set to True.

Argument defaults should be the same name from the schema, UPPER CASE, and kept under the Argument Defaults section.

 1from genie.metaparser.util.schemaengine import Optional
 2from genie.libs.clean import BaseStage
 3
 4class ChangeBootVariable(BaseStage):
 5
 6    # ============
 7    # Stage Schema
 8    # ============
 9    schema = {
10        Optional('images'): list,
11        Optional('timeout'): int,
12        Optional('config_register'): str,
13        Optional('current_running_image'): bool,
14    }
15
16    # =================
17    # Argument Defaults
18    # =================
19    TIMEOUT = 300
20    CONFIG_REGISTER = '0x2102'
21    CURRENT_RUNNING_IMAGE = False
22
23    # ==============================
24    # Execution order of Stage steps
25    # ==============================
26    exec_order = [
27
28    ]

In later sections you will discover how argument defaults are used.