1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package fr.paris.lutece.plugins.vault.business;
36
37 import com.bettercloud.vault.VaultException;
38 import fr.paris.lutece.plugins.vault.rs.VaultAPI;
39 import fr.paris.lutece.plugins.vault.service.EnvironnementUtil;
40 import fr.paris.lutece.plugins.vault.service.VaultService;
41 import fr.paris.lutece.plugins.vault.service.VaultUtil;
42 import fr.paris.lutece.portal.service.i18n.I18nService;
43 import fr.paris.lutece.portal.service.plugin.Plugin;
44 import fr.paris.lutece.portal.service.plugin.PluginService;
45 import fr.paris.lutece.portal.service.spring.SpringContextService;
46 import fr.paris.lutece.util.ReferenceList;
47
48 import java.util.List;
49 import java.util.Optional;
50
51
52
53
54 public final class EnvironnementHome
55 {
56
57 private static IEnvironnementDAO _dao = SpringContextService.getBean( "vault.environnementDAO" );
58 private static Plugin _plugin = PluginService.getPlugin( "vault" );
59
60 private static Integer _count = 1;
61
62 private EnvironnementHome( )
63 {
64
65 }
66
67
68
69
70
71
72
73
74 public static Environnement/../../../../fr/paris/lutece/plugins/vault/business/Environnement.html#Environnement">Environnement create( Environnement environnement )
75 {
76 List<Environnement> listEnv = EnvironnementHome.getEnvironnementListByType( environnement.getType( ) );
77 environnement.setCode( environnement.getType( ) + listEnv.size( ) );
78 _dao.insert( environnement, _plugin );
79
80 return environnement;
81 }
82
83
84
85
86
87
88
89
90 public static Environnement/../../../../fr/paris/lutece/plugins/vault/business/Environnement.html#Environnement">Environnement update( Environnement environnement, String strOldCode, String strOldToken) throws VaultException {
91 Application app = ApplicationHome.findByPrimaryKey( environnement.getIdapplication( ) ).get( );
92 environnement.setPath(EnvironnementUtil.getEnvironmentPath(app.getCode(),environnement.getCode()));
93 _dao.store( environnement, _plugin );
94 VaultService.getInstance().renameEnvironnement(app,environnement,strOldCode,strOldToken);
95 return environnement;
96 }
97
98
99
100
101
102
103
104 public static void remove( int nKey )
105 {
106 _dao.delete( nKey, _plugin );
107 }
108
109
110
111
112
113
114
115
116 public static Optional<Environnement> findByPrimaryKey( int nKey )
117 {
118 Optional<Environnement> env = _dao.load( nKey, _plugin );
119 Application app = ApplicationHome.findByPrimaryKey( env.get( ).getIdapplication( ) ).get( );
120 if ( env.isPresent( ) )
121 {
122 env.get( ).setType( env.get( ).getCode( ) );
123 env.get( ).setToken( VaultService.getInstance( ).getEnvAccessor( env.get( ).getId( ) ) );
124 env.get( ).setPath( EnvironnementUtil.getEnvironmentPath( app.getCode( ), env.get( ).getCode( ) ) );
125 env.get( ).setListProperties( VaultService.getInstance( ).getSecretsByEnv( app, env.get( ) ) );
126 }
127 return env;
128 }
129
130
131
132
133
134
135 public static List<Environnement> getEnvironnementsList( )
136 {
137 List<Environnement> envs = _dao.selectEnvironnementsList( _plugin );
138 if ( !envs.isEmpty( ) )
139 {
140 envs.forEach( x -> x.setToken( VaultService.getInstance( ).getEnvAccessor( x.getId( ) ) ) );
141 envs.forEach( x -> x.setPath(
142 EnvironnementUtil.getEnvironmentPath( ApplicationHome.findByPrimaryKey( x.getIdapplication( ) ).get( ).getCode( ), x.getCode( ) ) ) );
143 envs.forEach( x -> x
144 .setListProperties( VaultService.getInstance( ).getSecretsByEnv( ApplicationHome.findByPrimaryKey( x.getIdapplication( ) ).get( ), x ) ) );
145
146 }
147 return envs;
148 }
149
150
151
152
153
154
155 public static List<Integer> getIdEnvironnementsList( )
156 {
157 return _dao.selectIdEnvironnementsList( _plugin );
158 }
159
160
161
162
163
164
165 public static ReferenceList getEnvironnementsReferenceList( )
166 {
167 return _dao.selectEnvironnementsReferenceList( _plugin );
168 }
169
170
171
172
173
174
175
176
177 public static List<Environnement> getEnvironnementsListByIds( List<Integer> listIds )
178 {
179 List<Environnement> listEnvs = _dao.selectEnvironnementsListByIds( _plugin, listIds );
180 if ( !listEnvs.isEmpty( ) )
181 {
182 listEnvs.forEach( x -> {
183 x.setPath( EnvironnementUtil.getEnvironmentPath( ApplicationHome.findByPrimaryKey( x.getIdapplication( ) ).get( ).getCode( ), x.getCode( ) ) );
184 } );
185 }
186 return listEnvs;
187 }
188
189
190
191
192
193
194
195
196 public static List<Integer> getIdEnvironnementsListByApp( Integer idApp )
197 {
198 return _dao.selectIdEnvironnementByIdApp( idApp, _plugin );
199 }
200
201
202
203
204
205
206
207
208 public static List<Properties> getPropertiesList( Integer nIdEnv )
209 {
210 Environnement env = EnvironnementHome.findByPrimaryKey( nIdEnv ).get( );
211 Application app = ApplicationHome.findByPrimaryKey( env.getIdapplication( ) ).get( );
212 return VaultService.getInstance( ).getSecretsByEnv( app, env );
213 }
214
215
216
217
218
219
220
221
222 public static List<Environnement> getEnvironnementListByType( String type )
223 {
224 return _dao.selectEnvironnementByType( type, _plugin );
225 }
226
227 }