|
130 | 130 | from ansible.plugins.inventory import BaseInventoryPlugin, Constructable |
131 | 131 | from ansible.utils.display import Display |
132 | 132 |
|
133 | | -from ..version import collection_version |
| 133 | +from ..module_utils.client import initialize_upcloud_client |
134 | 134 |
|
135 | 135 | display = Display() |
136 | 136 |
|
137 | 137 | try: |
138 | | - import upcloud_api |
139 | 138 | from upcloud_api.errors import UpCloudAPIError |
140 | 139 | UC_AVAILABLE = True |
141 | 140 | except ImportError: |
@@ -164,37 +163,7 @@ def _initialize_upcloud_client(self): |
164 | 163 | self.token_env |
165 | 164 | ) |
166 | 165 |
|
167 | | - # Token support was added in upcloud-api 2.8.0, older versions will raise TypeError if token is provided. |
168 | | - # Ignore the error if token is not provided, in which case older version should work as well. |
169 | | - try: |
170 | | - credentials = upcloud_api.Credentials.parse( |
171 | | - username=self.username, |
172 | | - password=self.password, |
173 | | - token=self.token, |
174 | | - ) |
175 | | - self.client = upcloud_api.CloudManager(**credentials.dict) |
176 | | - except (AttributeError, TypeError): |
177 | | - try: |
178 | | - self.client = upcloud_api.CloudManager(self.username, self.password) |
179 | | - except Exception: |
180 | | - raise AnsibleError( |
181 | | - 'Invalid or missing UpCloud API credentials. ' |
182 | | - 'The version of upcloud-api you are using does not support token authentication or parsing credentials from the environment. ' |
183 | | - 'Update upcloud-api to version 2.8.0 or later.' |
184 | | - ) from None |
185 | | - |
186 | | - version = collection_version() or "dev" |
187 | | - self.client.api.user_agent = f"upcloud-ansible-inventory/{version}" |
188 | | - |
189 | | - api_root_env = "UPCLOUD_API_ROOT" |
190 | | - if os.getenv(api_root_env): |
191 | | - self.client.api.api_root = os.getenv(api_root_env) |
192 | | - |
193 | | - def _test_upcloud_credentials(self): |
194 | | - try: |
195 | | - self.client.authenticate() |
196 | | - except UpCloudAPIError: |
197 | | - raise AnsibleError("Invalid UpCloud API credentials.") |
| 166 | + self.client = initialize_upcloud_client(self.username, self.password, self.token) |
198 | 167 |
|
199 | 168 | def _fetch_servers(self): |
200 | 169 | return self.client.get_servers() |
@@ -386,7 +355,6 @@ def verify_file(self, path): |
386 | 355 |
|
387 | 356 | def _populate(self): |
388 | 357 | self._initialize_upcloud_client() |
389 | | - self._test_upcloud_credentials() |
390 | 358 | self._get_servers() |
391 | 359 | self._filter_servers() |
392 | 360 |
|
|
0 commit comments