Add command to stop Snikket instance

Amolith created

Change summary

forms/snikket_delete.rb |  0 
forms/snikket_stop.rb   |  9 +++++++++
lib/snikket.rb          | 25 +++++++++++++++++++++++++
sgx_jmp.rb              | 25 ++++++++++++++++++++++++-
4 files changed, 58 insertions(+), 1 deletion(-)

Detailed changes

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"
+)

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"
 

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(