lib/moped/cluster.rb in moped-2.0.0.beta3 vs lib/moped/cluster.rb in moped-2.0.0.beta4

- old
+ new

@@ -30,22 +30,38 @@ # @return [ Array<Node> ] The node peers. # @!attribute seeds # @return [ Array<Node> ] The seed nodes. attr_reader :options, :peers, :seeds - # Get the credentials for the cluster. + # Add a credential to the cluster # # @example Get the applied credentials. # node.credentials # - # @return [ Hash ] The credentials. + # @return [ Boolean ] true # # @since 2.0.0 - def credentials + def add_credential(db, username, password) @credentials ||= {} + @credentials[db] = [ username, password ] + apply_credentials end + # Remove a credential from the cluster + # + # @example Get the applied credentials. + # node.delete_credential(database_name) + # + # @return [ Boolean ] true + # + # @since 2.0.0 + def delete_credential(db) + return true unless @credentials + @credentials.delete(db) + apply_credentials + end + # Disconnects all nodes in the cluster. This should only be used in cases # where you know you're not going to use the cluster on the thread anymore # and need to force the connections to close. # # @return [ true ] True if the disconnect succeeded. @@ -222,11 +238,11 @@ # @since 1.0.0 def with_primary(&block) if node = nodes.find(&:primary?) begin node.ensure_primary do - return yield(node.apply_credentials(credentials)) + return yield(node) end rescue Errors::ConnectionFailure, Errors::ReplicaSetReconfigured end end raise Errors::ConnectionFailure, "Could not connect to a primary node for replica set #{inspect}" @@ -249,18 +265,35 @@ # @since 1.0.0 def with_secondary(&block) available_nodes = nodes.select(&:secondary?).shuffle! while node = available_nodes.shift begin - return yield(node.apply_credentials(credentials)) + return yield(node) rescue Errors::ConnectionFailure => e next end end raise Errors::ConnectionFailure, "Could not connect to a secondary node for replica set #{inspect}" end private + + # Apply the credentials on all nodes + # + # @api private + # + # @example Apply the credentials. + # cluster.apply_credentials + # + # @return [ Boolean ] True + # + # @since 2.0.0 + def apply_credentials + seeds.each do |node| + node.credentials = @credentials || {} + end + true + end # Get the boundary where a node that is down would need to be refreshed. # # @api private #