Exemplo n.º 1
0
    def delete_network(self, name):
        # type: (str) -> None
        """
        Delete network by name.

        :param name: name of the network to delete
        :type name: str
        """
        if name is None:
            raise ValueError('Network to be deleted must be supplied')
        json_data = workhelper.get_data_delete_network(self, name)
        resthelper.get_json_response(self, CoordConsts.SVC_RSC_DEL_NETWORK,
                                     json_data)
Exemplo n.º 2
0
    def _init_snapshot(self,
                       upload,
                       name=None,
                       overwrite=False,
                       background=False,
                       extra_args=None):
        # type: (str, Optional[str], bool, bool, Optional[Dict[str, Any]]) -> Union[str, Dict[str, str]]
        if self.network is None:
            self.set_network()

        if name is None:
            name = Options.default_snapshot_prefix + get_uuid()
        validate_name(name)

        if name in self.list_snapshots():
            if overwrite:
                self.delete_snapshot(name)
            else:
                raise ValueError(
                    'A snapshot named '
                    '{}'
                    ' already exists in network '
                    '{}'
                    '. '
                    'Use overwrite = True if you want to overwrite the '
                    'existing snapshot'.format(name, self.network))

        file_to_send = upload
        tmp_file_name = None  # type: Optional[Text]
        if os.path.isdir(upload):
            # delete=False because we re-open for reading
            with tempfile.NamedTemporaryFile(delete=False) as temp_zip_file:
                zip_dir(upload, temp_zip_file)
                tmp_file_name = file_to_send = temp_zip_file.name

        with open(file_to_send, 'rb') as fd:
            json_data = workhelper.get_data_upload_snapshot(self, name, fd)

            resthelper.get_json_response(self,
                                         CoordConsts.SVC_RSC_UPLOAD_SNAPSHOT,
                                         json_data)
        # Cleanup tmp file if we made one
        if tmp_file_name is not None:
            try:
                os.remove(tmp_file_name)
            except (OSError, IOError):
                # If we can't delete the file for some reason, let it be,
                # no need to crash initialization
                pass

        return self._parse_snapshot(name, background, extra_args)
Exemplo n.º 3
0
    def delete_snapshot(self, name):
        # type: (str) -> None
        """
        Delete specified snapshot from current network.

        :param name: name of the snapshot to delete
        :type name: str
        """
        self._check_network()
        if name is None:
            raise ValueError('Snapshot to be deleted must be supplied')
        json_data = workhelper.get_data_delete_snapshot(self, name)
        resthelper.get_json_response(self, CoordConsts.SVC_RSC_DEL_SNAPSHOT,
                                     json_data)
Exemplo n.º 4
0
    def list_incomplete_works(self):
        # type: () -> Dict[str, Any]
        """
        Get pending work that is incomplete.

        :return: JSON dictionary of question name to question object
        :rtype: dict
        """
        json_data = workhelper.get_data_list_incomplete_work(self)
        response = resthelper.get_json_response(
            self, CoordConsts.SVC_RSC_LIST_INCOMPLETE_WORK, json_data)
        return response
Exemplo n.º 5
0
    def list_networks(self):
        # type: () -> List[str]
        """
        List networks the session's API key can access.

        :return: network names
        :rtype: list
        """
        json_data = workhelper.get_data_list_networks(self)
        json_response = resthelper.get_json_response(
            self, CoordConsts.SVC_RSC_LIST_NETWORKS, json_data)

        return list(map(str, json_response['networklist']))
Exemplo n.º 6
0
def bf_sync_snapshots_sync_now(plugin, force=False):
    """
    Synchronize snapshots with specified plugin.

    :param plugin: name of the plugin to sync snapshots with
    :type plugin: string
    :param force: whether or not to overwrite any conflicts
    :type force: bool
    :return: json response containing result of snapshot sync from Batfish service
    :rtype: dict
    """
    json_data = workhelper.get_data_sync_snapshots_sync_now(
        bf_session, plugin, force)
    json_response = resthelper.get_json_response(
        bf_session, CoordConsts.SVC_RSC_SYNC_SNAPSHOTS_SYNC_NOW, json_data)
    return json_response
Exemplo n.º 7
0
def bf_sync_snapshots_update_settings(plugin, settings):
    """
    Update snapshot sync settings for the specified plugin.

    :param plugin: name of the plugin to update
    :type plugin: string
    :param settings: settings to update
    :type settings: dict
    :return: json response containing result of settings update from Batfish service
    :rtype: dict
    """
    json_data = workhelper.get_data_sync_snapshots_update_settings(
        bf_session, plugin, settings)
    json_response = resthelper.get_json_response(
        bf_session, CoordConsts.SVC_RSC_SYNC_SNAPSHOTS_UPDATE_SETTINGS,
        json_data)
    return json_response
Exemplo n.º 8
0
    def set_network(
        self, name: Optional[str] = None, prefix: str = Options.default_network_prefix
    ) -> str:
        """
        Configure the network used for analysis.

        :param name: name of the network to set. If `None`, a name will be generated
        :type name: str
        :param prefix: prefix to prepend to auto-generated network names if name is empty
        :type name: str

        :return: name of the configured network
        :rtype: str
        :raises BatfishException: if configuration fails
        """
        if name is None:
            name = prefix + get_uuid()
        validate_name(name, "network")

        try:
            net = restv2helper.get_network(self, name)
            self.network = str(net["name"])
            return self.network
        except HTTPError as e:
            if e.response.status_code != 404:
                raise BatfishException("Unknown error accessing network", e)

        json_data = workhelper.get_data_init_network(self, name)
        json_response = resthelper.get_json_response(
            self, CoordConsts.SVC_RSC_INIT_NETWORK, json_data
        )

        network_name = json_response.get(CoordConsts.SVC_KEY_NETWORK_NAME)
        if network_name is None:
            raise BatfishException(
                "Network initialization failed. Server response: {}".format(
                    json_response
                )
            )

        self.network = str(network_name)
        return self.network
Exemplo n.º 9
0
 def get_info(self):
     # type: () -> Dict[str, Any]
     """Get basic info about the Batfish service (including name, version, ...)."""
     return resthelper.get_json_response(self, '', useHttpGet=True)
Exemplo n.º 10
0
 def __init_snapshot_from_io(self, name, fd):
     # type: (str, IO) -> None
     json_data = workhelper.get_data_upload_snapshot(self, name, fd)
     resthelper.get_json_response(
         self, CoordConsts.SVC_RSC_UPLOAD_SNAPSHOT, json_data)