Skip to content

initial Pub/Sub #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gcp/cloud-pub-sub/MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REPO cpud-pub-sub
LOAD pubsub.yml example.yml
RESOURCES pubsub.js
22 changes: 22 additions & 0 deletions gcp/cloud-pub-sub/README.md
Original file line number Diff line number Diff line change
@@ -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 <your-entinty-instance>.yml

# run to trigger a "create" event
monk run <your-workspace>/<entity_name>

To delete it `monk delete`:

monk delete <your-workspace>/<entity_name>

This should remove Entity from Monk and the resource from GCP.
5 changes: 5 additions & 0 deletions gcp/cloud-pub-sub/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace: gcp

my-topic:
defines: gcp/cloud-pub-sub
name: monk-demo-1
147 changes: 147 additions & 0 deletions gcp/cloud-pub-sub/pubsub.js
Original file line number Diff line number Diff line change
@@ -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;
}
29 changes: 29 additions & 0 deletions gcp/cloud-pub-sub/pubsub.yml
Original file line number Diff line number Diff line change
@@ -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: ""