Skip to content

Commit

Permalink
Improve key pair management.
Browse files Browse the repository at this point in the history
  • Loading branch information
RockfordWei committed Aug 2, 2017
1 parent b185db5 commit c137106
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Sources/Aliyun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public class AcsKeyPair: PerfectLib.JSONConvertible, CustomStringConvertible, Eq
}

public func getJSONValues() -> [String: Any] {
return ["KeyPairName": name, "KeyPairFingerPrint": fingerPrint, "PrivateKeyBody": key]
var temp = ["KeyPairName": name, "KeyPairFingerPrint": fingerPrint, "PrivateKeyBody": key]
return temp.excludingNullStrings()
}

public func jsonEncodedString() throws -> String {
Expand Down Expand Up @@ -545,6 +546,24 @@ public class ECS: AcsRequest {
}
}

public func describeKeyPairs(region: String, _ completion: @escaping ([AcsKeyPair], String ) -> Void ) {
self.parameters = ["PageSize":"50"]
self.perform(product: self.product, action: "DescribeKeyPairs", regionId: region) {
json, msg in
if let a = json["KeyPairs"] as? [String: Any],
let b = a["KeyPair"] as? [Any] {
let kp = b.map { i -> AcsKeyPair in
let k = AcsKeyPair()
k.setJSONValues(i as? [String: Any] ?? [:])
return k
}
completion(kp, "")
} else {
completion([], msg)
}
}
}

public func describeSecurityGroups(region: String, _ completion: @escaping ([SecurityGroup], String)->()) {
self.parameters = ["PageSize":"50"]
self.perform(product: self.product, action: "DescribeSecurityGroups", regionId: region) {
Expand Down Expand Up @@ -687,8 +706,3 @@ public class ECS: AcsRequest {
}
}
}





7 changes: 7 additions & 0 deletions Tests/AliyunTests/AliyunTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ class AliyunTests: XCTestCase {
}
}
let ecs = ECS(access: access)
Sync().wait { sync in
ecs.describeKeyPairs(region: REGION) { keyPairs, msg in
print("============================== KEY PAIRS ========")
print(keyPairs)
sync.done()
}
}
Sync().wait { sync in
ecs.deleteKeyPairs(region: REGION, keyNames: keys) { suc, msg in
XCTAssertTrue(suc)
Expand Down

0 comments on commit c137106

Please sign in to comment.