diff --git a/Naas API/Naas_API_Space.ipynb b/Naas API/Naas_API_Space.ipynb new file mode 100644 index 0000000000..0614eeef1d --- /dev/null +++ b/Naas API/Naas_API_Space.ipynb @@ -0,0 +1,591 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Naas" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Naas API - Storage" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Tags:** #naas #api #space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Author:** [Loic Laville](https://www.linkedin.com/in/loic-laville/)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Naas API Space is a service to run your own container on Naas Infrastructure.**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Last update:** 2024-06-17 (Created: 2024-06-10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Description:** This notebook demonstrates how to use Naas API Space." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Import library" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import naas_python" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "user_id = \"7ed38998-59bb-473f-b14e-983279efbdca\"\n", + "workspace_id = \"cfda6ebb-1ea9-4668-a7e5-c599a66caf8c\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Command list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* [Create a space with the given specifications](#createspace)\n", + "* [List all spaces for the current user](#listspace)\n", + "* [Get a space with the given name](#getspace)\n", + "* [Update a space with the given specifications](#updatespace)\n", + "* [Delete the space with the given name](#deletespace)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Create Naas Space

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**required arguments:**\n", + "* **name** : Name of the space\n", + "* **image** : Image of the space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**optionals arguments:**\n", + "* **domain** : Domain of the space\n", + "* **env** : Environment variables for the Space container. _default: {}_\n", + "* **cpu** : CPU utilization for the Space container. _default: 1_\n", + "* **memory** : Memory utilization for the Space container. _default: 1Gi_\n", + "* **port** : Port for the Space container. _default: 5080_" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "containers = [\n", + " {\n", + " \"name\": \"test-space-lib\",\n", + " \"image\": \"nginx:latest\",\n", + " \"env\": {\"API_KEY\": \"123\"},\n", + " \"port\": 80,\n", + " \"cpu\": \"1\",\n", + " \"memory\": \"0Gi\"\n", + " },\n", + "]\n", + "\n", + "create_space = naas_python.space.create(\n", + " name=\"test-space-lib\",\n", + " domain=\"default\",\n", + " containers=containers\n", + ")\n", + "create_space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```js\n", + "Space(\n", + " name='test-space-lib', \n", + " user_id=UUID('7ed38998-59bb-473f-b14e-983279efbdca'), \n", + " id=UUID('ef1ada6f-e9f2-4b96-b040-ee2782b75ae4'), \n", + " domain='pending', \n", + " containers=\n", + " [Container(\n", + " name='test-space-lib', \n", + " image='nginx:latest', \n", + " env={'API_KEY': '123'}, \n", + " port=80, \n", + " cpu='1', \n", + " memory='1Gi'\n", + " )\n", + " ]\n", + ")\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### CLI command" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```sh\n", + "naas-python space create --help\n", + "```\n", + "```sh\n", + "naas-python space create \\\n", + "--name \"test-cli-2\" \\\n", + "--image \"nginx:latest\" \\\n", + "--domain \"default\" \\\n", + "--env='{\"API_KEY\": \"123\"}' \\\n", + "--cpu 1 \\\n", + "--memory \"1Gi\" \\\n", + "--port 80\n", + "```\n", + "\n", + "output :\n", + "```yaml\n", + "Name: test-cli-2\n", + "User ID: 7ed38998-59bb-473f-b14e-983279efbdca\n", + "ID: 20828e9e-e923-4885-80a1-1930bec9c398\n", + "Domain: pending\n", + "Containers:\n", + "- Name: test-cli-2\n", + " Image: nginx:latest\n", + " Environment Variables:\n", + " API_KEY: 123\n", + " Port: 80\n", + " CPU: 1\n", + " Memory: 1Gi\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

List Naas Spaces

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "list_space = naas_python.space.list()\n", + "list_space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```js\n", + "output :\n", + "SpaceListResponse(\n", + " spaces=\n", + " [Space(\n", + " name='test-space-lib', \n", + " user_id=UUID('7ed38998-59bb-473f-b14e-983279efbdca'), \n", + " id=UUID('5107a0bf-812b-4346-8a03-9453cbbbf26c'), \n", + " domain='https://test-space-lib.default.nebari.dev.naas.ai', containers=\n", + " [Container(name='test-space-lib', image='nginx:latest', env={'API_KEY': '123'}, port=80, cpu='1', memory='1Gi'\n", + ")])])\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### CLI command" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```sh\n", + "naas-python space list --help\n", + "```\n", + "```sh\n", + "naas-python space list\n", + "```\n", + "\n", + "output :\n", + "```yaml\n", + "test-space-lib\n", + "7ed38998-59bb-473f-b14e-983279efbdca\n", + "5107a0bf-812b-4346-8a03-9453cbbbf26c\n", + "https://test-space-lib.default.nebari.dev.naas.ai\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Get Naas Spaces

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**arguments:**\n", + "* name : Name of the space [required]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "get_space = naas_python.space.get(name=\"test-space-lib\")\n", + "get_space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```js\n", + "output :\n", + "Space(\n", + " name='test-space-lib', \n", + " user_id=UUID('7ed38998-59bb-473f-b14e-983279efbdca'), \n", + " id=UUID('5107a0bf-812b-4346-8a03-9453cbbbf26c'), \n", + " domain='https://test-space-lib.default.nebari.dev.naas.ai', \n", + " containers=[\n", + " Container(name='test-space-lib', image='nginx:latest', env={'API_KEY': '123'}, port=80, cpu='1', memory='1Gi')])\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### CLI command" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```sh\n", + "naas-python space get --help\n", + "```\n", + "```sh\n", + "naas-python space get --name 'test-space-lib'\n", + "```\n", + "\n", + "output :\n", + "```yaml\n", + "User ID: 7ed38998-59bb-473f-b14e-983279efbdca\n", + "ID: 5107a0bf-812b-4346-8a03-9453cbbbf26c\n", + "Domain: https://test-space-lib.default.nebari.dev.naas.ai\n", + "Containers:\n", + "- Name: test-space-lib\n", + " Image: nginx:latest\n", + " Environment Variables:\n", + " API_KEY: 123\n", + " Port: 80\n", + " CPU: 1\n", + " Memory: 1Gi\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Update Naas Space

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**arguments:**\n", + "\n", + "- name : Name of the space [default: None] [required]\n", + "- domain : Domain of the space [default: None]\n", + "- cpu : CPU utilization for the Space container [default: None] [required]\n", + "- memory : Memory utilization for the Space container [default: None] [required]\n", + "- env : Environment variables [default: None]\n", + "- image : Image of the space, e.g. placeholder/placeholder:latest [default: None] [required]\n", + "- port : Port of the container. [required]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "update_space = naas_python.space.update(\n", + " name=\"test-space-lib\",\n", + " cpu=\"2\",\n", + " memory=\"2Gi\",\n", + " env='{\"API_KEY\": \"123\"}',\n", + " image=\"alpine:latest\",\n", + " port=\"8080\"\n", + ")\n", + "update_space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```js\n", + "output :\n", + "Space(\n", + " name='test-space-lib', \n", + " user_id=UUID('7ed38998-59bb-473f-b14e-983279efbdca'), \n", + " id=UUID('688ab047-6e83-43a6-9881-6f7f8eac5407'), \n", + " domain='https://test-space-lib.default.nebari.dev.naas.ai', \n", + " containers=[\n", + " Container(name='test-space-lib', image='alpine:latest', env={'API_KEY': '123'}, port=8080, cpu='2', memory='2Gi')])\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### CLI command" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```sh\n", + "naas-python space update --help\n", + "```\n", + "```sh\n", + "naas-python space update \\\n", + "--name \"test-space-lib\" \\\n", + "--cpu \"2\" \\\n", + "--memory \"2Gi\" \\\n", + "--env '{\"API_KEY\": \"123\"}' \\\n", + "--image \"alpine:latest\" \\\n", + "--port \"8080\"\n", + "```\n", + "\n", + "output :\n", + "```yaml\n", + "Name: test-space-lib\n", + "User ID: 7ed38998-59bb-473f-b14e-983279efbdca\n", + "ID: 5107a0bf-812b-4346-8a03-9453cbbbf26c\n", + "Domain: https://test-space-lib.default.nebari.dev.naas.ai\n", + "Containers:\n", + "- Name: test-space-lib\n", + " Image: alpine:latest\n", + " Environment Variables:\n", + " API_KEY: 123\n", + " Port: 8080\n", + " CPU: 2\n", + " Memory: 2Gi\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Delete Naas Space

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**required argument:**\n", + "* **name** : Name of the space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "delete_space = naas_python.space.delete(\n", + " name=\"test-space-lib\"\n", + ")\n", + "delete_space" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### CLI command" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```sh\n", + "naas-python space delete --help\n", + "```\n", + "```sh\n", + "naas-python space delete --name \"test-space-lib\"\n", + "```\n", + "\n", + "output :\n", + "```yaml\n", + "\"Space test-space-lib deleted successfully\"\n", + "```" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}