Skip to content

Commit 1393761

Browse files
Bihan  RanaBihan  Rana
authored andcommitted
Add project_name config
1 parent 41f5ec3 commit 1393761

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/dstack/_internal/core/backends/digitalocean_base/api_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ def list_ssh_keys(self) -> List[Dict[str, Any]]:
3232
response.raise_for_status()
3333
return response.json()["ssh_keys"]
3434

35+
def list_projects(self) -> List[Dict[str, Any]]:
36+
response = self._make_request("GET", "/v2/projects")
37+
response.raise_for_status()
38+
return response.json()["projects"]
39+
40+
def get_project_id(self, project_name: str) -> Optional[str]:
41+
projects = self.list_projects()
42+
for project in projects:
43+
if project["name"] == project_name:
44+
return project["id"]
45+
return None
46+
3547
def create_ssh_key(self, name: str, public_key: str) -> Dict[str, Any]:
3648
payload = {"name": name, "public_key": public_key}
3749
response = self._make_request("POST", "/v2/account/keys", json=payload)

src/dstack/_internal/core/backends/digitalocean_base/compute.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def create_instance(
9696
else:
9797
backend_specific_commands = SETUP_COMMANDS
9898

99-
# Prepare droplet configuration
99+
project_id = self.api_client.get_project_id(self.config.project_name)
100100
droplet_config = {
101101
"name": instance_name,
102102
"region": instance_offer.region,
@@ -111,6 +111,7 @@ def create_instance(
111111
authorized_keys=instance_config.get_public_keys(),
112112
backend_specific_commands=backend_specific_commands,
113113
),
114+
**({"project_id": project_id} if project_id is not None else {}),
114115
}
115116

116117
droplet = self.api_client.create_droplet(droplet_config)

src/dstack/_internal/core/backends/digitalocean_base/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class BaseDigitalOceanBackendConfig(CoreModel):
1919
Literal["amddevcloud", "digitalocean"],
2020
Field(description="The type of backend"),
2121
] = "digitalocean"
22+
project_name: Annotated[str, Field(description="The name of the DigitalOcean project")] = None
2223
regions: Annotated[
2324
Optional[List[str]],
2425
Field(description="The list of DigitalOcean regions. Omit to use all regions"),

0 commit comments

Comments
 (0)