<commit_before># Copyright 2014 Cloudbase Solutions Srl
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import posixpath
import time

from oauth import oauth
from oslo.config import cfg
from six.moves.urllib import error
from six.moves.urllib import request

from cloudbaseinit.metadata.services import base
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.utils import x509constants

opts = [
    cfg.StrOpt('maas_metadata_url', default=None,
               help='The base URL for MaaS metadata'),
    cfg.StrOpt('maas_oauth_consumer_key', default="",
               help='The MaaS OAuth consumer key'),
    cfg.StrOpt('maas_oauth_consumer_secret', default="",
               help='The MaaS OAuth consumer secret'),
    cfg.StrOpt('maas_oauth_token_key', default="",
               help='The MaaS OAuth token key'),
    cfg.StrOpt('maas_oauth_token_secret', default="",
               help='The MaaS OAuth token secret'),
]

CONF = cfg.CONF
CONF.register_opts(opts)

LOG = logging.getLogger(__name__)


class MaaSHttpService(base.BaseMetadataService):
    _METADATA_2012_03_01 = '2012-03-01'

    def __init__(self):
        super(MaaSHttpService, self).__init__()
        self._enable_retry = True
        self._metadata_version = self._METADATA_2012_03_01

    def load(self):
        super(MaaSHttpService, self).load()

        if not CONF.maas_metadata_url:
            LOG.debug('MaaS metadata url not set')
        else:
            try:
                self._get_data('%s/meta-data/' % self._metadata_version)
                return True
            except Exception as ex:
                LOG.exception(ex)
                LOG.debug('Metadata not found at URL \'%s\'' %
                          CONF.maas_metadata_url)
        return False

    def _get_response(self, req):
        try:
            return request.urlopen(req)
        except error.HTTPError as ex:
            if ex.code == 404:
                raise base.NotExistingMetadataException()
            else:
                raise

    def _get_oauth_headers(self, url):
        consumer = oauth.OAuthConsumer(CONF.maas_oauth_consumer_key,
                                       CONF.maas_oauth_consumer_secret)
        token = oauth.OAuthToken(CONF.maas_oauth_token_key,
                                 CONF.maas_oauth_token_secret)

        parameters = {'oauth_version': "1.0",
                      'oauth_nonce': oauth.generate_nonce(),
                      'oauth_timestamp': int(time.time()),
                      'oauth_token': token.key,
                      'oauth_consumer_key': consumer.key}

        req = oauth.OAuthRequest(http_url=url, parameters=parameters)
        req.sign_request(oauth.OAuthSignatureMethod_PLAINTEXT(), consumer,
                         token)<commit_msg>The MAAS metadata service is not loaded<commit_after># Copyright 2014 Cloudbase Solutions Srl
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import posixpath
import time

from oauth import oauth
from oslo.config import cfg
from six.moves.urllib import error
from six.moves.urllib import request

from cloudbaseinit.metadata.services import base
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.utils import x509constants

opts = [
    cfg.StrOpt('maas_metadata_url', default=None,
               help='The base URL for MaaS metadata'),
    cfg.StrOpt('maas_oauth_consumer_key', default="",
               help='The MaaS OAuth consumer key'),
    cfg.StrOpt('maas_oauth_consumer_secret', default="",
               help='The MaaS OAuth consumer secret'),
    cfg.StrOpt('maas_oauth_token_key', default="",
               help='The MaaS OAuth token key'),
    cfg.StrOpt('maas_oauth_token_secret', default="",
               help='The MaaS OAuth token secret'),
]

CONF = cfg.CONF
CONF.register_opts(opts)

LOG = logging.getLogger(__name__)


class MaaSHttpService(base.BaseMetadataService):
    _METADATA_2012_03_01 = '2012-03-01'

    def __init__(self):
        super(MaaSHttpService, self).__init__()
        self._enable_retry = True
        self._metadata_version = self._METADATA_2012_03_01

    def load(self):
        super(MaaSHttpService, self).load()

        if not CONF.maas_metadata_url:
            LOG.debug('MaaS metadata url not set')
        else:
            try:
                self._get_cache_data('%s/meta-data/' % self._metadata_version)
                return True
            except Exception as ex:
                LOG.exception(ex)
                LOG.debug('Metadata not found at URL \'%s\'' %
                          CONF.maas_metadata_url)
        return False

    def _get_response(self, req):
        try:
            return request.urlopen(req)
        except error.HTTPError as ex:
            if ex.code == 404:
                raise base.NotExistingMetadataException()
            else:
                raise

    def _get_oauth_headers(self, url):
        consumer = oauth.OAuthConsumer(CONF.maas_oauth_consumer_key,
                                       CONF.maas_oauth_consumer_secret)
        token = oauth.OAuthToken(CONF.maas_oauth_token_key,
                                 CONF.maas_oauth_token_secret)

        parameters = {'oauth_version': "1.0",
                      'oauth_nonce': oauth.generate_nonce(),
                      'oauth_timestamp': int(time.time()),
                      'oauth_token': token.key,
                      'oauth_consumer_key': consumer.key}

        req = oauth.OAuthRequest(http_url=url, parameters=parameters)
        req.sign_request(oauth.OAuthSignatureMethod_PLAINTEXT(), consumer,
                         token)
{'fraction_non_alphanumeric': 0.0849536344600658, 'fraction_numerical': 0.011965300628178284, 'mean_word_length': 2.35187969924812, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4660940', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1880, 'n_words': 499}