Skip to content

Commit

Permalink
switch from rollback_id to version
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseshieh committed Apr 30, 2017
1 parent f390fad commit 64c7e68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
6 changes: 3 additions & 3 deletions gigalixir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ def scale(ctx, app_name, replicas, size):

@cli.command()
@click.argument('app_name')
@click.option('-r', '--rollback_id', default=None, help='The rollback id of the release to revert to. Use gigalixir get releases to find the rollback_id. If omitted, this defaults to the second most recent release.')
@click.option('-r', '--version', default=None, help='The version of the release to revert to. Use gigalixir get releases to find the version. If omitted, this defaults to the second most recent release.')
@click.pass_context
@report_errors
def rollback(ctx, app_name, rollback_id):
def rollback(ctx, app_name, version):
"""
Rollback to a previous release.
"""
gigalixir_app.rollback(ctx.obj['host'], app_name, rollback_id)
gigalixir_app.rollback(ctx.obj['host'], app_name, version)


@cli.command()
Expand Down
12 changes: 6 additions & 6 deletions gigalixir/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ def restart(host, app_name):
raise auth.AuthException()
raise Exception(r.text)

def rollback(host, app_name, rollback_id):
if rollback_id == None:
rollback_id = second_most_recent_rollback_id(host, app_name)
r = requests.post('%s/api/apps/%s/releases/%s/rollback' % (host, urllib.quote(app_name.encode('utf-8')), urllib.quote(rollback_id.encode('utf-8'))), headers = {
def rollback(host, app_name, version):
if version == None:
version = second_most_recent_version(host, app_name)
r = requests.post('%s/api/apps/%s/releases/%s/rollback' % (host, urllib.quote(app_name.encode('utf-8')), urllib.quote(str(version).encode('utf-8'))), headers = {
'Content-Type': 'application/json',
})
if r.status_code != 200:
if r.status_code == 401:
raise auth.AuthException()
raise Exception(r.text)

def second_most_recent_rollback_id(host, app_name):
def second_most_recent_version(host, app_name):
r = requests.get('%s/api/apps/%s/releases' % (host, urllib.quote(app_name.encode('utf-8'))), headers = {
'Content-Type': 'application/json',
})
Expand All @@ -133,7 +133,7 @@ def second_most_recent_rollback_id(host, app_name):
if len(data) < 2:
raise Exception("No release available to rollback to.")
else:
return data[1]["rollback_id"]
return data[1]["version"]

def run(host, app_name, module, function):
r = requests.post('%s/api/apps/%s/run' % (host, urllib.quote(app_name.encode('utf-8'))), headers = {
Expand Down
28 changes: 12 additions & 16 deletions test/gigalixir_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,44 +253,40 @@ def test_create_api_key():

@httpretty.activate
def test_get_releases():
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/apps/fake-app-name/releases', body='{"data":[{"slug_url":"another-fake-slug-url","sha":"another-fake-sha","rollback_id":"fake-rollback-id3","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:28:29.000+00:00"},{"slug_url":"fake-slug-url","sha":"fake-sha","rollback_id":"fake-rollback-id2","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:28:28.000+00:00"}]}', content_type='application/json')
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/apps/fake-app-name/releases', body='{"data":[{"sha":"another-fake-sha","version":1,"created_at":"2017-03-29T17:28:29.000+00:00","summary":"fake summary"},{"sha":"fake-sha","version":2,"created_at":"2017-03-29T17:28:28.000+00:00","summary":"fake summary"}]}', content_type='application/json')
runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['releases', 'fake-app-name'])
assert result.exit_code == 0
expect(httpretty.has_request()).to.be.true
assert result.output == """[
{
"created_at": "2017-03-29T17:28:28.000+00:00",
"customer_app_name": "gigalixir_getting_started",
"rollback_id": "fake-rollback-id2",
"sha": "fake-sha",
"slug_url": "fake-slug-url"
"summary": "fake summary",
"version": 2
},
{
"created_at": "2017-03-29T17:28:29.000+00:00",
"customer_app_name": "gigalixir_getting_started",
"rollback_id": "fake-rollback-id3",
"sha": "another-fake-sha",
"slug_url": "another-fake-slug-url"
"summary": "fake summary",
"version": 1
}
]
"""

@httpretty.activate
def test_rollback_without_eligible_release():
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/apps/fake-app-name/releases', body='{"data":[{"slug_url":"another-fake-slug-url","sha":"another-fake-sha","rollback_id":"fake-rollback-id3","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:28:29.000+00:00"}]}', content_type='application/json')
httpretty.register_uri(httpretty.POST, 'https://api.gigalixir.com/api/apps/fake-app-name/releases/fake-rollback-id2/rollback', body='', content_type='application/json')
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/apps/fake-app-name/releases', body='{"data":[{"sha":"another-fake-sha","version":1,"created_at":"2017-03-29T17:28:29.000+00:00","summary":"fake summary"}]}', content_type='application/json')
runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['rollback', 'fake-app-name'])
assert result.exit_code != 0
expect(httpretty.has_request()).to.be.true
expect(httpretty.has_request()).to.be.true
# expect(httpretty.has_request()).to.be.true
expect(len(httpretty.httpretty.latest_requests)).to.equal(1)

@httpretty.activate
def test_rollback_without_rollback_id():
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/apps/fake-app-name/releases', body='{"data":[{"slug_url":"another-fake-slug-url","sha":"another-fake-sha","rollback_id":"fake-rollback-id3","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:28:29.000+00:00"},{"slug_url":"fake-slug-url","sha":"fake-sha","rollback_id":"fake-rollback-id2","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:28:28.000+00:00"}]}', content_type='application/json')
httpretty.register_uri(httpretty.POST, 'https://api.gigalixir.com/api/apps/fake-app-name/releases/fake-rollback-id2/rollback', body='{"data":{"slug_url":"another-fake-slug-url","sha":"another-fake-sha","rollback_id":"52e5dc7b-30d2-4325-afe3-087edeb5f3c2","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:38:35.000+00:00"}}', content_type='application/json')
def test_rollback_without_version():
httpretty.register_uri(httpretty.GET, 'https://api.gigalixir.com/api/apps/fake-app-name/releases', body='{"data":[{"sha":"another-fake-sha","version":3,"created_at":"2017-03-29T17:28:29.000+00:00","summary":"fake summary"},{"sha":"fake-sha","version":2,"created_at":"2017-03-29T17:28:28.000+00:00","summary":"fake summary"}]}', content_type='application/json')
httpretty.register_uri(httpretty.POST, 'https://api.gigalixir.com/api/apps/fake-app-name/releases/2/rollback', body='', content_type='application/json')
runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['rollback', 'fake-app-name'])
assert result.output == ''
Expand All @@ -299,9 +295,9 @@ def test_rollback_without_rollback_id():

@httpretty.activate
def test_rollback():
httpretty.register_uri(httpretty.POST, 'https://api.gigalixir.com/api/apps/fake-app-name/releases/fake-rollback-id/rollback', body='{"data":{"slug_url":"another-fake-slug-url","sha":"another-fake-sha","rollback_id":"52e5dc7b-30d2-4325-afe3-087edeb5f3c2","customer_app_name":"gigalixir_getting_started","created_at":"2017-03-29T17:38:35.000+00:00"}}', content_type='application/json')
httpretty.register_uri(httpretty.POST, 'https://api.gigalixir.com/api/apps/fake-app-name/releases/1/rollback', body='', content_type='application/json')
runner = CliRunner()
result = runner.invoke(gigalixir.cli, ['rollback', 'fake-app-name', '-r', 'fake-rollback-id'])
result = runner.invoke(gigalixir.cli, ['rollback', 'fake-app-name', '-r', '1'])
assert result.output == ''
assert result.exit_code == 0
expect(httpretty.has_request()).to.be.true
Expand Down

0 comments on commit 64c7e68

Please sign in to comment.