Add cli commands to work with release networks
Added two commands to read/modify release networks fuel rel --rel 1 --networks --download Read networks from api and saves them in yaml format on file system, you should be able to modify them and send back with fuel rel --rel 1 --networks --upload DocImpact Change-Id: I774ab8341b45a86bbf77d266aa788424936776cc
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
from functools import partial
|
||||
from functools import wraps
|
||||
from itertools import imap
|
||||
import os
|
||||
|
||||
from fuelclient.cli.error import ArgumentException
|
||||
from fuelclient.cli.formatting import quote_and_join
|
||||
@@ -80,6 +81,15 @@ class Action(object):
|
||||
action_name=self.action_name
|
||||
)
|
||||
|
||||
def full_path_directory(self, directory, base_name):
|
||||
full_path = os.path.join(directory, base_name)
|
||||
if not os.path.exists(full_path):
|
||||
os.mkdir(full_path)
|
||||
return full_path
|
||||
|
||||
def default_directory(self, directory=None):
|
||||
return os.path.abspath(os.curdir if directory is None else directory)
|
||||
|
||||
|
||||
def wrap(method, args, f):
|
||||
"""wrap - is second order function, purpose of which is to
|
||||
|
||||
@@ -13,7 +13,10 @@
|
||||
# under the License.
|
||||
|
||||
from fuelclient.cli.actions.base import Action
|
||||
from fuelclient.cli.actions.base import check_all
|
||||
from fuelclient.cli.actions.base import check_any
|
||||
import fuelclient.cli.arguments as Args
|
||||
from fuelclient.cli.arguments import group
|
||||
from fuelclient.cli.formatting import format_table
|
||||
from fuelclient.objects.release import Release
|
||||
|
||||
@@ -28,8 +31,18 @@ class ReleaseAction(Action):
|
||||
self.args = [
|
||||
Args.get_release_arg('Specify particular release id'),
|
||||
Args.get_list_arg("List all available releases."),
|
||||
Args.get_network_arg("Node network configuration."),
|
||||
Args.get_dir_arg(
|
||||
"Select directory to which download release attributes"),
|
||||
group(
|
||||
Args.get_download_arg(
|
||||
"Download configuration of specific release"),
|
||||
Args.get_upload_arg(
|
||||
"Upload configuration to specific release")
|
||||
)
|
||||
]
|
||||
self.flag_func_map = (
|
||||
('network', self.network),
|
||||
(None, self.list),
|
||||
)
|
||||
|
||||
@@ -59,3 +72,25 @@ class ReleaseAction(Action):
|
||||
acceptable_keys=acceptable_keys
|
||||
)
|
||||
)
|
||||
|
||||
@check_all("release")
|
||||
@check_any("download", "upload")
|
||||
def network(self, params):
|
||||
"""Modify release networks configuration.
|
||||
fuel rel --rel 1 --network --download
|
||||
fuel rel --rel 2 --network --upload
|
||||
"""
|
||||
release = Release(params.release)
|
||||
dir_path = self.full_path_directory(
|
||||
params.dir, 'release_{0}'.format(params.release))
|
||||
full_path = '{0}/networks'.format(dir_path)
|
||||
if params.download:
|
||||
networks = release.get_networks()
|
||||
self.serializer.write_to_file(full_path, networks)
|
||||
print("Networks for release {0} "
|
||||
"downloaded into {1}.yaml".format(release.id, full_path))
|
||||
elif params.upload:
|
||||
networks = self.serializer.read_from_file(full_path)
|
||||
release.update_networks(networks)
|
||||
print("Networks for release {0} uploaded from {1}.yaml".format(
|
||||
release.id, full_path))
|
||||
|
||||
@@ -19,7 +19,16 @@ class Release(BaseObject):
|
||||
|
||||
class_api_path = "releases/"
|
||||
instance_api_path = "releases/{0}/"
|
||||
networks_path = 'releases/{0}/networks'
|
||||
|
||||
@classmethod
|
||||
def get_all(cls):
|
||||
map(cls.init_with_data, cls.get_all_data())
|
||||
|
||||
def get_networks(self):
|
||||
url = self.networks_path.format(self.id)
|
||||
return self.connection.get_request(url)
|
||||
|
||||
def update_networks(self, data):
|
||||
url = self.networks_path.format(self.id)
|
||||
return self.connection.put_request(url, data)
|
||||
|
||||
47
fuelclient/tests/test_release_networks_action.py
Normal file
47
fuelclient/tests/test_release_networks_action.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright 2014 Mirantis, 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 json
|
||||
|
||||
from mock import patch
|
||||
|
||||
from fuelclient.tests import base
|
||||
|
||||
|
||||
API_INPUT = {'config': 'nova_network'}
|
||||
API_OUTPUT = 'config: nova_network\n'
|
||||
|
||||
|
||||
@patch('fuelclient.client.requests')
|
||||
@patch('fuelclient.cli.serializers.open', create=True)
|
||||
@patch('fuelclient.cli.actions.base.os')
|
||||
class TestReleaseNetworkActions(base.UnitTestCase):
|
||||
|
||||
def test_release_network_download(self, mos, mopen, mrequests):
|
||||
mrequests.get().json.return_value = API_INPUT
|
||||
self.execute(['fuel', 'rel', '--rel', '1', '--network', '--download'])
|
||||
mopen().__enter__().write.assert_called_once_with(API_OUTPUT)
|
||||
|
||||
def test_release_network_upload(self, mos, mopen, mrequests):
|
||||
mopen().__enter__().read.return_value = API_OUTPUT
|
||||
self.execute(['fuel', 'rel', '--rel', '1', '--network', '--upload'])
|
||||
self.assertEqual(mrequests.put.call_count, 1)
|
||||
call_args = mrequests.put.call_args_list[0]
|
||||
url = call_args[0][0]
|
||||
kwargs = call_args[1]
|
||||
self.assertIn('releases/1/networks', url)
|
||||
self.assertEqual(
|
||||
json.loads(kwargs['data']), API_INPUT)
|
||||
Reference in New Issue
Block a user