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 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 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 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 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_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 = 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 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 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 test_navigate_value(self): value = { 'foo': { 'quiet': { 'tree': 'test', "trees": [0, 1] }, } } self.assertEqual(navigate_value(value, ["foo", "quiet", "tree"]), "test") self.assertEqual( 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_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 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_identity_object(all_opts): result = dict() result["id"] = None v = navigate_value(all_opts, ["ip_address"], None) result["ip_address"] = v v = navigate_value(all_opts, ["subnet_id"], None) result["subnet_id"] = v return result
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( "Found more than one resource(%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 async_wait_update(config, result, client, timeout): module = config.module path_parameters = { "subnet_id": ["subnet", "id"], } data = dict((key, navigate_value(result, path)) for key, path in path_parameters.items()) url = build_path(module, "subnets/{subnet_id}", data) def _query_status(): r = None try: r = client.get(url, timeout=timeout) except HwcClientException: return None, "" try: s = navigate_value(r, ["subnet", "status"]) return r, s except Exception: return None, "" try: return wait_to_finish( ["ACTIVE"], ["UNKNOWN"], _query_status, timeout) except Exception as ex: module.fail_json(msg="module(hwc_vpc_subnet): error " "waiting for api(update) to " "be done, error= %s" % str(ex))
def expand_update_dns_list(d, array_index): v = navigate_value(d, ["dns_address"], array_index) if v: if len(v) > 2: return v return None return []
def _build_query_link(opts): query_params = [] v = navigate_value(opts, ["enterprise_project_id"]) if v: query_params.append("enterprise_project_id=" + str(v)) v = navigate_value(opts, ["vpc_id"]) if v: query_params.append("vpc_id=" + str(v)) query_link = "?marker={marker}&limit=10" if query_params: query_link += "&" + "&".join(query_params) return query_link
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 async_wait_create(config, result, client, timeout): module = config.module path_parameters = { "peering_id": ["peering", "id"], } data = dict((key, navigate_value(result, path)) for key, path in path_parameters.items()) url = build_path(module, "v2.0/vpc/peerings/{peering_id}", data) def _query_status(): r = None try: r = client.get(url, timeout=timeout) except HwcClientException: return None, "" try: s = navigate_value(r, ["peering", "status"]) return r, s except Exception: return None, "" try: return wait_to_finish(["ACTIVE"], ["PENDING_ACCEPTANCE"], _query_status, timeout) except Exception as ex: module.fail_json(msg="module(hwc_vpc_peering_connect): error " "waiting for api(create) to " "be done, error= %s" % str(ex))
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
def _build_query_link(opts): query_params = [] v = navigate_value(opts, ["local_vpc_id"]) if v: query_params.append("vpc_id=" + str(v)) v = navigate_value(opts, ["name"]) if v: query_params.append("name=" + str(v)) query_link = "?marker={marker}&limit=10" if query_params: query_link += "&" + "&".join(query_params) return query_link
def expand_create_publicip(d, array_index): r = dict() v = navigate_value(d, ["ipv4_address"], array_index) if not is_empty_value(v): r["ip_address"] = v v = navigate_value(d, ["ip_version"], array_index) if not is_empty_value(v): r["ip_version"] = v v = navigate_value(d, ["type"], array_index) if not is_empty_value(v): r["type"] = v return r
def create(config): module = config.module client = config.client(get_region(module), "vpc", "project") opts = user_input_parameters(module) params = build_create_parameters(opts) r = send_create_request(module, params, client) module.params['id'] = navigate_value(r, ["security_group_rule", "id"])