def create_ea_defs(grid_id):
    print("\nCreating EA definitions...")
    print("-" * PRINT_LINE)
    print("")

    credentials = get_credentias()
    conn = utils.get_connector(credentials)

    if not (utils.get_features(conn).create_ea_def):
        LOG.error("WAPI Version '%s' is not supported - Script ABORTED!",
                  conn.wapi_version)
        exit(1)

    mgr = object_manager.InfobloxObjectManager(conn)
    ea_defs_created = mgr.create_required_ea_definitions(
        const.REQUIRED_EA_DEFS, reraise=True)
    if ea_defs_created:
        print("The following EA Definitions have been created: '%s'" %
              [ea_def['name'] for ea_def in ea_defs_created])
    else:
        print("All the EAs has been already created.")
    print("\n")

    grid_opts = config.get_infoblox_grid_opts(grid_id)
    gm_name = grid_opts['grid_master_name']
    member = objects.Member.search(conn, host_name=gm_name)

    if member is None:
        LOG.error("Cannot retrieve member information at GM='%s'" %
                  gm_name)
        exit(1)

    print("Adding grid configuration EAs to the grid master...")
    print("-" * PRINT_LINE)
    print("")
    ea_set = {}
    if member.extattrs is None:
        member.extattrs = objects.EA({})
    for ea, val in const.GRID_CONFIG_DEFAULTS.items():
        if (member.extattrs.get(ea) is None and
                not (val is None or val == [])):
            ea_set[ea] = val
            member.extattrs.set(ea, val)

    if ea_set:
        print("Grid configurations: '%s'" % ea_set)
        member.update()
    else:
        print("All the grid configurations have been already added.")
    print("\n")
def create_ea_defs(grid_id):
    print("\nCreating EA definitions...")
    print(("-" * PRINT_LINE))
    print("")

    credentials = get_credentias()
    conn = utils.get_connector(credentials)

    if not (utils.get_features(conn).create_ea_def):
        LOG.error("WAPI Version '%s' is not supported - Script ABORTED!",
                  conn.wapi_version)
        exit(1)

    mgr = object_manager.InfobloxObjectManager(conn)
    ea_defs_created = mgr.create_required_ea_definitions(
        const.REQUIRED_EA_DEFS, reraise=True)
    if ea_defs_created:
        print(("The following EA Definitions have been created: '%s'" %
               [ea_def['name'] for ea_def in ea_defs_created]))
    else:
        print("All the EAs has been already created.")
    print("\n")

    grid_opts = config.get_infoblox_grid_opts(grid_id)
    gm_name = grid_opts['grid_master_name']
    member = objects.Member.search(conn, host_name=gm_name)

    if member is None:
        LOG.error("Cannot retrieve member information at GM='%s'" % gm_name)
        exit(1)

    print("Adding grid configuration EAs to the grid master...")
    print(("-" * PRINT_LINE))
    print("")
    ea_set = {}
    if member.extattrs is None:
        member.extattrs = objects.EA({})
    for ea, val in list(const.GRID_CONFIG_DEFAULTS.items()):
        if (member.extattrs.get(ea) is None
                and not (val is None or val == [])):
            ea_set[ea] = val
            member.extattrs.set(ea, val)

    if ea_set:
        print(("Grid configurations: '%s'" % ea_set))
        member.update()
    else:
        print("All the grid configurations have been already added.")
    print("\n")
def participate_network_views(grid_id):
    print("Associating/Unassociating network views for OpenStack...")
    print(("-" * PRINT_LINE))
    print("")

    netview_input = None
    if cfg.CONF.participating_network_views:
        netview_input = cfg.CONF.participating_network_views
    elif ENV_NETWORK_VIEW_PARTICIPATION_LIST in os.environ:
        netview_input = os.environ[ENV_NETWORK_VIEW_PARTICIPATION_LIST]

    grid_id_str = str(grid_id)
    conn = utils.get_connector()

    ib_netviews = objects.NetworkView.search_all(conn)
    if not ib_netviews:
        print("No network view exists\n")
        return

    netview_names = [ib_netview.name for ib_netview in ib_netviews]
    print(("Found %s network views from the grid.\n" % len(netview_names)))

    operation = 'ASSOCIATION'
    if not netview_input and not cfg.CONF.script:
        expected_answers_operation = ['a', 'u']
        expected_answers_yes_no = ['y', 'n']

        question = ("Please type 'a' to associate network views, 'u' to "
                    "unassociate.")
        choice = ask_question(question, expected_answers_operation)
        operation = 'ASSOCIATION' if choice == 'a' else 'UNASSOCIATION'

        question = "Do you want to list network views?"
        choice = ask_question(question, expected_answers_yes_no)
        if choice == 'y':
            print((', '.join(netview_names)))
            print("")
        question = "Please provide a comma separated list of network views: "
        netview_input = input(question)

    if (not netview_input or not isinstance(netview_input, six.string_types)):
        msg = ("Participating network views should be in the "
               "comma-delimited string format.")
        if cfg.CONF.script:
            LOG.warning(msg)
        else:
            print(msg)
        return

    netview_list = []
    [
        netview_list.append(x.strip()) for x in netview_input.split(',')
        if x and x not in netview_list
    ]
    if not netview_list:
        print("")
        return

    for nv in netview_list:
        ib_netview_found = [
            ib_net for ib_net in ib_netviews if ib_net.name == nv
        ]
        if not ib_netview_found:
            print(("'%s' is not found." % nv))
            continue

        ib_netview = ib_netview_found[0]
        ea_netview = eam.get_ea_for_network_view(None, None, grid_id)
        if operation == 'ASSOCIATION':
            if ib_netview.extattrs is None:
                ib_netview.extattrs = ea_netview
            else:
                cloud_adapter_ids = ib_netview.extattrs.get(
                    const.EA_CLOUD_ADAPTER_ID)
                if cloud_adapter_ids:
                    if isinstance(cloud_adapter_ids, six.string_types):
                        cloud_adapter_ids = [cloud_adapter_ids]

                    found_ids = [
                        id for id in cloud_adapter_ids if id == grid_id_str
                    ]
                    if found_ids:
                        print(("'%s' already associated." % nv))
                        continue

                    cloud_adapter_ids.append(grid_id_str)
                else:
                    cloud_adapter_ids = [grid_id_str]

                ib_netview.extattrs.set(const.EA_CLOUD_ADAPTER_ID,
                                        cloud_adapter_ids)
            try:
                ib_netview.update()
            except ib_ex.InfobloxCannotUpdateObject as e:
                if 'Write permission' in e.msg:
                    print(("'%s' has no write permission." % nv))
                    continue
                else:
                    print(("'%s' failed to associate: %s" % (nv, e.msg)))
                    continue
            print(("'%s' is now associated." % nv))
        else:
            if ib_netview.extattrs is None:
                print(("'%s' not associated." % nv))
                continue

            cloud_adapter_ids = ib_netview.extattrs.get(
                const.EA_CLOUD_ADAPTER_ID)
            if cloud_adapter_ids:
                if isinstance(cloud_adapter_ids, six.string_types):
                    cloud_adapter_ids = [cloud_adapter_ids]

                found_ids = [
                    id for id in cloud_adapter_ids if id == grid_id_str
                ]
                if found_ids:
                    cloud_adapter_ids.remove(grid_id_str)
                    ib_netview.extattrs.set(const.EA_CLOUD_ADAPTER_ID,
                                            cloud_adapter_ids)
                    try:
                        ib_netview.update()
                    except ib_ex.InfobloxCannotUpdateObject as e:
                        if 'Write permission' in e.msg:
                            print(("'%s' has no write permission." % nv))
                            continue
                        else:
                            print(("'%s' failed to unassociated: %s" %
                                   (nv, e.msg)))
                            continue
                    print(("'%s' is now unassociated." % nv))
                else:
                    print(("'%s' not associated." % nv))
            else:
                print(("'%s' not associated." % nv))

    print("\n")
def participate_network_views(grid_id):
    print("Associating/Unassociating network views for OpenStack...")
    print("-" * PRINT_LINE)
    print("")

    netview_input = None
    if cfg.CONF.participating_network_views:
        netview_input = cfg.CONF.participating_network_views
    elif ENV_NETWORK_VIEW_PARTICIPATION_LIST in os.environ:
        netview_input = os.environ[ENV_NETWORK_VIEW_PARTICIPATION_LIST]

    grid_id_str = str(grid_id)
    conn = utils.get_connector()

    ib_netviews = objects.NetworkView.search_all(conn)
    if not ib_netviews:
        print("No network view exists\n")
        return

    netview_names = [ib_netview.name for ib_netview in ib_netviews]
    print("Found %s network views from the grid.\n" % len(netview_names))

    operation = 'ASSOCIATION'
    if not netview_input and not cfg.CONF.script:
        expected_answers_operation = ['a', 'u']
        expected_answers_yes_no = ['y', 'n']

        question = ("Please type 'a' to associate network views, 'u' to "
                    "unassociate.")
        choice = ask_question(question, expected_answers_operation)
        operation = 'ASSOCIATION' if choice == 'a' else 'UNASSOCIATION'

        question = "Do you want to list network views?"
        choice = ask_question(question, expected_answers_yes_no)
        if choice == 'y':
            print(', '.join(netview_names))
            print("")
        question = "Please provide a comma separated list of network views: "
        netview_input = raw_input(question)

    if (not netview_input or
            not isinstance(netview_input, six.string_types)):
        msg = ("Participating network views should be in the "
               "comma-delimited string format.")
        if cfg.CONF.script:
            LOG.warning(msg)
        else:
            print(msg)
        return

    netview_list = []
    [netview_list.append(x.strip()) for x in netview_input.split(',')
     if x and x not in netview_list]
    if not netview_list:
        print("")
        return

    for nv in netview_list:
        ib_netview_found = [ib_net for ib_net in ib_netviews
                            if ib_net.name == nv]
        if not ib_netview_found:
            print("'%s' is not found." % nv)
            continue

        ib_netview = ib_netview_found[0]
        ea_netview = eam.get_ea_for_network_view(None, None, grid_id)
        if operation == 'ASSOCIATION':
            if ib_netview.extattrs is None:
                ib_netview.extattrs = ea_netview
            else:
                cloud_adapter_ids = ib_netview.extattrs.get(
                    const.EA_CLOUD_ADAPTER_ID)
                if cloud_adapter_ids:
                    if isinstance(cloud_adapter_ids, six.string_types):
                        cloud_adapter_ids = [cloud_adapter_ids]

                    found_ids = [id for id in cloud_adapter_ids
                                 if id == grid_id_str]
                    if found_ids:
                        print("'%s' already associated." % nv)
                        continue

                    cloud_adapter_ids.append(grid_id_str)
                else:
                    cloud_adapter_ids = [grid_id_str]

                ib_netview.extattrs.set(const.EA_CLOUD_ADAPTER_ID,
                                        cloud_adapter_ids)
            try:
                ib_netview.update()
            except ib_ex.InfobloxCannotUpdateObject as e:
                if 'Write permission' in e.msg:
                    print("'%s' has no write permission." % nv)
                    continue
                else:
                    print("'%s' failed to associate: %s" % (nv, e.msg))
                    continue
            print("'%s' is now associated." % nv)
        else:
            if ib_netview.extattrs is None:
                print("'%s' not associated." % nv)
                continue

            cloud_adapter_ids = ib_netview.extattrs.get(
                const.EA_CLOUD_ADAPTER_ID)
            if cloud_adapter_ids:
                if isinstance(cloud_adapter_ids, six.string_types):
                    cloud_adapter_ids = [cloud_adapter_ids]

                found_ids = [id for id in cloud_adapter_ids
                             if id == grid_id_str]
                if found_ids:
                    cloud_adapter_ids.remove(grid_id_str)
                    ib_netview.extattrs.set(const.EA_CLOUD_ADAPTER_ID,
                                            cloud_adapter_ids)
                    try:
                        ib_netview.update()
                    except ib_ex.InfobloxCannotUpdateObject as e:
                        if 'Write permission' in e.msg:
                            print("'%s' has no write permission." % nv)
                            continue
                        else:
                            print("'%s' failed to unassociated: %s" %
                                  (nv, e.msg))
                            continue
                    print("'%s' is now unassociated." % nv)
                else:
                    print("'%s' not associated." % nv)
            else:
                print("'%s' not associated." % nv)

    print("\n")