Skip to content

Commit

Permalink
[AIRFLOW-5751] add get_uri method to Connection (#6426)
Browse files Browse the repository at this point in the history
(cherry-picked from 53422a8)
  • Loading branch information
dstandish authored and kaxil committed Dec 18, 2019
1 parent 48e357c commit eaf955d
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 124 deletions.
37 changes: 36 additions & 1 deletion airflow/models/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import json
from builtins import bytes
from urllib.parse import urlparse, unquote, parse_qsl
from urllib.parse import parse_qsl, quote, unquote, urlencode, urlparse

from sqlalchemy import Column, Integer, String, Boolean
from sqlalchemy.ext.declarative import declared_attr
Expand Down Expand Up @@ -145,6 +145,41 @@ def parse_from_uri(self, uri):
if uri_parts.query:
self.extra = json.dumps(dict(parse_qsl(uri_parts.query, keep_blank_values=True)))

def get_uri(self):
uri = '{}://'.format(str(self.conn_type).lower().replace('_', '-'))

authority_block = ''
if self.login is not None:
authority_block += quote(self.login, safe='')

if self.password is not None:
authority_block += ':' + quote(self.password, safe='')

if authority_block > '':

This comment has been minimized.

Copy link
@ashb

ashb Dec 18, 2019

Member

Is there a reason you used > here, and not !=, or just if authority_block:?

This comment has been minimized.

Copy link
@ashb

ashb Dec 18, 2019

Member

@dstandish ^^ (sorry, this is us backporting your fix, and it broke on py2.)

This comment has been minimized.

Copy link
@dstandish

dstandish Dec 18, 2019

Author Contributor

No; either != or if authority_block should be fine

authority_block += '@'

uri += authority_block

host_block = ''
if self.host:
host_block += quote(self.host, safe='')

if self.port:
if host_block > '':
host_block += ':{}'.format(self.port)
else:
host_block += '@:{}'.format(self.port)

if self.schema:
host_block += '/{}'.format(quote(self.schema, safe=''))

uri += host_block

if self.extra_dejson:
uri += '?{}'.format(urlencode(self.extra_dejson))

return uri

def get_password(self):
if self._password and self.is_encrypted:
fernet = get_fernet()
Expand Down
Loading

0 comments on commit eaf955d

Please sign in to comment.