Merge "Add BayModel to Magnum resources"
This commit is contained in:
commit
7d3979cec0
|
@ -0,0 +1,157 @@
|
|||
#
|
||||
# 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.
|
||||
|
||||
from heat.common.i18n import _
|
||||
from heat.engine import clients
|
||||
from heat.engine import constraints
|
||||
from heat.engine import properties
|
||||
from heat.engine import resource
|
||||
from heat.engine import support
|
||||
|
||||
|
||||
class BayModel(resource.Resource):
|
||||
"""
|
||||
A resource for the BayModel in Magnum.
|
||||
"""
|
||||
|
||||
support_status = support.SupportStatus(version='5.0.0')
|
||||
|
||||
PROPERTIES = (
|
||||
NAME, IMAGE, FLAVOR, MASTER_FLAVOR, KEYPAIR,
|
||||
EXTERNAL_NETWORK, FIXED_NETWORK, DNS_NAMESERVER,
|
||||
DOCKER_VOLUME_SIZE, SSH_AUTHORIZED_KEY, COE
|
||||
) = (
|
||||
'name', 'image', 'flavor', 'master_flavor', 'keypair',
|
||||
'external_network', 'fixed_network', 'dns_nameserver',
|
||||
'docker_volume_size', 'ssh_authorized_key', 'coe'
|
||||
)
|
||||
|
||||
properties_schema = {
|
||||
NAME: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The bay model name.'),
|
||||
),
|
||||
IMAGE: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The image name or UUID to use as a base image for this '
|
||||
'baymodel.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('glance.image')
|
||||
],
|
||||
required=True
|
||||
),
|
||||
FLAVOR: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The flavor of this bay model.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('nova.flavor')
|
||||
]
|
||||
),
|
||||
MASTER_FLAVOR: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The flavor of the master node for this bay model.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('nova.flavor')
|
||||
]
|
||||
),
|
||||
KEYPAIR: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The name or id of the nova ssh keypair.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('nova.keypair')
|
||||
],
|
||||
required=True
|
||||
),
|
||||
EXTERNAL_NETWORK: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The external network to attach the Bay.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('neutron.network')
|
||||
],
|
||||
required=True
|
||||
),
|
||||
FIXED_NETWORK: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The fixed network to attach the Bay.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('neutron.network')
|
||||
]
|
||||
),
|
||||
DNS_NAMESERVER: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The DNS nameserver address.'),
|
||||
constraints=[
|
||||
constraints.CustomConstraint('ip_addr')
|
||||
]
|
||||
|
||||
),
|
||||
DOCKER_VOLUME_SIZE: properties.Schema(
|
||||
properties.Schema.INTEGER,
|
||||
_('The size in GB of the docker volume.'),
|
||||
constraints=[
|
||||
constraints.Range(min=1),
|
||||
]
|
||||
),
|
||||
SSH_AUTHORIZED_KEY: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The SSH Authorized Key.'),
|
||||
),
|
||||
COE: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('The Container Orchestration Engine for this bay model.'),
|
||||
constraints=[
|
||||
constraints.AllowedValues(['kubernetes', 'swarm'])
|
||||
],
|
||||
required=True
|
||||
)
|
||||
}
|
||||
|
||||
default_client_name = 'magnum'
|
||||
|
||||
def handle_create(self):
|
||||
args = {
|
||||
'name': self.properties[self.NAME],
|
||||
'image_id': self.properties[self.IMAGE],
|
||||
'flavor_id': self.properties[self.FLAVOR],
|
||||
'master_flavor_id': self.properties[self.MASTER_FLAVOR],
|
||||
'keypair_id': self.properties[self.KEYPAIR],
|
||||
'external_network_id': self.properties[self.EXTERNAL_NETWORK],
|
||||
'fixed_network': self.properties[self.FIXED_NETWORK],
|
||||
'dns_nameserver': self.properties[self.DNS_NAMESERVER],
|
||||
'docker_volume_size': self.properties[self.DOCKER_VOLUME_SIZE],
|
||||
'ssh_authorized_key': self.properties[self.SSH_AUTHORIZED_KEY],
|
||||
'coe': self.properties[self.COE]
|
||||
}
|
||||
bm = self.client().baymodels.create(**args)
|
||||
self.resource_id_set(bm.uuid)
|
||||
|
||||
def handle_delete(self):
|
||||
if not self.resource_id:
|
||||
return
|
||||
try:
|
||||
self.client().baymodels.delete(self.resource_id)
|
||||
except Exception as exc:
|
||||
self.client_plugin().ignore_not_found(exc)
|
||||
|
||||
|
||||
def resource_mapping():
|
||||
return {
|
||||
'OS::Magnum::BayModel': BayModel
|
||||
}
|
||||
|
||||
|
||||
def available_resource_mapping():
|
||||
if not clients.has_client('magnum'):
|
||||
return {}
|
||||
|
||||
return resource_mapping()
|
|
@ -0,0 +1,85 @@
|
|||
#
|
||||
# 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 mock
|
||||
|
||||
from heat.common import template_format
|
||||
from heat.engine import resource
|
||||
from heat.engine.resources.openstack.magnum import baymodel
|
||||
from heat.engine import scheduler
|
||||
from heat.tests import common
|
||||
from heat.tests import utils
|
||||
|
||||
|
||||
magnum_template = '''
|
||||
heat_template_version: 2015-04-30
|
||||
resources:
|
||||
test_baymodel:
|
||||
type: OS::Magnum::BayModel
|
||||
properties:
|
||||
name: test_bay_model
|
||||
image: fedora-21-atomic-2
|
||||
flavor: m1.small
|
||||
master_flavor: m1.medium
|
||||
keypair: heat_key
|
||||
external_network: 0244b54d-ae1f-44f0-a24a-442760f1d681
|
||||
fixed_network: 0f59a3dd-fac1-4d03-b41a-d4115fbffa89
|
||||
dns_nameserver: 8.8.8.8
|
||||
docker_volume_size: 5
|
||||
ssh_authorized_key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB
|
||||
coe: 'swarm'
|
||||
'''
|
||||
|
||||
RESOURCE_TYPE = 'OS::Magnum::BayModel'
|
||||
|
||||
|
||||
class TestMagnumBayModel(common.HeatTestCase):
|
||||
def setUp(self):
|
||||
super(TestMagnumBayModel, self).setUp()
|
||||
self.ctx = utils.dummy_context()
|
||||
resource._register_class(RESOURCE_TYPE, baymodel.BayModel)
|
||||
t = template_format.parse(magnum_template)
|
||||
self.stack = utils.parse_stack(t)
|
||||
|
||||
resource_defns = self.stack.t.resource_definitions(self.stack)
|
||||
self.rsrc_defn = resource_defns['test_baymodel']
|
||||
self.client = mock.Mock()
|
||||
self.patchobject(baymodel.BayModel, 'client', return_value=self.client)
|
||||
self.stub_FlavorConstraint_validate()
|
||||
self.stub_KeypairConstraint_validate()
|
||||
self.stub_ImageConstraint_validate()
|
||||
self.stub_NetworkConstraint_validate()
|
||||
|
||||
def _create_resource(self, name, snippet, stack):
|
||||
self.resource_id = '12345'
|
||||
self.test_bay_model = self.stack['test_baymodel']
|
||||
value = mock.MagicMock(uuid=self.resource_id)
|
||||
self.client.baymodels.create.return_value = value
|
||||
bm = baymodel.BayModel(name, snippet, stack)
|
||||
scheduler.TaskRunner(bm.create)()
|
||||
return bm
|
||||
|
||||
def test_bay_model_create(self):
|
||||
bm = self._create_resource('bm', self.rsrc_defn, self.stack)
|
||||
self.assertEqual(self.resource_id, bm.resource_id)
|
||||
self.assertEqual((bm.CREATE, bm.COMPLETE), bm.state)
|
||||
|
||||
def test_bay_model_delete(self):
|
||||
bm = self._create_resource('bm', self.rsrc_defn, self.stack)
|
||||
scheduler.TaskRunner(bm.delete)()
|
||||
self.assertEqual((bm.DELETE, bm.COMPLETE), bm.state)
|
||||
|
||||
def test_resource_mapping(self):
|
||||
mapping = baymodel.resource_mapping()
|
||||
self.assertEqual(1, len(mapping))
|
||||
self.assertEqual(baymodel.BayModel, mapping[RESOURCE_TYPE])
|
Loading…
Reference in New Issue