Skip to content

Commit ccaedb3

Browse files
authored
Merge pull request #13 from IBM-Swift/scaling
Added auto-scaling; updated code to work with latest version of Swift-cfenv and Configuration.
2 parents f9cfc8a + ab79cfc commit ccaedb3

5 files changed

Lines changed: 338 additions & 254 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
[![Build Status](https://travis-ci.org/IBM-Swift/CloudConfiguration.svg?branch=master)](https://travis-ci.org/IBM-Swift/CloudConfiguration)
2+
![macOS](https://img.shields.io/badge/os-macOS-green.svg?style=flat)
3+
![Linux](https://img.shields.io/badge/os-linux-green.svg?style=flat)
4+
15
# CloudConfiguration
26

3-
The write once, run anywhere configuration library. Designed to be a platform agnostic way to automatically
7+
The write once, run anywhere configuration library. Designed to be a platform agnostic way to automatically
48
configuring connections to your hosting services.
59

610
## Platforms:
@@ -30,4 +34,3 @@ mongoDBService.hostname
3034
mongoDBService.port
3135

3236
```
33-

Sources/CloudFoundryConfig/Extensions.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ extension ConfigurationManager {
5656
throw ConfigurationManagerError.noServiceWithName(name)
5757
}
5858
}
59-
59+
6060
public func getObjectStorageService(name: String) throws -> ObjectStorageService {
61-
61+
6262
if let service = getService(spec: name),
6363
let objStrService = ObjectStorageService(withService: service) {
6464
return objStrService
6565
} else {
6666
throw ConfigurationManagerError.noServiceWithName(name)
6767
}
6868
}
69-
69+
7070
public func getPostgreSQLService(name: String) throws -> PostgreSQLService {
7171

7272
if let service = getService(spec: name),
@@ -107,4 +107,14 @@ extension ConfigurationManager {
107107
}
108108
}
109109

110+
public func getAutoScalingService(name: String) throws -> AutoScalingService {
111+
112+
if let service = getService(spec: name),
113+
let autoScalingService = AutoScalingService(withService: service) {
114+
return autoScalingService
115+
} else {
116+
throw ConfigurationManagerError.noServiceWithName(name)
117+
}
118+
}
119+
110120
}

Sources/CloudFoundryConfig/Services.swift

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,40 @@ public class CloudantService: Service {
5151
}
5252
}
5353

54+
public class AutoScalingService: Service {
55+
56+
public let username: String
57+
public let password: String
58+
public let appID: String
59+
public let url: String
60+
public let serviceID: String
61+
62+
public init?(withService service: Service) {
63+
64+
guard let credentials = service.credentials,
65+
let url = credentials["url"] as? String,
66+
let username = credentials["agentUsername"] as? String,
67+
let password = credentials["agentPassword"] as? String,
68+
let appID = credentials["app_id"] as? String,
69+
let serviceID = credentials["service_id"] as? String
70+
else {
71+
return nil
72+
}
73+
74+
self.url = url
75+
self.username = username
76+
self.password = password
77+
self.appID = appID
78+
self.serviceID = serviceID
79+
80+
super.init(name: service.name,
81+
label: service.label,
82+
plan: service.plan,
83+
tags: service.tags,
84+
credentials: service.credentials)
85+
}
86+
}
87+
5488
public class AlertNotificationService: Service {
5589

5690
public let url: String
@@ -177,9 +211,9 @@ public class ObjectStorageService: Service {
177211
public let password: String
178212
public let domainID: String
179213
public let domainName: String
180-
214+
181215
public init?(withService service: Service) {
182-
216+
183217
guard let credentials = service.credentials,
184218
let authURL = credentials["auth_url"] as? String,
185219
let project = credentials["project"] as? String,
@@ -191,11 +225,11 @@ public class ObjectStorageService: Service {
191225
let domainID = credentials["domainId"] as? String,
192226
let domainName = credentials["domainName"] as? String
193227
else {
194-
228+
195229
return nil
196-
230+
197231
}
198-
232+
199233
self.authURL = authURL
200234
self.project = project
201235
self.projectID = projectID
@@ -205,15 +239,15 @@ public class ObjectStorageService: Service {
205239
self.password = password
206240
self.domainID = domainID
207241
self.domainName = domainName
208-
242+
209243
super.init(name: service.name,
210244
label: service.label,
211245
plan: service.plan,
212246
tags: service.tags,
213247
credentials: service.credentials)
214-
248+
215249
}
216-
250+
217251
}
218252

219253
// other services
Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
* Copyright IBM Corporation 2017
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
2+
* Copyright IBM Corporation 2017
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
1616

1717
import XCTest
1818
import Foundation
@@ -21,77 +21,82 @@ import Configuration
2121

2222
class FileLoadTests: XCTestCase {
2323

24-
static var allTests : [(String, (FileLoadTests) -> () throws -> Void)] {
25-
return [
26-
("testServiceGetters", testServiceGetters),
27-
]
28-
}
24+
static var allTests : [(String, (FileLoadTests) -> () throws -> Void)] {
25+
return [
26+
("testServiceGetters", testServiceGetters),
27+
]
28+
}
2929

30-
func testServiceGetters() {
30+
func testServiceGetters() {
3131

32-
let manager = ConfigurationManager()
32+
let manager = ConfigurationManager()
3333

34-
do {
35-
// Modify relative path for your system, make more dynamic later
36-
let filePath = URL(fileURLWithPath: #file).appendingPathComponent("../config_example.json").standardized
37-
let loaded = manager.load(url: filePath)
38-
XCTAssertTrue(loaded)
34+
do {
35+
// Modify relative path for your system, make more dynamic later
36+
let filePath = URL(fileURLWithPath: #file).appendingPathComponent("../config_example.json").standardized
37+
manager.load(url: filePath)
3938

40-
let cloudantService = try manager.getCloudantService(name: "CloudantService")
39+
let cloudantService = try manager.getCloudantService(name: "CloudantService")
4140

42-
XCTAssertGreaterThan(cloudantService.host.characters.count, 0)
43-
XCTAssertGreaterThan(cloudantService.username.characters.count, 0)
44-
XCTAssertGreaterThan(cloudantService.password.characters.count, 0)
45-
XCTAssertNotEqual(cloudantService.port, 0)
46-
XCTAssertGreaterThan(cloudantService.url.characters.count, 0)
41+
XCTAssertGreaterThan(cloudantService.host.characters.count, 0)
42+
XCTAssertGreaterThan(cloudantService.username.characters.count, 0)
43+
XCTAssertGreaterThan(cloudantService.password.characters.count, 0)
44+
XCTAssertNotEqual(cloudantService.port, 0)
45+
XCTAssertGreaterThan(cloudantService.url.characters.count, 0)
4746

48-
let mongoDBService = try manager.getMongoDBService(name: "TodoList-MongoDB")
47+
let mongoDBService = try manager.getMongoDBService(name: "TodoList-MongoDB")
4948

50-
XCTAssertGreaterThan(mongoDBService.host.characters.count, 0)
51-
XCTAssertGreaterThan(mongoDBService.username.characters.count, 0)
52-
XCTAssertGreaterThan(mongoDBService.password.characters.count, 0)
53-
XCTAssertNotEqual(mongoDBService.port, 0)
54-
XCTAssertGreaterThan(mongoDBService.certificate.characters.count, 0)
49+
XCTAssertGreaterThan(mongoDBService.host.characters.count, 0)
50+
XCTAssertGreaterThan(mongoDBService.username.characters.count, 0)
51+
XCTAssertGreaterThan(mongoDBService.password.characters.count, 0)
52+
XCTAssertNotEqual(mongoDBService.port, 0)
53+
XCTAssertGreaterThan(mongoDBService.certificate.characters.count, 0)
5554

56-
let redisService = try manager.getRedisService(name: "RedisService")
55+
let redisService = try manager.getRedisService(name: "RedisService")
5756

58-
XCTAssertGreaterThan(redisService.host.characters.count, 0)
59-
XCTAssertGreaterThan(redisService.password.characters.count, 0)
60-
XCTAssertNotEqual(redisService.port, 0)
57+
XCTAssertGreaterThan(redisService.host.characters.count, 0)
58+
XCTAssertGreaterThan(redisService.password.characters.count, 0)
59+
XCTAssertNotEqual(redisService.port, 0)
6160

62-
let postgreSQLService = try manager.getPostgreSQLService(name: "TodoList-PostgreSQL")
61+
let postgreSQLService = try manager.getPostgreSQLService(name: "TodoList-PostgreSQL")
6362

64-
XCTAssertGreaterThan(postgreSQLService.host.characters.count, 0)
65-
XCTAssertGreaterThan(postgreSQLService.username.characters.count, 0)
66-
XCTAssertGreaterThan(postgreSQLService.password.characters.count, 0)
67-
XCTAssertNotEqual(postgreSQLService.port, 0)
63+
XCTAssertGreaterThan(postgreSQLService.host.characters.count, 0)
64+
XCTAssertGreaterThan(postgreSQLService.username.characters.count, 0)
65+
XCTAssertGreaterThan(postgreSQLService.password.characters.count, 0)
66+
XCTAssertNotEqual(postgreSQLService.port, 0)
6867

69-
let mySQLService = try manager.getMySQLService(name: "TodoList-MySQL")
68+
let mySQLService = try manager.getMySQLService(name: "TodoList-MySQL")
7069

71-
XCTAssertGreaterThan(mySQLService.database.characters.count, 0)
72-
XCTAssertGreaterThan(mySQLService.host.characters.count, 0)
73-
XCTAssertGreaterThan(mySQLService.username.characters.count, 0)
74-
XCTAssertGreaterThan(mySQLService.password.characters.count, 0)
75-
XCTAssertNotEqual(mySQLService.port, 0)
70+
XCTAssertGreaterThan(mySQLService.database.characters.count, 0)
71+
XCTAssertGreaterThan(mySQLService.host.characters.count, 0)
72+
XCTAssertGreaterThan(mySQLService.username.characters.count, 0)
73+
XCTAssertGreaterThan(mySQLService.password.characters.count, 0)
74+
XCTAssertNotEqual(mySQLService.port, 0)
7675

77-
let db2Service = try manager.getDB2Service(name: "TodoList-DB2-Analytics")
76+
let db2Service = try manager.getDB2Service(name: "TodoList-DB2-Analytics")
7877

79-
XCTAssertGreaterThan(db2Service.database.characters.count, 0)
80-
XCTAssertGreaterThan(db2Service.host.characters.count, 0)
81-
XCTAssertNotEqual(db2Service.port, 0)
82-
XCTAssertGreaterThan(db2Service.uid.characters.count, 0)
83-
XCTAssertGreaterThan(db2Service.pwd.characters.count, 0)
78+
XCTAssertGreaterThan(db2Service.database.characters.count, 0)
79+
XCTAssertGreaterThan(db2Service.host.characters.count, 0)
80+
XCTAssertNotEqual(db2Service.port, 0)
81+
XCTAssertGreaterThan(db2Service.uid.characters.count, 0)
82+
XCTAssertGreaterThan(db2Service.pwd.characters.count, 0)
8483

85-
let alertNotificationService = try manager.getAlertNotificationService(name: "IBM Alert Notification-xs")
86-
XCTAssertEqual(alertNotificationService.url, "https://ibmnotifybm.mybluemix.net/api/alerts/v1", "Alert Notification Service URL should match.")
87-
XCTAssertEqual(alertNotificationService.id, "21a084f4-4eb3-4de4-9834-33bdc7be5df9/d2a85740-da7a-4615-aabf-5bdc35c63618", "Alert Notification Service ID should match.")
88-
XCTAssertEqual(alertNotificationService.password, "alertnotification-pwd", "Alert Notification Service password should match.")
89-
XCTAssertEqual(alertNotificationService.swaggerUI, "https://ibmnotifybm.mybluemix.net/docs/alerts/v1", "Alert Notification Service swaggerUI should match.")
84+
let alertNotificationService = try manager.getAlertNotificationService(name: "IBM Alert Notification-xs")
85+
XCTAssertEqual(alertNotificationService.url, "https://ibmnotifybm.mybluemix.net/api/alerts/v1", "Alert Notification Service URL should match.")
86+
XCTAssertEqual(alertNotificationService.id, "21a084f4-4eb3-4de4-9834-33bdc7be5df9/d2a85740-da7a-4615-aabf-5bdc35c63618", "Alert Notification Service ID should match.")
87+
XCTAssertEqual(alertNotificationService.password, "alertnotification-pwd", "Alert Notification Service password should match.")
88+
XCTAssertEqual(alertNotificationService.swaggerUI, "https://ibmnotifybm.mybluemix.net/docs/alerts/v1", "Alert Notification Service swaggerUI should match.")
9089

91-
} catch {
92-
XCTFail("Could not load configuration. Error: \(error)")
93-
}
90+
let autoScalingService = try manager.getAutoScalingService(name: "IBM Auto Scaling-xs")
91+
XCTAssertEqual(autoScalingService.url, "https://auto-scaling.ibm.com", "Auto-Scaling Service URL should match.")
92+
XCTAssertEqual(autoScalingService.username, "21a084f4-4eb56-74547-3bdc7be5df9/d2a85740-da7a-4615-aabf-5bdc35c63618", "Auto-Scaling Service username should match.")
93+
XCTAssertEqual(autoScalingService.password, "auto-scaling-pwd", "Auto-Scaling Service password should match.")
94+
XCTAssertEqual(autoScalingService.appID, "auto-scaling-appID", "Auto-Scaling Service appID should match.")
95+
XCTAssertEqual(autoScalingService.serviceID, "auto-scaling-serviceID", "Auto-Scaling Service serviceID should match.")
9496

97+
} catch {
98+
XCTFail("Could not load configuration. Error: \(error)")
9599
}
100+
}
96101

97102
}

0 commit comments

Comments
 (0)