2020-02-14 09:11:38 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
import time
|
2020-02-14 09:11:38 +01:00
|
|
|
import base
|
|
|
|
import v2_swagger_client
|
|
|
|
from v2_swagger_client.rest import ApiException
|
|
|
|
|
|
|
|
class Artifact(base.Base):
|
2020-02-21 10:08:27 +01:00
|
|
|
def get_reference_info(self, project_name, repo_name, reference, **kwargs):
|
|
|
|
client = self._get_client(**kwargs)
|
2020-02-25 03:40:29 +01:00
|
|
|
params = {}
|
|
|
|
if "with_signature" in kwargs:
|
|
|
|
params["with_signature"] = kwargs["with_signature"]
|
2020-03-16 08:46:43 +01:00
|
|
|
if "with_tag" in kwargs:
|
|
|
|
params["with_tag"] = kwargs["with_tag"]
|
2020-03-10 07:55:55 +01:00
|
|
|
if "with_scan_overview" in kwargs:
|
|
|
|
params["with_scan_overview"] = kwargs["with_scan_overview"]
|
2020-02-25 03:40:29 +01:00
|
|
|
return client.get_artifact_with_http_info(project_name, repo_name, reference, **params )
|
|
|
|
|
|
|
|
def add_label_to_reference(self, project_name, repo_name, reference, label_id, **kwargs):
|
|
|
|
client = self._get_client(**kwargs)
|
|
|
|
label = v2_swagger_client.Label(id = label_id)
|
|
|
|
return client.add_label_with_http_info(project_name, repo_name, reference, label)
|
2020-02-21 10:08:27 +01:00
|
|
|
|
|
|
|
def copy_artifact(self, project_name, repo_name, _from, expect_status_code = 201, expect_response_body = None, **kwargs):
|
|
|
|
client = self._get_client(**kwargs)
|
|
|
|
|
|
|
|
try:
|
|
|
|
data, status_code, _ = client.copy_artifact_with_http_info(project_name, repo_name, _from)
|
|
|
|
except ApiException as e:
|
|
|
|
base._assert_status_code(expect_status_code, e.status)
|
|
|
|
if expect_response_body is not None:
|
|
|
|
base._assert_status_body(expect_response_body, e.body)
|
|
|
|
return
|
|
|
|
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
|
|
|
base._assert_status_code(201, status_code)
|
|
|
|
return data
|
2020-03-16 08:46:43 +01:00
|
|
|
|
|
|
|
def create_tag(self, project_name, repo_name, reference, tag_name, expect_status_code = 201, **kwargs):
|
|
|
|
client = self._get_client(**kwargs)
|
|
|
|
tag = v2_swagger_client.Tag(name = tag_name)
|
|
|
|
_, status_code, _ = client.create_tag_with_http_info(project_name, repo_name, reference, tag)
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
|
|
|
|
|
|
|
def delete_tag(self, project_name, repo_name, reference, tag_name, expect_status_code = 200, **kwargs):
|
|
|
|
client = self._get_client(**kwargs)
|
|
|
|
_, status_code, _ = client.delete_tag_with_http_info(project_name, repo_name, reference, tag_name)
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
2020-03-10 07:55:55 +01:00
|
|
|
|
|
|
|
def check_image_scan_result(self, project_name, repo_name, reference, expected_scan_status = "Success", **kwargs):
|
|
|
|
timeout_count = 30
|
|
|
|
scan_status=""
|
|
|
|
while True:
|
|
|
|
time.sleep(5)
|
|
|
|
timeout_count = timeout_count - 1
|
|
|
|
if (timeout_count == 0):
|
|
|
|
break
|
|
|
|
artifact = self.get_reference_info(project_name, repo_name, reference, **kwargs)
|
|
|
|
scan_status = artifact[0].scan_overview['application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0']["scan_status"]
|
|
|
|
if scan_status == expected_scan_status:
|
|
|
|
return
|
|
|
|
raise Exception("Scan image result is {}, not as expected {}.".format(scan_status, expected_scan_status))
|