Support to create public instance
Add two params for creating instance: '--is-public' and '--allowed-cidr' Change-Id: I3bc69e3d56d250b562543f0de2c78c05fcaefead Story: 2006500 Task: 36469
This commit is contained in:
parent
6a9479d510
commit
4d54360691
|
@ -268,17 +268,9 @@ class CreateDatabaseInstance(command.ShowOne):
|
|||
)
|
||||
parser.add_argument(
|
||||
'--nic',
|
||||
metavar='<net-id=<net-uuid>,v4-fixed-ip=<ip-addr>,'
|
||||
'port-id=<port-uuid>>',
|
||||
action='append',
|
||||
metavar='<net-id=<net-uuid>>',
|
||||
dest='nics',
|
||||
default=[],
|
||||
help=_("Create a NIC on the instance. Specify option multiple "
|
||||
"times to create multiple NICs. net-id: attach NIC to "
|
||||
"network with this ID (either port-id or net-id must be "
|
||||
"specified), v4-fixed-ip: IPv4 fixed address for NIC "
|
||||
"(optional), port-id: attach NIC to port with this ID "
|
||||
"(either port-id or net-id must be specified)."),
|
||||
help=_("Create instance in the given Neutron network."),
|
||||
)
|
||||
parser.add_argument(
|
||||
'--configuration',
|
||||
|
@ -325,6 +317,18 @@ class CreateDatabaseInstance(command.ShowOne):
|
|||
default=None,
|
||||
help=argparse.SUPPRESS,
|
||||
)
|
||||
parser.add_argument(
|
||||
'--is-public',
|
||||
action='store_true',
|
||||
help="Whether or not to make the instance public.",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--allowed-cidr',
|
||||
action='append',
|
||||
dest='allowed_cidrs',
|
||||
help="The IP CIDRs that are allowed to access the database "
|
||||
"instance.",
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
|
@ -361,41 +365,48 @@ class CreateDatabaseInstance(command.ShowOne):
|
|||
users = [{'name': n, 'password': p, 'databases': databases} for (n, p)
|
||||
in
|
||||
[z.split(':')[:2] for z in parsed_args.users]]
|
||||
|
||||
nics = []
|
||||
for nic_str in parsed_args.nics:
|
||||
nic_info = dict([(k, v) for (k, v) in [z.split("=", 1)[:2] for z in
|
||||
nic_str.split(",")]])
|
||||
# need one or the other, not both, not none (!= ~ XOR)
|
||||
if not (bool(nic_info.get('net-id')) != bool(
|
||||
nic_info.get('port-id'))):
|
||||
raise exceptions.\
|
||||
ValidationError(_("Invalid NIC argument: %s. Must specify "
|
||||
"either net-id or port-id but not both. "
|
||||
"Please refer to help.")
|
||||
% (_("nic='%s'") % nic_str))
|
||||
if parsed_args.nics:
|
||||
nic_info = dict(
|
||||
[(k, v) for (k, v) in [parsed_args.nics.split("=", 1)[:2]]]
|
||||
)
|
||||
if not nic_info.get('net-id'):
|
||||
raise exceptions.ValidationError(
|
||||
"net-id is not set in %s" % parsed_args.nics
|
||||
)
|
||||
nics.append(nic_info)
|
||||
|
||||
modules = []
|
||||
for module in parsed_args.modules:
|
||||
modules.append(osc_utils.find_resource(database.modules,
|
||||
module).id)
|
||||
instance = db_instances.create(parsed_args.name,
|
||||
flavor_id,
|
||||
volume=volume,
|
||||
databases=databases,
|
||||
users=users,
|
||||
restorePoint=restore_point,
|
||||
availability_zone=(parsed_args.
|
||||
availability_zone),
|
||||
datastore=parsed_args.datastore,
|
||||
datastore_version=(parsed_args.
|
||||
datastore_version),
|
||||
nics=nics,
|
||||
configuration=parsed_args.configuration,
|
||||
replica_of=replica_of,
|
||||
replica_count=replica_count,
|
||||
modules=modules,
|
||||
locality=locality,
|
||||
region_name=parsed_args.region)
|
||||
|
||||
access = {'is_public': False}
|
||||
if parsed_args.is_public:
|
||||
access['is_public'] = True
|
||||
if parsed_args.allowed_cidrs:
|
||||
access['allowed_cidrs'] = parsed_args.allowed_cidrs
|
||||
|
||||
instance = db_instances.create(
|
||||
parsed_args.name,
|
||||
flavor_id,
|
||||
volume=volume,
|
||||
databases=databases,
|
||||
users=users,
|
||||
restorePoint=restore_point,
|
||||
availability_zone=(parsed_args.availability_zone),
|
||||
datastore=parsed_args.datastore,
|
||||
datastore_version=(parsed_args.datastore_version),
|
||||
nics=nics,
|
||||
configuration=parsed_args.configuration,
|
||||
replica_of=replica_of,
|
||||
replica_count=replica_count,
|
||||
modules=modules,
|
||||
locality=locality,
|
||||
region_name=parsed_args.region,
|
||||
access=access
|
||||
)
|
||||
instance = set_attributes_for_print_detail(instance)
|
||||
return zip(*sorted(six.iteritems(instance)))
|
||||
|
||||
|
|
|
@ -196,7 +196,10 @@ class TestDatabaseInstanceCreate(TestInstances):
|
|||
'--nic', 'net-id=net1',
|
||||
'--replica_of', 'test',
|
||||
'--replica_count', '4',
|
||||
'--module', 'mod_id']
|
||||
'--module', 'mod_id',
|
||||
'--is-public',
|
||||
'--allowed-cidr', '10.0.0.1/24',
|
||||
'--allowed-cidr', '192.168.0.1/24']
|
||||
verifylist = [
|
||||
('name', 'test-name'),
|
||||
('flavor', '103'),
|
||||
|
@ -205,10 +208,12 @@ class TestDatabaseInstanceCreate(TestInstances):
|
|||
('users', ['u1:111', 'u2:111']),
|
||||
('datastore', "datastore"),
|
||||
('datastore_version', "datastore_version"),
|
||||
('nics', ['net-id=net1']),
|
||||
('nics', 'net-id=net1'),
|
||||
('replica_of', 'test'),
|
||||
('replica_count', 4),
|
||||
('modules', ['mod_id']),
|
||||
('is_public', True),
|
||||
('allowed_cidrs', ['10.0.0.1/24', '192.168.0.1/24'])
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, args, verifylist)
|
||||
columns, data = self.cmd.take_action(parsed_args)
|
||||
|
|
|
@ -97,7 +97,8 @@ class InstancesTest(testtools.TestCase):
|
|||
nics=nics, replica_of='test',
|
||||
replica_count=4,
|
||||
modules=['mod_id'],
|
||||
locality='affinity')
|
||||
locality='affinity',
|
||||
access={'is_public': True})
|
||||
self.assertEqual("/instances", p)
|
||||
self.assertEqual("instance", i)
|
||||
self.assertEqual(['db1', 'db2'], b["instance"]["databases"])
|
||||
|
@ -113,6 +114,7 @@ class InstancesTest(testtools.TestCase):
|
|||
self.assertEqual([{'id': 'mod_id'}], b["instance"]["modules"])
|
||||
self.assertEqual(4, b["instance"]["replica_count"])
|
||||
self.assertEqual('affinity', b["instance"]["locality"])
|
||||
self.assertEqual({'is_public': True}, b["instance"]["access"])
|
||||
|
||||
def test_list(self):
|
||||
page_mock = mock.Mock()
|
||||
|
|
|
@ -93,7 +93,7 @@ class Instances(base.ManagerWithFind):
|
|||
restorePoint=None, availability_zone=None, datastore=None,
|
||||
datastore_version=None, nics=None, configuration=None,
|
||||
replica_of=None, replica_count=None, modules=None,
|
||||
locality=None, region_name=None):
|
||||
locality=None, region_name=None, access=None, **kwargs):
|
||||
"""Create (boot) a new instance."""
|
||||
|
||||
body = {"instance": {
|
||||
|
@ -131,6 +131,8 @@ class Instances(base.ManagerWithFind):
|
|||
body["instance"]["locality"] = locality
|
||||
if region_name:
|
||||
body["instance"]["region_name"] = region_name
|
||||
if access:
|
||||
body["instance"]["access"] = access
|
||||
|
||||
return self._create("/instances", body, "instance")
|
||||
|
||||
|
|
Loading…
Reference in New Issue