(self, field, default=None)
return value if value else default
Returns a value from the operation for a specific set of field names.

    Args:
      field: a dsub-specific job metadata key
      default: default value to return if field does not exist or is empty.

    Returns:
      A text string for the field or a list for 'inputs'.

    Raises:
      ValueError: if the field label is not supported by the operation
Returns a value from the operation for a specific set of field names.
Returns a value from the operation for a specific set of field names .
def get_field(self, field, default=None):
    """Returns a value from the operation for a specific set of field names.

    Args:
      field: a dsub-specific job metadata key
      default: default value to return if field does not exist or is empty.

    Returns:
      A text string for the field or a list for 'inputs'.

    Raises:
      ValueError: if the field label is not supported by the operation
    """

    value = None
    if field == 'internal-id':
      value = self._op['name']
    elif field == 'user-project':
      if self._job_descriptor:
        value = self._job_descriptor.job_metadata.get(field)
    elif field in [
        'job-id', 'job-name', 'task-id', 'task-attempt', 'user-id',
        'dsub-version'
    ]:
      value = google_v2_operations.get_label(self._op, field)
    elif field == 'task-status':
      value = self._operation_status()
    elif field == 'logging':
      if self._job_descriptor:
        # The job_resources will contain the "--logging" value.
        # The task_resources will contain the resolved logging path.
        # Return the resolved logging path.
        task_resources = self._job_descriptor.task_descriptors[0].task_resources
        value = task_resources.logging_path

    elif field in ['envs', 'labels']:
      if self._job_descriptor:
        items = providers_util.get_job_and_task_param(
            self._job_descriptor.job_params,
            self._job_descriptor.task_descriptors[0].task_params, field)
        value = {item.name: item.value for item in items}
    elif field in [
        'inputs', 'outputs', 'input-recursives', 'output-recursives'
    ]:
      if self._job_descriptor:
        value = {}
        items = providers_util.get_job_and_task_param(
            self._job_descriptor.job_params,
            self._job_descriptor.task_descriptors[0].task_params, field)
        value.update({item.name: item.value for item in items})
    elif field == 'mounts':
      if self._job_descriptor:
        items = providers_util.get_job_and_task_param(
            self._job_descriptor.job_params,
            self._job_descriptor.task_descriptors[0].task_params, field)
        value = {item.name: item.value for item in items}
    elif field == 'create-time':
      ds = google_v2_operations.get_create_time(self._op)
      value = google_base.parse_rfc3339_utc_string(ds)
    elif field == 'start-time':
      ds = google_v2_operations.get_start_time(self._op)
      if ds:
        value = google_base.parse_rfc3339_utc_string(ds)
    elif field == 'end-time':
      ds = google_v2_operations.get_end_time(self._op)
      if ds:
        value = google_base.parse_rfc3339_utc_string(ds)

    elif field == 'status':
      # Short message like:
      #   "Pending", "VM starting", "<error message>", "Success", "Cancelled"
      value = self._operation_status()

    elif field == 'status-message':
      # Longer message
      msg, action, detail = self._operation_status_message()
      if msg.startswith('Execution failed:'):
        # msg may look something like
        # "Execution failed: action 2: pulling image..."
        # Emit the actual message ("pulling image...")
        msg = msg.split(': ', 2)[-1]
      value = msg

    elif field == 'status-detail':
      # As much detail as we can reasonably get from the operation
      msg, action, detail = self._operation_status_message()
      if detail:
        msg = detail

      if action:
        value = google_v2_operations.get_action_name(action) + ':\n' + msg
      else:
        value = msg

    elif field == 'last-update':
      last_update = google_v2_operations.get_last_update(self._op)
      if last_update:
        value = google_base.parse_rfc3339_utc_string(last_update)
    elif field == 'provider':
      return self._provider_name
    elif field == 'provider-attributes':
      value = {}

      # The ssh flag is determined by if an action named 'ssh' exists.
      value['ssh'] = self._is_ssh_enabled(self._op)

      value[
          'block-external-network'] = google_v2_operations.external_network_blocked(
              self._op)

      # The VM instance name and zone can be found in the WorkerAssignedEvent.
      # For a given operation, this may have occurred multiple times, so be
      # sure to grab the most recent.
      assigned_event_details = google_v2_operations.get_worker_assigned_event_details(
          self._op)
      if assigned_event_details:
        value['instance-name'] = assigned_event_details.get('instance')
        value['zone'] = assigned_event_details.get('zone')

      # The rest of the information comes from the request itself.
      # Note that for the v2alpha1 API, the returned operation contains
      # default values in the response, while the v2beta API omits fields
      # that match empty defaults (hence the "False", "[]", and empty string
      # default values in the get() calls below).
      resources = google_v2_operations.get_resources(self._op)
      value['regions'] = resources.get('regions', [])
      value['zones'] = resources.get('zones', [])
      if 'virtualMachine' in resources:
        vm = resources['virtualMachine']
        value['machine-type'] = vm.get('machineType')
        value['preemptible'] = vm.get('preemptible', False)

        value['boot-disk-size'] = vm.get('bootDiskSizeGb')
        value['network'] = google_v2_operations.get_vm_network_name(vm) or ''
        value['subnetwork'] = vm.get('network', {}).get('subnetwork', '')
        value['use_private_address'] = vm.get('network',
                                              {}).get('usePrivateAddress',
                                                      False)
        value['cpu_platform'] = vm.get('cpuPlatform', '')
        value['accelerators'] = vm.get('accelerators', [])
        value['enable-stackdriver-monitoring'] = vm.get(
            'enableStackdriverMonitoring', False)
        value['service-account'] = vm.get('serviceAccount', {}).get('email')
        if 'disks' in vm:
          datadisk = next(
              (d for d in vm['disks'] if d['name'] == _DATA_DISK_NAME))
          if datadisk:
            value['disk-size'] = datadisk.get('sizeGb')
            value['disk-type'] = datadisk.get('type')
    elif field == 'events':
      value = GoogleV2EventMap(self._op).get_filtered_normalized_events()
    elif field == 'script-name':
      if self._job_descriptor:
        value = self._job_descriptor.job_metadata.get(field)
    elif field == 'script':
      value = self._try_op_to_script_body()
    else:
      raise ValueError('Unsupported field: "%s"' % field)

    return value if value else default
def get_field ( self , field , default = None ) : value = None if field == 'internal-id' : value = self . _op [ 'name' ] elif field == 'user-project' : if self . _job_descriptor : value = self . _job_descriptor . job_metadata . get ( field ) elif field in [ 'job-id' , 'job-name' , 'task-id' , 'task-attempt' , 'user-id' , 'dsub-version' ] : value = google_v2_operations . get_label ( self . _op , field ) elif field == 'task-status' : value = self . _operation_status ( ) elif field == 'logging' : if self . _job_descriptor : # The job_resources will contain the "--logging" value. # The task_resources will contain the resolved logging path. # Return the resolved logging path. task_resources = self . _job_descriptor . task_descriptors [ 0 ] . task_resources value = task_resources . logging_path elif field in [ 'envs' , 'labels' ] : if self . _job_descriptor : items = providers_util . get_job_and_task_param ( self . _job_descriptor . job_params , self . _job_descriptor . task_descriptors [ 0 ] . task_params , field ) value = { item . name : item . value for item in items } elif field in [ 'inputs' , 'outputs' , 'input-recursives' , 'output-recursives' ] : if self . _job_descriptor : value = { } items = providers_util . get_job_and_task_param ( self . _job_descriptor . job_params , self . _job_descriptor . task_descriptors [ 0 ] . task_params , field ) value . update ( { item . name : item . value for item in items } ) elif field == 'mounts' : if self . _job_descriptor : items = providers_util . get_job_and_task_param ( self . _job_descriptor . job_params , self . _job_descriptor . task_descriptors [ 0 ] . task_params , field ) value = { item . name : item . value for item in items } elif field == 'create-time' : ds = google_v2_operations . get_create_time ( self . _op ) value = google_base . parse_rfc3339_utc_string ( ds ) elif field == 'start-time' : ds = google_v2_operations . get_start_time ( self . _op ) if ds : value = google_base . parse_rfc3339_utc_string ( ds ) elif field == 'end-time' : ds = google_v2_operations . get_end_time ( self . _op ) if ds : value = google_base . parse_rfc3339_utc_string ( ds ) elif field == 'status' : # Short message like: #   "Pending", "VM starting", "<error message>", "Success", "Cancelled" value = self . _operation_status ( ) elif field == 'status-message' : # Longer message msg , action , detail = self . _operation_status_message ( ) if msg . startswith ( 'Execution failed:' ) : # msg may look something like # "Execution failed: action 2: pulling image..." # Emit the actual message ("pulling image...") msg = msg . split ( ': ' , 2 ) [ - 1 ] value = msg elif field == 'status-detail' : # As much detail as we can reasonably get from the operation msg , action , detail = self . _operation_status_message ( ) if detail : msg = detail if action : value = google_v2_operations . get_action_name ( action ) + ':\n' + msg else : value = msg elif field == 'last-update' : last_update = google_v2_operations . get_last_update ( self . _op ) if last_update : value = google_base . parse_rfc3339_utc_string ( last_update ) elif field == 'provider' : return self . _provider_name elif field == 'provider-attributes' : value = { } # The ssh flag is determined by if an action named 'ssh' exists. value [ 'ssh' ] = self . _is_ssh_enabled ( self . _op ) value [ 'block-external-network' ] = google_v2_operations . external_network_blocked ( self . _op ) # The VM instance name and zone can be found in the WorkerAssignedEvent. # For a given operation, this may have occurred multiple times, so be # sure to grab the most recent. assigned_event_details = google_v2_operations . get_worker_assigned_event_details ( self . _op ) if assigned_event_details : value [ 'instance-name' ] = assigned_event_details . get ( 'instance' ) value [ 'zone' ] = assigned_event_details . get ( 'zone' ) # The rest of the information comes from the request itself. # Note that for the v2alpha1 API, the returned operation contains # default values in the response, while the v2beta API omits fields # that match empty defaults (hence the "False", "[]", and empty string # default values in the get() calls below). resources = google_v2_operations . get_resources ( self . _op ) value [ 'regions' ] = resources . get ( 'regions' , [ ] ) value [ 'zones' ] = resources . get ( 'zones' , [ ] ) if 'virtualMachine' in resources : vm = resources [ 'virtualMachine' ] value [ 'machine-type' ] = vm . get ( 'machineType' ) value [ 'preemptible' ] = vm . get ( 'preemptible' , False ) value [ 'boot-disk-size' ] = vm . get ( 'bootDiskSizeGb' ) value [ 'network' ] = google_v2_operations . get_vm_network_name ( vm ) or '' value [ 'subnetwork' ] = vm . get ( 'network' , { } ) . get ( 'subnetwork' , '' ) value [ 'use_private_address' ] = vm . get ( 'network' , { } ) . get ( 'usePrivateAddress' , False ) value [ 'cpu_platform' ] = vm . get ( 'cpuPlatform' , '' ) value [ 'accelerators' ] = vm . get ( 'accelerators' , [ ] ) value [ 'enable-stackdriver-monitoring' ] = vm . get ( 'enableStackdriverMonitoring' , False ) value [ 'service-account' ] = vm . get ( 'serviceAccount' , { } ) . get ( 'email' ) if 'disks' in vm : datadisk = next ( ( d for d in vm [ 'disks' ] if d [ 'name' ] == _DATA_DISK_NAME ) ) if datadisk : value [ 'disk-size' ] = datadisk . get ( 'sizeGb' ) value [ 'disk-type' ] = datadisk . get ( 'type' ) elif field == 'events' : value = GoogleV2EventMap ( self . _op ) . get_filtered_normalized_events ( ) elif field == 'script-name' : if self . _job_descriptor : value = self . _job_descriptor . job_metadata . get ( field ) elif field == 'script' : value = self . _try_op_to_script_body ( ) else : raise ValueError ( 'Unsupported field: "%s"' % field ) return value if value else default