From the course: Build REST APIs with FastAPI
Unlock this course with a free trial
Join today to access over 24,200 courses taught by industry experts.
Solution: Start and stop a VM - Python Tutorial
From the course: Build REST APIs with FastAPI
Solution: Start and stop a VM
Let's have a look at my solution. Okay. So this is the description. And now we start. So I'm going to use an enum for the machines. And this is something that Pydantic knows about. And when it's going to validate the image, it's going to check that it's one of these enums, otherwise, it's going to return an error. And what I'm going to use is an in-memory database. So I'm creating a lock on the database because requests can be multi-threaded. And the VMs is just basically an ID to a virtual machine. All right. So I'm creating my application and now the data model. So the image is an enum with Ubuntu, Debian, and Alpine. And the VM has a CPU count, memory size, and the image which is one of these. And now we have a post to start the VM. So I'm generating a new uuid. And then with the lock, I'm setting the dictionary to this VM and returning ID of the new created machine. In the post to stop, I'm getting an ID which is a string, and you see this is the path of the parameter that is…