72
72
self._node_uuid = wtypes.Unset
75
"Unique UUID for this port"
75
"""Unique UUID for this port"""
77
77
address = wsme.wsattr(types.macaddress, mandatory=True)
78
"MAC Address for this port"
78
"""MAC Address for this port"""
80
80
extra = {wtypes.text: types.MultiType(wtypes.text, six.integer_types)}
81
"This port's meta data"
81
"""This port's meta data"""
83
83
node_uuid = wsme.wsproperty(types.uuid, _get_node_uuid, _set_node_uuid,
85
"The UUID of the node this port belongs to"
85
"""The UUID of the node this port belongs to"""
87
87
links = wsme.wsattr([link.Link], readonly=True)
88
"A list containing a self link and associated port links"
88
"""A list containing a self link and associated port links"""
90
90
def __init__(self, **kwargs):
92
fields = list(objects.Port.fields.keys())
92
fields = list(objects.Port.fields)
93
93
# NOTE(lucasagomes): node_uuid is not part of objects.Port.fields
94
94
# because it's an API-only attribute
95
95
fields.append('node_uuid')
98
98
if not hasattr(self, field):
100
100
self.fields.append(field)
101
setattr(self, field, kwargs.get(field))
101
setattr(self, field, kwargs.get(field, wtypes.Unset))
103
103
# NOTE(lucasagomes): node_id is an attribute created on-the-fly
104
104
# by _set_node_uuid(), it needs to be present in the fields so
105
105
# that as_dict() will contain node_id field when converting it
106
106
# before saving it in the database.
107
107
self.fields.append('node_id')
108
setattr(self, 'node_uuid', kwargs.get('node_id'))
108
setattr(self, 'node_uuid', kwargs.get('node_id', wtypes.Unset))
111
def convert_with_links(cls, rpc_port, expand=True):
112
port = Port(**rpc_port.as_dict())
111
def _convert_with_links(port, url, expand=True):
114
113
port.unset_fields_except(['uuid', 'address'])
116
115
# never expose the node_id attribute
117
116
port.node_id = wtypes.Unset
119
port.links = [link.Link.make_link('self', pecan.request.host_url,
118
port.links = [link.Link.make_link('self', url,
120
119
'ports', port.uuid),
121
link.Link.make_link('bookmark',
122
pecan.request.host_url,
120
link.Link.make_link('bookmark', url,
123
121
'ports', port.uuid,
127
def convert_with_links(cls, rpc_port, expand=True):
128
port = Port(**rpc_port.as_dict())
129
return cls._convert_with_links(port, pecan.request.host_url, expand)
132
def sample(cls, expand=True):
130
133
sample = cls(uuid='27e3153e-d5bf-4b7e-b517-fb518e17f34c',
131
134
address='fe:54:00:77:07:d9',
132
135
extra={'foo': 'bar'},
135
138
# NOTE(lucasagomes): node_uuid getter() method look at the
136
139
# _node_uuid variable
137
140
sample._node_uuid = '7ae81bb3-dec3-4289-8d6c-da80bd8001ae'
141
return cls._convert_with_links(sample, 'http://localhost:6385', expand)
141
144
class PortCollection(collection.Collection):
142
145
"""API representation of a collection of ports."""
145
"A list containing ports objects"
148
"""A list containing ports objects"""
147
150
def __init__(self, **kwargs):
148
151
self._type = 'ports'
151
def convert_with_links(cls, rpc_ports, limit, url=None,
152
expand=False, **kwargs):
154
def convert_with_links(rpc_ports, limit, url=None, expand=False, **kwargs):
153
155
collection = PortCollection()
154
156
collection.ports = [Port.convert_with_links(p, expand)
155
157
for p in rpc_ports]