def update_backend(lb_client, module, backend, lb_id, backend_set_name): changed = False result = dict(changed=changed, backend=to_dict(backend)) backend_name = oci_lb_utils.get_backend_name(module) get_logger().info("Updating backend %s for backendset %s in load balancer %s", backend_name, backend_set_name, lb_id) update_backend_details = UpdateBackendDetails() for attribute in update_backend_details.attribute_map: changed = oci_utils.check_and_update_attributes( update_backend_details, attribute, module.params.get( attribute, None), getattr(backend, attribute), changed) get_logger().debug("Existing backend property values: %s, input property values: %s", backend, update_backend_details) if changed: result = oci_lb_utils.create_or_update_lb_resources_and_wait(resource_type="backend", function=lb_client.update_backend, kwargs_function={ 'update_backend_details': update_backend_details, 'load_balancer_id': lb_id, 'backend_set_name': backend_set_name, 'backend_name': backend_name}, lb_client=lb_client, get_fn=lb_client.get_backend, kwargs_get={'load_balancer_id': lb_id, 'backend_set_name': backend_set_name, 'backend_name': backend_name}, module=module ) get_logger().info("Successfully updated backend %s for backendset %s in load balancer %s", backend_name, backend_set_name, lb_id) else: get_logger().info("No update to the backend %s for backendset %s in load balancer %s as no " + "attribute changed", backend_name, backend_set_name, lb_id) return result
def delete_backend(lb_client, module): lb_id = module.params.get("load_balancer_id") backend_set_name = module.params.get("backend_set_name") backend_name = oci_lb_utils.get_backend_name(module) get_logger().info( "Deleting backend %s for backendset %s in load balancer %s", backend_name, backend_set_name, lb_id, ) result = oci_lb_utils.delete_lb_resources_and_wait( resource_type="backend", function=lb_client.delete_backend, kwargs_function={ "backend_set_name": backend_set_name, "load_balancer_id": lb_id, "backend_name": backend_name, }, lb_client=lb_client, get_fn=lb_client.get_backend, kwargs_get={ "load_balancer_id": lb_id, "backend_set_name": backend_set_name, "backend_name": backend_name, }, module=module, ) get_logger().info( "Successfully Deleted backend %s for backendset %s in load balancer %s", backend_name, backend_set_name, lb_id, ) return result
def create_backend(lb_client, module, lb_id, backend_set_name): backend_name = oci_lb_utils.get_backend_name(module) create_backend_details = CreateBackendDetails() for attribute in create_backend_details.attribute_map: create_backend_details.__setattr__( attribute, module.params.get(attribute, None)) get_logger().info("Creating backend for backendset %s with parameters %s", backend_set_name, str(create_backend_details)) get_logger().debug("backend ip_address: %s and port: %s", module.params['ip_address'], str(module.params['port'])) result = oci_lb_utils.create_or_update_lb_resources_and_wait(resource_type="backend", function=lb_client.create_backend, kwargs_function={ 'create_backend_details': create_backend_details, 'load_balancer_id': lb_id, 'backend_set_name': backend_set_name}, lb_client=lb_client, get_fn=lb_client.get_backend, kwargs_get={'load_balancer_id': lb_id, 'backend_set_name': backend_set_name, 'backend_name': backend_name}, module=module ) get_logger().info("Successfully created backend for backendset %s with parameters %s", backend_set_name, str(create_backend_details)) return result
def create_or_update_backend(lb_client, module): backend = None result = dict(changed=False, backend="") lb_id = module.params.get("load_balancer_id") backend_set_name = module.params.get("backend_set_name") backend = oci_utils.get_existing_resource( lb_client.get_backend, module, load_balancer_id=lb_id, backend_set_name=backend_set_name, backend_name=oci_lb_utils.get_backend_name(module), ) try: if backend: result = update_backend(lb_client, module, backend, lb_id, backend_set_name) else: result = oci_utils.check_and_create_resource( resource_type="backend", create_fn=create_backend, kwargs_create={ "lb_client": lb_client, "module": module, "lb_id": lb_id, "backend_set_name": backend_set_name, }, list_fn=lb_client.list_backends, kwargs_list={ "load_balancer_id": lb_id, "backend_set_name": backend_set_name, }, module=module, model=CreateBackendDetails(), ) except ServiceError as ex: get_logger().error("Unable to create/update backend due to: %s", ex.message) module.fail_json(msg=ex.message) except ClientError as ex: get_logger().error("Unable to create/update backend due to: %s", str(ex)) module.fail_json(msg=str(ex)) return result