Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions syncano/models/instances.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import json

import six
from syncano.exceptions import SyncanoValueError

from . import fields
from .base import Model
Expand Down Expand Up @@ -74,9 +77,37 @@ class Meta:
'list': {
'methods': ['post', 'get'],
'path': '/v1.1/instances/',
},
'config': {
'methods': ['put', 'get'],
'path': '/v1.1/instances/{name}/snippets/config/',
}
}

def get_config(self):
properties = self.get_endpoint_data()
http_method = 'GET'
endpoint = self._meta.resolve_endpoint('config', properties, http_method)
connection = self._get_connection()
return connection.request(http_method, endpoint)['config']

def set_config(self, config):
Copy link
Copy Markdown
Contributor

@opalczynski opalczynski Jul 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe check here if this is a valid dict:

        if isinstance(config, six.string_types):
            try:
                config = json.loads(config)
            except (ValueError, TypeError):
                raise SyncanoValueError('...')

        if not isinstance(config, dict):
            raise SyncanoValueError('...')

if isinstance(config, six.string_types):
try:
config = json.loads(config)
except (ValueError, TypeError):
raise SyncanoValueError('Config string is not a parsable JSON.')

if not isinstance(config, dict):
raise SyncanoValueError('Retrieved Config is not a valid dict object.')

properties = self.get_endpoint_data()
http_method = 'PUT'
endpoint = self._meta.resolve_endpoint('config', properties, http_method)
data = {'config': config}
connection = self._get_connection()
connection.request(http_method, endpoint, data=data)


class ApiKey(Model):
"""
Expand Down
43 changes: 43 additions & 0 deletions tests/integration_test_snippet_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-

from syncano.exceptions import SyncanoValueError
from tests.integration_test import InstanceMixin, IntegrationTest


class SnippetConfigTest(InstanceMixin, IntegrationTest):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test here which check something like this:

  1. Create config with some field
  2. Update the config with new field
  3. Check if some field is still present in config after update.


def test_update_config(self):
config = {
'num': 123,
'foo': 'bar',
'arr': [1, 2, 3, 4],
'another': {
'num': 123,
'foo': 'bar',
'arr': [1, 2, 3, 4]
}
}
self.instance.set_config(config)
saved_config = self.instance.get_config()
self.assertDictContainsSubset(config, saved_config, 'Retrieved config should be equal to saved config.')

def test_update_invalid_config(self):
with self.assertRaises(SyncanoValueError):
self.instance.set_config('invalid config')
with self.assertRaises(SyncanoValueError):
self.instance.set_config([1, 2, 3])

def test_update_existing_config(self):
config = {
'foo': 'bar'
}
self.instance.set_config(config)
saved_config = self.instance.get_config()
self.assertIn('foo', saved_config, 'Retrieved config should contain saved key.')
new_config = {
'new_foo': 'new_bar'
}
self.instance.set_config(new_config)
saved_config = self.instance.get_config()
self.assertDictContainsSubset(new_config, saved_config, 'Retrieved config should be equal to saved config.')
self.assertNotIn('foo', saved_config, 'Retrieved config should not contain old keys.')