Skip to content

Commit c1d8339

Browse files
authored
Merge pull request #606 from splitgraph/feature/sgr-cloud-seed-changes
Feature/sgr cloud seed changes
2 parents d4587b0 + f1a436f commit c1d8339

5 files changed

Lines changed: 26 additions & 13 deletions

File tree

splitgraph/cloud/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,9 +948,9 @@ def _make_plugin(plugin_dict: Dict[str, Any]) -> Plugin:
948948
supports_mount=plugin_dict["supportsMount"],
949949
)
950950

951-
def get_all_plugins(self) -> List[Plugin]:
951+
def get_all_plugins(self, seed: Optional[str] = None) -> List[Plugin]:
952952
response = self._gql(
953-
{"query": GET_PLUGINS, "operationName": "ExternalPlugins"},
953+
{"query": GET_PLUGINS, "operationName": "ExternalPlugins", "variables": {"seed": seed}},
954954
handle_errors=True,
955955
anonymous_ok=True,
956956
)

splitgraph/cloud/project/generation.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import base64
22
import itertools
33
import os
4+
import random
5+
import string
46
from io import StringIO
57
from pathlib import Path
68
from typing import Any, Dict, List, Mapping, Optional, Tuple
79

810
import ruamel.yaml
9-
from pydantic import BaseModel
11+
from pydantic import BaseModel, Field
1012
from ruamel.yaml import CommentedMap as CM
1113
from ruamel.yaml import CommentedSeq as CS
1214

@@ -143,12 +145,20 @@ def stub_plugin(plugin: Plugin, namespace: str, repository: str, is_live: bool =
143145
return ruamel_dict
144146

145147

148+
def _get_seed_uid() -> str:
149+
return "".join(
150+
random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits)
151+
for _ in range(10)
152+
)
153+
154+
146155
class ProjectSeed(BaseModel):
147156
"""
148157
Contains all information required to generate a Splitgraph project + optionally
149158
a dbt model for GitHub Actions
150159
"""
151160

161+
seed_uid: str = Field(default_factory=_get_seed_uid)
152162
namespace: str
153163
plugins: List[str]
154164
include_dbt: bool = False
@@ -162,11 +172,12 @@ def decode(cls, encoded: str) -> "ProjectSeed":
162172

163173

164174
def generate_project(
165-
api_client: GQLAPIClient, seed: ProjectSeed, basedir: Path, github_repo: Optional[str] = None
175+
api_client: GQLAPIClient, seed: str, basedir: Path, github_repo: Optional[str] = None
166176
) -> None:
167-
all_plugins = {p.plugin_name: p for p in api_client.get_all_plugins()}
177+
all_plugins = {p.plugin_name: p for p in api_client.get_all_plugins(seed)}
168178

169-
credentials, repositories, repository_info = generate_splitgraph_yml(all_plugins, seed)
179+
decoded_seed = ProjectSeed.decode(seed)
180+
credentials, repositories, repository_info = generate_splitgraph_yml(all_plugins, decoded_seed)
170181

171182
yml = ruamel.yaml.YAML()
172183
with open(os.path.join(basedir, "splitgraph.credentials.yml"), "w") as f:
@@ -176,7 +187,7 @@ def generate_project(
176187
yml.dump(repositories, f)
177188

178189
# Generate the dbt project
179-
if seed.include_dbt:
190+
if decoded_seed.include_dbt:
180191
dbt_repo, _, is_dbt = repository_info[-1]
181192
assert is_dbt
182193
dbt_sources = [r for r, _, is_dbt in repository_info if not is_dbt]

splitgraph/cloud/queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@
244244
}
245245
"""
246246

247-
GET_PLUGINS = """query ExternalPlugins {
248-
externalPlugins {
247+
GET_PLUGINS = """query ExternalPlugins($seed: String) {
248+
externalPlugins(seed: $seed) {
249249
pluginName
250250
name
251251
description

splitgraph/commandline/cloud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,11 +1179,11 @@ def validate_c(repositories_file):
11791179
def seed_c(remote, seed, github_repository, directory):
11801180
"""Generate a starter Splitgraph Cloud project from a seed."""
11811181
from splitgraph.cloud import GQLAPIClient
1182-
from splitgraph.cloud.project.generation import ProjectSeed, generate_project
1182+
from splitgraph.cloud.project.generation import generate_project
11831183

11841184
client = GQLAPIClient(remote)
11851185

1186-
generate_project(client, ProjectSeed.decode(seed), directory, github_repo=github_repository)
1186+
generate_project(client, seed, directory, github_repo=github_repository)
11871187
click.echo(f"Splitgraph project generated in {os.path.abspath(directory)}.")
11881188

11891189

test/splitgraph/cloud/project/test_generation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def test_generate_project_no_dbt(snapshot):
2323
), TemporaryDirectory() as tmpdir:
2424
generate_project(
2525
GQLAPIClient("anyremote"),
26-
seed=ProjectSeed(namespace="myns", plugins=["postgres_fdw", "airbyte-postgres"]),
26+
seed=ProjectSeed(
27+
namespace="myns", plugins=["postgres_fdw", "airbyte-postgres"]
28+
).encode(),
2729
basedir=tmpdir,
2830
github_repo="my/repo",
2931
)
@@ -63,7 +65,7 @@ def test_generate_project_with_dbt(snapshot):
6365
GQLAPIClient("anyremote"),
6466
seed=ProjectSeed(
6567
namespace="myns", plugins=["postgres_fdw", "airbyte-postgres"], include_dbt=True
66-
),
68+
).encode(),
6769
basedir=tmpdir,
6870
github_repo="my/repo",
6971
)

0 commit comments

Comments
 (0)