From 2133cbe4a595813f37cc9f615be45c46a167f821 Mon Sep 17 00:00:00 2001 From: Burakhan Date: Sun, 12 Mar 2023 00:15:14 +0300 Subject: [PATCH 1/2] initial --- gcp/cloud-pub-sub/MANIFEST | 3 + gcp/cloud-pub-sub/README.md | 22 +++++ gcp/cloud-pub-sub/example.yml | 5 ++ gcp/cloud-pub-sub/pubsub.js | 147 ++++++++++++++++++++++++++++++++++ gcp/cloud-pub-sub/pubsub.yml | 29 +++++++ 5 files changed, 206 insertions(+) create mode 100644 gcp/cloud-pub-sub/MANIFEST create mode 100644 gcp/cloud-pub-sub/README.md create mode 100644 gcp/cloud-pub-sub/example.yml create mode 100644 gcp/cloud-pub-sub/pubsub.js create mode 100644 gcp/cloud-pub-sub/pubsub.yml diff --git a/gcp/cloud-pub-sub/MANIFEST b/gcp/cloud-pub-sub/MANIFEST new file mode 100644 index 0000000..209fbc0 --- /dev/null +++ b/gcp/cloud-pub-sub/MANIFEST @@ -0,0 +1,3 @@ +REPO cşpud-pub-sub +LOAD pubsub.yml example.yml +RESOURCES pubsub.js \ No newline at end of file diff --git a/gcp/cloud-pub-sub/README.md b/gcp/cloud-pub-sub/README.md new file mode 100644 index 0000000..02bea17 --- /dev/null +++ b/gcp/cloud-pub-sub/README.md @@ -0,0 +1,22 @@ +# Cloud Pub Sub + +Entity to manage RDS Instance. +It will allow us to create new RDS Instance. + +## Usage + +We'll use Monk CLI to load and create our pub sub resource. +Copy `example.yaml` and update the vars according to your needs. +Full schema available here: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics#Topic + + # load templates + monk load pubsub.yml .yml + + # run to trigger a "create" event + monk run / + +To delete it `monk delete`: + + monk delete / + +This should remove Entity from Monk and the resource from GCP. \ No newline at end of file diff --git a/gcp/cloud-pub-sub/example.yml b/gcp/cloud-pub-sub/example.yml new file mode 100644 index 0000000..74ff671 --- /dev/null +++ b/gcp/cloud-pub-sub/example.yml @@ -0,0 +1,5 @@ +namespace: gcp + +my-topic: + defines: gcp/cloud-pub-sub + name: monk-demo-1 \ No newline at end of file diff --git a/gcp/cloud-pub-sub/pubsub.js b/gcp/cloud-pub-sub/pubsub.js new file mode 100644 index 0000000..8045784 --- /dev/null +++ b/gcp/cloud-pub-sub/pubsub.js @@ -0,0 +1,147 @@ +let gcp = require("cloud/gcp"); +let cli = require("cli"); +let http = require("http"); + +let createSubscription = function (def) { + cli.output("createSubscription"); + let data = { + "name": "sub-" + def["name"], + "topic": "projects/" + gcp.getProject() + "/topics/" + def["name"] + }; + let res = gcp.put("https://pubsub.googleapis.com/v1/projects/" + gcp.getProject() + "/subscriptions/" + data.name, { + "body": JSON.stringify(data), + "headers": { + "Content-Type": "application/json" + } + }); + + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + cli.output(JSON.stringify(res.body)); + return JSON.parse(res.body) +} + +let deleteSubscription = function (def) { + cli.output("deleteSubscription"); + let data = { + "name": "sub-" + def["name"], + }; + let res = gcp.delete("https://pubsub.googleapis.com/v1/projects/" + gcp.getProject() + "/subscriptions/" + data.name, { + "headers": { + "Content-Type": "application/json" + } + }); + + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + return JSON.parse(res.body) +} + +let getSubscription = function (def) { + let data = { + "name": "sub-" + def["name"], + }; + let res = gcp.get("https://pubsub.googleapis.com/v1/projects/" + gcp.getProject() + "/subscriptions/" + data.name, { + "headers": { + "Content-Type": "application/json" + } + }); + + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + cli.output(JSON.stringify(res.body)); + return JSON.parse(res.body) +} + + + +let createTopic = function (def) { + cli.output("createTopic"); + let data = { + "name": def["name"] + }; + let res = gcp.put("https://pubsub.googleapis.com/v1/projects/" + gcp.getProject() + "/topics/" + def["name"], { + "body": JSON.stringify(data), + "headers": { + "Content-Type": "application/json" + }, + "timeout": 60 + }); + + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + return JSON.parse(res.body) + +} + + +let deleteTopic = function (def) { + let res = gcp.delete("https://pubsub.googleapis.com/v1/projects/" + gcp.getProject() + "/topics/" + def["name"]); + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + return res +} + +let getTopic = function (def) { + let res = gcp.get("https://pubsub.googleapis.com/v1/projects/" + gcp.getProject() + "/topics/" + def["name"]); + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + return JSON.parse(res.body) + +} + +function main(def, state, ctx) { + cli.output(ctx.action); + let res = {}; + let resSubs = {}; + cli.output(JSON.stringify(state)); + switch (ctx.action) { + case "recreate": + case "create": { + createTopic(def); + state["topic_name"] = res["name"]; + break; + } + case "purge": + if (state["subscription_name"]) { + deleteSubscription(def); + } + res = deleteTopic(def); + state["status"] = "deleted"; + break; + case "get": { + res = getTopic(def); + state["topic_name"] = res["name"]; + break; + } + case "create-subscription": { + resSubs = createSubscription(def); + state["status"] = "create-subscription"; + state["subscription_name"] = resSubs["name"]; + break; + } + case "delete-subscription": { + resSubs = deleteSubscription(def); + delete state["subscription_name"]; + break; + } + case "get-subscription": { + resSubs = getSubscription(def); + state["subscription_name"] = resSubs["name"];; + break; + } + default: + // no action defined + return; + } + if (res.error) { + throw new Error(res.error + ", body " + res.body); + } + return state; +} \ No newline at end of file diff --git a/gcp/cloud-pub-sub/pubsub.yml b/gcp/cloud-pub-sub/pubsub.yml new file mode 100644 index 0000000..9d49af1 --- /dev/null +++ b/gcp/cloud-pub-sub/pubsub.yml @@ -0,0 +1,29 @@ +namespace: gcp + +cloud-pub-sub: + defines: entity + schema: + required: [ "name" ] + name: + type: string + settings: + enconding: + type: string + first-revision-id: + type: string + last-revision-id: + type: string + message-retention-duration: + type: string + retention-acknowledgement-period: + type: string + requires: + - cloud/gcp + lifecycle: + sync: <<< pubsub.js + update: "" + recreate: "" + get: "" + create-subscription: "" + delete-subscription: "" + get-subscription: "" \ No newline at end of file From 5f13f64e96d0efb12c030fae0203ac87bbae5ee2 Mon Sep 17 00:00:00 2001 From: Burakhan Date: Sun, 19 Mar 2023 02:47:53 +0300 Subject: [PATCH 2/2] fixed typo --- gcp/cloud-pub-sub/MANIFEST | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcp/cloud-pub-sub/MANIFEST b/gcp/cloud-pub-sub/MANIFEST index 209fbc0..43fe3a4 100644 --- a/gcp/cloud-pub-sub/MANIFEST +++ b/gcp/cloud-pub-sub/MANIFEST @@ -1,3 +1,3 @@ -REPO cşpud-pub-sub +REPO cpud-pub-sub LOAD pubsub.yml example.yml RESOURCES pubsub.js \ No newline at end of file