Exemple #1
0
class Uploader:
    """
    Uploads files to Sharepoint.
    """
    def __init__(self, params):
        self._debug = params.debug
        self._subsite = params.subsite
        self._logger = Logger("spuppy", "logs", params.debug)
        self._path = params.directory
        self._files = params.files
        self._out = params.out

    def main(self):
        """
        Orchestrate upload of a file using given configurations.
        """

        spconf = SharepointConfig(self._subsite)
        runtime = RuntimeConfig(self._debug)
        fconf = FilesConfig(self._path, self._files, self._out)
        verify = fconf.verify()

        if self._debug:
            self._logger.debug(spconf)
            self._logger.debug(runtime)
            self._logger.debug(fconf)

        if verify:
            for v in verify:
                self._logger.error(v)
            return

        if self._debug:
            self._logger.debug(fconf)

        try:
            sp = Sharepoint(fconf, spconf, runtime, self._logger)
            sp.get_token()
            if self._debug:
                self._logger.debug(sp)

            if not sp.verify_url():
                return

            if fconf.out_folder and not sp.add_folder():
                return

            sp.upload_files()
        except Exception as e:  # pylint: disable=broad-except
            # This `except` is intentionally broad to capture it in
            # the log for posterity. main exists anyway.
            self._logger.exception(e)
class GoDaddyConnector:
    def __init__(self, connection_params: Config, logger: Logger):
        self._connection_params = Config(
        ) if connection_params is None else connection_params
        self._logger = Logger() if logger is None else logger
        self._domain = 'esceer.com'
        self._dns_type = 'A'
        self._dns_record_name = '@'

    def fetch_ip_from_dns(self) -> str:
        self._logger.debug('Fetching current ip set in dns...')
        response = requests.get(self._get_url(), headers=self._get_headers())
        self._logger.debug(response.content)
        return IpUtils.gather_ip_from_dns_response(
            response.content.decode('utf-8'))

    def update_dns(self, target_ip: str) -> str:
        self._logger.debug('Updating dns information...')
        response = requests.put(self._get_url(),
                                data=self._build_new_dns_info(target_ip),
                                headers=self._get_headers())
        self._logger.debug(response.content)
        return response.content

    def _get_url(self) -> str:
        return '%s/v1/domains/%s/records/%s/%s' % (
            self._connection_params.get_godaddy_url_base(), self._domain,
            self._dns_type, self._dns_record_name)

    def _get_headers(self):
        return {
            'Authorization':
            'sso-key %s:%s' % (self._connection_params.get_api_key(),
                               self._connection_params.get_api_secret()),
            'accept':
            'application/json',
            'Content-Type':
            'application/json'
        }

    def _build_new_dns_info(self, target_ip: str):
        return '[{ "data": "%s", "ttl": 3600 }]' % target_ip