|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | + |
| 14 | +from os import getenv |
| 15 | +from typing import Optional |
| 16 | + |
| 17 | +from influxdb_client import InfluxDBClient, Organization |
| 18 | + |
| 19 | +from testcontainers.influxdb import InfluxDbContainer |
| 20 | + |
| 21 | + |
| 22 | +class InfluxDb2Container(InfluxDbContainer): |
| 23 | + """ |
| 24 | + Docker container for InfluxDB 2.x. |
| 25 | + Official Docker images for InfluxDB are hosted at https://hub.docker.com/_/influxdb/. |
| 26 | +
|
| 27 | + Example: |
| 28 | +
|
| 29 | + .. doctest:: |
| 30 | +
|
| 31 | + >>> from testcontainers.influxdb2 import InfluxDb2Container |
| 32 | +
|
| 33 | + >>> with InfluxDb2Container() as influxdb2: |
| 34 | + ... version = influxdb2.get_version() |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + image: str = "influxdb:latest", |
| 40 | + # in the container, the default port for influxdb is often 8086 and not likely to change |
| 41 | + container_port: int = 8086, |
| 42 | + # specifies the port on the host machine where influxdb is exposed; a random available port otherwise |
| 43 | + host_port: Optional[int] = None, |
| 44 | + # parameters used by the InfluxDSB 2.x Docker container when spawned in setup mode |
| 45 | + # (which is likely what you want). In setup mode, init_mode should be "setup" and all |
| 46 | + # the other parameters should be set (via this constructor or their respective |
| 47 | + # environment variables); retention does not need to be explicitely set. |
| 48 | + init_mode: Optional[str] = None, |
| 49 | + admin_token: Optional[str] = None, |
| 50 | + username: Optional[str] = None, |
| 51 | + password: Optional[str] = None, |
| 52 | + org_name: Optional[str] = None, |
| 53 | + bucket: Optional[str] = None, |
| 54 | + retention: Optional[str] = None, |
| 55 | + **docker_client_kw, |
| 56 | + ): |
| 57 | + super().__init__(image, container_port, host_port, **docker_client_kw) |
| 58 | + |
| 59 | + configuration = { |
| 60 | + "DOCKER_INFLUXDB_INIT_MODE": init_mode, |
| 61 | + "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN": admin_token, |
| 62 | + "DOCKER_INFLUXDB_INIT_USERNAME": username, |
| 63 | + "DOCKER_INFLUXDB_INIT_PASSWORD": password, |
| 64 | + "DOCKER_INFLUXDB_INIT_ORG": org_name, |
| 65 | + "DOCKER_INFLUXDB_INIT_BUCKET": bucket, |
| 66 | + "DOCKER_INFLUXDB_INIT_RETENTION": retention, |
| 67 | + } |
| 68 | + for env_key, constructor_param in configuration.items(): |
| 69 | + env_value = constructor_param or getenv(env_key) |
| 70 | + if env_value: |
| 71 | + self.with_env(env_key, env_value) |
| 72 | + |
| 73 | + def start(self) -> "InfluxDb2Container": |
| 74 | + """ |
| 75 | + Overridden for better typing reason |
| 76 | + """ |
| 77 | + return super().start() |
| 78 | + |
| 79 | + def get_client( |
| 80 | + self, token: Optional[str] = None, org_name: Optional[str] = None, **influxdb_client_kwargs |
| 81 | + ) -> tuple[InfluxDBClient, Organization]: |
| 82 | + """ |
| 83 | + Returns an instance of the influxdb client with the associated test organization created |
| 84 | + when the container is spawn; for InfluxDB 2.x versions. |
| 85 | + - https://github.com/influxdata/influxdb-client-python |
| 86 | + - https://pypi.org/project/influxdb-client/ |
| 87 | +
|
| 88 | + This InfluxDB client requires to specify the organization when using most of the API's endpoints, |
| 89 | + as an Organisation instance rather than its name or id (deprecated). As a convenience, this |
| 90 | + client getter can also retrieve and return the organization instance along with the client. |
| 91 | + Otherwise, None is returned in place of the organization instance. |
| 92 | +
|
| 93 | + This organization is created when spawning the container in setup mode (which is likely what you |
| 94 | + want) by giving its name to the 'org_name' parameter constructor. |
| 95 | + """ |
| 96 | + |
| 97 | + influxclient = InfluxDBClient(self.get_url(), token=token, **influxdb_client_kwargs) |
| 98 | + |
| 99 | + if org_name is None: |
| 100 | + return influxclient, None |
| 101 | + |
| 102 | + orgs = influxclient.organizations_api().find_organizations(org=org_name) |
| 103 | + if len(orgs) == 0: |
| 104 | + raise ValueError(f"Could not retrieved the Organization corresponding to name '{org_name}'") |
| 105 | + |
| 106 | + return influxclient, orgs[0] |
0 commit comments