How to update existing JSON with a smaller subset JSON in Ansible? - json

I have this task:
- name: Get Status page url
uri:
url: "{{ responseversion.json.results[0]._links.self }}?expand=version,body.storage"
method: GET
return_content: yes
body_format: json
force_basic_auth: yes
headers:
Authorization: Basic blablab
register: responseurl
- name: print body without updates
debug: var=responseurl
- debug:
msg: "{{ responseurl | combine({'json':{'body':{ 'version':{'minorEdit':true, 'number':responseversion.json.results[0].version.number+1} }}}) }}"
Basically I get a response, get the page "version" and "minorEdit" keys from response and replace them and their values in the original response. There are other keys besides "version", I need to keep them also, as well as the neighboring keys inside "version" key.
The targeted "version" key before update looks like this:
"version": {
"_expandable": {
"content": "/rest/api/content/164794022"
},
"_links": {
"self": "https://hostname.com/confluence/rest/experimental/content/164794022/version/4"
},
"by": {
"_expandable": {
"status": ""
},
"_links": {
"self": "https://hostname.com/confluence/rest/api/user?key=ff80808164c5cf240164c6e351d30002"
},
"displayName": "my name",
"profilePicture": {
"height": 48,
"isDefault": false,
"path": "/confluence/download/attachments/98107443/user-avatar",
"width": 48
},
"type": "known",
"userKey": "ff80808164c5cf240164c6e351d30002",
"username": "my name"
},
"hidden": false,
"message": "",
"minorEdit": false,
"number": 4,
"when": "2021-08-25T15:36:35.285+03:00"
}
The final debug task print the version key like this:
"json": {
"body": {
"version": {
"minorEdit": true,
"number": 5
}
}
}
You can see the JSON for replacement inline, inside the "combine" function call. Whole JSON key gets overriden.

Related

AppScript - AppSheet API Post Issues

I'm attempting to send a row to an appsheet app with their provided API. I have it all set up in app script and I'm getting a code 200 (success) but it's not adding the data to the spreadsheet. Am I doing something wrong?
function testingAPI(){
let appId = APP ID HERE
var url = `https://api.appsheet.com/api/v2/apps/${appId}/tables/opportunity/Action`;
var options = {
"method": "post",
"headers": {
"applicationAccessKey": ACCESS KEY HERE
},
"httpBody": {
"Action": "Add",
"Properties": {
"Locale": "en-US",
"Location": "47.623098, -122.330184",
"Timezone": "Pacific Standard Time",
"UserSettings": {
"Option 1": "value1",
"Option 2": "value2"
}
},
"Rows": [
{
"IntentionSetID": "1H3t8Dt",
"AgentID": "11234",
"ActivityID": 12,
"taskComplete": true,
"taskVerified": false,
"task_verified_by": "",
"proof": "https://drive.google.com/open?id=1CpzebeLtAljqHTsFHvVCCDFc-A6aXC3O",
"agent_expected_part_detail": "",
"poc_expected_part_detail": '',
"assigned_point_value": '',
"points_assigned_by": "10/31/2014",
"actual_part_detail": "8:15:25",
"verification_date": "18:30:33"
}
]
}
};
var response = UrlFetchApp.fetch(url, options);
console.log(response.getResponseCode())
}
I was able to find that the API call is indeed going through, but it's saying the "Action" is missing:
{
"RestAPIVersion": 2,
"TableName": "opportunity",
"AppTemplateVersion": "1.000247",
"Errors": "'Action' is missing.",
"AppTemplateName": "e280cf5a-e2de-4d24-89d3-95d384a2c044",
"Operation": "REST API invoke",
"RecordType": "Stop",
"Result": "Success",
"ResultSuccess": true,
"StatusCode": "OK"
}
changing httpBody to payload got me a step further, so now it's recognizing the "add" aspect, but it's still not understanding the properties correctly. it's showing a bunch of escape characters.
REST API:
{
"Action": "Add",
"Properties": {},
"Rows": []
}
Properties:
{
"RestAPIVersion": 2,
"TableName": "opportunity",
"AppTemplateVersion": "1.000250",
"Action": "Add",
"Errors": "API 'Properties' does not have unexpected Type of 'JObject'. Value is: '{\r\n \"Action\": \"Add\",\r\n \"Properties\": \"{Timezone=Pacific Standard Time, Location=47.623098, -122.330184, Locale=en-US}\",\r\n \"Rows\": \"[Ljava.lang.Object;#489763c2\"\r\n}'",
"AppTemplateName": "e280cf5a-e2de-4d24-89d3-95d384a2c044",
"Operation": "REST API invoke",
"RecordType": "Start",
"Result": "Failure"
}
The AppSheets API POST https://api.appsheet.com/api/v2/apps/{appId}/tables/{tableName}/Action can be used to invoke an action. This means that the action should be created in AppSheet before it can be called.
Resources
Invoke an Action | AppSheet

How to read a value from json output

Basically, I need to read a value from a JSON output and use it in subsequent tasks. So, I tried with_items, loop, but nothing worked.
Sample JSON that I generated from a registered variable:
TASK [local_volume_mount : debug Info from device that is parted] **************
Monday 29 March 2021 21:33:39 +0000 (0:00:02.271) 0:00:02.417 **********
ok: [node1] => {
"partitioned_device_live_info": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"disk": {
"dev": "/dev/nvme2n1",
"logical_block": 512,
"model": "SAMSUNG MZQLW960HMJP-00003",
"physical_block": 512,
"size": 937692504.0,
"table": "msdos",
"unit": "kib"
},
"failed": false,
"invocation": {
"module_args": {
"align": "optimal",
"device": "/dev/nvme2n1",
"flags": null,
"label": "msdos",
"name": null,
"number": 1,
"part_end": "100%",
"part_start": "0%",
"part_type": "primary",
"state": "present",
"unit": "KiB"
}
},
"item": [
{
"device": "/dev/nvme2n1",
"partitions": [
{
"end": "100%",
"number": 1,
"start": "0%",
"storage_class": "ssd-wkr-services"
}
]
},
{
"end": "100%",
"number": 1,
"start": "0%",
"storage_class": "ssd-wkr-services"
}
],
"partitions": [
{
"begin": 1024.0,
"end": 937692160.0,
"flags": [],
"fstype": "",
"name": "",
"num": 1,
"size": 937691136.0,
"unit": "kib"
}
],
"script": ""
}
]
}
}
So, from the above I need to read the partitions.num value and use it in the next task, but, I don't know how to do that.
Task:
- name: THIS IS BEING TESTED
debug:
var: "{{ item.partitions }}"
ignore_errors: no
#loop: "{{ partitioned_device_live_info.results }}"
with_items: "{{ partitioned_device_live_info.results }}"
Output of the above task is
'dict object' has no attribute 'partitions'
I want to store that value item.partitions.num in a variable and then use it in further tasks.
Figured out using msg, below is my change
- name: THIS IS BEING TESTED with MSG and with_items
debug:
msg: "{{ item.partitions[0].num }}"
with_items: "{{ partitioned_device_live_info.results }}"
You can always use regex in ansible to set facts. More details stackoverflow
Following are some use cases
- name: Long form task does not
ansible.builtin.replace:
path: /etc/hosts
regexp: '\b(localhost)(\d*)\b'
replace: '\1\2.localdomain\2 \1\2'
- name: Explicitly specifying positional matched groups in replacement
ansible.builtin.replace:
path: /etc/ssh/sshd_config
regexp: '^(ListenAddress[ ]+)[^\n]+$'
replace: '\g<1>0.0.0.0'
Setting fact
- name: set version in file after replacement
set_fact:
version_in_file: "{{ version_deployment_file | regex_search(docker_image_version) }}"
More details - ansible.builtin.replace

Access to a JSON subobject with VueJS

I currently have a page in JavaScript that fetches data from an API. The problem is that I can't access the service.statusgroup.name object in the response.
I get this error: "TypeError: service.statusgroup is null". But when I remove .name, I don't get any error.
JSON Response:
{
"id": 2,
"title": "Website",
"description": "Website accessibility",
"status": "0",
"statusgroup": {
"id": 1,
"name": "Services", <-- Can't get this
"created_at": "2021-03-14T17:08:15.204Z",
"updated_at": "2021-03-14T17:40:29.739Z"
},
"created_at": "2021-03-14T15:06:18.524Z",
"updated_at": "2021-03-14T17:42:22.235Z"
}
JavaScript Fetch (VueJS):
export default {
name: 'Status',
mounted() {
// Fetch data from API
axios.get('http://localhost:1337/statuspages')
.then((reponse) => {
this.services = reponse.data
})
},
data: function() {
return {
services: null,
}
},
Display of the reponse:
<template v-for="service in services">
<v-list-item :key="service">
{{ service.statusgroup.name }}
</v-list-item>
</template>

parse json response using ansible

I need to parse the json response from an API request using ansible and then take action on items returned in the response. Here is part of my ansible playbook -
```- name: Invoke Import API
uri:
url: "{{ atl_bitbucket_dataset_url }}/rest/api/1.0/migration/imports"
user: admin
password: "{{ atl_bitbucket_admin_password }}"
method: POST
follow_redirects: yes
force_basic_auth: yes
creates: "{{ atl_product_home_shared }}/data/migration/import/lock.file"
body: "{ \"archivePath\": \"{{ atl_bitbucket_dataset_url | basename }}\" }"
body_format: json
return_content: yes
register: response
until: response.status == 200
retries: 6
delay: 15
failed_when: response.response.json.state != 'INITIALISING'
- name: get status of import
debug: var=response```
and here is the json response i got on a previous run
```TASK [bitbucket_dataset_restore : get status of import] ************************
ok: [localhost] => {
"response": {
"attempts": 1,
"cache_control": "no-cache, no-transform",
"changed": false,
"connection": "close",
"content": "{\"id\":1,\"initiator\":{\"name\":\"admin\",\"emailAddress\":\"admin#yourcompany.com\",\"id\":1,\"displayName\":\"AdminIstrator\",\"active\":true,\"slug\":\"admin\",\"type\":\"NORMAL\",\"links\":{\"self\":[{\"href\":\"http://bbdc-test-loadbala-t6vnlr2363vl-1404860112.us-west-2.elb.amazonaws.com/users/admin\"}]}},\"nodeId\":\"e72aa995-5016-4b2c-80e8-edba5eda3ab4\",\"progress\":{\"percentage\":0},\"startDate\":1574803309090,\"state\":\"INITIALISING\",\"type\":\"com.atlassian.bitbucket.migration.import\",\"updateDate\":1574803309090}",
"content_type": "application/json;charset=UTF-8",
"cookies": {},
"cookies_string": "",
"date": "Tue, 26 Nov 2019 21:21:49 GMT",
"elapsed": 1,
"failed": false,
"failed_when_result": False,
"json": {
"id": 1,
"initiator": {
"active": True,
"displayName": "AdminIstrator",
"emailAddress": "admin#yourcompany.com",
"id": 1,
"links": {
"self": [
{
"href": "http://bbdc-test-loadbala-t6vnlr2363vl-1404860112.us-west-2.elb.amazonaws.com/users/admin"
}
]
},
"name": "admin",
"slug": "admin",
"type": "NORMAL"
},
"nodeId": "e72aa995-5016-4b2c-80e8-edba5eda3ab4",
"progress": {
"percentage": 0
},
"startDate": 1574803309090,
"state": "INITIALISING",
"type": "com.atlassian.bitbucket.migration.import",
"updateDate": 1574803309090
},
"msg": "OK (unknown bytes)",
"redirected": False,
"status": 200,
"transfer_encoding": "chunked",
"url": "http://localhost:7990/rest/api/1.0/migration/imports",
"vary": "accept-encoding,x-auserid,cookie,x-ausername,accept-encoding",
"warnings": [
"The value True (type bool) in a string field was converted to u'True' (type string). If this does not look like what you expect, quote the entire value to ensure it does not change."
],
"x_arequestid": "#XXAH7Kx1281x1x0",
"x_asen": "SEN-500",
"x_auserid": "1",
"x_ausername": "admin",
"x_content_type_options": "nosniff"
}
}```
I want to retrieve the value "state": "INITIALISING" and the "id": 1. I have tried various methods response.response.json.state or response['response']['json']['state']. And it has not worked. Can anyone help me on how i can retrieve those values so i can use in other tasks further down?
I was able to fix this by adding is defined. So my code looks like this -
failed_when: output is defined and output.json is defined and output.json.state != 'INITIALISING'

MongoDB, NodeJS: updating an embedded document with new members

Using: MongoDB and native nodeJS mongoDB driver.
I'm trying to parse all the data from fb graph api, send it to my API and then save it to my DB.
PUT handling in my server:
//Update user's data
app.put('/api/users/:fbuser_id/:category', function(req, res) {
var body = JSON.stringify(req.body);
var rep = /"data":/;
body = body.replace(rep, '"' + req.params.category + '"' + ':');
req.body = JSON.parse(body);
db.fbusers.update({
id: req.params.fbuser_id
}, {
$set: req.body
}, {
safe: true,
multi: false
},
function(e, result) {
if (e) return next(e)
res.send((result === 1) ? {
msg: 'success'
} : {
msg: 'error'
})
});
});
I'm sending 25 elements at a time, and this code just overrides instead of updating the document.
Data I'm sending to the API:
{
"data": [
{
"category": "App page",
"name": "SoundCloud",
"id": "7919071058",
"created_time": "2013-09-16T18:16:59+0000"
},
{
...and so on
}
]
}
Basically my API changes "data" key from sent json to the category name, f.e.:
PUT to /api/users/000/likes will change the "data" key to "likes":
{
"likes": [
{
"category": "App page",
"name": "SoundCloud",
"id": "7919071058",
"created_time": "2013-09-16T18:16:59+0000"
},
{
...and so on
}
]
}
Then this JSON is put to the db.
Hierarchy in mongodb:
{
"_id": ObjectID("556584c8e908f0042836edce"),
"id": "0000000000000",
"email": "XXXX#gmail.com",
"first_name": "XXXXXXXX",
"gender": "male",
"last_name": "XXXXXXXXXX",
"link": "https://www.facebook.com/app_scoped_user_id/0000000000000/",
"locale": "en_US",
"name": "XXXXXXXXXX XXXXXXXXXX",
"timezone": 3,
"updated_time": "2015-05-26T18:11:59+0000",
"verified": true,
"likes": [
{
"category": "App page",
"name": "SoundCloud",
"id": "7919071058",
"created_time": "2013-09-16T18:16:59+0000"
},
{
"category": "App page",
"name": "SoundCloud",
"id": "7919071058",
"created_time": "2013-09-16T18:16:59+0000"
},
{
....and so on
}
]
}
So the problem is that my api overrides the field (in this case "likes") with newly sent data, instead of appending it to already existing data document.
I am pretty sure that I should be using other parameter than "$put" in the update, however, I have no idea which one and how to pass parameters to it programatically.
Use $push with the $each modifier to append multiple values to the array field.
var newLikes = [
{/* new item here */},
{/* new item here */},
{/* new item here */},
];
db.fbusers.update(
{ _id: req.params.fbuser_id },
{ $push: { likes: { $each: newLikes } } }
);
See also the $addToSet operator, it adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.