diff --git a/forms/snikket_result.rb b/forms/snikket_result.rb new file mode 100644 index 0000000000000000000000000000000000000000..de1ceb0d12d4a993781d2634d93d627e13190a68 --- /dev/null +++ b/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 +) diff --git a/lib/snikket.rb b/lib/snikket.rb index 0896a7bcd8d4f25167eb17631419f53c5251c325..e76eafcfe70e8b424571c24853fce1bbb5cb0afc 100644 --- a/lib/snikket.rb +++ b/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( diff --git a/sgx_jmp.rb b/sgx_jmp.rb index ae6956631b51deacd893f296f444bc0b8963f9d2..366d0546586e6c0bda0ae2f1951924ba121e3a5c 100644 --- a/sgx_jmp.rb +++ b/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