Save computes to database

This commit is contained in:
grossmj committed 2021-04-05 14:21:41 +09:30
1 parent e607793e74
commit 566e326b57
13 files changed
+515 -337

No files matched your search

+27 -5
View File
@@ -15,12 +15,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, validator
from typing import List, Optional, Union
from uuid import UUID
from uuid import UUID, uuid4
from enum import Enum
from .nodes import NodeType
from .base import DateTimeModelMixin
class Protocol(str, Enum):
@@ -37,12 +38,11 @@ class ComputeBase(BaseModel):
Data to create a compute.
"""
compute_id: Optional[Union[str, UUID]] = None
name: Optional[str] = None
protocol: Protocol
host: str
port: int = Field(..., gt=0, le=65535)
user: Optional[str] = None
name: Optional[str] = None
class ComputeCreate(ComputeBase):
@@ -50,6 +50,7 @@ class ComputeCreate(ComputeBase):
Data to create a compute.
"""
compute_id: Union[str, UUID] = Field(default_factory=uuid4)
password: Optional[str] = None
class Config:
@@ -63,6 +64,24 @@ class ComputeCreate(ComputeBase):
}
}
@validator("name", always=True)
def generate_name(cls, name, values):
if name is not None:
return name
else:
protocol = values.get("protocol")
host = values.get("host")
port = values.get("port")
user = values.get("user")
if user:
# due to random user generated by 1.4 it's common to have a very long user
if len(user) > 14:
user = user[:11] + "..."
return "{}://{}@{}:{}".format(protocol, user, host, port)
else:
return "{}://{}:{}".format(protocol, host, port)
class ComputeUpdate(ComputeBase):
"""
@@ -96,7 +115,7 @@ class Capabilities(BaseModel):
disk_size: int = Field(..., description="Disk size on this compute")
class Compute(ComputeBase):
class Compute(DateTimeModelMixin, ComputeBase):
"""
Data returned for a compute.
"""
@@ -110,6 +129,9 @@ class Compute(ComputeBase):
last_error: Optional[str] = Field(None, description="Last error found on the compute")
capabilities: Optional[Capabilities] = None
class Config:
orm_mode = True
class AutoIdlePC(BaseModel):
"""