async def delete_single_provider(id: str): provider = mongo.get_db().providers.find_one({"_id": PyObjectId(id)}) if provider: result = mongo.get_db().providers.delete_one({"_id": PyObjectId(id)}) return {"status": "success", "message": result} else: return {"status": "fail", "message": "no provider matched"}
async def sync_single_provider(id: str): provider = mongo.get_db().providers.find_one({"_id": PyObjectId(id)}) resources = build_provider_resources(provider) browndfields = discover_brownfields(resources) bf_list = list(map(dict, [bf for bf in browndfields])) result = mongo.get_db().servers.insert_many(bf_list) inserted_count = len(result.inserted_ids) return { "status": "success", "message": f"inserted {inserted_count} server(s)" }
async def sync_resource(): servers = [] for provider in mongo.get_db().providers.find(): servers += build_provider_resources(provider) mongo.get_db().servers.delete_many( {"tenant_id": "113ac03bf6e341be9ae4df61a59354dc"}) response = discover_brownfields(servers) res = list(map(dict, [r for r in response])) mongo.get_db().servers.insert_many(res) # ret.inserted_ids # for r in response: # mongo.get_db().servers.insert_one(r.dict(by_alias=True)) return {'servers': response}
async def delete_all_providers(): result = mongo.get_db().providers.delete_many({"region": None}) deleted_count = result.deleted_count return { "status": "success", "message": f"deleted {deleted_count} provider(s)" }
async def list_server(): servers = [] for server in mongo.get_db().servers.find(): servers.append(Server(**server)) return {'servers': servers}
async def create_single_provider(provider: Provider): if hasattr(provider, 'id'): delattr(provider, 'id') result = mongo.get_db().providers.insert_one(provider.dict(by_alias=True)) provider.id = result.inserted_id return {'provider': provider}
async def retrieve_single_provider(id: str): provider = mongo.get_db().providers.find_one({"_id": PyObjectId(id)}) return {'provider': Provider(**provider)}
async def retrieve_all_providers(): providers = [] for provider in mongo.get_db().providers.find(): providers.append(Provider(**provider)) return {'providers': providers}