Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable VCL snippets #26

Merged
merged 12 commits into from
Aug 22, 2017
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ $ ansible-galaxy install Jimdo.fastly
| response | false | string | Ok |
| status | false | integer | 200 |

### VCL Snippets

[Fastly documentation](https://docs.fastly.com/api/config#snippet)

| Field | Required | Type | Default |
|:----------|:---------|:----------------------------------------|:--------|
| name | true | string | |
| dynamic | false | integer | 0 |
| type | false | string | "init" |
| content | true | string | |
| priority | false | integer | 100 |

### Settings

[Fastly documentation](https://docs.fastly.com/api/config#settings)
Expand Down
53 changes: 53 additions & 0 deletions library/fastly_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
required: false
description:
- List of response objects
vcl_snippets:
required: false
description:
- List of VCL snippets
settings:
required: false
description:
Expand Down Expand Up @@ -76,6 +80,17 @@
src: http://test3.example.net req.url.path
ignore_if_set: 0
priority: 10
vcl_snippets
- name: Deliver stale content
dynamic: 0
type: deliver
content: >
if (resp.status >= 500 && resp.status < 600) {
if (stale.exists) {
restart;
}
}
priority: 110
response_objects:
- name: Set 301 status code
status: 301
Expand Down Expand Up @@ -327,6 +342,24 @@ def __init__(self, config, validate_choices):
self.status = self.read_config(config, validate_choices, 'status')


class FastlyVclSnippet(FastlyObject):
schema = {
'name': dict(required=True, type='str', default=None),
'dynamic': dict(required=False, type='int', default=0),
'type': dict(required=False, type='str', default='init'),
'content': dict(required=True, type='str', default=None),
'priority': dict(required=False, type='int', default=100)
}
sort_key = lambda f: f.name

def __init__(self, config, validate_choices):
self.name = self.read_config(config, validate_choices, 'name')
self.dynamic = self.read_config(config, validate_choices, 'dynamic')
self.type = self.read_config(config, validate_choices, 'type')
self.content = self.read_config(config, validate_choices, 'content')
self.priority = self.read_config(config, validate_choices, 'priority')


class FastlySettings(FastlyObject):
schema = {
'general.default_ttl': dict(required=False, type='int', default=3600)
Expand All @@ -350,6 +383,7 @@ def __init__(self, configuration, validate_choices = True):
self.gzips = []
self.headers = []
self.response_objects = []
self.snippets = []
self.settings = FastlySettings(dict(), validate_choices)

if 'domains' in configuration and configuration['domains'] is not None:
Expand Down Expand Up @@ -380,6 +414,10 @@ def __init__(self, configuration, validate_choices = True):
for response_object in configuration['response_objects']:
self.response_objects.append(FastlyResponseObject(response_object, validate_choices))

if 'snippets' in configuration and configuration['snippets'] is not None:
for snippet in configuration['snippets']:
self.snippets.append(FastlyVclSnippet(snippet, validate_choices))

if 'settings' in configuration and configuration['settings'] is not None:
self.settings = FastlySettings(configuration['settings'], validate_choices)

Expand All @@ -391,6 +429,7 @@ def __eq__(self, other):
and sorted(self.gzips, key=FastlyGzip.sort_key) == sorted(other.gzips, key=FastlyGzip.sort_key) \
and sorted(self.headers, key=FastlyHeader.sort_key) == sorted(other.headers, key=FastlyHeader.sort_key) \
and sorted(self.response_objects, key=FastlyResponseObject.sort_key) == sorted(other.response_objects, key=FastlyResponseObject.sort_key) \
and sorted(self.snippets, key=FastlyVclSnippet.sort_key) == sorted(other.snippets, key=FastlyVclSnippet.sort_key) \
and self.settings == other.settings

def __ne__(self, other):
Expand Down Expand Up @@ -561,6 +600,15 @@ def create_response_object(self, service_id, version, response_object):
raise Exception("Error creating response object for for service %s, version %s (%s)" % (
service_id, version, response.payload['detail']))

def create_vcl_snippet(self, service_id, version, vcl_snippet):
response = self._request('/service/%s/version/%s/snippet' % (service_id, version), 'POST', vcl_snippet)

if response.status == 200:
return response.payload
else:
raise Exception("Error creating VCL snippet '%s' for service %s, version %s (%s)" % (vcl_snippet['name'],
service_id, version, response.payload['detail']))

def create_settings(self, service_id, version, settings):
response = self._request('/service/%s/version/%s/settings' % (service_id, version), 'PUT', settings)
if response.status == 200:
Expand Down Expand Up @@ -633,6 +681,9 @@ def deploy_version_with_configuration(self, service_id, configuration, activate_
for response_object in configuration.response_objects:
self.client.create_response_object(service_id, version_number, response_object)

for vcl_snippet in configuration.snippets:
self.client.create_vcl_snippet(service_id, version_number, vcl_snippet)

if configuration.settings:
self.client.create_settings(service_id, version_number, configuration.settings)

Expand Down Expand Up @@ -670,6 +721,7 @@ def __init__(self):
gzips=dict(default=None, required=False, type='list'),
headers=dict(default=None, required=False, type='list'),
response_objects=dict(default=None, required=False, type='list'),
vcl_snippets=dict(default=None, required=False, type='list'),
settings=dict(default=None, required=False, type='dict'),
),
supports_check_mode=False
Expand All @@ -694,6 +746,7 @@ def configuration(self):
'gzips': self.module.params['gzips'],
'headers': self.module.params['headers'],
'response_objects': self.module.params['response_objects'],
'snippets': self.module.params['vcl_snippets'],
'settings': self.module.params['settings']
})
except FastlyValidationError as err:
Expand Down
Loading