access and register sub object 2

* Access sub element and register the object
  • Adapt the object for loop

Demo

source

Main task yaml file
    tasks:
    - name: task
      task:
      - func: cmd
        desc: |
          this will get a response of:
          Output: {
            "LoadBalancerDescriptions": [
              {
                "LoadBalancerName": "xx-elb",
                "DNSName": "x-y-z.elb.amazonaws.com",
                ...........
                "Instances": [
                  {
                    "InstanceId": "i-1234567890"
                  },
                  {
                    "InstanceId": "i-9876543210"
                  }
                ],
                ...........
              }
            ]
          }
        do:
        - name: print
          desc: mock only - pretent to call it
          cmd: 'aws elb describe-load-balancers --load-balancer-names my-web-app-elb'
      - func: cmd
        vars:
          last_result:
            Output: |
              {
               "LoadBalancerDescriptions": [
                 {
                   "LoadBalancerName": "xx-elb",
                   "DNSName": "x-y-z.elb.amazonaws.com",
                   "Instances": [
                     {
                       "InstanceId": "i-1234567890"
                     },
                     {
                       "InstanceId": "i-9876543210"
                     }
                   ],
                 }
               ]
               }
        dvars:
        - name: elb
          value: '{{.last_result.Output}}'
          desc: |
            this will map the reponse to name of elb
            then convert it to an object and register to cache
            the object name would be kept the same called elb, so now elb is a object instead of string content
            it is marked as in taskScope, means that it will be available to be acessible in the next func call in Main task
          flags:
          - keepName
          - toObj
          - reg
          - taskScope
          - v
        - name: void
          desc: |
            index .elb.LoadBalancerDescriptions 0 will locate the sub object of Instances
            objToYml template func is chained and called to to convert the objToYml
            then the yml is again converted to an object
            then registered as name of instances_1 which is a object
            the dvar name "void" means it does not register the string value to cached
    
            * note: instances_1 is a object of type of *interface{}
    
            "instances_1": (*[]interface {})({
              {
                "InstanceId": "i-02f5cf3cdb572d627"
              },
              {
                "InstanceId": "i-00e53fd8688e9193a"
              }
            })
    
            So you can use access and reference instances_1 in template value, however this internal type will not match what is required for loop tag, even though it looks like it is iteratable
    
            You can access, reference to sub element and iterate it using the go template though, for example:
            '{{(index .instances_1. 0).Instances}}' => "i-02f5cf3cdb572d627"
          value: '{{(index .elb.LoadBalancerDescriptions 0).Instances |objToYml|ymlToObj|reg
            "instances_1"}}'
        - name: void
          desc: |
            regObj is a template func as short hand to register the chain through object into the cache
            the name of the object is instances_2
          value: '{{(index .elb.LoadBalancerDescriptions 0).Instances |regObj "instances_2"}}'
        do:
        - name: printObj
          cmd: instances_1
        - name: printObj
          cmd: instances_2
      - func: cmd
        do:
        - name: inspect
          cmd:
          - debug_vars
      - func: cmd
        dvars:
        - name: instances_3
          desc: |
            index .elb.LoadBalancerDescriptions 0 will locate the sub object of Instances
            objToYml template func is chained and called to to convert the objToYml, now the result is yml string
            toObj flag decorate the behavior to convert the yml string to an object and the object name is kept the same as instances_3
    
            * note the different of instances_3 and ( instances_1 or instances_2) is the internal data type
    
            instances_3 data type is a plain slice/array and we can use this for the loop tag
            {
              {
                "InstanceId": "i-02f5cf3cdb572d627"
              },
              {
                "InstanceId": "i-00e53fd8688e9193a"
              }
            }
          value: '{{(index .elb.LoadBalancerDescriptions 0).Instances | objToYml}}'
          flags:
          - v
          - keepName
          - toObj
        loop: 'instances_3'
        do:
        - name: print
          cmd: '{{.loopitem.InstanceId}}'
    
Main log file
    loading [Config]:  ./tests/functests/upconfig.yml
    Main config:
                 Version -> 1.0.0
                  RefDir -> ./tests/functests
                 WorkDir -> cwd
              AbsWorkDir -> /up_project/up
                TaskFile -> c0178
                 Verbose -> vvv
              ModuleName -> self
               ShellType -> /bin/sh
           MaxCallLayers -> 8
                 Timeout -> 3600000
     MaxModuelCallLayers -> 256
               EntryTask -> task
      ModRepoUsernameRef -> 
      ModRepoPasswordRef -> 
    work dir: /up_project/up
    -exec task: task
    loading [Task]:  ./tests/functests/c0178
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1: [
    this will get a response of:
    Output: {
      "LoadBalancerDescriptions": [
        {
          "LoadBalancerName": "xx-elb",
          "DNSName": "x-y-z.elb.amazonaws.com",
          ...........
          "Instances": [
            {
              "InstanceId": "i-1234567890"
            },
            {
              "InstanceId": "i-9876543210"
            }
          ],
          ...........
        }
      ]
    }
    ]
    self: final context exec vars:
    
    (*core.Cache)({
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [print: mock only - pretent to call it ]
    aws elb describe-load-balancers --load-balancer-names my-web-app-elb
    -Step2:
    dvar> elb:
    "{\n \"LoadBalancerDescriptions\": [\n   {\n     \"LoadBalancerName\": \"xx-elb\",\n     \"DNSName\": \"x-y-z.elb.amazonaws.com\",\n     \"Instances\": [\n       {\n         \"InstanceId\": \"i-1234567890\"\n       },\n       {\n         \"InstanceId\": \"i-9876543210\"\n       }\n     ],\n   }\n ]\n }\n"
    
    -
    {
     "LoadBalancerDescriptions": [
       {
         "LoadBalancerName": "xx-elb",
         "DNSName": "x-y-z.elb.amazonaws.com",
         "Instances": [
           {
             "InstanceId": "i-1234567890"
           },
           {
             "InstanceId": "i-9876543210"
           }
         ],
       }
     ]
     }
    
    dvar[object]> elb:
    {
      "LoadBalancerDescriptions": {
        {
          "Instances": {
            {
              "InstanceId": "i-1234567890"
            },
            {
              "InstanceId": "i-9876543210"
            }
          },
          "LoadBalancerName": "xx-elb",
          "DNSName": "x-y-z.elb.amazonaws.com"
        }
      }
    }
    
    self: final context exec vars:
    
    (*core.Cache)({
      "instances_1": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "instances_2": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com",
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            }
          }
        }
      },
      "up_runtime_task_layer_number": 0,
      "last_result": {
        "Output": "{\n \"LoadBalancerDescriptions\": [\n   {\n     \"LoadBalancerName\": \"xx-elb\",\n     \"DNSName\": \"x-y-z.elb.amazonaws.com\",\n     \"Instances\": [\n       {\n         \"InstanceId\": \"i-1234567890\"\n       },\n       {\n         \"InstanceId\": \"i-9876543210\"\n       }\n     ],\n   }\n ]\n }\n"
      }
    })
    
    ~SubStep1: [printObj:  ]
    object:
     instances_1: (*[]interface {})({
      {
        "InstanceId": "i-1234567890"
      },
      {
        "InstanceId": "i-9876543210"
      }
    })
    
    ~SubStep2: [printObj:  ]
    object:
     instances_2: (*[]interface {})({
      {
        "InstanceId": "i-1234567890"
      },
      {
        "InstanceId": "i-9876543210"
      }
    })
    
    -Step3:
    self: final context exec vars:
    
    (*core.Cache)({
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com",
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            }
          }
        }
      },
      "instances_1": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "instances_2": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [inspect:  ]
     1: inspect[debug_vars]
    -debug vars-
    "UpRunTimeVars"
    (*core.Cache)({
      "up_runtime_task_layer_number": 0
    })
    
    "RuntimeVarsAndDvarsMerged"
    (*core.Cache)({
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com",
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            }
          }
        }
      },
      "instances_1": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "instances_2": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      })
    })
    
    "ExecbaseVars"
    (*core.Cache)({
      "instances_1": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "instances_2": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com",
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            }
          }
        }
      }
    })
    
    "TaskVars"
    (*core.Cache)({
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com",
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            }
          }
        }
      }
    })
    
    "ExecContextVars"
    (*core.Cache)({
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com",
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            }
          }
        }
      },
      "instances_1": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "instances_2": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "up_runtime_task_layer_number": 0
    })
    
    --
    -Step4:
    dvar> instances_3:
    "- InstanceId: i-1234567890\n- InstanceId: i-9876543210\n"
    
    -
    - InstanceId: i-1234567890
    - InstanceId: i-9876543210
    
    dvar[object]> instances_3:
    {
      {
        "InstanceId": "i-1234567890"
      },
      {
        "InstanceId": "i-9876543210"
      }
    }
    
    self: final context exec vars:
    
    (*core.Cache)({
      "elb": {
        "LoadBalancerDescriptions": {
          {
            "Instances": {
              {
                "InstanceId": "i-1234567890"
              },
              {
                "InstanceId": "i-9876543210"
              }
            },
            "LoadBalancerName": "xx-elb",
            "DNSName": "x-y-z.elb.amazonaws.com"
          }
        }
      },
      "instances_1": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "instances_2": (*[]interface {})({
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }),
      "up_runtime_task_layer_number": 0,
      "instances_3": {
        {
          "InstanceId": "i-1234567890"
        },
        {
          "InstanceId": "i-9876543210"
        }
      }
    })
    
    ~SubStep1: [print:  ]
    i-1234567890
    ~SubStep1: [print:  ]
    i-9876543210
    
Logs with different verbose level
Raw logs with different verbose level