0

I have an API access to a url that looks a little like this:

curl https://api_url.com/device_groups/ -u api_key:

Which outputs in JSON:

{
   "data":[
      {
         "type":"device_group",
         "id":85015,
         "attributes":{
            "name":"Initial"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":85683,
         "attributes":{
            "name":"mode-4"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":85684,
         "attributes":{
            "name":"Employees-3"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":506044
                  },
                  {
                     "type":"device",
                     "id":658670
                  },
                  {
                     "type":"device",
                     "id":506034
                  },
                  {
                     "type":"device",
                     "id":506037
                  },
                  {
                     "type":"device",
                     "id":506038
                  },
                  {
                     "type":"device",
                     "id":506046
                  },
                  {
                     "type":"device",
                     "id":506043
                  },
                  {
                     "type":"device",
                     "id":658669
                  },
                  {
                     "type":"device",
                     "id":506036
                  },
                  {
                     "type":"device",
                     "id":502256
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":91589,
         "attributes":{
            "name":"Subcontractors-2"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":658668
                  },
                  {
                     "type":"device",
                     "id":658671
                  },
                  {
                     "type":"device",
                     "id":506051
                  },
                  {
                     "type":"device",
                     "id":506048
                  },
                  {
                     "type":"device",
                     "id":506040
                  },
                  {
                     "type":"device",
                     "id":506050
                  },
                  {
                     "type":"device",
                     "id":506042
                  },
                  {
                     "type":"device",
                     "id":502236
                  },
                  {
                     "type":"device",
                     "id":506041
                  },
                  {
                     "type":"device",
                     "id":523577
                  },
                  {
                     "type":"device",
                     "id":506053
                  },
                  {
                     "type":"device",
                     "id":655780
                  },
                  {
                     "type":"device",
                     "id":508070
                  },
                  {
                     "type":"device",
                     "id":506052
                  },
                  {
                     "type":"device",
                     "id":506047
                  },
                  {
                     "type":"device",
                     "id":506035
                  },
                  {
                     "type":"device",
                     "id":506045
                  },
                  {
                     "type":"device",
                     "id":506039
                  },
                  {
                     "type":"device",
                     "id":506049
                  },
                  {
                     "type":"device",
                     "id":589426
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":92316,
         "attributes":{
            "name":"Employees-2"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":673452
                  },
                  {
                     "type":"device",
                     "id":576554
                  },
                  {
                     "type":"device",
                     "id":672077
                  },
                  {
                     "type":"device",
                     "id":589167
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":92325,
         "attributes":{
            "name":"Employees"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":94908,
         "attributes":{
            "name":"mode 2"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":501727
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":95017,
         "attributes":{
            "name":"Mode 1"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":95381,
         "attributes":{
            "name":"Employees-test"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":658672
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":95382,
         "attributes":{
            "name":"Subcontracters-test"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      }
   ],
   "has_more":false
}

I need to move each device into a predefined device group id and then return the device to their original group taken from the initial JSON curl.

eg.

curl https://api_url.com/group/$predefined_group_id/devices/506044 -u api_key: -X POST

curl https://api_url.com/group/$original_group_id/devices/506044 -u api_key: -X POST

Can you also exclude specific device_groups either by id or description as a variable?

1

The jq command

jq -r '
    .data.id as $groupid |
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file

... would extract the group ID, and then all the id values from the data array. For each id value, it would construct a URL that it would output.

To use these generated URLs in calls to curl:

jq -r '
    .data.id as $groupid |
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file |
xargs -I {} curl -X POST -u api_key: {}

Given the JSON document in the question, this would execute the following commands:

curl -X POST -u api_key: https://api_url.com/group/85684/devices/506044
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506034
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506037
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506038
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506046
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506043
curl -X POST -u api_key: https://api_url.com/group/85684/devices/658669
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506036
curl -X POST -u api_key: https://api_url.com/group/85684/devices/502256

You could also use xargs to call curl as few times as possible with those URLs:

jq -r '
    .data.id as $groupid |
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file |
xargs curl -X POST -u api_key:

This would generate the single command

curl -X POST -u api_key: https://api_url.com/group/85684/devices/506044 https://api_url.com/group/85684/devices/506034 https://api_url.com/group/85684/devices/506037 https://api_url.com/group/85684/devices/506038 https://api_url.com/group/85684/devices/506046 https://api_url.com/group/85684/devices/506043 https://api_url.com/group/85684/devices/658669 https://api_url.com/group/85684/devices/506036 https://api_url.com/group/85684/devices/502256

You may provide a group ID from the command line instead of using the one found in the data like so (using the variable some_groupid):

jq -r --arg groupid "$some_groupid" '
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file
4
  • Your original answer works on the original question but when the JSON array includes multiple device_groups it breaks the jq. I'm plodding through to try and understand why.
    – th3joker
    Nov 19 at 15:52
  • I think it's the inclusion of multiple group_id's and I'm not sure how to allow for that. I mean I can write something nasty around it but your original answer is so clean I hate to besmirch your good work with something that awful.
    – th3joker
    Nov 19 at 16:06
  • @th3joker I no longer understand what it is you want to do.
    – they
    Nov 19 at 18:13
  • took me a while but I figured out how to do it with some playing around with what you'd written. I'm sure it would be much neater if you'd done it, was nice to learn some new stuff and I wouldn't have been able to do that without your help ;-)
    – th3joker
    Nov 19 at 21:15
0

This does exactly what I wanted, I'm sure @they could make it look a lot more elegant but it does what I needed.

#!/bin/bash

# Set groupid also read and write API variables
group="80000"
read_api="read_api"
write_api="write_api"

# get all devicesid and groupid and create groups.json file
curl https://api_url.com/device_groups -u "$read_api": > groups.json

# Move all devices into predefined groupid
jq -r --arg groupid $group '
    .data[].relationships.devices.data[].id |
    @sh "https://api_url.com/device_groups/\($groupid)/devices/\(.)"' groups.json |
xargs curl -X POST -u "$write_api":

# Wait for the groupid to propagate
sleep 60;

# Move all devices into original groupid
jq -r '
    .data[] |
    "https://api_url.com/device_groups/\(.id)/devices/\(.relationships.devices.data[].id)"' groups.json |
xargs curl -X POST -u "$write_api":

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.