Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/azure state client #28

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion elx/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ def params(self) -> Dict[str, Any]:
"client": self.client,
}

def has_existing_state(self, state_file_name: str) -> bool:
"""
Checks for a pre-existing state file.

Args:
state_file_name (str): The name of the state file to load.

Returns:
bool: Boolean flag to indicate whether there is a pre-existing state file.
"""
raise NotImplementedError


class S3StateClient(StateClient):
"""
Expand Down Expand Up @@ -61,6 +73,32 @@ def client(self):
os.environ["AZURE_STORAGE_CONNECTION_STRING"]
)

@property
def container_name(self) -> str:
"""
Gives the container name where state files are stored in Azure Blob Storage.

Returns:
str: Name of the container.
"""
return self.base_path.replace("azure://", "")

def has_existing_state(self, state_file_name: str) -> bool:
"""
Checks for a pre-existing state file.

Args:
state_file_name (str): The name of the state file to load.

Returns:
bool: Boolean flag to indicate whether there is a pre-existing state file.
"""
# Get container client where the state file would be located
container = self.client.get_container_client(container=self.container_name)

# Check if state file exists
return container.get_blob_client(blob=state_file_name).exists()


class GCSStateClient(StateClient):
"""
Expand Down Expand Up @@ -92,6 +130,18 @@ class LocalStateClient(StateClient):
def params(self) -> dict:
return {}

def has_existing_state(self, state_file_name: str) -> bool:
"""
Checks for a pre-existing state file.

Args:
state_file_name (str): The name of the state file to load.

Returns:
bool: Boolean flag to indicate whether there is a pre-existing state file.
"""
return Path(f"{self.base_path}/{state_file_name}").exists()


def state_client_factory(base_path: str) -> StateClient:
if base_path.startswith("s3://"):
Expand Down Expand Up @@ -123,7 +173,7 @@ def load(self, state_file_name: str) -> dict:
Returns:
dict: The contents of the state file.
"""
if not Path(f"{self.base_path}/{state_file_name}").exists():
if not self.state_client.has_existing_state(state_file_name):
return {}

with open(
Expand Down
Loading