Command to lookup snikket instance

Stephen Paul Weber created

Change summary

forms/snikket_result.rb | 10 ++++++++++
lib/snikket.rb          | 35 +++++++++++++++++++++++++++++++++++
sgx_jmp.rb              | 28 ++++++++++++++++++++++++++++
3 files changed, 73 insertions(+)

Detailed changes

forms/snikket_result.rb 🔗

@@ -0,0 +1,10 @@
+result!
+
+title "Snikket Instance Details"
+
+field(
+	type: "text-single",
+	var: "instance-id",
+	label: "Instance ID",
+	value: @instance.instance_id
+)

lib/snikket.rb 🔗

@@ -198,6 +198,41 @@ module Snikket
 		end
 	end
 
+	class DomainInfo < Blather::Stanza::Iq
+		register nil, "domain-info", "xmpp:snikket.org/hosting/v1"
+
+		def self.new(type=nil, to=nil, id=nil, domain: nil)
+			stanza = super(type || :set, to, id)
+			node = Nokogiri::XML::Node.new("domain-info", stanza.document)
+			node.default_namespace = registered_ns
+			stanza << node
+			stanza.domain = domain if domain
+			stanza
+		end
+
+		def domain=(domain)
+			query.at_xpath("./ns:domain", ns: self.class.registered_ns)&.remove
+			node = Nokogiri::XML::Node.new("domain", document)
+			node.default_namespace = self.class.registered_ns
+			node.content = domain
+			query << node
+		end
+
+		def instance_id
+			query
+				.at_xpath("./ns:instance-id", ns: self.class.registered_ns)
+				&.content
+		end
+
+		def custom?
+			!!query.at_xpath("./ns:custom", ns: self.class.registered_ns)
+		end
+
+		def query
+			at_xpath("./ns:domain-info", ns: self.class.registered_ns)
+		end
+	end
+
 	class CustomerInstance
 		def self.for(customer, domain, launched)
 			new(

sgx_jmp.rb 🔗

@@ -941,6 +941,34 @@ Command.new(
 	end
 }.register(self).then(&CommandList.method(:register))
 
+Command.new(
+	"find snikket",
+	"Lookup Snikket Instance",
+	list_for: ->(customer: nil, **) { customer&.admin? }
+) {
+	Command.customer.then do |customer|
+		raise AuthError, "You are not an admin" unless customer&.admin?
+
+		Command.reply { |reply|
+			reply.allowed_actions = [:next]
+			reply.command << FormTemplate.render("snikket_launch")
+		}.then { |response|
+			domain = response.form.field("domain").value.to_s
+			IQ_MANAGER.write(Snikket::DomainInfo.new(
+				nil, CONFIG[:snikket_hosting_api],
+				domain: domain
+			))
+		}.then { |instance|
+			Command.finish do |reply|
+				reply.command << FormTemplate.render(
+					"snikket_result",
+					instance: instance
+				)
+			end
+		}
+	end
+}.register(self).then(&CommandList.method(:register))
+
 def reply_with_note(iq, text, type: :info)
 	reply = iq.reply
 	reply.status = :completed