import logging
import requests
import json


def add_intersight_resource_group(AUTH, MOIDS, CLAIM_CONFIG):
    """ Create an Intersight Resource Group for Claimed Devices """

    request_body = {
        "Name":CLAIM_CONFIG['partner_id'] + "-rg",
        "Qualifier":"Allow-Selectors",
        "Selectors":[
            {
                "Selector": (
                    "/api/v1/asset/DeviceRegistrations?$filter=Moid in('" +
                    ",".join(MOIDS) + "')"
                )
            }
        ]
    }
    logging.info(request_body)

    response = requests.post(
        CLAIM_CONFIG['intersight_base_url'] + 'resource/Groups',
        data=json.dumps(request_body),
        auth=AUTH
    )
    logging.info(response.text)

    response_json = response.json()
    logging.info("RESOURCE GROUP: " + response_json["Moid"])

    return response_json["Moid"]