def create_path_routes(path_routes_list): if path_routes_list is None: raise ClientError( 'path_routes is mandatory attribute for path_route_set and can not be empty.' ) HashedPathMatchType = oci_utils.generate_subclass(PathMatchType) path_match_type = dict( EXACT_MATCH=HashedPathMatchType( match_type=PathMatchType.MATCH_TYPE_EXACT_MATCH), FORCE_LONGEST_PREFIX_MATCH=HashedPathMatchType( match_type=PathMatchType.MATCH_TYPE_FORCE_LONGEST_PREFIX_MATCH), PREFIX_MATCH=HashedPathMatchType( match_type=PathMatchType.MATCH_TYPE_PREFIX_MATCH), SUFFIX_MATCH=HashedPathMatchType( match_type=PathMatchType.MATCH_TYPE_SUFFIX_MATCH)) result_path_routes = list() HashedPathRoute = oci_utils.generate_subclass(PathRoute) for path_route_entry in path_routes_list: path_route = HashedPathRoute() backend_set_name = path_route_entry.get('backend_set_name', None) path = path_route_entry.get('path', None) path_match_type = path_match_type.get( path_route_entry.get('path_match_type', None).get('match_type', None)) if backend_set_name is None or path is None or path_match_type is None: raise ClientError( Exception( "backend_set_name, path and path_match_type are mandatory attributes for" " back_ends and can not be empty.")) path_route.backend_set_name = backend_set_name path_route.path = path path_route.path_match_type = path_match_type result_path_routes.append(path_route) return result_path_routes
def create_listeners(listener_details_dict): if listener_details_dict is None: return None result_listeners = dict() attributes = [ "default_backend_set_name", "port", "protocol", "hostname_names", "path_route_set_name", ] optional_attributes = ["hostname_names", "path_route_set_name"] for key, value in six.iteritems(listener_details_dict): listener_details = ListenerDetails() for attribute in attributes: if value.get(attribute) is None and attribute not in optional_attributes: raise ClientError( Exception( attribute + " is mandatory and must not be empty for listener" ) ) listener_details.__setattr__(attribute, value.get(attribute)) listener_details.ssl_configuration = create_ssl_configuration( value.get("ssl_configuration", None) ) listener_details.connection_configuration = create_connection_configuration( value.get("connection_configuration", None) ) result_listeners.update({key: listener_details}) return result_listeners
def update_db_home(db_client, module, db_home_id): result = dict() db_home = oci_utils.get_existing_resource( db_client.get_db_home, module, db_home_id=db_home_id) if db_home is None: raise ClientError(Exception("No DB Home with id " + db_home_id + " is found for update")) last_patch_history_entry_id = db_home.last_patch_history_entry_id input_version_dict = module.params.get('patch_details', None) update_db_home_details = UpdateDbHomeDetails() version_changed, patch_details = oci_db_utils.is_version_changed( db_client.get_db_home_patch_history_entry, db_client.get_db_home_patch, db_home.db_version, input_version_dict, last_patch_history_entry_id, db_home_id=db_home_id) if version_changed: update_db_home_details.db_version = patch_details result = oci_utils.update_and_wait(resource_type='db_home', update_fn=db_client.update_db_home, kwargs_update={ 'db_home_id': db_home_id, 'update_db_home_details': update_db_home_details}, client=db_client, get_fn=db_client.get_db_home, get_param='db_home_id', module=module) else: result['db_home'] = to_dict(db_home) result['changed'] = False return result
def create_health_checker(health_checker_details): if health_checker_details is None: return None HashedHealthCheckerDetails = oci_utils.generate_subclass(HealthCheckerDetails) health_checker = HashedHealthCheckerDetails() attribute_to_default_value_dict = dict( { "interval_in_millis": 10000, "port": 0, "response_body_regex": ".*", "retries": 3, "return_code": 200, "timeout_in_millis": 3000, } ) protocol = health_checker_details.get("protocol", None) url_path = health_checker_details.get("url_path", None) if protocol is None or url_path is None: raise ClientError( Exception( "protocol and url_path are mandatory attributes for health_checker and can not be empty." ) ) health_checker.protocol = protocol health_checker.url_path = url_path for key, value in six.iteritems(attribute_to_default_value_dict): health_checker.__setattr__(key, health_checker_details.get(key, value)) return health_checker
def test_create_user_with_group_failure_no_group_exists( identity_client, get_ids_from_group_names, add_user_to_groups, create_and_wait_patch, delete_user_patch, ): error_message = "No group named error_group exists" user = get_user() create_and_wait_patch.return_value = dict({ "user": to_dict(user), "changed": True }) delete_user_patch.return_value = None get_ids_from_group_names.side_effect = ClientError(error_message) add_user_to_groups.return_value = None try: oci_user.create_user( identity_client, get_module( dict({ "create_or_reset_ui_password": False, "user_groups": ["admin", "network"], "compartment_id": "test_compartment", })), ) except Exception as ex: assert error_message in ex.args[0]
def create_backends(backends_list): if backends_list is None: return None result_backends = list() attribute_to_default_value_dict = dict( {"backup": False, "drain": False, "offline": False, "weight": 1} ) HashedBackendDetails = oci_utils.generate_subclass(BackendDetails) for backend_entry in backends_list: backend = HashedBackendDetails() ip_address = backend_entry.get("ip_address", None) port = backend_entry.get("port", None) if ip_address is None or port is None: raise ClientError( Exception( "ip_address and port are mandatory attributes for back_ends and can not " "be empty." ) ) backend.ip_address = ip_address backend.port = port for key, value in six.iteritems(attribute_to_default_value_dict): backend.__setattr__(key, backend_entry.get(key, value)) result_backends.append(backend) return result_backends
def get_options_objects(options): dhcp_options = [] for option in options: dhcp_option = None if option["type"] == "DomainNameServer": dhcp_option = oci_utils.create_hashed_instance(DhcpDnsOption) dhcp_option.type = "DomainNameServer" server_type = option["server_type"] dhcp_option.server_type = server_type if server_type == "CustomDnsServer": dhcp_option.custom_dns_servers = option.get( "custom_dns_servers", None) else: dhcp_option.custom_dns_servers = [] elif option["type"] == "SearchDomain": dhcp_option = oci_utils.create_hashed_instance( DhcpSearchDomainOption) dhcp_option.type = "SearchDomain" search_domain_names = option["search_domain_names"] if search_domain_names: dhcp_option.search_domain_names = option["search_domain_names"] else: raise ClientError( "search_domain_names field should not be empty") dhcp_options.append(dhcp_option) return dhcp_options
def update_group(identity_client, existing_group, module): if existing_group is None: raise ClientError( Exception("No Group with id " + module.params.get("group_id") + " is found for update")) users = module.params["users"] compartment_id = module.params.get("compartment_id") existing_group, description_tags_changed = update_group_description_and_tags( identity_client, existing_group, module) user_changed = False if users is not None: existing_group_members = get_existing_group_members( identity_client, existing_group.compartment_id, existing_group.id) if users: user_changed = update_group_users( identity_client, compartment_id, users, existing_group_members, existing_group, module, ) else: user_changed = delete_all_users_from_group(identity_client, compartment_id, existing_group) group_dict = to_dict(existing_group) return (description_tags_changed or user_changed), group_dict
def get_options_objects(options): dhcp_options = [] for option in options: dhcp_option = None if option['type'] == 'DomainNameServer': dhcp_option = oci_utils.create_hashed_instance(DhcpDnsOption) dhcp_option.type = 'DomainNameServer' server_type = option['server_type'] dhcp_option.server_type = server_type if server_type == 'CustomDnsServer': dhcp_option.custom_dns_servers = option.get( 'custom_dns_servers', None) else: dhcp_option.custom_dns_servers = [] elif option['type'] == 'SearchDomain': dhcp_option = oci_utils.create_hashed_instance( DhcpSearchDomainOption) dhcp_option.type = 'SearchDomain' search_domain_names = option['search_domain_names'] if search_domain_names: dhcp_option.search_domain_names = option['search_domain_names'] else: raise ClientError( 'serarch_domain_names field should not be empty') dhcp_options.append(dhcp_option) return dhcp_options
def update_load_balancer(lb_client, module, load_balancer): if load_balancer is None: raise ClientError( Exception("No Load Balancer with id " + module.params.get('load_balancer_id') + " is found for update")) result = dict(load_balancer=to_dict(load_balancer), changed=False) name = module.params['display_name'] update_load_balancer_details = UpdateLoadBalancerDetails() if load_balancer.display_name.strip() != name.strip(): get_logger().info( "Updating the display name of load balancer from %s to %s", load_balancer.display_name, name) update_load_balancer_details.display_name = name result = oci_lb_utils.create_or_update_lb_resources_and_wait( resource_type="load_balancer", function=lb_client.update_load_balancer, kwargs_function={ 'update_load_balancer_details': update_load_balancer_details, 'load_balancer_id': load_balancer.id }, lb_client=lb_client, get_fn=lb_client.get_load_balancer, kwargs_get={'load_balancer_id': load_balancer.id}, module=module) get_logger().info( "Successfully updated the display name of load balancer from %s to %s", load_balancer.display_name, name) if not result['changed']: get_logger().info( "Unable to update display name of load balancer as the new name is same as old" ) return result
def update_user(identity_client, existing_user, module): if existing_user is None: raise ClientError( Exception("No User with id " + module.params.get('user_id') + " is found for update")) groups = module.params['user_groups'] reset_password = module.params['create_or_reset_ui_password'] blocked = module.params['blocked'] ui_password = None group_changed = modify_group_memberships(identity_client, groups, existing_user, module) existing_user, state_changed = unblock_user(identity_client, blocked, existing_user) ui_password, password_changed = reset_ui_password(identity_client, existing_user, reset_password) existing_user, description_tags_changed = update_user_description_and_tags( identity_client, existing_user, module) if not description_tags_changed: existing_user = to_dict(existing_user) if password_changed: existing_user.update({'password': ui_password}) user_changed = description_tags_changed or group_changed \ or password_changed or state_changed return user_changed, existing_user
def create_backend_sets(backend_sets_dicts): if backend_sets_dicts is None: return None result_backend_sets = dict() for key, value in six.iteritems(backend_sets_dicts): backend_sets_details = BackendSetDetails() health_checker = value.get("health_checker", None) policy = value.get("policy", None) if health_checker is None or policy is None: raise ClientError( Exception( "health_checker and policy are mandatory attributes for back_end_sets and can not be empty." ) ) backend_sets_details.policy = value["policy"] backend_sets_details.health_checker = create_health_checker(health_checker) backend_sets_details.backends = create_backends(value.get("backends", None)) backend_sets_details.session_persistence_configuration = create_session_persistence_configuration( value.get("session_persistence_configuration", None) ) backend_sets_details.ssl_configuration = create_ssl_configuration( value.get("ssl_configuration", None) ) result_backend_sets.update({key: backend_sets_details}) return result_backend_sets
def test_generate_wallet_client_error(db_client, call_with_backoff_patch): error_message = "Wallet file not valid" module = get_module(dict(password="******", wallet_file="test_wallet_file")) call_with_backoff_patch.side_effect = ClientError(Exception(error_message)) try: oci_autonomous_data_warehouse.generate_wallet(db_client, module) except Exception as ex: assert error_message in ex.args[0]
def update_route_table(virtual_network_client, existing_route_table, module): if existing_route_table is None: raise ClientError( Exception( "No Route Table with id " + module.params.get("rt_id") + " is found for update" ) ) result = dict(route_table=to_dict(existing_route_table), changed=False) input_route_rules = module.params.get("route_rules") purge_route_rules = module.params["purge_route_rules"] delete_route_rules = module.params["delete_route_rules"] name_tag_changed = False route_rules_changed = False existing_route_rules = existing_route_table.route_rules update_route_table_details = UpdateRouteTableDetails() attributes_to_compare = ["display_name", "freeform_tags", "defined_tags"] for attribute in attributes_to_compare: name_tag_changed = oci_utils.check_and_update_attributes( update_route_table_details, attribute, module.params.get(attribute), getattr(existing_route_table, attribute), name_tag_changed, ) if input_route_rules is not None: if input_route_rules: route_rules_object_list = get_route_rules(input_route_rules) route_rules, route_rules_changed = oci_utils.get_component_list_difference( get_hashed_route_rules(route_rules_object_list), get_hashed_route_rules(existing_route_rules), purge_route_rules, delete_route_rules, ) else: route_rules = [] route_rules_changed = True if route_rules_changed: update_route_table_details.route_rules = route_rules else: update_route_table_details.route_rules = existing_route_rules if name_tag_changed or route_rules_changed: result = oci_utils.update_and_wait( resource_type="route_table", update_fn=virtual_network_client.update_route_table, kwargs_update={ "rt_id": existing_route_table.id, "update_route_table_details": update_route_table_details, }, client=virtual_network_client, get_fn=virtual_network_client.get_route_table, get_param="rt_id", module=module, ) return result
def test_create_or_update_exporte_client_error(file_storage_client, check_and_create_resource_patch): error_message = 'export attribute has no value' module = get_module() check_and_create_resource_patch.side_effect = ClientError( Exception(error_message)) try: oci_export.create_or_update_export(file_storage_client, module) except Exception as ex: assert error_message in ex.args[0]
def test_create_or_update_backend_client_error(lb_client, check_and_create_resource_patch, get_existing_backend_patch): module = get_module() error_message = 'Work Request Failed' check_and_create_resource_patch.side_effect = ClientError(Exception('Work Request Failed')) get_existing_backend_patch.return_value = None try: oci_load_balancer_backend.create_or_update_backend(lb_client, module) except Exception as ex: assert error_message in ex.args[0]
def test_create_or_update_smtp_credentiale_client_error(identity_client, check_and_create_resource_patch): error_message = 'mount target attribute has no value' module = get_module() check_and_create_resource_patch.side_effect = ClientError( Exception(error_message)) try: oci_smtp_credential.create_or_update_smtp_credential(identity_client, module) except Exception as ex: assert error_message in ex.args[0]
def test_create_certificate_client_error(lb_client, get_certificate_patch): error_message = 'Work Request Failed' module = get_module(dict()) get_certificate_patch.return_value = None create_or_update_lb_resources_and_wait_patch.side_effect = ClientError( Exception('Work Request Failed')) try: oci_load_balancer_certificate.create_certificate(lb_client, module) except Exception as ex: assert error_message in ex.args[0]
def test_update_export_sete_client_error(file_storage_client, check_and_update_resource_patch): error_message = "export set attribute has no value" module = get_module(dict({"export_set_id": "ocid1.exportset.aaa"})) check_and_update_resource_patch.side_effect = ClientError( Exception(error_message)) try: oci_export_set.update_export_set(file_storage_client, module) except Exception as ex: assert error_message in ex.args[0]
def test_launch_or_update_db_system_client_error( db_client, check_and_create_resource_patch): error_message = 'databse attribute has no value' module = get_module(dict()) check_and_create_resource_patch.side_effect = ClientError( Exception(error_message)) try: oci_db_system.launch_or_update_db_system(db_client, module) except Exception as ex: assert error_message in ex.args[0]
def start_or_stop_autonomous_data_warehouse(db_client, module): autonomous_data_warehouse_id = module.params.get('autonomous_data_warehouse_id') autonomous_data_warehouse = oci_utils.get_existing_resource( db_client.get_autonomous_data_warehouse, module, autonomous_data_warehouse_id=autonomous_data_warehouse_id) if autonomous_data_warehouse is None: raise ClientError(Exception("No Autonomous Data Warehouse with id " + autonomous_data_warehouse_id + " is found for update")) return perform_start_or_stop(db_client, autonomous_data_warehouse, autonomous_data_warehouse_id, module)
def test_create_or_update_db_node_client_error(db_client, get_existing_resource_patch): error_message = "Db Node id is mandatory" module = get_module(dict()) get_existing_resource_patch.side_effect = ClientError( Exception(error_message)) try: oci_db_node.perform_db_node_action(db_client, module) except Exception as ex: assert error_message in ex.args[0]
def test_launch_or_update_autonomous_data_warehousee_client_error( db_client, check_and_create_resource_patch): error_message = 'databse attribute has no value' module = get_module() check_and_create_resource_patch.side_effect = ClientError( Exception(error_message)) try: oci_autonomous_data_warehouse.create_or_update_autonomous_data_warehouse( db_client, module) except Exception as ex: assert error_message in ex.args[0]
def update_load_balancer(lb_client, module, load_balancer): if load_balancer is None: raise ClientError( Exception("No Load Balancer with id " + module.params.get("load_balancer_id") + " is found for update")) result = dict(load_balancer=to_dict(load_balancer), changed=False) name = module.params["display_name"] defined_tags = module.params["defined_tags"] freeform_tags = module.params["freeform_tags"] update_load_balancer_details = UpdateLoadBalancerDetails() changed = False if name is not None and load_balancer.display_name.strip() != name.strip(): get_logger().info( "Updating the display name of load balancer from %s to %s", load_balancer.display_name, name, ) changed = True if freeform_tags is not None and load_balancer.freeform_tags != freeform_tags: get_logger().info( "Updating the freeform_tags of load balancer from {0} to {1}". format(load_balancer.freeform_tags, freeform_tags)) changed = True if defined_tags is not None and load_balancer.defined_tags != defined_tags: get_logger().info( "Updating the defined_tags of load balancer from {0} to {1}". format(load_balancer.defined_tags, defined_tags)) changed = True if changed is True: update_load_balancer_details.display_name = name update_load_balancer_details.freeform_tags = freeform_tags update_load_balancer_details.defined_tags = defined_tags result = oci_lb_utils.create_or_update_lb_resources_and_wait( resource_type="load_balancer", function=lb_client.update_load_balancer, kwargs_function={ "update_load_balancer_details": update_load_balancer_details, "load_balancer_id": load_balancer.id, }, lb_client=lb_client, get_fn=lb_client.get_load_balancer, kwargs_get={"load_balancer_id": load_balancer.id}, module=module, ) get_logger().info( "Successfully updated the parameters of load balancer") if not result["changed"]: get_logger().info( "Unable to update load balancer as the new parameter is same as old one" ) return result
def create_db_home_from_backup_details(db_home_dict): if db_home_dict is None: raise ClientError( Exception( "Proper value for attribute db_home is mandatory for creating DB System" )) create_db_home_for_backup_details = CreateDbHomeFromBackupDetails() create_db_home_for_backup_details.database = oci_db_utils.create_database_from_backup_details( db_home_dict.get("database", None)) create_db_home_for_backup_details.display_name = db_home_dict.get( "display_name") return create_db_home_for_backup_details
def create_database_from_backup_details(database_dict): if database_dict is None: raise ClientError( Exception( "Proper value for attribute database is mandatory for creating this component" )) create_database_from_backup_details = CreateDatabaseFromBackupDetails() for attribute in create_database_from_backup_details.attribute_map.keys(): create_database_from_backup_details.__setattr__( attribute, database_dict.get(attribute, None)) return create_database_from_backup_details
def verify_work_request(lb_client, response): work_request_id = None if response is not None: work_request_id = response.headers.get('opc-work-request-id') oci.wait_until(lb_client, lb_client.get_work_request(work_request_id), evaluate_response=lambda r: r.data.lifecycle_state in ['SUCCEEDED', 'FAILED']) response = lb_client.get_work_request(work_request_id) if response.data.lifecycle_state == 'FAILED': raise ClientError(Exception(response.data.error_details)) return response
def create_db_home(db_home_dict): if db_home_dict is None: raise ClientError( Exception( 'Proper value for attribute db_home is mandatory for creating DB System' )) create_db_home_details = CreateDbHomeDetails() create_db_home_details.database = oci_db_utils.create_database_details( db_home_dict.get('database', None)) create_db_home_details.db_version = db_home_dict.get('db_version') create_db_home_details.display_name = db_home_dict.get('display_name') return create_db_home_details
def test_delete_backend_client_error(lb_client, verify_work_request_patch, get_existing_backend_patch): error_message = "Work Request Failed" module = get_module() backend = get_backend() lb_client.delete_backend.return_value = get_response( 200, None, backend, None) get_existing_backend_patch.return_value = backend verify_work_request_patch.side_effect = ClientError(Exception('Work Request Failed')) try: oci_load_balancer_backend.delete_backend(lb_client, module) except Exception as ex: assert error_message in ex.args[0]
def get_user_ids_from_user_names(identity_client, user_names, module): user_ids = [] users = oci_utils.list_all_resources( identity_client.list_users, compartment_id=module.params.get("compartment_id")) user_id_dict = dict((user_name, user.id) for user in users for user_name in user_names if user.name.strip() == user_name.strip()) try: user_ids = [user_id_dict[user_name] for user_name in user_names] except KeyError as ex: raise ClientError("User " + ex.args[0] + " does not exists") return user_ids