Skip to content

Commit

Permalink
Merge pull request #10 from ibm-quantum/#6-add_edit_backends_in_all_p…
Browse files Browse the repository at this point in the history
…rojects

Add edit_backends_in_all_projects.py
  • Loading branch information
Matt-Stypulkoski committed Feb 10, 2022
2 parents 514430c + eb25a78 commit 25b13ef
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ Duplicate `user` entries are not accepted, and only the first instance of that `
&ensp;If adding: `python edit_backends.py <hub> add <group> <backend_name> -project <project> -priority <priority>`</br>
&ensp;If removing: `python edit_backends.py <hub> remove <group> <backend_name> -project <project> -priority <priority>`

-----

### edit_backends_in_all_projects.py
**What it does**: Adds or Removes a backend from all Projects in a Group within your Hub</br>
**Parameters**:</br>
&ensp;`<hub>`: Required. The name of your hub.</br>
&ensp;`<action>`: Required. Either `add` or `remove`.</br>
&ensp;`<group>`: Required. The name of the parent group.</br>
&ensp;`<backend>`: Required. The name of the backend to add or remove.</br>
&ensp;`<-priority>`: Required for `add` action. Integer priority of the backend, between 1 and 10,000.</br>

**Usage**:</br>
&ensp;If adding: `python edit_backends_in_all_projects.py <hub> add <group> <backend_name> -priority <priority>`</br>
&ensp;If removing: `python edit_backends_in_all_projects.py <hub> remove <group> <backend_name>`

## How to contribute

Expand Down
96 changes: 96 additions & 0 deletions edit_backends_in_all_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

import requests
import argparse
import sys
import time
from auth import get_access_token

API_URL = "https://api-qcon.quantum-computing.ibm.com/api"

parser = argparse.ArgumentParser(description="Add or Remove a backend from all Projects in the given Hub/Group")
parser.add_argument('hub', type=str, help="The Hub that contains the given Group")
parser.add_argument('action', type=str, choices=['add', 'remove'], help="Whether or not to add or remove the backend from all projects")
parser.add_argument('group', type=str, help="Name of the group to add/remove the given backend from all Projects")
parser.add_argument('backend', type=str, help="Name of the backend to be added or removed")
parser.add_argument('-priority', type=int, help="Priority of the backend (if adding). Must be an integer between 1 and 10000")
args = parser.parse_args()

hub = args.hub
action = args.action
group = args.group
backend = args.backend
priority = args.priority

access_token = get_access_token()
headers = {'X-Access-Token': access_token}

# Need to check for the presence of a priority value if adding a backend
if action == 'add':
if priority is None or priority not in range(1, 10001):
sys.exit('You must provide a priority between 1 and 10000. Use the -h flag for more info')
else:
if priority is not None:
print("You do not need to use the -priority arg when removing systems")


# Get names of Projects in Group
projects_list = []
try:
url = f'{API_URL}/Network/{hub}'
response = requests.get(url=url, headers=headers)

response.raise_for_status()
hub_data = response.json()

if "groups" in hub_data and "projects" in hub_data["groups"][group]:
for project in hub_data["groups"][group]["projects"].values():
projects_list.append(project["name"])
except requests.HTTPError as http_err:
sys.exit(f"Could not get hub data due to HTTPError: {http_err}")
except Exception as err:
sys.exit(f"Could not get hub data due to: {err}")


# Call API to add or remove backend from all Projects found in Group
if action == 'add':
for project in projects_list:
try:
time.sleep(2)
url = f'{API_URL}/Network/{hub}/Groups/{group}/Projects/{project}/devices'
response = requests.post(url, headers=headers, json={'name': backend, 'priority': priority})
response.raise_for_status()
print(f"{backend} was added to {project}")
except requests.HTTPError as http_err:
sys.exit(f"Could not add {backend} to {project} due to HTTPError: {http_err}")
except Exception as err:
sys.exit(f"Could not add {backend} to {project} due due to: {err}")
print(f"{backend} has been added to all projects in {group}")

else:
for project in projects_list:
try:
time.sleep(2)
url = f'{API_URL}/Network/{hub}/Groups/{group}/Projects/{project}/devices'
response = requests.get(url, headers=headers)
data = response.json()
project_devices = [x['backend_name'] for x in data]
response.raise_for_status()
if backend not in project_devices:
print(f"{project} does not have access to {backend}. Skipping...")
else:
url = f'{API_URL}/Network/{hub}/Groups/{group}/Projects/{project}/devices/{backend}'
response = requests.delete(url, headers=headers)
response.raise_for_status()
print(f"{backend} was removed from {project}")
except requests.HTTPError as http_err:
sys.exit(f"Could not remove {backend} to {project} due due to HTTPError: {http_err}")
except Exception as err:
sys.exit(f"Could not remove {backend} to {project} due due to: {err}")
print(f"{backend} has been deleted from all projects in {group}")

0 comments on commit 25b13ef

Please sign in to comment.