# Tests filtering resource providers by aggregates

fixtures:
    - APIFixture

defaults:
    request_headers:
        x-auth-token: admin
        content-type: application/json
        accept: application/json
        openstack-api-version: placement latest

tests:

- name: post new provider 1
  POST: /resource_providers
  data:
      name: rp_1
      uuid: 893337e9-1e55-49f0-bcfe-6a2f16fbf2f7
  status: 200

- name: post new provider 2
  POST: /resource_providers
  data:
      name: rp_2
      uuid: 5202c48f-c960-4eec-bde3-89c4f22a17b9
  status: 200

- name: post new provider 3
  POST: /resource_providers
  data:
      name: rp_3
      uuid: 0621521c-ad3a-4f9c-9b72-2933788fab19
  status: 200

- name: get by aggregates no result
  GET: '/resource_providers?member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91'
  response_json_paths:
      $.resource_providers: []

- name: associate an aggregate with rp1
  PUT: /resource_providers/893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/aggregates
  data:
      aggregates:
        - 83a3d69d-8920-48e2-8914-cadfd8fa2f91
      resource_provider_generation: 0
  status: 200

- name: get by aggregates one result
  GET: '/resource_providers?member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91'
  response_json_paths:
      $.resource_providers[0].uuid: 893337e9-1e55-49f0-bcfe-6a2f16fbf2f7

- name: get by aggregates one result no in
  GET: '/resource_providers?member_of=83a3d69d-8920-48e2-8914-cadfd8fa2f91'
  response_json_paths:
      $.resource_providers[0].uuid: 893337e9-1e55-49f0-bcfe-6a2f16fbf2f7

- name: get by aggregates no result not a uuid
  GET: '/resource_providers?member_of=not+a+uuid'
  status: 400
  response_strings:
    - "Expected 'member_of' parameter to contain valid UUID(s)."
  response_json_paths:
      $.errors[0].title: Bad Request

- name: associate an aggregate with rp2
  PUT: /resource_providers/5202c48f-c960-4eec-bde3-89c4f22a17b9/aggregates
  data:
      aggregates:
        - 83a3d69d-8920-48e2-8914-cadfd8fa2f91
      resource_provider_generation: 0
  status: 200

- name: get by aggregates two result
  GET: '/resource_providers?member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91'
  response_json_paths:
      $.resource_providers.`len`: 2
      $.resource_providers[0].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/
      $.resource_providers[1].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/

- name: associate another aggregate with rp2
  PUT: /resource_providers/5202c48f-c960-4eec-bde3-89c4f22a17b9/aggregates
  data:
      aggregates:
        - 99652f11-9f77-46b9-80b7-4b1989be9f8c
      resource_provider_generation: 1
  status: 200

- name: get by both aggregates two
  GET: '/resource_providers?member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91,99652f11-9f77-46b9-80b7-4b1989be9f8c'
  response_json_paths:
      $.resource_providers.`len`: 2
      $.resource_providers[0].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/
      $.resource_providers[1].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/

- name: clear aggregates on rp1
  PUT: /resource_providers/893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/aggregates
  data:
      aggregates: []
      resource_provider_generation: 1
  status: 200

- name: get by both aggregates one
  desc: only one result because we disassociated aggregates in the PUT above
  GET: '/resource_providers?member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91,99652f11-9f77-46b9-80b7-4b1989be9f8c'
  response_json_paths:
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: 5202c48f-c960-4eec-bde3-89c4f22a17b9

- name: error on old microversion
  GET: '/resource_providers?member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91,99652f11-9f77-46b9-80b7-4b1989be9f8c'
  request_headers:
      openstack-api-version: placement 1.1
  status: 400
  response_strings:
      - 'Invalid query string parameters'
  response_json_paths:
      $.errors[0].title: Bad Request

- name: error on bogus query parameter
  GET: '/resource_providers?assoc_with_aggregate=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91,99652f11-9f77-46b9-80b7-4b1989be9f8c'
  status: 400
  response_strings:
      - 'Invalid query string parameters'
  response_json_paths:
      $.errors[0].title: Bad Request

- name: error trying multiple member_of params prior correct microversion
  GET: '/resource_providers?member_of=83a3d69d-8920-48e2-8914-cadfd8fa2f91&member_of=99652f11-9f77-46b9-80b7-4b1989be9f8c'
  request_headers:
      openstack-api-version: placement 1.23
  status: 400
  response_strings:
      - 'Multiple member_of parameters are not supported'
  response_json_paths:
      $.errors[0].title: Bad Request

- name: multiple member_of params with no results
  GET: '/resource_providers?member_of=83a3d69d-8920-48e2-8914-cadfd8fa2f91&member_of=99652f11-9f77-46b9-80b7-4b1989be9f8c'
  status: 200
  response_json_paths:
      # No provider is associated with both aggregates
      resource_providers: []

- name: associate two aggregates with rp2
  PUT: /resource_providers/5202c48f-c960-4eec-bde3-89c4f22a17b9/aggregates
  data:
      aggregates:
        - 99652f11-9f77-46b9-80b7-4b1989be9f8c
        - 83a3d69d-8920-48e2-8914-cadfd8fa2f91
      resource_provider_generation: 2
  status: 200

- name: multiple member_of params AND together to result in one provider
  GET: '/resource_providers?member_of=83a3d69d-8920-48e2-8914-cadfd8fa2f91&member_of=99652f11-9f77-46b9-80b7-4b1989be9f8c'
  status: 200
  response_json_paths:
      # One provider is now associated with both aggregates
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: 5202c48f-c960-4eec-bde3-89c4f22a17b9

- name: associate two aggregates to rp1, one of which overlaps with rp2
  PUT: /resource_providers/893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/aggregates
  data:
      aggregates:
        - 282d469e-29e2-4a8a-8f2e-31b3202b696a
        - 83a3d69d-8920-48e2-8914-cadfd8fa2f91
      resource_provider_generation: 2
  status: 200

- name: two AND'd member_ofs with one OR'd member_of
  GET: '/resource_providers?member_of=83a3d69d-8920-48e2-8914-cadfd8fa2f91&member_of=in:99652f11-9f77-46b9-80b7-4b1989be9f8c,282d469e-29e2-4a8a-8f2e-31b3202b696a'
  status: 200
  response_json_paths:
      # Both rp1 and rp2 returned because both are associated with agg 83a3d69d
      # and each is associated with either agg 99652f11 or agg 282s469e
      $.resource_providers.`len`: 2
      $.resource_providers[0].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/
      $.resource_providers[1].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/

- name: two AND'd member_ofs using same agg UUID
  GET: '/resource_providers?member_of=282d469e-29e2-4a8a-8f2e-31b3202b696a&member_of=282d469e-29e2-4a8a-8f2e-31b3202b696a'
  status: 200
  response_json_paths:
      # Only rp2 returned since it's the only one associated with the duplicated agg
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: /893337e9-1e55-49f0-bcfe-6a2f16fbf2f7/

# Tests for negative aggregate membership from microversion 1.32
# Now the aggregation map is as below
# {
#  893337e9-1e55-49f0-bcfe-6a2f16fbf2f7 (rp_1):
#      [83a3d69d-8920-48e2-8914-cadfd8fa2f91, 282d469e-29e2-4a8a-8f2e-31b3202b696a]
#  5202c48f-c960-4eec-bde3-89c4f22a17b9 (rp_2)
#      [83a3d69d-8920-48e2-8914-cadfd8fa2f91, 99652f11-9f77-46b9-80b7-4b1989be9f8c]
#  0621521c-ad3a-4f9c-9b72-2933788fab19 (rp_3):
#      []
# }
- name: negative agg error on old microversion with ! prefix
  GET: /resource_providers?member_of=!282d469e-29e2-4a8a-8f2e-31b3202b696a
  status: 400
  request_headers:
    openstack-api-version: placement 1.31
  response_strings:
      - "Forbidden member_of parameters are not supported in the specified microversion"

- name: negative agg error on old microversion with !in prefix
  GET: /allocation_candidates?resources=VCPU:1&member_of=!in:282d469e-29e2-4a8a-8f2e-31b3202b696a
  status: 400
  request_headers:
    openstack-api-version: placement 1.31
  response_strings:
      - "Forbidden member_of parameters are not supported in the specified microversion"

- name: negative agg error on invalid agg
  GET: /resource_providers?member_of=!(^o^)
  status: 400
  request_headers:
    openstack-api-version: placement 1.32
  response_strings:
      - "Invalid query string parameters: Expected 'member_of' parameter to contain valid UUID(s)."

- name: negative agg error on invalid usage of in prefix
  GET: /resource_providers?resources=VCPU:1&member_of=in:99652f11-9f77-46b9-80b7-4b1989be9f8c,!282d469e-29e2-4a8a-8f2e-31b3202b696a
  status: 400
  request_headers:
    openstack-api-version: placement 1.32
  response_strings:
      - "Invalid query string parameters: Expected 'member_of' parameter to contain valid UUID(s)."

- name: negative agg
  GET: /resource_providers?member_of=!282d469e-29e2-4a8a-8f2e-31b3202b696a
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      # rp_2 is excluded
      $.resource_providers.`len`: 2
      $.resource_providers[0].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|0621521c-ad3a-4f9c-9b72-2933788fab19/
      $.resource_providers[1].uuid: /5202c48f-c960-4eec-bde3-89c4f22a17b9|0621521c-ad3a-4f9c-9b72-2933788fab19/

- name: negative agg multiple
  GET: /resource_providers?member_of=!282d469e-29e2-4a8a-8f2e-31b3202b696a&member_of=!99652f11-9f77-46b9-80b7-4b1989be9f8c
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      # Both rp_1 and rp_2 are excluded
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: 0621521c-ad3a-4f9c-9b72-2933788fab19

- name: negative agg with in prefix
  GET: /resource_providers?member_of=!in:282d469e-29e2-4a8a-8f2e-31b3202b696a,99652f11-9f77-46b9-80b7-4b1989be9f8c
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      # The same results as above
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: 0621521c-ad3a-4f9c-9b72-2933788fab19

- name: negative agg with positive agg
  GET: /resource_providers?member_of=!282d469e-29e2-4a8a-8f2e-31b3202b696a&member_of=83a3d69d-8920-48e2-8914-cadfd8fa2f91
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      # only rp_2 is returned
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: 5202c48f-c960-4eec-bde3-89c4f22a17b9

- name: negative agg multiple with positive agg
  GET: /resource_providers?member_of=!in:282d469e-29e2-4a8a-8f2e-31b3202b696a,83a3d69d-8920-48e2-8914-cadfd8fa2f91&member_of=99652f11-9f77-46b9-80b7-4b1989be9f8c
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      # no rp is returned
      $.resource_providers.`len`: 0

# This request is equivalent to the one in "negative agg with positive agg"
- name: negative agg with the same agg on positive get rp
  GET: /resource_providers?member_of=!282d469e-29e2-4a8a-8f2e-31b3202b696a&member_of=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91,282d469e-29e2-4a8a-8f2e-31b3202b696a
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      $.resource_providers.`len`: 1
      $.resource_providers[0].uuid: 5202c48f-c960-4eec-bde3-89c4f22a17b9

- name: negative agg with the same agg on positive no rp
  GET: /resource_providers?member_of=!282d469e-29e2-4a8a-8f2e-31b3202b696a&member_of=282d469e-29e2-4a8a-8f2e-31b3202b696a
  status: 200
  request_headers:
    openstack-api-version: placement 1.32
  response_json_paths:
      # no rp is returned
      $.resource_providers.`len`: 0
