lfidapi create group checks if group exists 26/15826/2
authorAric Gardner <agardner@linuxfoundation.org>
Thu, 6 Jun 2019 17:25:14 +0000 (13:25 -0400)
committerAric Gardner <agardner@linuxfoundation.org>
Thu, 6 Jun 2019 17:40:35 +0000 (13:40 -0400)
There is a bug in lfidapi where if a group exists
the reponse code does the right thing but some
shadow group is created.

Add a check so that we only create a group
if it does not yet exist.

Signed-off-by: Aric Gardner <agardner@linuxfoundation.org>
Change-Id: Ia80400cf74431a8ffdea1ae06459969649d94501

lftools/lfidapi.py
releasenotes/notes/lfidapi-3265c24947b95d20.yaml [new file with mode: 0644]

index cd464f0..3033c13 100755 (executable)
@@ -30,6 +30,16 @@ def check_response_code(response):
                                                          response.text))
 
 
+def helper_check_group_exists(group):
+    """List members of a group."""
+    access_token, url = oauth_helper()
+    url = PARSE(url, group)
+    headers = {'Authorization': 'Bearer ' + access_token}
+    response = requests.get(url, headers=headers)
+    status_code = response.status_code
+    return status_code
+
+
 def helper_search_members(group):
     """List members of a group."""
     access_token, url = oauth_helper()
@@ -77,15 +87,19 @@ def helper_invite(email, group):
 
 def helper_create_group(group):
     """Create group."""
-    access_token, url = oauth_helper()
-    url = '{}/'.format(url)
-    headers = {'Authorization': 'Bearer ' + access_token}
-    data = {"title": group, "type": "group"}
-    print(data)
-    response = requests.post(url, json=data, headers=headers)
-    check_response_code(response)
-    result = (response.json())
-    print(json.dumps(result, indent=4, sort_keys=True))
+    check_response_code = helper_check_group_exists(group)
+    if check_response_code == 200:
+        print("Group {} already exists exiting...".format(group))
+    else:
+        access_token, url = oauth_helper()
+        url = '{}/'.format(url)
+        headers = {'Authorization': 'Bearer ' + access_token}
+        data = {"title": group, "type": "group"}
+        print(data)
+        response = requests.post(url, json=data, headers=headers)
+        check_response_code(response)
+        result = (response.json())
+        print(json.dumps(result, indent=4, sort_keys=True))
 
 
 def helper_match_ldap_to_info(info_file, group, noop):
diff --git a/releasenotes/notes/lfidapi-3265c24947b95d20.yaml b/releasenotes/notes/lfidapi-3265c24947b95d20.yaml
new file mode 100644 (file)
index 0000000..767be7b
--- /dev/null
@@ -0,0 +1,4 @@
+---
+fixes:
+  - |
+    lfidapi create group checks if group exists before posting