Skip to content

Commit 8611354

Browse files
authored
Merge pull request #5 from rolivieri/master
Fixed test cases; added new Service (AlertNotification) and test cases; added docker-compose file
2 parents 100feed + f9afe69 commit 8611354

5 files changed

Lines changed: 149 additions & 82 deletions

File tree

Sources/CloudFoundryConfig/Extensions.swift

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,40 @@ public enum ConfigurationManagerError: Error {
2929
}
3030

3131
extension ConfigurationManager {
32-
32+
3333
private func findService(name: String) throws -> Service? {
34-
34+
3535
let appEnv = try CloudFoundryEnv.getAppEnv(configManager: self)
3636
return appEnv.getService(spec: name)
37-
37+
3838
}
39-
39+
4040
public func getCloudantService(name: String) throws -> CloudantService {
41-
41+
4242
if let service = try findService(name: name),
4343
let cloudantService = CloudantService(withService: service) {
4444
return cloudantService
4545
} else {
4646
throw ConfigurationManagerError.noServiceWithName(name)
4747
}
48-
49-
48+
49+
5050
}
51-
51+
5252
public func getMongoDBService(name: String) throws -> MongoDBService {
53-
53+
5454
if let service = try findService(name: name),
5555
let mongoDBService = MongoDBService(withService: service) {
5656
return mongoDBService
5757
} else {
5858
throw ConfigurationManagerError.noServiceWithName(name)
5959
}
60-
61-
60+
61+
6262
}
63-
63+
6464
public func getRedisService(name: String) throws -> RedisService {
65-
65+
6666
if let service = try findService(name: name),
6767
let redisService = RedisService(withService: service) {
6868
return redisService
@@ -71,66 +71,77 @@ extension ConfigurationManager {
7171
}
7272

7373
}
74-
74+
7575
public func getPostgreSQLService(name: String) throws -> PostgreSQLService {
76-
76+
7777
if let service = try findService(name: name),
7878
let postgreSQLService = PostgreSQLService(withService: service) {
7979
return postgreSQLService
8080
} else {
8181
throw ConfigurationManagerError.noServiceWithName(name)
8282
}
83-
83+
8484
}
85-
85+
8686
public func getMySQLService(name: String) throws -> MySQLService {
87-
87+
8888
if let service = try findService(name: name),
8989
let mySQLService = MySQLService(withService: service) {
9090
return mySQLService
9191
} else {
9292
throw ConfigurationManagerError.noServiceWithName(name)
9393
}
94-
94+
9595
}
96-
96+
9797
public func getDB2Service(name: String) throws -> DB2Service {
98-
98+
9999
if let service = try findService(name: name),
100100
let myDB2Service = DB2Service(withService: service) {
101101
return myDB2Service
102102
} else {
103103
throw ConfigurationManagerError.noServiceWithName(name)
104104
}
105-
105+
106+
}
107+
108+
public func getAlertNotificationService(name: String) throws -> AlertNotificationService {
109+
110+
if let service = try findService(name: name),
111+
let alertNotificationService = AlertNotificationService(withService: service) {
112+
return alertNotificationService
113+
} else {
114+
throw ConfigurationManagerError.noServiceWithName(name)
115+
}
116+
106117
}
107-
118+
108119
public var applicationPort: Int {
109-
120+
110121
if let port = try? CloudFoundryEnv.getAppEnv().port {
111122
return port
112123
} else {
113-
return 8090
124+
return 8080
114125
}
115-
126+
116127
}
117128
}
118129

119130

120131
extension CloudFoundryEnv {
121-
132+
122133
public static func getAppEnv(configManager: ConfigurationManager) throws -> AppEnv {
123-
134+
124135
let servs = configManager["VCAP_SERVICES"]
125136

126137
let app = configManager["VCAP_APPLICATION"]
127138
var vcap: [String:Any] = [:]
128-
139+
129140
vcap["application"] = app
130141
vcap["services"] = servs
131142
var config: [String:Any] = [:]
132143
config["vcap"] = vcap
133144
return try AppEnv(options: config)
134145
}
135-
146+
136147
}

Sources/CloudFoundryConfig/Services.swift

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
import Foundation
1918

2019
import CloudFoundryEnv
2120

2221
public class CloudantService: Service {
23-
22+
2423
public let host : String
2524
public let username : String
2625
public let password : String
2726
public let port : Int
2827
public let url : String
29-
28+
3029
public init?(withService service: Service) {
3130

3231
guard let credentials = service.credentials,
@@ -37,13 +36,13 @@ public class CloudantService: Service {
3736
let url = credentials["url"] as? String else {
3837
return nil
3938
}
40-
39+
4140
self.host = host
4241
self.username = username
4342
self.password = password
4443
self.port = port
4544
self.url = url
46-
45+
4746
super.init(name: service.name,
4847
label: service.label,
4948
plan: service.plan,
@@ -53,21 +52,51 @@ public class CloudantService: Service {
5352
}
5453
}
5554

55+
public class AlertNotificationService: Service {
56+
57+
public let url: String
58+
public let id: String
59+
public let password: String
60+
public let swaggerUI: String
61+
62+
public init?(withService service: Service) {
63+
64+
guard let credentials = service.credentials,
65+
let url = credentials["url"] as? String,
66+
let id = credentials["name"] as? String,
67+
let swaggerUI = credentials["swaggerui"] as? String,
68+
let password = credentials["password"] as? String else {
69+
return nil
70+
}
71+
72+
self.url = url
73+
self.id = id
74+
self.password = password
75+
self.swaggerUI = swaggerUI
76+
77+
super.init(name: service.name,
78+
label: service.label,
79+
plan: service.plan,
80+
tags: service.tags,
81+
credentials: service.credentials)
82+
}
83+
}
84+
5685
public class MongoDBService: Service {
57-
86+
5887
public let host : String
5988
public let username : String
6089
public let password : String
6190
public let port : Int
6291
public let certificate : String
63-
92+
6493
public init?(withService service: Service) {
65-
94+
6695
guard let credentials = service.credentials else {
6796
print("Service credentials were nil.")
6897
return nil
6998
}
70-
99+
71100
// Use SSL uri if available
72101
let initialURI = credentials["uri"] as? String ?? ""
73102
let uris = initialURI.components(separatedBy: ",")
@@ -82,7 +111,7 @@ public class MongoDBService: Service {
82111
} else {
83112
uriValue = uris.first
84113
}
85-
114+
86115
guard let stringURL = uriValue, stringURL.characters.count > 0,
87116
let url = URL(string: stringURL),
88117
let host = url.host,
@@ -92,13 +121,13 @@ public class MongoDBService: Service {
92121
let certificate = credentials["ca_certificate_base64"] as? String else {
93122
return nil
94123
}
95-
124+
96125
self.host = host
97126
self.username = username
98127
self.password = password
99128
self.port = port
100129
self.certificate = certificate
101-
130+
102131
super.init(name: service.name,
103132
label: service.label,
104133
plan: service.plan,
@@ -110,13 +139,13 @@ public class MongoDBService: Service {
110139

111140

112141
public class RedisService: Service {
113-
142+
114143
public let host : String
115144
public let password : String
116145
public let port : Int
117146

118147
public init?(withService service: Service) {
119-
148+
120149
guard let credentials = service.credentials,
121150
let uri = credentials["uri"] as? String,
122151
let url = URL(string: uri),
@@ -129,7 +158,7 @@ public class RedisService: Service {
129158
self.host = host
130159
self.password = password
131160
self.port = port
132-
161+
133162
super.init(name: service.name,
134163
label: service.label,
135164
plan: service.plan,
@@ -139,14 +168,14 @@ public class RedisService: Service {
139168
}
140169

141170
public class PostgreSQLService: Service {
142-
171+
143172
public let host : String
144173
public let port : Int
145174
public let username : String
146175
public let password : String
147-
176+
148177
public init?(withService service: Service) {
149-
178+
150179
guard let credentials = service.credentials,
151180
let uri = credentials["uri"] as? String,
152181
let url = URL(string: uri),
@@ -156,12 +185,12 @@ public class PostgreSQLService: Service {
156185
let password = url.password else {
157186
return nil
158187
}
159-
188+
160189
self.host = host
161190
self.port = port
162191
self.username = username
163192
self.password = password
164-
193+
165194
super.init(name: service.name,
166195
label: service.label,
167196
plan: service.plan,
@@ -172,13 +201,13 @@ public class PostgreSQLService: Service {
172201
}
173202

174203
public class MySQLService: Service {
175-
204+
176205
public let database : String
177206
public let host : String
178207
public let username : String
179208
public let password : String
180209
public let port : Int
181-
210+
182211
public init?(withService service: Service) {
183212

184213
guard let credentials = service.credentials,
@@ -196,26 +225,26 @@ public class MySQLService: Service {
196225
self.username = username
197226
self.password = password
198227
self.port = port
199-
228+
200229
super.init(name: service.name,
201230
label: service.label,
202231
plan: service.plan,
203232
tags: service.tags,
204233
credentials: service.credentials)
205234
}
206-
235+
207236
}
208237

209238
public class DB2Service: Service {
210-
239+
211240
public let database : String
212241
public let host : String
213242
public let port : Int
214243
public let uid : String
215244
public let pwd : String
216-
245+
217246
public init?(withService service: Service) {
218-
247+
219248
guard let credentials = service.credentials,
220249
let database = credentials["db"] as? String,
221250
let host = credentials["host"] as? String,
@@ -224,19 +253,19 @@ public class DB2Service: Service {
224253
let pwd = credentials["password"] as? String else {
225254
return nil
226255
}
227-
256+
228257
self.database = database
229258
self.host = host
230259
self.port = port
231260
self.uid = uid
232261
self.pwd = pwd
233-
262+
234263
super.init(name: service.name,
235264
label: service.label,
236265
plan: service.plan,
237266
tags: service.tags,
238267
credentials: service.credentials)
239-
268+
240269
}
241-
270+
242271
}

0 commit comments

Comments
 (0)