Using Conditions

Edit your Oracle Cloud Stack template and customize how a stack is created under different conditions.

For an introduction, see What are Conditions.

Using Conditional Functions

Oracle Cloud Stack supports these conditional functions: Equals, GreaterThan, LessThan, Contains, Empty, Match, Not, And, Or.

These functions are executed at runtime by Oracle Cloud Stack when a stack is being created. Each function evaluates two input strings and returns either the string true or the string false at the function’s location in the template. The inputs to a conditional function can be literal strings or functions that return strings, such as GetParam.

For example, consider a resource definition within a template that contains three parameters:

myresource:
  type: xyz
  parameters:
    dbEnabled: {"Fn::Equals":[{"Fn::GetParam":"dbVersion"},"12.2"]}
    cacheEnabled: {"Fn::GreaterThan":[{"Fn::GetParam":"cacheSize"},"0"]}
    backupEnabled: {"Fn::Not":[{"Fn::Equals":[{"Fn::GetParam":"container"},""]}]}

These conditional functions result in the following logic:

  • The dbEnabled parameter is set to "true" only if the value of the dbVersion template parameter is set to the value "12.2".

  • The cacheEnabled parameter is set to "true" only if the value of the cacheSize template parameter is greater than zero.

  • The backupEnabled parameter is set to "true" only if the value of the container template parameter is not an empty string.

For more information, see List of Functions.

Defining Global Conditions

You often need to use the same conditional expression (cacheSize is greater than zero) at multiple locations in your template. Alternatively, you can define these conditions in a single location within the template and then refer to them throughout the template.

Define global conditions within the conditions node of your template file. Give each condition a name and an expression whose value evaluates to true or false. For example:

conditions:
  includeMyApp: {"Fn::Equals":[{"Fn::GetParam":"dbVersion"},"12.2"]}

This example template defines a condition named includeMyApp whose value is true only if the value of the dbVersion template parameter is equal to "12.2".

Using Global Conditions with Functions

Use the If function to evaluate a global condition at some location in your template. Provide the name of the global condition along with two string values. If the specified condition evaluates to true the If function returns the first value. Otherwise the function returns the second value.

In the following example, the shape parameter for a resource is assigned the value "oc3" if the includeMyApp condition is true, or the value "oc4" if the condition is false:

myresource:
  type: xyz
  parameters:
    shape: {"Fn::If":["includeMyApp","oc3","oc4"]}

The If function can also be used to remove the current object, attribute or sequence item after evaluating its condition. Use the variable OPC::NoValue for either the true or false arguments. In this example, the dbService parameter is dynamically removed from this resource’s definition if the useDB condition is false.

myresource:
  type: xyz
  parameters:
    clusterSize: 2
    dbService: {"Fn::If":["useDB","MyOracleDB-1","OPC::NoValue"]}

Assigning a Global Condition to a Resource

Each resource in a template can optionally be assigned the name of a global condition. If this condition evaluates to true, the resource will be created as part of the stack. If it evaluates to false, the resource (and any resources on which it depends) will not be created.

Place the condition attribute within a resource’s definition, after the parameters. For example:

resources:
  myapp:
    type: xyz
    parameters:
      param1: value1
      param2: value2
    condition: includeMyApp

Using Conditions in Template Parameters

You can also use conditional functions and global conditions within the definition of a template parameter or a parameter group. These conditions control whether or not the parameter or group is displayed in the Oracle Cloud Stack console when creating a stack. If the condition evaluates to false, the parameter or group is hidden in the user interface. If it evaluates to true, the parameter or group is shown. These conditions are dynamically reevaluated each time the user modifies the value of a template parameter.

For example, you can use a boolean (check box) parameter to determine whether or not another parameter is shown to the user:

parameters:
  useBackup:
    label: Use Backup
    type: Boolean
    mandatory: false
    default: false
  backupPath:
    label: Backup Path
    type: String
    condition: {"Fn::Equals":[{"Fn::GetParam":"useBackup"},"true"]}

Alternatively, use the same parameter and a global condition to control when an entire parameter group is shown to the user:

parameters:
  useBackup:
    label: Use Backup
    type: Boolean
    mandatory: false
    default: false
  ...
parameterGroups:
  - label: Backup Configuration
    parameters: [ backupPath, backupTime ]
    condition: backupEnabled
conditions:
  backupEnabled: {"Fn::Equals":[{"Fn::GetParam":"useBackup"},"true"]}