Skip to content

Commit

Permalink
Merge branch '102-test-module-extractor-module' into 'main'
Browse files Browse the repository at this point in the history
Resolve "Test module extractor module"

Closes #102

See merge request pub/terrareg!72
  • Loading branch information
MatthewJohn committed May 6, 2022
2 parents b0b25f0 + b1e8f3c commit b2440b3
Show file tree
Hide file tree
Showing 6 changed files with 552 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ cache:
paths:
- .cache/pip
- venv/
- /usr/local/bin/terraform-docs

before_script:
# Install terraform-docs
- test -f /usr/local/bin/terraform-docs || { wget https://github.com/terraform-docs/terraform-docs/releases/download/v0.16.0/terraform-docs-v0.16.0-linux-amd64.tar.gz && tar -zxvf terraform-docs-v0.16.0-linux-amd64.tar.gz && chmod +x terraform-docs && mv terraform-docs /usr/local/bin/ && rm terraform-docs-v0.16.0-linux-amd64.tar.gz; }
- python --version # For debugging
- pip install --proxy=$http_proxy virtualenv
- virtualenv venv
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ There are common attributes that can be added to each of variable objects, which

## Changelog

### v1.0.2

* Add exception to be thrown when upload fails to analyse terraform
* Add tests for module extraction

### v1.0.1

* Add package updates to Dockerfile
Expand Down
6 changes: 6 additions & 0 deletions terrareg/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ class ProviderNameNotPermittedError(TerraregError):
"""Provider name not in list of allowed providers."""

pass


class UnableToProcessTerraformError(TerraregError):
"""An error occurred whilst attempting to process terraform."""

pass
25 changes: 15 additions & 10 deletions terrareg/module_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from terrareg.models import Example, ModuleVersion, Submodule
from terrareg.database import Database
from terrareg.errors import (
UnableToProcessTerraformError,
UnknownFiletypeError,
InvalidTerraregMetadataFileError,
MetadataDoesNotContainRequiredAttributeError
Expand All @@ -36,15 +37,6 @@ def __init__(self, module_version: ModuleVersion):
self._module_version = module_version
self._extract_directory = tempfile.TemporaryDirectory() # noqa: R1732
self._upload_directory = tempfile.TemporaryDirectory() # noqa: R1732
self._source_file = None

@property
def source_file(self):
"""Generate/return source filename."""
if self._source_file is None:
filename = secure_filename(self._upload_file.filename)
self._source_file = safe_join_paths(self.upload_directory, filename)
return self._source_file

@property
def extract_directory(self):
Expand All @@ -70,7 +62,11 @@ def __exit__(self, *args, **kwargs):
@staticmethod
def _run_terraform_docs(module_path):
"""Run terraform docs and return output."""
terradocs_output = subprocess.check_output(['terraform-docs', 'json', module_path])
try:
terradocs_output = subprocess.check_output(['terraform-docs', 'json', module_path])
except subprocess.CalledProcessError as exc:
raise UnableToProcessTerraformError('An error occurred whilst processing the terraform code.')

return json.loads(terradocs_output)

@staticmethod
Expand Down Expand Up @@ -244,6 +240,15 @@ def __init__(self, upload_file, *args, **kwargs):
"""Store member variables."""
super(ApiUploadModuleExtractor, self).__init__(*args, **kwargs)
self._upload_file = upload_file
self._source_file = None

@property
def source_file(self):
"""Generate/return source filename."""
if self._source_file is None:
filename = secure_filename(self._upload_file.filename)
self._source_file = safe_join_paths(self.upload_directory, filename)
return self._source_file

def _save_upload_file(self):
"""Save uploaded file to uploads directory."""
Expand Down
Empty file.
Loading

0 comments on commit b2440b3

Please sign in to comment.