def delete(self): if self.exists( ): # todo: check for other states of pod that could affect this deletion action data = self.api_core().delete_namespaced_pod( name=self.name, namespace=self.cluster.namespace().name) return status_ok(message="pod deleted", data=data) return status_ok(message="pod not deleted (since it didn't exist)")
def info_raw(self): try: data = self.api_core().read_namespaced_pod( name=self.name, namespace=self.cluster.namespace().name) return status_ok(data=data) except Exception as exception: return status_error(error=exception)
def security_group_create(self, security_group_name, description, vpc_id=None, tags=None): if self.security_group_exists(security_group_name=security_group_name): security_group_id = self.security_group( security_group_name=security_group_name ).get( 'GroupId' ) # todo see what is a better way to implement this workflow, since at the moment there are two full calls to the self.security_groups() method return status_warning( message= f'Security group already existed: {security_group_name}', data={'security_group_id': security_group_id}) kwargs = { 'Description': description, 'GroupName': security_group_name, 'TagSpecifications': self.tag_specifications_create(tags=tags, resource_type='security-group') } if vpc_id: kwargs['VpcId'] = vpc_id security_group_id = self.client().create_security_group( **kwargs).get('GroupId') return status_ok( message=f'Security group created ok: {security_group_name}', data={'security_group_id': security_group_id})
def log_stream_delete(self, log_group_name, log_stream_name): if self.log_stream_exists(log_group_name, log_stream_name) is False: return status_warning( f'log stream did not exist: {log_group_name}') self.client().delete_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name) return status_ok(message=f"log stream deleted ok: {log_stream_name}")
def log_group_create(self, log_group_name, tags=None): if self.log_group_exists(log_group_name): return status_warning( f'log group already existed: {log_group_name}') kwargs = {'logGroupName': log_group_name} if tags: kwargs['tags'] = tags self.client().create_log_group(**kwargs) return status_ok(message=f"log group created ok: {log_group_name}")
def create(self, manifest): try: pod_info = self.api_core().create_namespaced_pod( body=manifest, namespace=self.cluster.namespace().name) return status_ok(message="pod created", data=pod_info) except ApiException as exception: exception_body = json_loads(exception.body) return status_error(message=exception_body.get('message'), error=exception) except Exception as exception: return status_error(error=exception)
def create(self): if self.exists(): return status_error( message=f"namespace already existed: {self.name}") try: manifest = client.V1Namespace(metadata=client.V1ObjectMeta( name=self.name)) result = self.cluster.api_core().create_namespace(manifest) except Exception as exception: return status_error(data=exception) return status_ok(message="namespace created", data=result)
def check_aws_session(self): try: self.caller_identity() return status_ok() except ClientError as client_error: exception = client_error.response.get('Error') error_code = exception.get("Code") error_message = exception.get('Message') message = (f"\tCurrent Security credentials are invalid! \n\n" f"\tPlease check: current environment values, " f"profile name or value in your ~/.aws/credentials file" f"\n\n" f"\tBoto3 Error message: {error_code} {error_message}") return status_error(message=message, error=client_error)
def log_stream_create(self, log_group_name, log_stream_name): if self.log_group_not_exists( log_group_name=log_group_name ): # if the log group doesn't exist, create it self.log_group_create(log_group_name).get('status') try: self.client().create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name) return status_ok( message=f"log stream created ok: {log_stream_name}") except ClientError as exception: if exception.response['Error'][ 'Code'] == 'ResourceAlreadyExistsException': return status_warning( message=f"log stream already existed: {log_stream_name}") return status_error(message=f'exception', error=exception)
def validate_create_kwargs(self, kwargs): name = kwargs.get('FunctionName') code = kwargs.get('Code', {}) image_uri = code.get('ImageUri') s3_bucket = code.get('S3Bucket') s3_key = code.get('S3Key') if len(name) > 64: return status_error( message="lambda functions name cannot be bigger than 64 chars", data=self.name) if Lambda(name=name).exists(): return status_warning( message=f'lambda function already exists: {name}') if image_uri is None: if self.s3().file_not_exists(s3_bucket, s3_key): return status_error( message= f'for function {name}, could not find provided s3 bucket and s3 key: {s3_bucket} {s3_key}' ) return status_ok()
def delete(self): if self.exists(): result = self.cluster.api_core().delete_namespace(name=self.name) return status_ok(message="namespace deleted", data=result) return status_warning(message="namespace already existed")
def pre_process_a_folder(item: DIRECTORY): pre_processor = Pre_Processor() pre_processor.process_folder(folder_to_process=item.folder) return status_ok(message="Directory added")
def clear_data_and_status_folders(): pre_processor = Pre_Processor() pre_processor.clear_data_and_status_folders() return status_ok(message="Data cleared from HD2")