Created
January 21, 2021 23:14
-
-
Save dereckmartin/fe730a43a35cb2992419acbd875aa856 to your computer and use it in GitHub Desktop.
Export Harbor projects to puppet-harbor hieradata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import requests | |
from requests.auth import HTTPBasicAuth | |
import urllib3 | |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
''' | |
Translate Harbor projects data to Puppet Hieradata | |
for puppet-harbor module [https://github.com/walkamongus/puppet-harbor] | |
1. Set Harbor host (i.e., https://host.domain.com) | |
2. Set Harbor username | |
3. Set Harbor password | |
4. Set hierakey (i.e., harbor::harbor_projects:) | |
5. Execute | |
Output Example: | |
harbor::harbor_projects: | |
'sample-project': | |
ensure: present | |
public: true | |
members: ['sample.user'] | |
member_groups: ['sample-group'] | |
''' | |
host = "" | |
user = "" | |
password = "" | |
hierakey = "" | |
projects_endpoint = "/api/projects" | |
projects = requests.get(host + projects_endpoint, auth=HTTPBasicAuth(user, password), verify=False) | |
print (hierakey) | |
for project in projects.json(): | |
project_members = requests.get(host + projects_endpoint + "/" + str(project['project_id']) + "/members", auth=HTTPBasicAuth(user, password), verify=False) | |
print (" '{}':".format(project['name'])) | |
print (" ensure: present") | |
print (" public: {}".format(project['metadata']['public'])) | |
members = [] | |
groups = [] | |
for project_member in project_members.json(): | |
if project_member['entity_type'] == "u": | |
if project_member['entity_name'] != "admin": | |
members.append(project_member['entity_name']) | |
elif project_member['entity_type'] == "g": | |
groups.append(project_member['entity_name']) | |
if members: | |
print (" members: ['{}']".format("', '".join(members))) | |
if groups: | |
print (" member_groups: ['{}']".format("', '".join(groups))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment