diff --git a/forms/snikket_id.rb b/forms/snikket_delete.rb similarity index 100% rename from forms/snikket_id.rb rename to forms/snikket_delete.rb diff --git a/forms/snikket_stop.rb b/forms/snikket_stop.rb new file mode 100644 index 0000000000000000000000000000000000000000..95b4519e64d72b16200b9b173e0a750b1e7b8762 --- /dev/null +++ b/forms/snikket_stop.rb @@ -0,0 +1,9 @@ +form! +title "Stop Snikket Instance" + +instructions "Stop Snikket instance by id" + +field( + var: "instance_id", + label: "Instance ID" +) diff --git a/lib/snikket.rb b/lib/snikket.rb index 804b8a271f57277731f5841e77b360caf8c79ae4..05cde182906414173a01255855a389742d27fcc8 100644 --- a/lib/snikket.rb +++ b/lib/snikket.rb @@ -173,6 +173,31 @@ module Snikket end end + class Stop < Blather::Stanza::Iq + register nil, "stop", "xmpp:snikket.org/hosting/v1" + + def self.new(type=nil, to=nil, id=nil, instance_id: nil) + stanza = super(type || :set, to, id) + node = Nokogiri::XML::Node.new("stop", stanza.document) + node.default_namespace = registered_ns + stanza << node + stanza.instance_id = instance_id if instance_id + stanza + end + + def instance_id=(instance_id) + query.at_xpath("./ns:instance-id", ns: self.class.registered_ns)&.remove + node = Nokogiri::XML::Node.new("instance-id", document) + node.default_namespace = self.class.registered_ns + node.content = instance_id + query << node + end + + def query + at_xpath("./ns:stop", ns: self.class.registered_ns) + end + end + class Delete < Blather::Stanza::Iq register nil, "delete", "xmpp:snikket.org/hosting/v1" diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 289335f3259833445f2a5eaa9ac31edb3da99f6d..ddcb48ade0e96fcfe0027eac5b6f3b70ccc4728c 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -917,6 +917,29 @@ Command.new( end }.register(self).then(&CommandList.method(:register)) +Command.new( + "stop snikket", + "STOP 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_stop") + }.then { |response| + instance_id = response.form.field("instance_id").value.to_s + IQ_MANAGER.write(Snikket::Stop.new( + nil, CONFIG[:snikket_hosting_api], + instance_id: instance_id + )) + }.then { |iq| + Command.finish(iq.to_s) + } + end +}.register(self).then(&CommandList.method(:register)) + Command.new( "delete snikket", "DELETE Snikket Instance", @@ -927,7 +950,7 @@ Command.new( Command.reply { |reply| reply.allowed_actions = [:next] - reply.command << FormTemplate.render("snikket_id") + reply.command << FormTemplate.render("snikket_delete") }.then { |response| instance_id = response.form.field("instance_id").value.to_s IQ_MANAGER.write(Snikket::Delete.new(