added Fastly extension driver

This commit is contained in:
amitgandhinz
2014-02-19 15:12:18 -05:00
parent 0ec3dc4573
commit 10cb9cc173
15 changed files with 286 additions and 131 deletions

7
cdn/provider/__init__.py Normal file
View File

@@ -0,0 +1,7 @@
"""CDN Provider Extensions"""
from cdn.provider import base
# Hoist classes into package namespace
CDNProviderBase = base.CDNProviderBase
HostBase = base.HostBase

92
cdn/provider/base.py Normal file
View File

@@ -0,0 +1,92 @@
# Copyright (c) 2013 Rackspace, Inc.
#
# 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 abc
import six
from oslo.config import cfg
_LIMITS_OPTIONS = [
cfg.IntOpt('default_hostname_paging', default=10,
help='Default hostname pagination size')
]
_LIMITS_GROUP = 'limits:storage'
@six.add_metaclass(abc.ABCMeta)
class ProviderBase(object):
"""Base class for CDN Provider extensions
:param conf: Configuration containing options for this extension.
:type conf: `oslo.config.ConfigOpts`
"""
def __init__(self, conf):
self.conf = conf
@six.add_metaclass(abc.ABCMeta)
class CDNProviderBase(ProviderBase):
"""Interface definition for CDN Provider Extensions.
CDN Provider extensions are responsible for implementing the
communication with the CDN Network and Edge nodes.
Connection information and driver-specific options are
loaded from the config file.
:param conf: Configuration containing options for this extension.
:type conf: `oslo.config.ConfigOpts`
"""
def __init__(self, conf):
super(CDNProviderBase, self).__init__(conf)
self.conf.register_opts(_LIMITS_OPTIONS, group=_LIMITS_GROUP)
self.limits_conf = self.conf[_LIMITS_GROUP]
@abc.abstractmethod
def is_alive(self):
"""Check whether the provider is ready."""
raise NotImplementedError
@abc.abstractproperty
def host_controller(self):
"""Returns the extension's hostname controller."""
raise NotImplementedError
@six.add_metaclass(abc.ABCMeta)
class HostBase(object):
"""This class is responsible for managing hostnames.
Hostname operations include CRUD, etc.
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def list(self):
raise NotImplementedError
@abc.abstractmethod
def create(self):
raise NotImplementedError
@abc.abstractmethod
def delete(self):
raise NotImplementedError
@abc.abstractmethod
def get(self):
raise NotImplementedError

View File

@@ -0,0 +1,6 @@
"""Fastly CDN Extension for CDN"""
from cdn.provider.fastly import driver
# Hoist classes into package namespace
CDNProvider = driver.CDNProvider

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2014 Rackspace, Inc.
# Copyright (c) 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,15 +13,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from pecan import make_app
"""Exports Fastly CDN controllers.
Field Mappings:
In order to reduce the disk / memory space used,
fields name will be, most of the time, the first
letter of their long name. Fields mapping will be
updated and documented in each controller class.
"""
def setup_app(config):
from cdn.provider.fastly import hosts
app_conf = dict(config.app)
return make_app(
app_conf.pop('root'),
logging=getattr(config, 'logging', {}),
**app_conf
)
HostController = hosts.HostController

View File

@@ -0,0 +1,56 @@
# Copyright (c) 2014 Rackspace, Inc.
#
# 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.
"""Fastly CDN Provider implementation."""
from cdn.common import decorators
from cdn.openstack.common import log as logging
from cdn import provider
from cdn.provider.fastly import controllers
from oslo.config import cfg
import fastly
LOG = logging.getLogger(__name__)
FASTLY_OPTIONS = [
cfg.StrOpt('apikey', help='Fastly API Key'),
]
FASTLY_GROUP = 'drivers:provider:fastly'
class CDNProvider(provider.CDNProviderBase):
def __init__(self, conf):
super(CDNProvider, self).__init__(conf)
self.conf.register_opts(FASTLY_OPTIONS,
group=FASTLY_GROUP)
self.fastly_conf = self.conf[FASTLY_GROUP]
self.client = fastly.connect(self.fastly_conf.apikey)
def is_alive(self):
return True
def client(self):
return self.client
@decorators.lazy_property(write=False)
def host_controller(self):
return controllers.HostController(self)

View File

@@ -0,0 +1,38 @@
# Copyright (c) 2014 Rackspace, Inc.
#
# 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 fastly
from cdn.provider import base
class HostController(base.HostBase):
def __init__(self, driver):
super(HostController, self).__init__()
self.client = driver.client
def list(self):
print "get list of hostnames from fastly"
def create(self, hostname):
print self.client.list_services()
def delete(self):
print "delete hostname at fastly"
def get(self):
print "get hostname from fastly"

View File

@@ -0,0 +1,6 @@
"""CDN Extension for CDN"""
from cdn.provider.sample import driver
# Hoist classes into package namespace
CDNProvider = driver.CDNProvider

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2014 Rackspace, Inc.
# Copyright (c) 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,19 +13,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from hosts import HostsController
from pecan import expose
from pecan.rest import RestController
"""Exports Sample CDN controllers.
Field Mappings:
In order to reduce the disk / memory space used,
fields name will be, most of the time, the first
letter of their long name. Fields mapping will be
updated and documented in each controller class.
"""
class HomeController(RestController):
from cdn.provider.sample import hosts
hosts = HostsController()
@expose('json')
def get_all(self):
'''return the HOME document for the API
'''
return dict(
version='1.0'
)
HostController = hosts.HostController

View File

@@ -0,0 +1,38 @@
# Copyright (c) 2014 Rackspace, Inc.
#
# 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.
"""CDN Provider implementation."""
from cdn.common import decorators
from cdn.openstack.common import log as logging
from cdn import provider
from cdn.provider.sample import controllers
from oslo.config import cfg
LOG = logging.getLogger(__name__)
class CDNProvider(provider.CDNProviderBase):
def __init__(self, conf):
super(CDNProvider, self).__init__(conf)
def is_alive(self):
return True
@decorators.lazy_property(write=False)
def host_controller(self):
return controllers.HostController()

View File

@@ -13,16 +13,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from pecan import expose
from v1 import HomeController
# stevedore/example/simple.py
from cdn.provider import base
class RootController(object):
class HostController(base.HostBase):
v1 = HomeController()
def list(self):
print "get list of hostnames from sample"
@expose('json')
def notfound(self):
'''return the generic 404 response
'''
return dict(status=404, message="Not Found")
def create(self, hostname):
print "create hostname at sample"
def delete(self):
print "delete hostname at sample"
def get(self):
print "get hostname from sample"

View File

@@ -1,43 +0,0 @@
# Copyright (c) 2013 Rackspace, Inc.
#
# 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 abc
import six
from cdn import transport
from cdn.transport import DriverBase
from wsgiref import simple_server
@six.add_metaclass(abc.ABCMeta)
class Driver(transport.DriverBase):
def __init__(self, conf):
super(DriverBase, self).__init__()
self.app = None
def listen(self):
"""Self-host using 'bind' and 'port' from the WSGI config group."""
bind = '127.0.0.1'
port = '8080'
msgtmpl = (u'Serving on host %(bind)s:%(port)s')
print(msgtmpl)
httpd = simple_server.make_server(bind=bind,
port=port,
app=self.app)
httpd.serve_forever()

View File

@@ -1,55 +0,0 @@
# Copyright (c) 2014 Rackspace, Inc.
#
# 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.
from pecan import expose, response
from pecan.rest import RestController
class HostsController(RestController):
@expose('json')
def get_all(self):
'''return the list of hostnames
'''
return dict(
hostname='www.sample.com'
)
@expose('json')
def get(self, id):
'''return the configuration of the hostname
'''
return dict(
hostname=id,
description='My Sample Website'
)
@expose('json')
def put(self, id):
'''add the hostname
'''
response.status = 201
return dict(
hostname=id,
description='My Sample Website'
)
@expose('json')
def delete(self, id):
'''delete the hostname
'''
response.status = 204
return None

View File

@@ -40,3 +40,10 @@ port = 8888
[drivers:storage:mongodb]
uri = mongodb://localhost
database = cdn
[drivers:storage:cassandra]
cluster = [0.0.0.0]
keyspace = cdn
[drivers:provider:fastly]
apikey = "MYAPIKEY"

View File

@@ -14,3 +14,6 @@ WebOb>=1.2.3,<1.3
stevedore>=0.10
six>=1.4.1
oslo.config>=1.2.0
-e git+https://github.com/zebrafishlabs/fastly-python#egg=fastly-python
httplib2>=0.8