def delete_transit_gateway_connection(user, user_id, gateway_id, connection_id): from doosra.tasks.other.transit_gateway_tasks import task_delete_transit_gateway_connection transit_gateway = doosradb.session.query(TransitGateway).get(gateway_id) if not transit_gateway: current_app.logger.info(f"No IBM Transit Gateway found with ID {gateway_id}") return Response("No Transit Gateway Found. Please Create One.", status=404) tg_connection = doosradb.session.query(TransitGatewayConnection).get(connection_id) if not tg_connection: current_app.logger.info(f"No IBM Transit Gateway Connection found with ID {connection_id}") return Response("No Transit Gateway Connectoin Found. Please Create One.", status=404) if not transit_gateway.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask( task_id=task_delete_transit_gateway_connection.delay(connection_id=connection_id).id, type_="TRANSIT-GATEWAY-CONNECTION", region=transit_gateway.region, action="DELETE", cloud_id=transit_gateway.ibm_cloud.id, resource_id=tg_connection.id) doosradb.session.add(task) tg_connection.status = DELETING doosradb.session.commit() current_app.logger.info(DELETE_TRANSIT_GATEWAY_CONNECTION.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_ibm_security_group_rule(user_id, user, security_group_id): """ Add IBM Security Group Rule :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param security_group_id: ID for this request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_security_group_rule data = request.get_json(force=True) security_group = doosradb.session.query(IBMSecurityGroup).filter_by( id=security_group_id, cloud_id=data['cloud_id']).first() if not security_group: return Response("SECURITY_GROUP_NOT_FOUND", status=404) cloud = doosradb.session.query(IBMCloud).filter_by( id=security_group.ibm_cloud.id, project_id=user.project.id).first() if not cloud: current_app.logger.info("No IBM cloud found with ID {id}".format(id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask( task_create_ibm_security_group_rule.delay(security_group_id, data, user_id, user.project.id).id, "SECURITY-GROUP-RULE", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(SECURITY_GROUP_RULE_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def update_transit_gateway(user, user_id, gateway_id): """ Update API Call for Transit Gateway. Only `name` and `is_global_route` can be update. :param user: :param user_id: :param gateway_id: """ from doosra.tasks.other.transit_gateway_tasks import task_update_transit_gateway data = request.get_json(force=True) transit_gateway = doosradb.session.query(TransitGateway).get(gateway_id) if not transit_gateway: current_app.logger.info(f"No IBM Transit Gateway found with ID {gateway_id}") return Response("No Transit Gateway Found. Please Create One.", status=404) if data.get('name') and data["name"] != transit_gateway.name: transit_gateway.name = data["name"] if "is_global_route" in data.keys(): transit_gateway.is_global_route = data["is_global_route"] doosradb.session.commit() task = IBMTask( task_update_transit_gateway.delay(gateway_id).id, type_="TRANSIT-GATEWAY", region=transit_gateway.region, action="UPDATE", cloud_id=transit_gateway.ibm_cloud.id, resource_id=transit_gateway.id) doosradb.session.add(task) transit_gateway.status = UPDATING doosradb.session.commit() resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_ibm_image(user_id, user): """ Add IBM Image :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_image data = request.get_json(force=True) cloud = doosradb.session.query(IBMCloud).filter_by(id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info("No IBM cloud found with ID {id}".format(id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) existing_image = doosradb.session.query(IBMImage).filter_by(name=data['name'], cloud_id=data['cloud_id'], region=data.get('region')).first() if existing_image: return Response("Error conflicting image names", status=409) task = IBMTask(task_create_ibm_image.delay(data, user_id, user.project.id).id, "IMAGE", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(IMAGE_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def create_ibm_floating_ip(user_id, user): """ Add an IBM floating ip :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_floating_ip data = request.get_json(force=True) cloud = doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info( "No IBM cloud project found with ID {id}".format( id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask(task_create_floating_ip.delay(data, user_id, user.project.id).id, "FLOATING-IP", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(FLOATING_IP_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_ibm_acl(user_id, user): """ Add an IBM ACL :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_network_acl data = request.get_json(force=True) cloud = doosradb.session.query(IBMCloud).filter_by(id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info("No IBM cloud found with ID {id}".format(id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) network_acl = doosradb.session.query(IBMNetworkAcl).filter_by(name=data['name'], cloud_id=data['cloud_id'], region=data.get('region')).first() if network_acl: return Response("ERROR_CONFLICTING_ACL_NAME", status=409) task = IBMTask( task_create_ibm_network_acl.delay(cloud.id, data.get("name"), data.get("region"), data, user_id, user.project.id).id, "NETWORK-ACL", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(ACL_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def create_ibm_load_balancer(user_id, user): """ Add an IBM load balancer :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_load_balancer data = request.get_json(force=True) cloud = doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info( "No IBM cloud project found with ID {id}".format( id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) vpc = doosradb.session.query(IBMVpcNetwork).filter_by( id=data["vpc_id"], cloud_id=data["cloud_id"]).first() if not vpc: current_app.logger.info( "No IBM VPC found with ID {id}".format(id=data['vpc_id'])) return Response(status=404) load_balancer = doosradb.session.query(IBMLoadBalancer).filter_by( name=data['name'], cloud_id=data['cloud_id'], region=vpc.region).first() if load_balancer: return Response("ERROR_CONFLICTING_LOAD_BALANCER_NAME", status=409) for subnet in data['subnets']: subnet = IBMSubnet.query.get(subnet['id']) if not subnet: current_app.logger.info( "No IBM Subnet with ID {id}".format(id=subnet['id'])) return Response(status=404) task = IBMTask(task_create_ibm_load_balancer.delay(data, user_id, user.project.id).id, "LOAD-BALANCER", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(LB_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_transit_gateway_connection(user_id, user, transit_gateway_id): """ :param user_id: :param user: :param transit_gateway_id: """ from doosra.tasks.other.transit_gateway_tasks import task_create_transit_gateway_connection data = request.get_json(force=True) cloud = doosradb.session.query(IBMCloud).filter_by(id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info("No IBM cloud found with ID {id}".format(id=data['cloud_id'])) return Response("No IBM cloud found with ID {id}".format(id=data['cloud_id']), status=404) transit_gateway = doosradb.session.query(TransitGateway).get(transit_gateway_id) if not transit_gateway: current_app.logger.info(f"Transit Gateway with id `{transit_gateway_id}` not found!") return Response(f"Transit Gateway with id `{transit_gateway_id}` not found!", status=409) if data["network_type"] == "vpc": vpc = doosradb.session.query(IBMVpcNetwork).filter_by(id=data["vpc_id"]).first() if not vpc: current_app.logger.info("No IBM VPC Network found with ID {id}".format(id=data['vpc_id'])) return Response("No IBM VPC Network found with ID {id}".format(id=data['vpc_id']), status=404) vpc_connection = doosradb.session.query(TransitGatewayConnection).filter_by(network_id=vpc.crn).first() if vpc_connection: current_app.logger.info(f"Provided VPC `{vpc.name}` is already connected with Transit Gateway") return Response( f"Provided VPC `{vpc.name}` is already attached to one of the Transit Gateway", status=409) else: transit_gateway_connections = doosradb.session.query(TransitGatewayConnection).filter_by( transit_gateway_id=transit_gateway_id, network_type=data["network_type"]).all() if len(transit_gateway_connections) >= 1: return Response("Classic Connection is already connected with cloud {id}".format(id=data["cloud_id"]), status=409) transit_gateway_connection = doosradb.session.query(TransitGatewayConnection).filter_by( name=data['name'], transit_gateway_id=transit_gateway_id).first() if transit_gateway_connection: return Response("ERROR_CONFLICTING_CONNECTION_NAME", status=409) task = IBMTask( task_create_transit_gateway_connection.delay(data.get("name"), transit_gateway_id, cloud.id, data).id, "TRANSIT-GATEWAY-CONNECTION", "ADD", cloud.id, request_payload=data) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(TRANSIT_GATEWAY_CONNECTION_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_vpn_connection(user_id, user, vpn_gateway_id, connection_id): """ Delete an IBM VPN Gateway Connection :param connection_id: ID of the Connection :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param vpn_gateway_id: vpn_gateway_id for VPN Gateway :return: Response object from flask package """ from doosra.tasks.ibm.vpn_tasks import task_delete_ibm_vpn_connection_workflow vpn_gateway = IBMVpnGateway.query.get(vpn_gateway_id) vpn_connection = doosradb.session.query(IBMVpnConnection).filter_by( id=connection_id).first() if not vpn_gateway: current_app.logger.info( "No IBM VPN Gateway found with ID {name}".format( name=vpn_gateway.name)) if not vpn_connection: current_app.logger.info( "No IBM VPN Connection found with ID {id} for VPN Gateway {name}". format(id=connection_id, name=vpn_gateway.name)) return Response(status=404) if not vpn_gateway.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) if vpn_gateway.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask(task_id=None, type_="VPN-CONNECTION", region=vpn_connection.region, action="DELETE", cloud_id=vpn_connection.ibm_cloud.id, resource_id=vpn_connection.id) doosradb.session.add(task) vpn_connection.status = DELETING doosradb.session.commit() task_delete_ibm_vpn_connection_workflow.delay( task_id=task.id, cloud_id=vpn_connection.ibm_cloud.id, region=vpn_connection.region, vpn_connection_id=vpn_connection.id) current_app.logger.info(VPN_CONNECTION_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_dedicated_host_group(user_id, user, dedicated_host_group_id): """ Delete an IBM Dedicated Host Group :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param dedicated_host_group_id: ID of the dedicated host group :return: Response object from flask package """ from doosra.tasks.ibm.dedicated_host_tasks import task_delete_dedicated_host_group data = request.get_json(force=True) ibm_cloud = doosradb.session.query(IBMCloud).filter_by(id=data["cloud_id"], project_id=user.project.id).first() if not ibm_cloud: error_message = "No IBM Cloud found with ID {id}".format(id=data['cloud_id']) LOGGER.info(error_message) return Response(error_message, status=404) if ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: error_message = IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS LOGGER.info(error_message) return Response(error_message, status=400) dedicated_host_group = ibm_cloud.dedicated_host_groups.filter_by(id=dedicated_host_group_id).first() if not dedicated_host_group: return Response(status=404) if dedicated_host_group.dedicated_hosts.count(): return Response("Please remove attached dedicated hosts first", status=400) ibm_task = IBMTask(None, "DEDICATED-HOST-GROUP", "DELETE", ibm_cloud.id) doosradb.session.add(ibm_task) doosradb.session.commit() task_delete_dedicated_host_group.apply_async( kwargs={ 'ibm_task_id': ibm_task.id, 'cloud_id': ibm_cloud.id, 'dedicated_host_group_id': dedicated_host_group_id } ) LOGGER.info(DEDICATED_HOST_GROUP_DELETE.format(dh_group_id=dedicated_host_group_id, email=user.email)) return Response(json.dumps(ibm_task.to_json()), status=202, mimetype="application/json")
def delete_ibm_k8s_cluster(user_id, user, k8s_cluster_id): """ Delete an IBM K8S Cluster :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param k8s_cluster_id: k8s_cluster_id for K8S cluster :return: Response object from flask package """ from doosra.tasks.ibm.kubernetes_tasks import task_delete_ibm_k8s_cluster_workflow k8s_cluster = doosradb.session.query(KubernetesCluster).filter_by( id=k8s_cluster_id).first() if k8s_cluster.status in [ 'PENDING', 'IN_PROGRESS', 'CREATING', 'CREATION_PENDING' ]: LOGGER.info( "IBM K8S Cluster Migration in Progress with ID {id}".format( id=k8s_cluster_id)) return Response(status=405) if not k8s_cluster: LOGGER.info( "No IBM K8S Cluster found with ID {id}".format(id=k8s_cluster_id)) return Response(status=404) if k8s_cluster.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not k8s_cluster.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask(task_id=None, type_="K8S_CLUSTER", region=k8s_cluster.ibm_vpc_network.region, action="DELETE", cloud_id=k8s_cluster.ibm_cloud.id, resource_id=k8s_cluster.id) doosradb.session.add(task) k8s_cluster.status = DELETING doosradb.session.commit() task_delete_ibm_k8s_cluster_workflow.delay( task_id=task.id, cloud_id=k8s_cluster.ibm_cloud.id, region=k8s_cluster.ibm_vpc_network.region, k8s_cluster_id=k8s_cluster.id) LOGGER.info(K8S_CLUSTER_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def attach_subnet_to_acl(user_id, user, vpc_id, subnet_id): """ Add IBM subnet to Network ACL :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param vpc_id: VPC ID for this request :param subnet_id: Subnet ID for this request :param acl_id: Network ACL ID for this request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_attach_subnet_to_acl data = request.get_json(force=True) cloud = (doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first()) if not cloud: current_app.logger.info( "No IBM cloud found with ID {id}".format(id=data["cloud_id"])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) subnet = (doosradb.session.query(IBMSubnet).filter_by( id=subnet_id, vpc_id=vpc_id).first()) if not subnet: current_app.logger.info( "No IBM Subnet found with ID {id}".format(id=subnet_id)) return Response(status=404) acl = (doosradb.session.query(IBMNetworkAcl).filter_by( id=data["acl_id"], cloud_id=data["cloud_id"]).first()) if not subnet: current_app.logger.info( "No IBM Network ACL found with ID {id}".format(id=data["acl_id"])) return Response(status=404) task = IBMTask( task_attach_subnet_to_acl.delay(subnet_id, acl.id).id, "SUBNET", "UPDATE", cloud.id, ) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(ATTACH_SUBNET_TO_ACL.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_vpc_routes(user_id, user, vpc_id, id): """ This request deletes a route. This operation cannot be reversed. :param vpc_id: VPC ID :param user_id: ID of the user initiating the request :param id: ID of the Route to Delete :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.ibm.vpcs_tasks import task_delete_ibm_vpc_route_workflow vpc = doosradb.session.query(IBMVpcNetwork).filter_by(id=vpc_id).first() vpc_route = (doosradb.session.query(IBMVpcRoute).filter_by( id=id, vpc_id=vpc_id).first()) if not vpc: current_app.logger.info( "No IBM VPC found with ID {id}".format(id=vpc_id)) return Response(status=404) if not vpc_route: current_app.logger.info( "No IBM Route `{route_id}` found for the VPC with ID `{vpc_id}`". format(route_id=id, vpc_id=vpc_id)) if not vpc.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) if vpc.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask(task_id=None, type_="VPC-ROUTE", region=vpc.region, action="DELETE", cloud_id=vpc.cloud_id, resource_id=vpc_route.id) doosradb.session.add(task) vpc_route.status = DELETING doosradb.session.commit() task_delete_ibm_vpc_route_workflow.delay(task_id=task.id, cloud_id=vpc.cloud_id, region=vpc.region, vpc_route_id=vpc_route.id) current_app.logger.info(VPC_ROUTE_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def create_ibm_ssh_key(user_id, user): """ Add an IBM ssh key :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ssh_key data = request.get_json(force=True) cloud = doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info( "No IBM cloud project found with ID {id}".format( id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) ssh_key_name = doosradb.session.query(IBMSshKey).filter_by( name=data['name'], cloud_id=data['cloud_id'], region=data.get('region')).first() if ssh_key_name: return Response("Error conflicting ssh key name", status=409) ssh_public_key = doosradb.session.query(IBMSshKey).filter_by( public_key=data['public_key'], cloud_id=data['cloud_id'], region=data['region']).first() if ssh_public_key: return Response("Error conflicting ssh public key", status=409) task = IBMTask(task_create_ssh_key.delay(data, user_id, user.project.id).id, "SSH-KEY", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(SSH_KEY_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_ibm_vpn_gateway(user_id, user): """ Add IBM VPN Gateway :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_vpn_gateway data = request.get_json(force=True) data['resource_group'] = data.get('resource_group', 'default') cloud = doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info( "No IBM cloud found with ID {id}".format(id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) vpc = doosradb.session.query(IBMVpcNetwork).filter_by( id=data["vpc_id"], cloud_id=data["cloud_id"]).first() if not vpc: current_app.logger.info( "No IBM VPC found with ID {id}".format(id=data['vpc_id'])) return Response(status=404) vpn_gateway = doosradb.session.query(IBMVpnGateway).filter_by( name=data['name'], cloud_id=data['cloud_id'], region=vpc.region).first() if vpn_gateway: return Response("ERROR_CONFLICTING_VPN_GATEWAY_NAME", status=409) task = IBMTask(task_create_ibm_vpn_gateway.delay(data.get("name"), vpc.id, data, user_id, user.project.id).id, "VPN-GATEWAY", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(VPN_GATEWAY_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_ipsec_policy(user_id, user, ipsec_policy_id): """ Delete an IBM IPSEC Policy :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param ipsec_policy_id for IPSEC Policy :return: Response object from flask package """ from doosra.tasks.ibm.vpn_tasks import task_delete_ibm_ipsec_policy_workflow ipsec_policy = doosradb.session.query(IBMIPSecPolicy).filter_by( id=ipsec_policy_id).first() if not ipsec_policy: current_app.logger.info( "No IBM IPSEC Policy found with ID {id}".format( id=ipsec_policy_id)) return Response(status=404) if not ipsec_policy.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) if ipsec_policy.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask(task_id=None, type_="IPSEC-POLICY", region=ipsec_policy.region, action="DELETE", cloud_id=ipsec_policy.ibm_cloud.id, resource_id=ipsec_policy.id) doosradb.session.add(task) ipsec_policy.status = DELETING doosradb.session.commit() task_delete_ibm_ipsec_policy_workflow.delay( task_id=task.id, cloud_id=ipsec_policy.ibm_cloud.id, region=ipsec_policy.region, ipsec_policy_id=ipsec_policy.id) current_app.logger.info(IPSEC_POLICY_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_public_gateway(user_id, user, public_gateway_id): """ Delete an IBM Public Gateway :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param public_gateway_id: public_gateway_id for Public Gateway :return: Response object from flask package """ from doosra.tasks.ibm.public_gateway_tasks import task_delete_public_gateway_workflow public_gateway = doosradb.session.query(IBMPublicGateway).filter_by( id=public_gateway_id).first() if not public_gateway: current_app.logger.info( "No IBM public_gateway found with ID {id}".format( id=public_gateway_id)) return Response(status=404) if public_gateway.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not public_gateway.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask(task_id=None, type_="PUBLIC_GATEWAY", region=public_gateway.region, action="DELETE", cloud_id=public_gateway.ibm_cloud.id, resource_id=public_gateway.id) doosradb.session.add(task) public_gateway.status = DELETING doosradb.session.commit() task_delete_public_gateway_workflow.delay( task_id=task.id, cloud_id=public_gateway.ibm_cloud.id, region=public_gateway.region, public_gateway_id=public_gateway.id) current_app.logger.info(PUBLIC_GATEWAY_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_subnet(user_id, user, vpc_id, subnet_id): """ Delete an IBM subnet :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param vpc_id: vpc_id for IBM subnet :param subnet_id: subnet_id for IBM subnet :return: Response object from flask package """ from doosra.tasks.ibm.subnet_tasks import task_delete_ibm_subnet_workflow subnet = (doosradb.session.query(IBMSubnet).filter_by( id=subnet_id, vpc_id=vpc_id).first()) if not subnet: current_app.logger.info( "No IBM Subnet found with ID {id}".format(id=subnet_id)) return Response(status=404) if not subnet.ibm_vpc_network.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) if subnet.ibm_vpc_network.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask(task_id=None, type_="SUBNET", region=subnet.region, action="DELETE", cloud_id=subnet.ibm_vpc_network.ibm_cloud.id, resource_id=subnet.id) doosradb.session.add(task) subnet.status = DELETING doosradb.session.commit() task_delete_ibm_subnet_workflow.delay( task_id=task.id, cloud_id=subnet.ibm_vpc_network.ibm_cloud.id, region=subnet.region, subnet_id=subnet.id) current_app.logger.info(SUBNET_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_load_balancer(user_id, user, load_balancer_id): """ Delete an IBM load balancer :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param loadBalancer_id: loadBalancer_id for load balancer :return: Response object from flask package """ from doosra.tasks.ibm.load_balancer_tasks import task_delete_ibm_load_balancer_workflow load_balancer = doosradb.session.query(IBMLoadBalancer).filter_by( id=load_balancer_id).first() if not load_balancer: current_app.logger.info( "No IBM load balancer found with ID {id}".format( id=load_balancer_id)) return Response(status=404) if load_balancer.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not load_balancer.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask(task_id=None, type_="LOAD-BALANCER", region=load_balancer.region, action="DELETE", cloud_id=load_balancer.ibm_cloud.id, resource_id=load_balancer.id) doosradb.session.add(task) load_balancer.status = DELETING doosradb.session.commit() task_delete_ibm_load_balancer_workflow.delay( task_id=task.id, cloud_id=load_balancer.ibm_cloud.id, region=load_balancer.region, load_balancer_id=load_balancer.id) current_app.logger.info(LB_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_ibm_ipsec_policy(user_id, user): """ Add IBM IPSec Policy :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_ipsec_policy data = request.get_json(force=True) # Below are the optional fields: we should use default Resource Group if not provided: PayLoad remains the same data['resource_group'] = data.get('resource_group', 'default') cloud = doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first() if not cloud: current_app.logger.info( "No IBM cloud found with ID {id}".format(id=data['cloud_id'])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) ipsec_policy = doosradb.session.query(IBMIPSecPolicy).filter_by( name=data['name'], cloud_id=data['cloud_id'], region=data.get('region')).first() if ipsec_policy: return Response("ERROR_CONFLICTING_IPSEC_POLICY_NAME", status=409) task = IBMTask(task_create_ibm_ipsec_policy.delay(data.get("name"), cloud.id, data, user_id, user.project.id).id, "IPSEC-POLICY", "ADD", cloud.id, request_payload=json.dumps(data)) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(IPSEC_POLICY_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_floating_ip(user_id, user, floating_ip_id): """ Delete an IBM floating ip :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param floating ip: floating_ip_id for floating ip :return: Response object from flask package """ from doosra.tasks.ibm.floating_ip_tasks import task_delete_ibm_floating_ip_workflow floating_ip = doosradb.session.query(IBMFloatingIP).filter_by( id=floating_ip_id).first() if not floating_ip: current_app.logger.info( "No IBM floating ip found with ID {id}".format(id=floating_ip_id)) return Response(status=404) if floating_ip.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not floating_ip.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask(task_id=None, type_="FLOATING-IP", region=floating_ip.region, action="DELETE", cloud_id=floating_ip.ibm_cloud.id, resource_id=floating_ip.id) doosradb.session.add(task) floating_ip.status = DELETING doosradb.session.commit() task_delete_ibm_floating_ip_workflow.delay( task_id=task.id, cloud_id=floating_ip.ibm_cloud.id, region=floating_ip.region, floating_ip_id=floating_ip.id) current_app.logger.info(FLOATING_IP_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_ssh_key(user_id, user, ssh_key_id): """ Delete an IBM ssh_keys :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param ssh: ssh_key_id for ssh keys :return: Response object from flask package """ from doosra.tasks.ibm.ssh_key_tasks import task_delete_ssh_key_workflow ssh_key = doosradb.session.query(IBMSshKey).filter_by( id=ssh_key_id).first() if not ssh_key: current_app.logger.info( "No IBM ssh key found with ID {id}".format(id=ssh_key_id)) return Response(status=404) if ssh_key.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not ssh_key.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask(task_id=None, type_="SSH-KEY", region=ssh_key.region, action="DELETE", cloud_id=ssh_key.ibm_cloud.id, resource_id=ssh_key.id) doosradb.session.add(task) ssh_key.status = DELETING doosradb.session.commit() task_delete_ssh_key_workflow.delay(task_id=task.id, cloud_id=ssh_key.ibm_cloud.id, region=ssh_key.region, ssh_key_id=ssh_key.id) current_app.logger.info(SSH_KEY_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_instance(user_id, user, instance_id): """ Delete an IBM Instance :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param instance_id: instance ID for IBM Insntace :return: Response object from flask package """ from doosra.tasks.ibm.instance_tasks import task_delete_ibm_instance_workflow instance = doosradb.session.query(IBMInstance).filter_by( id=instance_id).first() if not instance: LOGGER.info( "No IBM Instance found with ID {id}".format(id=instance_id)) return Response(status=404) if instance.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not instance.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) task = IBMTask(task_id=None, type_="INSTANCE", region=instance.region, action="DELETE", cloud_id=instance.ibm_cloud.id, resource_id=instance.id) doosradb.session.add(task) instance.status = DELETING doosradb.session.commit() task_delete_ibm_instance_workflow.delay(task_id=task.id, cloud_id=instance.ibm_cloud.id, region=instance.region, instance_id=instance.id) LOGGER.info(INSTANCE_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def delete_ibm_vpc_address_prefix(user_id, user, vpc_id, address_prefix_id): """ Delete an IBM vpc address prefix :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.ibm.address_prefix_tasks import task_delete_address_prefix_workflow address_prefix = (doosradb.session.query(IBMAddressPrefix).filter_by( id=address_prefix_id).first()) if not address_prefix: current_app.logger.info( "No IBM address prefix found with ID {id}".format( id=address_prefix_id)) return Response(status=404) if not address_prefix.ibm_vpc_network.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) if address_prefix.ibm_vpc_network.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) task = IBMTask(task_id=None, type_="ADDRESS-PREFIX", region=address_prefix.ibm_vpc_network.region, action="DELETE", cloud_id=address_prefix.ibm_vpc_network.cloud_id, resource_id=address_prefix.id) doosradb.session.add(task) doosradb.session.commit() task_delete_address_prefix_workflow.delay( task_id=task.id, cloud_id=address_prefix.ibm_vpc_network.cloud_id, region=address_prefix.ibm_vpc_network.region, addr_prefix_id=address_prefix.id) current_app.logger.info(DELETE_ADDRESS_PREFIX.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def create_ibm_dedicated_host_group(user_id, user): """ Add an IBM Dedicated Host Group :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :return: Response object from flask package """ from doosra.tasks.ibm.dedicated_host_tasks import task_create_dedicated_host_group data = request.get_json(force=True) ibm_cloud = doosradb.session.query(IBMCloud).filter_by(id=data["cloud_id"], project_id=user.project.id).first() if not ibm_cloud: error_message = "No IBM Cloud found with ID {id}".format(id=data['cloud_id']) LOGGER.info(error_message) return Response(error_message, status=404) if ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: error_message = IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS LOGGER.info(error_message) return Response(error_message, status=400) dedicated_host_group = \ ibm_cloud.dedicated_host_groups.filter_by( name=data['name'], cloud_id=data['cloud_id'], region=data["region"] ).first() if dedicated_host_group: error_message = "ERROR_CONFLICTING_DEDICATED_HOST_GROUP_NAME" LOGGER.info(error_message) return Response(error_message, status=409) if data.get("resource_group"): dh_resource_group = ibm_cloud.resource_groups.filter_by(name=data["resource_group"]).first() if not dh_resource_group: return Response(f"Resource Group {data['resource_group']} not found", 404) ibm_task = IBMTask(None, "DEDICATED-HOST-GROUP", "ADD", ibm_cloud.id, request_payload=json.dumps(data)) doosradb.session.add(ibm_task) doosradb.session.commit() task_create_dedicated_host_group.apply_async(kwargs={'ibm_task_id': ibm_task.id, 'data': data}) LOGGER.info(DEDICATED_HOST_GROUP_CREATE.format(email=user.email)) return Response(json.dumps({"task_id": ibm_task.id}), status=202, mimetype="application/json")
def sync_address_prefixes(user_id, user, vpc_id): """ Initiate sync address prefixes for IBM cloud account. If there is already a sync in progress, return same task's status. :return: """ from doosra.tasks.other.ibm_tasks import task_get_ibm_address_prefixes vpc = doosradb.session.query(IBMVpcNetwork).filter_by(id=vpc_id).first() if not vpc: current_app.logger.info( "No VPC found with ID {vpc_id}".format(vpc_id=vpc_id)) return Response(status=404) ibm_cloud = doosradb.session.query(IBMCloud).filter_by( id=vpc.cloud_id, project_id=user.project.id).first() if not ibm_cloud: current_app.logger.info( "No IBM Cloud account found with ID {cloud_id}".format( cloud_id=vpc.cloud_id)) return Response(status=404) if ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) sync_task = ibm_cloud.ibm_tasks.filter_by(type="ADDRESS-PREFIX").first() if sync_task and sync_task.status == CREATED and not request.args.get( 'force'): return Response(status=202) elif sync_task: doosradb.session.delete(sync_task) task = IBMTask(None, "ADDRESS-PREFIX", "SYNC", ibm_cloud.id) doosradb.session.add(task) doosradb.session.commit() task_get_ibm_address_prefixes.apply_async(kwargs={ 'vpc_id': vpc.id, 'task_id': task.id }, queue='sync_queue') return Response(status=202)
def delete_ibm_acl(user_id, user, acl_id): """ Delete an IBM Network ACL :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param acl_id: ID for ACL object :return: Response object from flask package """ from doosra.tasks.ibm.acl_tasks import task_delete_network_acl_workflow network_acl = doosradb.session.query(IBMNetworkAcl).filter_by(id=acl_id).first() if not network_acl: current_app.logger.info("No IBM Network ACL found with ID {id}".format(id=acl_id)) return Response(status=404) if network_acl.ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) if not network_acl.ibm_cloud.project_id == user.project.id: return Response("INVALID_IBM_CLOUD", status=400) if network_acl.is_default: return Response("ERROR_DEFAULT_ACL", status=400) if network_acl.subnets.all(): return Response("ERROR_SUBNETS_ATTACHED", status=400) task = IBMTask( task_id=None, type_="NETWORK-ACL", region=network_acl.region, action="DELETE", cloud_id=network_acl.ibm_cloud.id, resource_id=network_acl.id) doosradb.session.add(task) network_acl.status = DELETING doosradb.session.commit() task_delete_network_acl_workflow.delay(task_id=task.id, cloud_id=network_acl.ibm_cloud.id, region=network_acl.region, ibm_network_acl_id=network_acl.id) current_app.logger.info(ACL_DELETE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def add_ibm_subnet(user_id, user, vpc_id): """ Add IBM subnet :param user_id: ID of the user initiating the request :param user: object of the user initiating the request :param vpc_id: VPC ID for this request :return: Response object from flask package """ from doosra.tasks.other.ibm_tasks import task_create_ibm_subnet data = request.get_json(force=True) cloud = (doosradb.session.query(IBMCloud).filter_by( id=data["cloud_id"], project_id=user.project.id).first()) if not cloud: current_app.logger.info( "No IBM cloud found with ID {id}".format(id=data["cloud_id"])) return Response(status=404) if cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) subnet = (doosradb.session.query(IBMSubnet).filter_by( name=data["name"], vpc_id=vpc_id, zone=data["zone"]).first()) if subnet: return Response("ERROR_CONFLICTING_SUBNET_NAME", status=409) task = IBMTask( task_create_ibm_subnet.delay(data.get("name"), vpc_id, data, user_id, user.project.id).id, "SUBNET", "ADD", cloud.id, request_payload=json.dumps(data), ) doosradb.session.add(task) doosradb.session.commit() current_app.logger.info(SUBNET_CREATE.format(user.email)) resp = jsonify({"task_id": task.id}) resp.status_code = 202 return resp
def sync_instance_profiles(user_id, user, cloud_id, region): """ Initiate sync instance profiles for IBM cloud account. If there is already a sync in progress, return same task's status. :return: """ from doosra.tasks.other.ibm_tasks import task_get_instance_profiles ibm_cloud = doosradb.session.query(IBMCloud).filter_by( id=cloud_id, project_id=user.project.id).first() if not ibm_cloud: current_app.logger.info( "No IBM Cloud account found with ID {cloud_id}".format( cloud_id=cloud_id)) return Response(status=404) if ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) sync_task = ibm_cloud.ibm_tasks.filter_by(type="INSTANCE-PROFILE", region=region).first() if sync_task and sync_task.status == CREATED and not request.args.get( 'force'): return Response(status=202) elif sync_task: doosradb.session.delete(sync_task) task = IBMTask(None, "INSTANCE-PROFILE", "SYNC", ibm_cloud.id, region=region) doosradb.session.add(task) doosradb.session.commit() task_get_instance_profiles.apply_async(kwargs={ 'cloud_id': ibm_cloud.id, 'task_id': task.id, 'region': region }, queue='sync_queue') return Response(status=202)
def sync_cos_buckets_objects(user_id, user, cloud_id, region): """ Initiate sync Buckets and its Objects for IBM cloud account. If there is already a sync in progress, return same task's status. :return: """ # TODO all parameter is needed all or qcow2 from doosra.tasks.other.ibm_tasks import task_get_cos_buckets ibm_cloud = doosradb.session.query(IBMCloud).filter_by( id=cloud_id, project_id=user.project.id).first() if not ibm_cloud: current_app.logger.info( "No IBM Cloud account found with ID {cloud_id}".format( cloud_id=cloud_id)) return Response(status=404) if ibm_cloud.status == IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS: return Response(IBMCloud.STATUS_ERROR_DUPLICATE_RESOURCE_GROUPS, status=400) sync_task = ibm_cloud.ibm_tasks.filter_by(type="BUCKET-OBJECT", region=region).first() if sync_task and sync_task.status == CREATED and not request.args.get( 'force'): return Response(status=202) elif sync_task: doosradb.session.delete(sync_task) primary_objects = request.args.get('primary_objects', True) task = IBMTask(None, "BUCKET-OBJECT", "SYNC", ibm_cloud.id, region=region) doosradb.session.add(task) doosradb.session.commit() task_get_cos_buckets.apply_async(kwargs={ 'cloud_id': ibm_cloud.id, 'task_id': task.id, 'region': region, 'get_objects': True, 'primary_objects': primary_objects }, queue='sync_queue') return Response(status=202)