Skip to content

Commit

Permalink
Merge pull request #50 from IBM-Swift/iam
Browse files Browse the repository at this point in the history
Watson IAM service credential migration
  • Loading branch information
Christian Compton authored Jun 21, 2018
2 parents 4b50eee + 664f2e6 commit b37c1c6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The following services are currently supported by this library. Therefore, you c
- [PostgreSQL](https://console.ng.bluemix.net/catalog/services/compose-for-postgresql/)
- [Push SDK](https://console.ng.bluemix.net/catalog/services/push-notifications)
- [Redis](https://console.ng.bluemix.net/catalog/services/redis-cloud)
- [Watson Conversation](https://console.ng.bluemix.net/catalog/services/conversation)
- [Watson Assistant](https://console.ng.bluemix.net/catalog/services/watson-assistant-formerly-conversation)
- [Weather Company Data](https://console.bluemix.net/catalog/services/weather-company-data)

If you don't see listed above the service you intend to use in your Swift application, you can leverage the generic `getDictionary(name: String)` method to get the corresponding credentials:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,34 @@
* limitations under the License.
*/

/// NaturalLangUnderstandingCredentials class
/// NaturalLanguageUnderstandingCredentials class
///
/// Contains the credentials for a Natural Language Understanding service instance.
public class NaturalLangUnderstandingCredentials: Credentials {
// Just a simpler wrapper to provide a type for natural language credentials
public class NaturalLanguageUnderstandingCredentials {

public let apiKey: String
public let url: String

public init(apiKey: String, url: String) {
self.apiKey = apiKey
self.url = url
}
}

extension CloudEnv {

/// Returns a NaturalLangUnderstandingCredentials object with the corresponding credentials.
/// Returns a NaturalLanguageUnderstandingCredentials object with the corresponding credentials.
///
/// - Parameter name: The key to lookup the credentials object.
public func getNaturalLangUnderstandingCredentials(name: String) -> NaturalLangUnderstandingCredentials? {
public func getNaturalLanguageUnderstandingCredentials(name: String) -> NaturalLanguageUnderstandingCredentials? {

guard let credentials = getDictionary(name: name),
let username = credentials["username"] as? String,
let password = credentials["password"] as? String,
let apiKey = credentials["apikey"] as? String,
let url = credentials["url"] as? String else {

return nil
}

return NaturalLangUnderstandingCredentials(
username: username,
password: password,
url: url)
return NaturalLanguageUnderstandingCredentials(apiKey: apiKey, url: url)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@
* limitations under the License.
*/

/// WatsonConversationCredentials class
/// WatsonAssistantCredentials class
///
/// Contains the credentials for a Watson Conversation service instance.
public class WatsonConversationCredentials: Credentials {
// Just a simpler wrapper to provide a type for conversation credentials
/// Contains the credentials for a Watson Assistant service instance.
public class WatsonAssistantCredentials {

public let apiKey: String
public let url: String

public init(apiKey: String, url: String) {
self.apiKey = apiKey
self.url = url
}
}

extension CloudEnv {

/// Returns an WatsonConversationCredentials object with the corresponding credentials.
/// Returns an WatsonAssistantCredentials object with the corresponding credentials.
///
/// - Parameter name: The key to lookup the environment variable.
public func getWatsonConversationCredentials(name: String) -> WatsonConversationCredentials? {
public func getWatsonAssistantCredentials(name: String) -> WatsonAssistantCredentials? {
guard let credentials = getDictionary(name: name),
let username = credentials["username"] as? String,
let password = credentials["password"] as? String,
let url = credentials["url"] as? String else {
let apiKey = credentials["apikey"] as? String,
let url = credentials["url"] as? String else {

return nil
}

return WatsonConversationCredentials(username: username, password: password, url: url)
return WatsonAssistantCredentials(apiKey: apiKey, url: url)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class NaturalLangUnderstandingTests: XCTestCase {
class NaturalLanguageUnderstandingTests: XCTestCase {

static var allTests : [(String, (NaturalLangUnderstandingTests) -> () throws -> Void)] {
static var allTests : [(String, (NaturalLanguageUnderstandingTests) -> () throws -> Void)] {
return [
("testGetCredentials", testGetCredentials),
]
Expand All @@ -31,13 +31,12 @@ class NaturalLangUnderstandingTests: XCTestCase {
// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getNaturalLangUnderstandingCredentials(name: "NLUKey") else {
guard let credentials = cloudEnv.getNaturalLanguageUnderstandingCredentials(name: "NLUKey") else {
XCTFail("Could not load Natural Language Understanding service credentials.")
return
}

XCTAssertEqual(credentials.username, "natural-language-user")
XCTAssertEqual(credentials.password, "natural-language-pwd")
XCTAssertEqual(credentials.apiKey, "natural-language-apikey")
XCTAssertEqual(credentials.url, "https://gateway.watsonplatform.net/natural-language-understanding/api")

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class WatsonConversationTests: XCTestCase {
class WatsonAssistantTests: XCTestCase {

static var allTests : [(String, (WatsonConversationTests) -> () throws -> Void)] {
static var allTests : [(String, (WatsonAssistantTests) -> () throws -> Void)] {
return [
("testGetCredentials", testGetCredentials),
]
Expand All @@ -31,17 +31,12 @@ class WatsonConversationTests: XCTestCase {
// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getWatsonConversationCredentials(name: "ConversationKey") else {
XCTFail("Could not load Watson Conversation service credentials.")
guard let credentials = cloudEnv.getWatsonAssistantCredentials(name: "AssistantKey") else {
XCTFail("Could not load Watson Assistant service credentials.")
return
}

XCTAssertEqual(credentials.username, "conversation-user", "Watson Conversation service username should match.")
XCTAssertEqual(credentials.password, "conversation-pwd", "Watson Conversation service password should match.")
XCTAssertEqual(credentials.url, "https://gateway.watsonplatform.net/conversation/api", "Watson Conversation service url should match.")
XCTAssertEqual(credentials.port, 443, "Watson Conversation service port should match.")
XCTAssertEqual(credentials.host, "gateway.watsonplatform.net", "Watson Conversation service host should match.")
XCTAssertTrue(credentials.secured, "Watson Conversation service url should be secured.")
XCTAssertEqual(credentials.apiKey, "assistant-apikey", "Watson Assistant service username should match.")
XCTAssertEqual(credentials.url, "https://gateway.watsonplatform.net/assistant/api", "Watson Assistant service url should match.")
}

}
8 changes: 3 additions & 5 deletions Tests/CloudEnvironmentTests/resources/config_cf_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@
"conversation": [
{
"credentials": {
"password": "conversation-pwd",
"url": "https://gateway.watsonplatform.net/conversation/api",
"username": "conversation-user"
"apikey": "assistant-apikey",
"url": "https://gateway.watsonplatform.net/assistant/api"
},
"label": "conversation",
"name": "ConversationService",
Expand All @@ -62,9 +61,8 @@
"natural-language-understanding": [
{
"credentials": {
"password": "natural-language-pwd",
"apikey": "natural-language-apikey",
"url": "https://gateway.watsonplatform.net/natural-language-understanding/api",
"username": "natural-language-user"
},
"label": "natural-language-understanding",
"name": "NLUService",
Expand Down
2 changes: 1 addition & 1 deletion Tests/CloudEnvironmentTests/resources/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
]
}
},
"ConversationKey": {
"AssistantKey": {
"credentials": {
"searchPatterns": [
"cloudfoundry:ConversationService",
Expand Down
4 changes: 2 additions & 2 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ XCTMain([
testCase(PostgreSQLTests.allTests),
testCase(PushSDKTests.allTests),
testCase(RedisTests.allTests),
testCase(WatsonConversationTests.allTests),
testCase(NaturalLangUnderstandingTests.allTests),
testCase(WatsonAssistantTests.allTests),
testCase(NaturalLanguageUnderstandingTests.allTests),
testCase(WeatherCompanyDataTests.allTests),
testCase(EnvironmentVariablesTests.allTests),
testCase(LocalFileTests.allTests),
Expand Down

0 comments on commit b37c1c6

Please sign in to comment.