|
1 | | -import json |
2 | 1 | import logging |
3 | | -import os |
4 | 2 | import re |
5 | 3 | import socket |
6 | 4 | from functools import lru_cache |
|
16 | 14 | APPLICATION_AMZ_JSON_1_1, |
17 | 15 | APPLICATION_X_WWW_FORM_URLENCODED, |
18 | 16 | AWS_REGION_US_EAST_1, |
19 | | - ENV_DEV, |
20 | 17 | HEADER_LOCALSTACK_ACCOUNT_ID, |
21 | 18 | LOCALHOST, |
22 | | - REGION_LOCAL, |
23 | 19 | ) |
24 | | -from localstack.utils.strings import is_string, is_string_or_bytes, to_str |
| 20 | +from localstack.utils.strings import is_string_or_bytes, to_str |
25 | 21 |
|
26 | 22 | # set up logger |
27 | 23 | LOG = logging.getLogger(__name__) |
@@ -54,83 +50,6 @@ def get_valid_regions_for_service(service_name): |
54 | 50 | return regions |
55 | 51 |
|
56 | 52 |
|
57 | | -# TODO: Remove, this is super legacy functionality |
58 | | -class Environment: |
59 | | - def __init__(self, region=None, prefix=None): |
60 | | - # target is the runtime environment to use, e.g., |
61 | | - # 'local' for local mode |
62 | | - self.region = region or get_local_region() |
63 | | - # prefix can be 'prod', 'stg', 'uat-1', etc. |
64 | | - self.prefix = prefix |
65 | | - |
66 | | - def apply_json(self, j): |
67 | | - if isinstance(j, str): |
68 | | - j = json.loads(j) |
69 | | - self.__dict__.update(j) |
70 | | - |
71 | | - @staticmethod |
72 | | - def from_string(s): |
73 | | - parts = s.split(":") |
74 | | - if len(parts) == 1: |
75 | | - if s in PREDEFINED_ENVIRONMENTS: |
76 | | - return PREDEFINED_ENVIRONMENTS[s] |
77 | | - parts = [get_local_region(), s] |
78 | | - if len(parts) > 2: |
79 | | - raise Exception('Invalid environment string "%s"' % s) |
80 | | - region = parts[0] |
81 | | - prefix = parts[1] |
82 | | - return Environment(region=region, prefix=prefix) |
83 | | - |
84 | | - @staticmethod |
85 | | - def from_json(j): |
86 | | - if not isinstance(j, dict): |
87 | | - j = j.to_dict() |
88 | | - result = Environment() |
89 | | - result.apply_json(j) |
90 | | - return result |
91 | | - |
92 | | - def __str__(self): |
93 | | - return "%s:%s" % (self.region, self.prefix) |
94 | | - |
95 | | - |
96 | | -PREDEFINED_ENVIRONMENTS = {ENV_DEV: Environment(region=REGION_LOCAL, prefix=ENV_DEV)} |
97 | | - |
98 | | - |
99 | | -# TODO: Remove |
100 | | -def get_environment(env=None, region_name=None): |
101 | | - """ |
102 | | - Return an Environment object based on the input arguments. |
103 | | -
|
104 | | - Parameter `env` can be either of: |
105 | | - * None (or empty), in which case the rules below are applied to (env = os.environ['ENV'] or ENV_DEV) |
106 | | - * an Environment object (then this object is returned) |
107 | | - * a string '<region>:<name>', which corresponds to Environment(region='<region>', prefix='<prefix>') |
108 | | - * the predefined string 'dev' (ENV_DEV), which implies Environment(region='local', prefix='dev') |
109 | | - * a string '<name>', which implies Environment(region=DEFAULT_REGION, prefix='<name>') |
110 | | -
|
111 | | - Additionally, parameter `region_name` can be used to override DEFAULT_REGION. |
112 | | - """ |
113 | | - if not env: |
114 | | - if "ENV" in os.environ: |
115 | | - env = os.environ["ENV"] |
116 | | - else: |
117 | | - env = ENV_DEV |
118 | | - elif not is_string(env) and not isinstance(env, Environment): |
119 | | - raise Exception("Invalid environment: %s" % env) |
120 | | - |
121 | | - if is_string(env): |
122 | | - env = Environment.from_string(env) |
123 | | - if region_name: |
124 | | - env.region = region_name |
125 | | - if not env.region: |
126 | | - raise Exception('Invalid region in environment: "%s"' % env) |
127 | | - return env |
128 | | - |
129 | | - |
130 | | -def is_local_env(env): |
131 | | - return not env or env.region == REGION_LOCAL or env.prefix == ENV_DEV |
132 | | - |
133 | | - |
134 | 53 | # NOTE: This method should not be used as it is not guaranteed to return the correct region |
135 | 54 | # In the near future it will be deprecated and removed |
136 | 55 | def get_region(): |
@@ -211,11 +130,6 @@ def inject_test_credentials_into_env(env): |
211 | 130 | env["AWS_SECRET_ACCESS_KEY"] = "test" |
212 | 131 |
|
213 | 132 |
|
214 | | -# TODO: remove |
215 | | -def inject_region_into_env(env, region): |
216 | | - env["AWS_REGION"] = region |
217 | | - |
218 | | - |
219 | 133 | def extract_region_from_auth_header(headers: Dict[str, str], use_default=True) -> str: |
220 | 134 | auth = headers.get("Authorization") or "" |
221 | 135 | region = re.sub(r".*Credential=[^/]+/[^/]+/([^/]+)/.*", r"\1", auth) |
|
0 commit comments