Created
August 3, 2017 20:57
-
-
Save adam-stokes/489a62d962da4c640770a502f1525d55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Field: | |
| """ Field class with validation | |
| """ | |
| def __init__(self, | |
| label=None, | |
| widget=None, | |
| key=None, | |
| storable=True, | |
| error=None, | |
| required=True, | |
| validator=None): | |
| self.label = label | |
| self.widget = widget | |
| self.key = key | |
| self.storable = storable | |
| self.error = Text("") | |
| self.required = required | |
| self.validator = validator | |
| def validate(self): | |
| """ Validator for field | |
| """ | |
| self.error.set_text("") | |
| if self.required and not self.value: | |
| self.error.set_text("This field is required and cannot be empty.") | |
| return False | |
| if self.validator and callable(self.validator): | |
| is_valid, msg = self.validator() | |
| if not is_valid: | |
| self.error.set_text(msg) | |
| return False | |
| return True | |
| @property | |
| def value(self): | |
| return self.widget.value | |
| @value.setter # NOQA | |
| def value(self, value): | |
| self.widget.value = value | |
| class Form: | |
| """ Form containing widget fields | |
| """ | |
| def __init__(self, widgets): | |
| self._fields = [] | |
| for w in widgets: | |
| key = w.key.replace('-', '_') | |
| setattr(self, key, w) | |
| self._fields.append(getattr(self, key)) | |
| def fields(self): | |
| return self._fields | |
| class AWS(BaseProvider): | |
| def __init__(self): | |
| super().__init__() | |
| self.auth_type = 'access-key' | |
| self.cloud_type = 'ec2' | |
| self.access_key = None | |
| self.secret_key = None | |
| self.form = [Field(label='AWS Access Key', | |
| widget=StringEditor(), | |
| key='access-key'), | |
| Field(label='AWS Secret Key', | |
| widget=StringEditor(), | |
| key='secret-key')] | |
| @property | |
| def default_region(self): | |
| return 'us-east-1' | |
| async def configure_tools(self): | |
| """ Configure AWS CLI. | |
| """ | |
| creds = CredentialManager(self.cloud, self.credential) | |
| for key, value in creds.to_dict().items(): | |
| try: | |
| await arun(['aws', 'configure', '--profile', cred_name], | |
| input='{}\n{}\n\n\n'.format(creds['access-key'], | |
| creds['secret-key']), | |
| check=True) | |
| except CalledProcessError as e: | |
| app.log.error('Failed to configure AWS CLI profile: {}'.format( | |
| e.stderr)) | |
| raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment