harbor仓库接口python操作类

harbor之前的部署配置和介绍可以查看我之前的笔记,这里整理出了一个harbor仓库的操作类,以后可以直接用的上。

#!/usr/bin/python3
#coding:utf-8
import requests
import json

class Hub(object):
    def __init__(self, host, port, username, passwd):
        self.host = host
        self.port = port
        self.username = username
        self.passwd = passwd
        self.token = self.get_token()

    def get_token(self):
        url = "http://{}:{}/service/token?account={}&service=harbor-registry&scope=registry:catalog:*".format(self.host, self.port, self.username)
        try:
            r = requests.get(url, auth=(self.username, self.passwd))
            token_dict = json.loads(r.text)
            token = token_dict['token']
        except Exception as e:
            print(e)
        return token

    def systeminfo(self):
        url = "http://{}:{}/api/v2.0/systeminfo".format(self.host, self.port)
        headers = {
            "Content-Type": "application/json",
            "authorization": "bearer "+self.token,
        }
        try:
            r = requests.get(url, headers=headers)
        except Exception as e:
            print(e)
        return r.json()

    def statistics(self):
        url = "http://{}:{}/api/v2.0/statistics".format(self.host, self.port)
        headers = {
            "Content-Type": "application/json",
            "authorization": "bearer "+self.token,
        }
        try:
            r = requests.get(url, headers=headers)
        except Exception as e:
            print(e)
        return r.json()

    def project_list(self, page=1, page_size=15):
        url = "http://{}:{}/api/v2.0/projects?page={}&page_size={}".format(self.host, self.port, page, page_size)
        headers = {
            "Content-Type": "application/json",
            "authorization": "bearer "+self.token,
        }
        try:
            r = requests.get(url, headers=headers)
        except Exception as e:
            print(e)
        return r.json()

    def image_list(self, project_name, page=1, page_size=15):
        url = "http://{}:{}/api/v2.0/projects/{}/repositories?page={}&page_size={}".format(self.host, self.port, project_name, page, page_size)
        headers = {
            "Content-Type": "application/json",
            "authorization": "bearer "+self.token,
        }
        try:
            r = requests.get(url, headers=headers)
        except Exception as e:
            print(e)
        return r.json()

    def tag_list(self, image_name, page=1, page_size=15):
        url = "http://{}:{}/api/v2.0/projects/orionx/repositories/{}/artifacts?with_tag=false&with_scan_overview=true&with_label=true&page={}&page_size={}".format(self.host, self.port, image_name, page, page_size)
        headers = {
            "Content-Type": "application/json",
            "authorization": "bearer "+self.token,
        }
        try:
            r = requests.get(url, headers=headers)
        except Exception as e:
            print(e)
        return r.json()

if __name__ == "__main__":
    vhub = Hub("192.168.1.78", 5000, "admin", "admin123")
    #print(vhub.get_token())
    #print(vhub.project_list())
    print(vhub.systeminfo())

执行测试返回

{'auth_mode': 'db_auth', 'harbor_version': 'v2.5.1-b0506782', 'self_registration': False}


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/886.html