1
+ namespace Testcontainers . InfluxDb ;
2
+
3
+ /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
4
+ [ PublicAPI ]
5
+ public sealed class InfluxDbBuilder : ContainerBuilder < InfluxDbBuilder , InfluxDbContainer , InfluxDbConfiguration >
6
+ {
7
+ public const string InfluxDbImage = "influxdb:2.7" ;
8
+
9
+ public const ushort InfluxDbPort = 8086 ;
10
+
11
+ public const string DefaultUsername = "username" ;
12
+
13
+ public const string DefaultPassword = "password" ;
14
+
15
+ public const string DefaultOrganization = "organization" ;
16
+
17
+ public const string DefaultBucket = "bucket" ;
18
+
19
+ /// <summary>
20
+ /// Initializes a new instance of the <see cref="InfluxDbBuilder" /> class.
21
+ /// </summary>
22
+ public InfluxDbBuilder ( )
23
+ : this ( new InfluxDbConfiguration ( ) )
24
+ {
25
+ DockerResourceConfiguration = Init ( ) . DockerResourceConfiguration ;
26
+ }
27
+
28
+ /// <summary>
29
+ /// Initializes a new instance of the <see cref="InfluxDbBuilder" /> class.
30
+ /// </summary>
31
+ /// <param name="resourceConfiguration">The Docker resource configuration.</param>
32
+ private InfluxDbBuilder ( InfluxDbConfiguration resourceConfiguration )
33
+ : base ( resourceConfiguration )
34
+ {
35
+ DockerResourceConfiguration = resourceConfiguration ;
36
+ }
37
+
38
+ /// <inheritdoc />
39
+ protected override InfluxDbConfiguration DockerResourceConfiguration { get ; }
40
+
41
+ /// <summary>
42
+ /// Sets the InfluxDb username.
43
+ /// </summary>
44
+ /// <param name="username">The InfluxDb username.</param>
45
+ /// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
46
+ public InfluxDbBuilder WithUsername ( string username )
47
+ {
48
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( username : username ) )
49
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_USERNAME" , username ) ;
50
+ }
51
+
52
+ /// <summary>
53
+ /// Sets the InfluxDb password.
54
+ /// </summary>
55
+ /// <param name="password">The InfluxDb password.</param>
56
+ /// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
57
+ public InfluxDbBuilder WithPassword ( string password )
58
+ {
59
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( password : password ) )
60
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_PASSWORD" , password ) ;
61
+ }
62
+
63
+ /// <summary>
64
+ /// Sets the InfluxDb organization.
65
+ /// </summary>
66
+ /// <param name="organization">The InfluxDb organization.</param>
67
+ /// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
68
+ public InfluxDbBuilder WithOrganization ( string organization )
69
+ {
70
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( organization : organization ) )
71
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_ORG" , organization ) ;
72
+ }
73
+
74
+ /// <summary>
75
+ /// Sets the InfluxDb bucket.
76
+ /// </summary>
77
+ /// <param name="bucket">The InfluxDb bucket.</param>
78
+ /// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
79
+ public InfluxDbBuilder WithBucket ( string bucket )
80
+ {
81
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( bucket : bucket ) )
82
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_BUCKET" , bucket ) ;
83
+ }
84
+
85
+ /// <summary>
86
+ /// Sets the InfluxDb admin token.
87
+ /// </summary>
88
+ /// <param name="adminToken">The InfluxDb admin token.</param>
89
+ /// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
90
+ public InfluxDbBuilder WithAdminToken ( string adminToken )
91
+ {
92
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( adminToken : adminToken ) )
93
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_ADMIN_TOKEN" , adminToken ) ;
94
+ }
95
+
96
+ /// <summary>
97
+ /// Sets the InfluxDb retention.
98
+ /// </summary>
99
+ /// <param name="retention">The InfluxDb retention.</param>
100
+ /// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
101
+ public InfluxDbBuilder WithRetention ( string retention )
102
+ {
103
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( retention : retention ) )
104
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_RETENTION" , retention ) ;
105
+ }
106
+
107
+ /// <inheritdoc />
108
+ public override InfluxDbContainer Build ( )
109
+ {
110
+ Validate ( ) ;
111
+ return new InfluxDbContainer ( DockerResourceConfiguration , TestcontainersSettings . Logger ) ;
112
+ }
113
+
114
+ /// <inheritdoc />
115
+ protected override InfluxDbBuilder Init ( )
116
+ {
117
+ return base . Init ( )
118
+ . WithImage ( InfluxDbImage )
119
+ . WithPortBinding ( InfluxDbPort , true )
120
+ . WithEnvironment ( "DOCKER_INFLUXDB_INIT_MODE" , "setup" )
121
+ . WithUsername ( DefaultUsername )
122
+ . WithPassword ( DefaultPassword )
123
+ . WithOrganization ( DefaultOrganization )
124
+ . WithBucket ( DefaultBucket )
125
+ . WithWaitStrategy ( Wait . ForUnixContainer ( ) . UntilHttpRequestIsSucceeded ( request =>
126
+ request . ForPort ( InfluxDbPort ) . ForPath ( "/ping" ) . ForStatusCode ( HttpStatusCode . NoContent ) ) ) ;
127
+ }
128
+
129
+ /// <inheritdoc />
130
+ protected override void Validate ( )
131
+ {
132
+ base . Validate ( ) ;
133
+
134
+ _ = Guard . Argument ( DockerResourceConfiguration . Username , nameof ( DockerResourceConfiguration . Username ) )
135
+ . NotNull ( )
136
+ . NotEmpty ( ) ;
137
+
138
+ _ = Guard . Argument ( DockerResourceConfiguration . Password , nameof ( DockerResourceConfiguration . Password ) )
139
+ . NotNull ( )
140
+ . NotEmpty ( ) ;
141
+
142
+ _ = Guard . Argument ( DockerResourceConfiguration . Organization , nameof ( DockerResourceConfiguration . Organization ) )
143
+ . NotNull ( )
144
+ . NotEmpty ( ) ;
145
+
146
+ _ = Guard . Argument ( DockerResourceConfiguration . Bucket , nameof ( DockerResourceConfiguration . Bucket ) )
147
+ . NotNull ( )
148
+ . NotEmpty ( ) ;
149
+ }
150
+
151
+ /// <inheritdoc />
152
+ protected override InfluxDbBuilder Clone ( IResourceConfiguration < CreateContainerParameters > resourceConfiguration )
153
+ {
154
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( resourceConfiguration ) ) ;
155
+ }
156
+
157
+ /// <inheritdoc />
158
+ protected override InfluxDbBuilder Clone ( IContainerConfiguration resourceConfiguration )
159
+ {
160
+ return Merge ( DockerResourceConfiguration , new InfluxDbConfiguration ( resourceConfiguration ) ) ;
161
+ }
162
+
163
+ /// <inheritdoc />
164
+ protected override InfluxDbBuilder Merge ( InfluxDbConfiguration oldValue , InfluxDbConfiguration newValue )
165
+ {
166
+ return new InfluxDbBuilder ( new InfluxDbConfiguration ( oldValue , newValue ) ) ;
167
+ }
168
+ }
0 commit comments