def expand_list_allowed_address_pairs(d, array_index): new_array_index = dict() if array_index: new_array_index.update(array_index) req = [] v = navigate_value(d, ["allowed_address_pairs"], new_array_index) n = len(v) if v else 1 for i in range(n): new_array_index["allowed_address_pairs"] = i transformed = dict() v = navigate_value(d, ["allowed_address_pairs", "ip_address"], new_array_index) transformed["ip_address"] = v v = navigate_value(d, ["allowed_address_pairs", "mac_address"], new_array_index) transformed["mac_address"] = v for v in transformed.values(): if v is not None: req.append(transformed) break return req if req else None
def expand_list_extra_dhcp_opts(d, array_index): new_array_index = dict() if array_index: new_array_index.update(array_index) req = [] v = navigate_value(d, ["extra_dhcp_opts"], new_array_index) n = len(v) if v else 1 for i in range(n): new_array_index["extra_dhcp_opts"] = i transformed = dict() v = navigate_value(d, ["extra_dhcp_opts", "name"], new_array_index) transformed["opt_name"] = v v = navigate_value(d, ["extra_dhcp_opts", "value"], new_array_index) transformed["opt_value"] = v for v in transformed.values(): if v is not None: req.append(transformed) break return req if req else None
def build_update_parameters(opts): params = dict() v = navigate_value(opts, ["dhcp_enable"], None) if v is not None: params["dhcp_enable"] = v v = expand_update_dns_list(opts, None) if v is not None: params["dnsList"] = v v = navigate_value(opts, ["name"], None) if not is_empty_value(v): params["name"] = v v = expand_update_primary_dns(opts, None) if v is not None: params["primary_dns"] = v v = expand_update_secondary_dns(opts, None) if v is not None: params["secondary_dns"] = v if not params: return params params = {"subnet": params} return params
def expand_scheduled_policy_opts(d): opts = d.get("scheduled_policy") if not opts: return None params = dict() v = navigate_value(opts, ["launch_time"]) if not is_empty_value(v): params["launch_time"] = v v = navigate_value(opts, ["recurrence_type"]) if not is_empty_value(v): params["recurrence_type"] = v v = navigate_value(opts, ["recurrence_value"]) if not is_empty_value(v): params["recurrence_value"] = v v = navigate_value(opts, ["start_time"]) if not is_empty_value(v): params["start_time"] = v v = navigate_value(opts, ["end_time"]) if not is_empty_value(v): params["end_time"] = v return params
def _build_identity_object(module, all_opts): filters = module.params.get("filters") opts = dict() for k, v in all_opts.items(): opts[k] = v if k in filters else None result = dict() v = navigate_value(opts, ["destination"], None) result["destination"] = v result["id"] = None v = navigate_value(opts, ["next_hop"], None) result["nexthop"] = v result["tenant_id"] = None v = navigate_value(opts, ["type"], None) result["type"] = v v = navigate_value(opts, ["vpc_id"], None) result["vpc_id"] = v return result
def build_update_parameters(opts): params = dict() v = expand_update_allowed_address_pairs(opts, None) if v is not None: params["allowed_address_pairs"] = v v = expand_update_extra_dhcp_opts(opts, None) if v is not None: params["extra_dhcp_opts"] = v v = navigate_value(opts, ["name"], None) if not is_empty_value(v): params["name"] = v v = navigate_value(opts, ["security_groups"], None) if not is_empty_value(v): params["security_groups"] = v if not params: return params params = {"port": params} return params
def expand_update_allowed_address_pairs(d, array_index): new_array_index = dict() if array_index: new_array_index.update(array_index) req = [] v = navigate_value(d, ["allowed_address_pairs"], new_array_index) if not v: return req n = len(v) for i in range(n): new_array_index["allowed_address_pairs"] = i transformed = dict() v = navigate_value(d, ["allowed_address_pairs", "ip_address"], new_array_index) if not is_empty_value(v): transformed["ip_address"] = v v = navigate_value(d, ["allowed_address_pairs", "mac_address"], new_array_index) if not is_empty_value(v): transformed["mac_address"] = v if transformed: req.append(transformed) return req
def _build_identity_object(module, all_opts): filters = module.params.get("filters") opts = dict() for k, v in all_opts.items(): opts[k] = v if k in filters else None result = dict() v = expand_list_accept_vpc_info(opts, None) result["accept_vpc_info"] = v v = navigate_value(opts, ["description"], None) result["description"] = v result["id"] = None v = navigate_value(opts, ["name"], None) result["name"] = v v = expand_list_request_vpc_info(opts, None) result["request_vpc_info"] = v result["status"] = None return result
def build_create_parameters(opts): params = dict() v = navigate_value(opts, ["destination"], None) if not is_empty_value(v): params["destination"] = v v = navigate_value(opts, ["next_hop"], None) if not is_empty_value(v): params["nexthop"] = v v = navigate_value(opts, ["type"], None) if not is_empty_value(v): params["type"] = v v = navigate_value(opts, ["vpc_id"], None) if not is_empty_value(v): params["vpc_id"] = v if not params: return params params = {"route": params} return params
def build_create_parameters(opts): params = dict() v = expand_create_accept_vpc_info(opts, None) if not is_empty_value(v): params["accept_vpc_info"] = v v = navigate_value(opts, ["description"], None) if not is_empty_value(v): params["description"] = v v = navigate_value(opts, ["name"], None) if not is_empty_value(v): params["name"] = v v = expand_create_request_vpc_info(opts, None) if not is_empty_value(v): params["request_vpc_info"] = v if not params: return params params = {"peering": params} return params
def flatten_attachments(d, array_index): v = navigate_value(d, ["read", "attachments"], array_index) if not v: return None n = len(v) result = [] new_ai = dict() if array_index: new_ai.update(array_index) for i in range(n): new_ai["read.attachments"] = i val = dict() v = navigate_value(d, ["read", "attachments", "attached_at"], new_ai) val["attached_at"] = v v = navigate_value(d, ["read", "attachments", "attachment_id"], new_ai) val["attachment_id"] = v v = navigate_value(d, ["read", "attachments", "device"], new_ai) val["device"] = v v = navigate_value(d, ["read", "attachments", "server_id"], new_ai) val["server_id"] = v for v in val.values(): if v is not None: result.append(val) break return result if result else None
def expand_create_disks_param(d, array_index): disks = [] new_ai = dict() if array_index: new_ai.update(array_index) v = navigate_value(d, ["disks"], new_ai) if not v: return disks n = len(v) for i in range(n): new_ai["disks"] = i transformed = dict() v = navigate_value(d, ["disks", "disk_type"], new_ai) if not is_empty_value(v): transformed["disk_type"] = v v = navigate_value(d, ["disks", "volume_type"], new_ai) if not is_empty_value(v): transformed["volume_type"] = v v = navigate_value(d, ["disks", "size"], new_ai) if not is_empty_value(v): transformed["size"] = v if transformed: disks.append(transformed) return disks
def test_navigate_value(self): value = { 'foo': { 'quiet': { 'tree': 'test', "trees": [0, 1] }, } } self.assertEquals(navigate_value(value, ["foo", "quiet", "tree"]), "test") self.assertEquals( navigate_value(value, ["foo", "quiet", "trees"], {"foo.quiet.trees": 1}), 1) self.assertRaisesRegexp(HwcModuleException, r".* key\(q\) is not exist in dict", navigate_value, value, ["foo", "q", "tree"]) self.assertRaisesRegexp(HwcModuleException, r".* the index is out of list", navigate_value, value, ["foo", "quiet", "trees"], {"foo.quiet.trees": 2})
def expand_update_extra_dhcp_opts(d, array_index): new_array_index = dict() if array_index: new_array_index.update(array_index) req = [] v = navigate_value(d, ["extra_dhcp_opts"], new_array_index) if not v: return req n = len(v) for i in range(n): new_array_index["extra_dhcp_opts"] = i transformed = dict() v = navigate_value(d, ["extra_dhcp_opts", "name"], new_array_index) if not is_empty_value(v): transformed["opt_name"] = v v = navigate_value(d, ["extra_dhcp_opts", "value"], new_array_index) if not is_empty_value(v): transformed["opt_value"] = v if transformed: req.append(transformed) return req
def expand_list_bandwidth_id(d, array_index): v = navigate_value(d, ["dedicated_bandwidth"], array_index) sbwid = navigate_value(d, ["shared_bandwidth_id"], array_index) if v and sbwid: raise Exception("don't input shared_bandwidth_id and " "dedicated_bandwidth at same time") return sbwid
def update_properties(module, response, array_index, exclude_output=False): r = user_input_parameters(module) v = navigate_value(response, ["read", "ip_address"], array_index) r["ip_address"] = v v = navigate_value(response, ["read", "subnet_id"], array_index) r["subnet_id"] = v return r
def _build_identity_object(all_opts): result = dict() v = navigate_value(all_opts, ["description"], None) result["description"] = v v = navigate_value(all_opts, ["direction"], None) result["direction"] = v v = navigate_value(all_opts, ["ethertype"], None) result["ethertype"] = v result["id"] = None v = navigate_value(all_opts, ["port_range_max"], None) result["port_range_max"] = v v = navigate_value(all_opts, ["port_range_min"], None) result["port_range_min"] = v v = navigate_value(all_opts, ["protocol"], None) result["protocol"] = v result["remote_address_group_id"] = None v = navigate_value(all_opts, ["remote_group_id"], None) result["remote_group_id"] = v v = navigate_value(all_opts, ["remote_ip_prefix"], None) result["remote_ip_prefix"] = v v = navigate_value(all_opts, ["security_group_id"], None) result["security_group_id"] = v return result
def update_properties(module, response, array_index, exclude_output=False): r = user_input_parameters(module) v = navigate_value(response, ["read", "description"], array_index) r["description"] = v v = navigate_value(response, ["read", "direction"], array_index) r["direction"] = v v = navigate_value(response, ["read", "ethertype"], array_index) r["ethertype"] = v v = navigate_value(response, ["read", "port_range_max"], array_index) r["port_range_max"] = v v = navigate_value(response, ["read", "port_range_min"], array_index) r["port_range_min"] = v v = navigate_value(response, ["read", "protocol"], array_index) r["protocol"] = v v = navigate_value(response, ["read", "remote_group_id"], array_index) r["remote_group_id"] = v v = navigate_value(response, ["read", "remote_ip_prefix"], array_index) r["remote_ip_prefix"] = v v = navigate_value(response, ["read", "security_group_id"], array_index) r["security_group_id"] = v return r
def main(): """Main function""" module = build_module() config = Config(module, "as", verify=False) try: resource = dict() if module.params.get('id'): # read as group resource by id resource = read_resource(config) else: # search as group resource by name v = search_resource(config) n = len(v) if n > 1: raise Exception("Found more than one resource(%s)" % ", ".join([ navigate_value(i, ["scaling_group_id"]) for i in v ])) if n == 1: module.params['id'] = navigate_value(v[0], ["scaling_group_id"]) resource = v[0] changed = False result = dict() if module.params['state'] == 'present': if not resource: if not module.check_mode: result['action'] = "create" create(config) changed = True else: obj = build_identity_object(module) if are_different_dicts(obj, resource): if not module.check_mode: result['action'] = "update" update(config) changed = True else: if resource: if not module.check_mode: result['action'] = "delete" delete(config) changed = True except Exception as ex: module.fail_json(msg=str(ex)) else: result['changed'] = changed result['id'] = module.params['id'] module.exit_json(**result)
def expand_create_accept_vpc_info(d, array_index): r = dict() v = navigate_value(d, ["peering_vpc", "project_id"], array_index) if not is_empty_value(v): r["tenant_id"] = v v = navigate_value(d, ["peering_vpc", "vpc_id"], array_index) if not is_empty_value(v): r["vpc_id"] = v return r
def build_create_parameters(opts): params = dict() v = navigate_value(opts, ["availability_zone"], None) if not is_empty_value(v): params["availability_zone"] = v v = navigate_value(opts, ["backup_id"], None) if not is_empty_value(v): params["backup_id"] = v v = navigate_value(opts, ["description"], None) if not is_empty_value(v): params["description"] = v v = navigate_value(opts, ["enterprise_project_id"], None) if not is_empty_value(v): params["enterprise_project_id"] = v v = navigate_value(opts, ["image_id"], None) if not is_empty_value(v): params["imageRef"] = v v = expand_create_metadata(opts, None) if not is_empty_value(v): params["metadata"] = v v = navigate_value(opts, ["enable_share"], None) if not is_empty_value(v): params["multiattach"] = v v = navigate_value(opts, ["name"], None) if not is_empty_value(v): params["name"] = v v = navigate_value(opts, ["size"], None) if not is_empty_value(v): params["size"] = v v = navigate_value(opts, ["snapshot_id"], None) if not is_empty_value(v): params["snapshot_id"] = v v = navigate_value(opts, ["volume_type"], None) if not is_empty_value(v): params["volume_type"] = v if not params: return params params = {"volume": params} return params
def _init(config): module = config.module if module.params.get('id'): return v = search_resource(config) n = len(v) if n > 1: raise Exception("find more than one resources(%s)" % ", ".join([navigate_value(i, ["id"]) for i in v])) if n == 1: module.params['id'] = navigate_value(v[0], ["id"])
def expand_list_accept_vpc_info(d, array_index): r = dict() v = navigate_value(d, ["peering_vpc", "project_id"], array_index) r["tenant_id"] = v v = navigate_value(d, ["peering_vpc", "vpc_id"], array_index) r["vpc_id"] = v for v in r.values(): if v is not None: return r return None
def main(): """Main function""" module = build_module() config = Config(module, "vpc") try: resource = None if module.params['id']: resource = True else: v = search_resource(config) if len(v) > 1: raise Exception( "find more than one resources(%s)" % ", ".join([navigate_value(i, ["id"]) for i in v])) if len(v) == 1: resource = v[0] module.params['id'] = navigate_value(resource, ["id"]) result = {} changed = False if module.params['state'] == 'present': if resource is None: if not module.check_mode: create(config) changed = True current = read_resource(config, exclude_output=True) expect = user_input_parameters(module) if are_different_dicts(expect, current): if not module.check_mode: update(config) changed = True result = read_resource(config) result['id'] = module.params.get('id') else: if resource: if not module.check_mode: delete(config) changed = True except Exception as ex: module.fail_json(msg=str(ex)) else: result['changed'] = changed module.exit_json(**result)
def main(): """Main function""" module = build_module() config = Config(module, "vpc") try: resource = None if module.params.get("id"): resource = get_resource_by_id(config) if module.params['state'] == 'present': opts = user_input_parameters(module) if are_different_dicts(resource, opts): raise Exception( "Cannot change option from (%s) to (%s) for an" " existing route.(%s)" % (resource, opts, config.module.params.get( 'id'))) else: v = search_resource(config) if len(v) > 1: raise Exception("Found more than one resource(%s)" % ", ".join([ navigate_value(i, ["id"]) for i in v])) if len(v) == 1: resource = update_properties(module, {"read": v[0]}, None) module.params['id'] = navigate_value(resource, ["id"]) result = {} changed = False if module.params['state'] == 'present': if resource is None: if not module.check_mode: resource = create(config) changed = True result = resource else: if resource: if not module.check_mode: delete(config) changed = True except Exception as ex: module.fail_json(msg=str(ex)) else: result['changed'] = changed module.exit_json(**result)
def _build_identity_object(module, all_opts): filters = module.params.get("filters") opts = dict() for k, v in all_opts.items(): opts[k] = v if k in filters else None result = dict() v = navigate_value(opts, ["admin_state_up"], None) result["admin_state_up"] = v v = expand_list_allowed_address_pairs(opts, None) result["allowed_address_pairs"] = v result["binding_host_id"] = None result["binding_vnic_type"] = None result["device_id"] = None result["device_owner"] = None result["dns_name"] = None v = expand_list_extra_dhcp_opts(opts, None) result["extra_dhcp_opts"] = v v = expand_list_fixed_ips(opts, None) result["fixed_ips"] = v result["id"] = None result["mac_address"] = None v = navigate_value(opts, ["name"], None) result["name"] = v v = navigate_value(opts, ["subnet_id"], None) result["network_id"] = v v = navigate_value(opts, ["security_groups"], None) result["security_groups"] = v result["status"] = None result["tenant_id"] = None return result
def update(config): module = config.module client = config.client(get_region(module), "autoscaling", "project") params = build_update_parameters(module.params) r = send_update_request(module, params, client) module.params['id'] = navigate_value(r, ["scaling_group_id"])
def async_wait(config, result, client, timeout): module = config.module path_parameters = { "job_id": ["job_id"], } data = dict((key, navigate_value(result, path)) for key, path in path_parameters.items()) url = build_path(module, "jobs/{job_id}", data) def _query_status(): r = None try: r = client.get(url, timeout=timeout) except HwcClientException: return None, "" try: s = navigate_value(r, ["status"]) return r, s except Exception: return None, "" try: return wait_to_finish(["SUCCESS"], ["RUNNING", "INIT"], _query_status, timeout) except Exception as ex: module.fail_json(msg="module(hwc_evs_disk): error " "waiting to be done, error= %s" % str(ex))
def update_properties(module, response, array_index, exclude_output=False): r = user_input_parameters(module) v = navigate_value(response, ["read", "destination"], array_index) r["destination"] = v v = navigate_value(response, ["read", "nexthop"], array_index) r["next_hop"] = v v = navigate_value(response, ["read", "type"], array_index) r["type"] = v v = navigate_value(response, ["read", "vpc_id"], array_index) r["vpc_id"] = v return r
def _build_query_link(opts): query_link = "?marker={marker}&limit=10" v = navigate_value(opts, ["security_group_id"]) if v: query_link += "&security_group_id=" + str(v) return query_link