Debian Codegraph @

ansible_collections/community/general/plugins/modules/hwc_vpc_port.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2019 Huawei
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 ###############################################################################
10 # Documentation
11 ###############################################################################
12
13 DOCUMENTATION = r"""
14 module: hwc_vpc_port
15 description:
16 - VPC port management.
17 short_description: Creates a resource of VPC/Port in Huawei Cloud
18 version_added: '0.2.0'
19 author: Huawei Inc. (@huaweicloud)
20 requirements:
21 - keystoneauth1 >= 3.6.0
22 attributes:
23 check_mode:
24 support: full
25 diff_mode:
26 support: none
27 options:
28 state:
29 description:
30 - Whether the given object should exist in Huawei Cloud.
31 type: str
32 choices: ['present', 'absent']
33 default: 'present'
34 timeouts:
35 description:
36 - The timeouts for each operations.
37 type: dict
38 default: {}
39 suboptions:
40 create:
41 description:
42 - The timeouts for create operation.
43 type: str
44 default: '15m'
45 subnet_id:
46 description:
47 - Specifies the ID of the subnet to which the port belongs.
48 type: str
49 required: true
50 admin_state_up:
51 description:
52 - Specifies the administrative state of the port.
53 type: bool
54 allowed_address_pairs:
55 description:
56 - Specifies a set of zero or more allowed address pairs.
57 type: list
58 elements: dict
59 suboptions:
60 ip_address:
61 description:
62 - Specifies the IP address. It cannot set it to 0.0.0.0. Configure an independent security group for the port if
63 a large CIDR block (subnet mask less than 24) is configured for parameter allowed_address_pairs.
64 type: str
65 mac_address:
66 description:
67 - Specifies the MAC address.
68 type: str
69 extra_dhcp_opts:
70 description:
71 - Specifies the extended option of DHCP.
72 type: list
73 elements: dict
74 suboptions:
75 name:
76 description:
77 - Specifies the option name.
78 type: str
79 value:
80 description:
81 - Specifies the option value.
82 type: str
83 ip_address:
84 description:
85 - Specifies the port IP address.
86 type: str
87 name:
88 description:
89 - Specifies the port name. The value can contain no more than 255 characters.
90 type: str
91 security_groups:
92 description:
93 - Specifies the ID of the security group.
94 type: list
95 elements: str
96 extends_documentation_fragment:
97 - community.general._hwc
98 - community.general._attributes
99 """
100
101 EXAMPLES = r"""
102 # create a port
103 - name: Create vpc
104 hwc_network_vpc:
105 cidr: "192.168.100.0/24"
106 name: "ansible_network_vpc_test"
107 register: vpc
108 - name: Create subnet
109 hwc_vpc_subnet:
110 gateway_ip: "192.168.100.32"
111 name: "ansible_network_subnet_test"
112 dhcp_enable: true
113 vpc_id: "{{ vpc.id }}"
114 cidr: "192.168.100.0/26"
115 register: subnet
116 - name: Create a port
117 community.general.hwc_vpc_port:
118 subnet_id: "{{ subnet.id }}"
119 ip_address: "192.168.100.33"
120 """
121
122 RETURN = r"""
123 subnet_id:
124 description:
125 - Specifies the ID of the subnet to which the port belongs.
126 type: str
127 returned: success
128 admin_state_up:
129 description:
130 - Specifies the administrative state of the port.
131 type: bool
132 returned: success
133 allowed_address_pairs:
134 description:
135 - Specifies a set of zero or more allowed address pairs.
136 type: list
137 returned: success
138 contains:
139 ip_address:
140 description:
141 - Specifies the IP address. It cannot set it to 0.0.0.0. Configure an independent security group for the port if a
142 large CIDR block (subnet mask less than 24) is configured for parameter allowed_address_pairs.
143 type: str
144 returned: success
145 mac_address:
146 description:
147 - Specifies the MAC address.
148 type: str
149 returned: success
150 extra_dhcp_opts:
151 description:
152 - Specifies the extended option of DHCP.
153 type: list
154 returned: success
155 contains:
156 name:
157 description:
158 - Specifies the option name.
159 type: str
160 returned: success
161 value:
162 description:
163 - Specifies the option value.
164 type: str
165 returned: success
166 ip_address:
167 description:
168 - Specifies the port IP address.
169 type: str
170 returned: success
171 name:
172 description:
173 - Specifies the port name. The value can contain no more than 255 characters.
174 type: str
175 returned: success
176 security_groups:
177 description:
178 - Specifies the ID of the security group.
179 type: list
180 returned: success
181 mac_address:
182 description:
183 - Specifies the port MAC address.
184 type: str
185 returned: success
186 """
187
188 from ansible_collections.community.general.plugins.module_utils._hwc_utils import (
189 Config,
190 HwcClientException,
191 HwcClientException404,
192 HwcModule,
193 are_different_dicts,
194 build_path,
195 get_region,
196 is_empty_value,
197 navigate_value,
198 wait_to_finish,
199 )
200
201
202 def build_module():
203 return HwcModule(
204 argument_spec=dict(
205 state=dict(default="present", choices=["present", "absent"], type="str"),
206 timeouts=dict(
207 type="dict",
208 options=dict(
209 create=dict(default="15m", type="str"),
210 ),
211 default=dict(),
212 ),
213 subnet_id=dict(type="str", required=True),
214 admin_state_up=dict(type="bool"),
215 allowed_address_pairs=dict(
216 type="list",
217 elements="dict",
218 options=dict(ip_address=dict(type="str"), mac_address=dict(type="str")),
219 ),
220 extra_dhcp_opts=dict(
221 type="list", elements="dict", options=dict(name=dict(type="str"), value=dict(type="str"))
222 ),
223 ip_address=dict(type="str"),
224 name=dict(type="str"),
225 security_groups=dict(type="list", elements="str"),
226 ),
227 supports_check_mode=True,
228 )
229
230
231 def main():
232 """Main function"""
233
234 module = build_module()
235 config = Config(module, "vpc")
236
237 try:
238 resource = None
239 if module.params["id"]:
240 resource = True
241 else:
242 v = search_resource(config)
243 if len(v) > 1:
244 raise Exception(f"Found more than one resource({', '.join([navigate_value(i, ['id']) for i in v])})")
245
246 if len(v) == 1:
247 resource = v[0]
248 module.params["id"] = navigate_value(resource, ["id"])
249
250 result = {}
251 changed = False
252 if module.params["state"] == "present":
253 if resource is None:
254 if not module.check_mode:
255 create(config)
256 changed = True
257
258 current = read_resource(config, exclude_output=True)
259 expect = user_input_parameters(module)
260 if are_different_dicts(expect, current):
261 if not module.check_mode:
262 update(config)
263 changed = True
264
265 result = read_resource(config)
266 result["id"] = module.params.get("id")
267 else:
268 if resource:
269 if not module.check_mode:
270 delete(config)
271 changed = True
272
273 except Exception as ex:
274 module.fail_json(msg=str(ex))
275
276 else:
277 result["changed"] = changed
278 module.exit_json(**result)
279
280
281 def user_input_parameters(module):
282 return {
283 "admin_state_up": module.params.get("admin_state_up"),
284 "allowed_address_pairs": module.params.get("allowed_address_pairs"),
285 "extra_dhcp_opts": module.params.get("extra_dhcp_opts"),
286 "ip_address": module.params.get("ip_address"),
287 "name": module.params.get("name"),
288 "security_groups": module.params.get("security_groups"),
289 "subnet_id": module.params.get("subnet_id"),
290 }
291
292
293 def create(config):
294 module = config.module
295 client = config.client(get_region(module), "vpc", "project")
296 timeout = 60 * int(module.params["timeouts"]["create"].rstrip("m"))
297 opts = user_input_parameters(module)
298
299 params = build_create_parameters(opts)
300 r = send_create_request(module, params, client)
301 obj = async_wait_create(config, r, client, timeout)
302 module.params["id"] = navigate_value(obj, ["port", "id"])
303
304
305 def update(config):
306 module = config.module
307 client = config.client(get_region(module), "vpc", "project")
308 opts = user_input_parameters(module)
309
310 params = build_update_parameters(opts)
311 if params:
312 send_update_request(module, params, client)
313
314
315 def delete(config):
316 module = config.module
317 client = config.client(get_region(module), "vpc", "project")
318
319 send_delete_request(module, None, client)
320
321 url = build_path(module, "ports/{id}")
322
323 def _refresh_status():
324 try:
325 client.get(url)
326 except HwcClientException404:
327 return True, "Done"
328
329 except Exception:
330 return None, ""
331
332 return True, "Pending"
333
334 timeout = 60 * int(module.params["timeouts"]["create"].rstrip("m"))
335 try:
336 wait_to_finish(["Done"], ["Pending"], _refresh_status, timeout)
337 except Exception as ex:
338 module.fail_json(msg=f"module(hwc_vpc_port): error waiting for api(delete) to be done, error= {ex}")
339
340
341 def read_resource(config, exclude_output=False):
342 module = config.module
343 client = config.client(get_region(module), "vpc", "project")
344
345 res = {}
346
347 r = send_read_request(module, client)
348 res["read"] = fill_read_resp_body(r)
349
350 array_index = {
351 "read.fixed_ips": 0,
352 }
353
354 return update_properties(module, res, array_index, exclude_output)
355
356
357 def _build_query_link(opts):
358 query_params = []
359
360 v = navigate_value(opts, ["subnet_id"])
361 if v:
362 query_params.append(f"network_id={v}")
363
364 v = navigate_value(opts, ["name"])
365 if v:
366 query_params.append(f"name={v}")
367
368 v = navigate_value(opts, ["admin_state_up"])
369 if v:
370 query_params.append(f"admin_state_up={v}")
371
372 query_link = "?marker={marker}&limit=10"
373 if query_params:
374 query_link += f"&{'&'.join(query_params)}"
375
376 return query_link
377
378
379 def search_resource(config):
380 module = config.module
381 client = config.client(get_region(module), "vpc", "project")
382 opts = user_input_parameters(module)
383 identity_obj = _build_identity_object(opts)
384 query_link = _build_query_link(opts)
385 link = f"ports{query_link}"
386
387 result = []
388 p = {"marker": ""}
389 while True:
390 url = link.format(**p)
391 r = send_list_request(module, client, url)
392 if not r:
393 break
394
395 for item in r:
396 item = fill_list_resp_body(item)
397 if not are_different_dicts(identity_obj, item):
398 result.append(item)
399
400 if len(result) > 1:
401 break
402
403 p["marker"] = r[-1].get("id")
404
405 return result
406
407
408 def build_create_parameters(opts):
409 params = dict()
410
411 v = navigate_value(opts, ["admin_state_up"], None)
412 if not is_empty_value(v):
413 params["admin_state_up"] = v
414
415 v = expand_create_allowed_address_pairs(opts, None)
416 if not is_empty_value(v):
417 params["allowed_address_pairs"] = v
418
419 v = expand_create_extra_dhcp_opts(opts, None)
420 if not is_empty_value(v):
421 params["extra_dhcp_opts"] = v
422
423 v = expand_create_fixed_ips(opts, None)
424 if not is_empty_value(v):
425 params["fixed_ips"] = v
426
427 v = navigate_value(opts, ["name"], None)
428 if not is_empty_value(v):
429 params["name"] = v
430
431 v = navigate_value(opts, ["subnet_id"], None)
432 if not is_empty_value(v):
433 params["network_id"] = v
434
435 v = navigate_value(opts, ["security_groups"], None)
436 if not is_empty_value(v):
437 params["security_groups"] = v
438
439 if not params:
440 return params
441
442 params = {"port": params}
443
444 return params
445
446
447 def expand_create_allowed_address_pairs(d, array_index):
448 new_array_index = dict()
449 if array_index:
450 new_array_index.update(array_index)
451
452 req = []
453
454 v = navigate_value(d, ["allowed_address_pairs"], new_array_index)
455 if not v:
456 return req
457 n = len(v)
458 for i in range(n):
459 new_array_index["allowed_address_pairs"] = i
460 transformed = dict()
461
462 v = navigate_value(d, ["allowed_address_pairs", "ip_address"], new_array_index)
463 if not is_empty_value(v):
464 transformed["ip_address"] = v
465
466 v = navigate_value(d, ["allowed_address_pairs", "mac_address"], new_array_index)
467 if not is_empty_value(v):
468 transformed["mac_address"] = v
469
470 if transformed:
471 req.append(transformed)
472
473 return req
474
475
476 def expand_create_extra_dhcp_opts(d, array_index):
477 new_array_index = dict()
478 if array_index:
479 new_array_index.update(array_index)
480
481 req = []
482
483 v = navigate_value(d, ["extra_dhcp_opts"], new_array_index)
484 if not v:
485 return req
486 n = len(v)
487 for i in range(n):
488 new_array_index["extra_dhcp_opts"] = i
489 transformed = dict()
490
491 v = navigate_value(d, ["extra_dhcp_opts", "name"], new_array_index)
492 if not is_empty_value(v):
493 transformed["opt_name"] = v
494
495 v = navigate_value(d, ["extra_dhcp_opts", "value"], new_array_index)
496 if not is_empty_value(v):
497 transformed["opt_value"] = v
498
499 if transformed:
500 req.append(transformed)
501
502 return req
503
504
505 def expand_create_fixed_ips(d, array_index):
506 new_array_index = dict()
507 if array_index:
508 new_array_index.update(array_index)
509
510 req = []
511
512 transformed = dict()
513
514 v = navigate_value(d, ["ip_address"], new_array_index)
515 if not is_empty_value(v):
516 transformed["ip_address"] = v
517
518 if transformed:
519 req.append(transformed)
520
521 return req
522
523
524 def send_create_request(module, params, client):
525 url = "ports"
526 try:
527 r = client.post(url, params)
528 except HwcClientException as ex:
529 msg = f"module(hwc_vpc_port): error running api(create), error: {ex}"
530 module.fail_json(msg=msg)
531
532 return r
533
534
535 def async_wait_create(config, result, client, timeout):
536 module = config.module
537
538 path_parameters = {
539 "port_id": ["port", "id"],
540 }
541 data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
542
543 url = build_path(module, "ports/{port_id}", data)
544
545 def _query_status():
546 r = None
547 try:
548 r = client.get(url, timeout=timeout)
549 except HwcClientException:
550 return None, ""
551
552 try:
553 s = navigate_value(r, ["port", "status"])
554 return r, s
555 except Exception:
556 return None, ""
557
558 try:
559 return wait_to_finish(["ACTIVE", "DOWN"], ["BUILD"], _query_status, timeout)
560 except Exception as ex:
561 module.fail_json(msg=f"module(hwc_vpc_port): error waiting for api(create) to be done, error= {ex}")
562
563
564 def build_update_parameters(opts):
565 params = dict()
566
567 v = expand_update_allowed_address_pairs(opts, None)
568 if v is not None:
569 params["allowed_address_pairs"] = v
570
571 v = expand_update_extra_dhcp_opts(opts, None)
572 if v is not None:
573 params["extra_dhcp_opts"] = v
574
575 v = navigate_value(opts, ["name"], None)
576 if not is_empty_value(v):
577 params["name"] = v
578
579 v = navigate_value(opts, ["security_groups"], None)
580 if not is_empty_value(v):
581 params["security_groups"] = v
582
583 if not params:
584 return params
585
586 params = {"port": params}
587
588 return params
589
590
591 def expand_update_allowed_address_pairs(d, array_index):
592 new_array_index = dict()
593 if array_index:
594 new_array_index.update(array_index)
595
596 req = []
597
598 v = navigate_value(d, ["allowed_address_pairs"], new_array_index)
599 if not v:
600 return req
601 n = len(v)
602 for i in range(n):
603 new_array_index["allowed_address_pairs"] = i
604 transformed = dict()
605
606 v = navigate_value(d, ["allowed_address_pairs", "ip_address"], new_array_index)
607 if not is_empty_value(v):
608 transformed["ip_address"] = v
609
610 v = navigate_value(d, ["allowed_address_pairs", "mac_address"], new_array_index)
611 if not is_empty_value(v):
612 transformed["mac_address"] = v
613
614 if transformed:
615 req.append(transformed)
616
617 return req
618
619
620 def expand_update_extra_dhcp_opts(d, array_index):
621 new_array_index = dict()
622 if array_index:
623 new_array_index.update(array_index)
624
625 req = []
626
627 v = navigate_value(d, ["extra_dhcp_opts"], new_array_index)
628 if not v:
629 return req
630 n = len(v)
631 for i in range(n):
632 new_array_index["extra_dhcp_opts"] = i
633 transformed = dict()
634
635 v = navigate_value(d, ["extra_dhcp_opts", "name"], new_array_index)
636 if not is_empty_value(v):
637 transformed["opt_name"] = v
638
639 v = navigate_value(d, ["extra_dhcp_opts", "value"], new_array_index)
640 if not is_empty_value(v):
641 transformed["opt_value"] = v
642
643 if transformed:
644 req.append(transformed)
645
646 return req
647
648
649 def send_update_request(module, params, client):
650 url = build_path(module, "ports/{id}")
651
652 try:
653 r = client.put(url, params)
654 except HwcClientException as ex:
655 msg = f"module(hwc_vpc_port): error running api(update), error: {ex}"
656 module.fail_json(msg=msg)
657
658 return r
659
660
661 def send_delete_request(module, params, client):
662 url = build_path(module, "ports/{id}")
663
664 try:
665 r = client.delete(url, params)
666 except HwcClientException as ex:
667 msg = f"module(hwc_vpc_port): error running api(delete), error: {ex}"
668 module.fail_json(msg=msg)
669
670 return r
671
672
673 def send_read_request(module, client):
674 url = build_path(module, "ports/{id}")
675
676 r = None
677 try:
678 r = client.get(url)
679 except HwcClientException as ex:
680 msg = f"module(hwc_vpc_port): error running api(read), error: {ex}"
681 module.fail_json(msg=msg)
682
683 return navigate_value(r, ["port"], None)
684
685
686 def fill_read_resp_body(body):
687 result = dict()
688
689 result["admin_state_up"] = body.get("admin_state_up")
690
691 v = fill_read_resp_allowed_address_pairs(body.get("allowed_address_pairs"))
692 result["allowed_address_pairs"] = v
693
694 result["binding_host_id"] = body.get("binding_host_id")
695
696 result["binding_vnic_type"] = body.get("binding_vnic_type")
697
698 result["device_id"] = body.get("device_id")
699
700 result["device_owner"] = body.get("device_owner")
701
702 result["dns_name"] = body.get("dns_name")
703
704 v = fill_read_resp_extra_dhcp_opts(body.get("extra_dhcp_opts"))
705 result["extra_dhcp_opts"] = v
706
707 v = fill_read_resp_fixed_ips(body.get("fixed_ips"))
708 result["fixed_ips"] = v
709
710 result["id"] = body.get("id")
711
712 result["mac_address"] = body.get("mac_address")
713
714 result["name"] = body.get("name")
715
716 result["network_id"] = body.get("network_id")
717
718 result["security_groups"] = body.get("security_groups")
719
720 result["status"] = body.get("status")
721
722 result["tenant_id"] = body.get("tenant_id")
723
724 return result
725
726
727 def fill_read_resp_allowed_address_pairs(value):
728 if not value:
729 return None
730
731 result = []
732 for item in value:
733 val = dict()
734
735 val["ip_address"] = item.get("ip_address")
736
737 val["mac_address"] = item.get("mac_address")
738
739 result.append(val)
740
741 return result
742
743
744 def fill_read_resp_extra_dhcp_opts(value):
745 if not value:
746 return None
747
748 result = []
749 for item in value:
750 val = dict()
751
752 val["opt_name"] = item.get("opt_name")
753
754 val["opt_value"] = item.get("opt_value")
755
756 result.append(val)
757
758 return result
759
760
761 def fill_read_resp_fixed_ips(value):
762 if not value:
763 return None
764
765 result = []
766 for item in value:
767 val = dict()
768
769 val["ip_address"] = item.get("ip_address")
770
771 result.append(val)
772
773 return result
774
775
776 def update_properties(module, response, array_index, exclude_output=False):
777 r = user_input_parameters(module)
778
779 v = navigate_value(response, ["read", "admin_state_up"], array_index)
780 r["admin_state_up"] = v
781
782 v = r.get("allowed_address_pairs")
783 v = flatten_allowed_address_pairs(response, array_index, v, exclude_output)
784 r["allowed_address_pairs"] = v
785
786 v = r.get("extra_dhcp_opts")
787 v = flatten_extra_dhcp_opts(response, array_index, v, exclude_output)
788 r["extra_dhcp_opts"] = v
789
790 v = navigate_value(response, ["read", "fixed_ips", "ip_address"], array_index)
791 r["ip_address"] = v
792
793 if not exclude_output:
794 v = navigate_value(response, ["read", "mac_address"], array_index)
795 r["mac_address"] = v
796
797 v = navigate_value(response, ["read", "name"], array_index)
798 r["name"] = v
799
800 v = navigate_value(response, ["read", "security_groups"], array_index)
801 r["security_groups"] = v
802
803 v = navigate_value(response, ["read", "network_id"], array_index)
804 r["subnet_id"] = v
805
806 return r
807
808
809 def flatten_allowed_address_pairs(d, array_index, current_value, exclude_output):
810 n = 0
811 result = current_value
812 has_init_value = True
813 if result:
814 n = len(result)
815 else:
816 has_init_value = False
817 result = []
818 v = navigate_value(d, ["read", "allowed_address_pairs"], array_index)
819 if not v:
820 return current_value
821 n = len(v)
822
823 new_array_index = dict()
824 if array_index:
825 new_array_index.update(array_index)
826
827 for i in range(n):
828 new_array_index["read.allowed_address_pairs"] = i
829
830 val = dict()
831 if len(result) >= (i + 1) and result[i]:
832 val = result[i]
833
834 v = navigate_value(d, ["read", "allowed_address_pairs", "ip_address"], new_array_index)
835 val["ip_address"] = v
836
837 v = navigate_value(d, ["read", "allowed_address_pairs", "mac_address"], new_array_index)
838 val["mac_address"] = v
839
840 if len(result) >= (i + 1):
841 result[i] = val
842 else:
843 for v in val.values():
844 if v is not None:
845 result.append(val)
846 break
847
848 return result if (has_init_value or result) else current_value
849
850
851 def flatten_extra_dhcp_opts(d, array_index, current_value, exclude_output):
852 n = 0
853 result = current_value
854 has_init_value = True
855 if result:
856 n = len(result)
857 else:
858 has_init_value = False
859 result = []
860 v = navigate_value(d, ["read", "extra_dhcp_opts"], array_index)
861 if not v:
862 return current_value
863 n = len(v)
864
865 new_array_index = dict()
866 if array_index:
867 new_array_index.update(array_index)
868
869 for i in range(n):
870 new_array_index["read.extra_dhcp_opts"] = i
871
872 val = dict()
873 if len(result) >= (i + 1) and result[i]:
874 val = result[i]
875
876 v = navigate_value(d, ["read", "extra_dhcp_opts", "opt_name"], new_array_index)
877 val["name"] = v
878
879 v = navigate_value(d, ["read", "extra_dhcp_opts", "opt_value"], new_array_index)
880 val["value"] = v
881
882 if len(result) >= (i + 1):
883 result[i] = val
884 else:
885 for v in val.values():
886 if v is not None:
887 result.append(val)
888 break
889
890 return result if (has_init_value or result) else current_value
891
892
893 def send_list_request(module, client, url):
894 r = None
895 try:
896 r = client.get(url)
897 except HwcClientException as ex:
898 msg = f"module(hwc_vpc_port): error running api(list), error: {ex}"
899 module.fail_json(msg=msg)
900
901 return navigate_value(r, ["ports"], None)
902
903
904 def _build_identity_object(all_opts):
905 result = dict()
906
907 v = navigate_value(all_opts, ["admin_state_up"], None)
908 result["admin_state_up"] = v
909
910 v = expand_list_allowed_address_pairs(all_opts, None)
911 result["allowed_address_pairs"] = v
912
913 result["binding_host_id"] = None
914
915 result["binding_vnic_type"] = None
916
917 result["device_id"] = None
918
919 result["device_owner"] = None
920
921 result["dns_name"] = None
922
923 v = expand_list_extra_dhcp_opts(all_opts, None)
924 result["extra_dhcp_opts"] = v
925
926 v = expand_list_fixed_ips(all_opts, None)
927 result["fixed_ips"] = v
928
929 result["id"] = None
930
931 result["mac_address"] = None
932
933 v = navigate_value(all_opts, ["name"], None)
934 result["name"] = v
935
936 v = navigate_value(all_opts, ["subnet_id"], None)
937 result["network_id"] = v
938
939 v = navigate_value(all_opts, ["security_groups"], None)
940 result["security_groups"] = v
941
942 result["status"] = None
943
944 result["tenant_id"] = None
945
946 return result
947
948
949 def expand_list_allowed_address_pairs(d, array_index):
950 new_array_index = dict()
951 if array_index:
952 new_array_index.update(array_index)
953
954 req = []
955
956 v = navigate_value(d, ["allowed_address_pairs"], new_array_index)
957
958 n = len(v) if v else 1
959 for i in range(n):
960 new_array_index["allowed_address_pairs"] = i
961 transformed = dict()
962
963 v = navigate_value(d, ["allowed_address_pairs", "ip_address"], new_array_index)
964 transformed["ip_address"] = v
965
966 v = navigate_value(d, ["allowed_address_pairs", "mac_address"], new_array_index)
967 transformed["mac_address"] = v
968
969 for v in transformed.values():
970 if v is not None:
971 req.append(transformed)
972 break
973
974 return req if req else None
975
976
977 def expand_list_extra_dhcp_opts(d, array_index):
978 new_array_index = dict()
979 if array_index:
980 new_array_index.update(array_index)
981
982 req = []
983
984 v = navigate_value(d, ["extra_dhcp_opts"], new_array_index)
985
986 n = len(v) if v else 1
987 for i in range(n):
988 new_array_index["extra_dhcp_opts"] = i
989 transformed = dict()
990
991 v = navigate_value(d, ["extra_dhcp_opts", "name"], new_array_index)
992 transformed["opt_name"] = v
993
994 v = navigate_value(d, ["extra_dhcp_opts", "value"], new_array_index)
995 transformed["opt_value"] = v
996
997 for v in transformed.values():
998 if v is not None:
999 req.append(transformed)
1000 break
1001
1002 return req if req else None
1003
1004
1005 def expand_list_fixed_ips(d, array_index):
1006 new_array_index = dict()
1007 if array_index:
1008 new_array_index.update(array_index)
1009
1010 req = []
1011
1012 transformed = dict()
1013
1014 v = navigate_value(d, ["ip_address"], new_array_index)
1015 transformed["ip_address"] = v
1016
1017 for v in transformed.values():
1018 if v is not None:
1019 req.append(transformed)
1020 break
1021
1022 return req if req else None
1023
1024
1025 def fill_list_resp_body(body):
1026 result = dict()
1027
1028 result["admin_state_up"] = body.get("admin_state_up")
1029
1030 v = fill_list_resp_allowed_address_pairs(body.get("allowed_address_pairs"))
1031 result["allowed_address_pairs"] = v
1032
1033 result["binding_host_id"] = body.get("binding_host_id")
1034
1035 result["binding_vnic_type"] = body.get("binding_vnic_type")
1036
1037 result["device_id"] = body.get("device_id")
1038
1039 result["device_owner"] = body.get("device_owner")
1040
1041 result["dns_name"] = body.get("dns_name")
1042
1043 v = fill_list_resp_extra_dhcp_opts(body.get("extra_dhcp_opts"))
1044 result["extra_dhcp_opts"] = v
1045
1046 v = fill_list_resp_fixed_ips(body.get("fixed_ips"))
1047 result["fixed_ips"] = v
1048
1049 result["id"] = body.get("id")
1050
1051 result["mac_address"] = body.get("mac_address")
1052
1053 result["name"] = body.get("name")
1054
1055 result["network_id"] = body.get("network_id")
1056
1057 result["security_groups"] = body.get("security_groups")
1058
1059 result["status"] = body.get("status")
1060
1061 result["tenant_id"] = body.get("tenant_id")
1062
1063 return result
1064
1065
1066 def fill_list_resp_allowed_address_pairs(value):
1067 if not value:
1068 return None
1069
1070 result = []
1071 for item in value:
1072 val = dict()
1073
1074 val["ip_address"] = item.get("ip_address")
1075
1076 val["mac_address"] = item.get("mac_address")
1077
1078 result.append(val)
1079
1080 return result
1081
1082
1083 def fill_list_resp_extra_dhcp_opts(value):
1084 if not value:
1085 return None
1086
1087 result = []
1088 for item in value:
1089 val = dict()
1090
1091 val["opt_name"] = item.get("opt_name")
1092
1093 val["opt_value"] = item.get("opt_value")
1094
1095 result.append(val)
1096
1097 return result
1098
1099
1100 def fill_list_resp_fixed_ips(value):
1101 if not value:
1102 return None
1103
1104 result = []
1105 for item in value:
1106 val = dict()
1107
1108 val["ip_address"] = item.get("ip_address")
1109
1110 result.append(val)
1111
1112 return result
1113
1114
1115 if __name__ == "__main__":
1116 main()