Search code examples
ibm-cloud-infrastructure

How to use multiple objectfilters in python


I am using the following object filter to get a list of virtual servers

object_filter1 = {
    'virtualGuests': {
    'dedicatedAccountHostOnlyFlag': {'operation': 1},
    'powerState': {'name': {'operation': 'Running'}}
    }

virtualServers = client['Account'].getVirtualGuests(mask=object_mask,filter=object_filter)

How can I define another filter with contition powerState.name == RUNNING and primaryIpAddress attribute having a value? (!=null) or set? I want to use both these filters applied to my result i.e. either object_filter1 condition or the 2nd condition must be true


Solution

  • Please, try the following example:

    import SoftLayer
    # For nice debug output:
    from pprint import pprint as pp
    
    API_USERNAME = 'set me'
    API_KEY = 'set me'
    
    
    filterInstance = {
      'virtualGuests': {
        'powerState': {
          'name': {
            'operation': 'Running'
          }
        },
        'primaryIpAddress': {
          'operation': 'not null'
        }
      }
    }
    
    # Creates a new connection to the API service.
    client = SoftLayer.Client(
        username=API_USERNAME,
        api_key=API_KEY
    )
    
    try:
        result = client['SoftLayer_Account'].getVirtualGuests( filter = filterInstance)
        pp(result)
    
    except SoftLayer.SoftLayerAPIError as e:
        pp('Failed ...  faultCode=%s, faultString=%s'
            % (e.faultCode, e.faultString))
    

    References:

    object-filters

    python::ticket_filter