Skip to content

Commit

Permalink
Merge pull request #136 from exporterhubqa/main
Browse files Browse the repository at this point in the history
Request to release.v.4.0.2
  • Loading branch information
ralfyang committed Sep 29, 2021
2 parents a266948 + df1ec72 commit 3ab7d89
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 110 deletions.
216 changes: 135 additions & 81 deletions api/exporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ def check_starred(self, user, exporter, headers, repo_info):

def get_csv(self, token):
repo = f"{settings.ORGANIZATION}/exporterhub.io"
url = f"https://api.github.com/repos/{repo}/contents/contents/exporter_description.csv"
file = "exporter_description.csv"
url = f"https://api.github.com/repos/{repo}/contents/contents/{file}"
result = requests.get(url, headers={'Content-Type':'application/json','Authorization': 'token ' + token})
data = result.json()
csv_file = []
Expand All @@ -470,7 +471,7 @@ def get_csv(self, token):
csv_file.append(
[csv_detail[0], csv_detail[1], csv_detail[2]]
)

content = {
'sha' : data['sha'],
'csv_file': csv_file
Expand All @@ -479,21 +480,68 @@ def get_csv(self, token):
return content

elif result.status_code == 404:
return "GITHUB_URL_NON_EXISTED"

def push_to_github(self, token, message, content, sha):
repo = f"{settings.ORGANIZATION}/exporterhub.io"
url = f"https://api.github.com/repos/{repo}/contents/contents/exporter_description.csv"
content = ""
return content

return "GITHUB_GET_REPO_ERROR"

def push_to_github(self, token, message, exporter_id, name, description):
repo = f"{settings.ORGANIZATION}/exporterhub.io"
file = "exporter_description.csv"
url = f"https://api.github.com/repos/{repo}/contents/contents/{file}"
result = requests.get(url, headers={'Content-Type':'application/json','Authorization': 'token ' + token})
data = result.json()
csv_file = []
request_content = ''
count = 0

if result.status_code == 200:
content = base64.b64decode(data['content']).decode('utf-8')
details = [v for v in content.split('\n') if v]
for detail in details:
csv_detail = detail.split('"')
csv_detail = [v for v in csv_detail if v]
csv_detail = [v for v in csv_detail if ',' != v]
csv_detail = [v for v in csv_detail if ', ' != v]
csv_file.append(
[csv_detail[0], csv_detail[1], csv_detail[2]]
)
for row in csv_file:
if row[0] == str(exporter_id):
if description:
request_content += f'"{row[0]}","{row[1]}","{description}"' + '\n'
count += 1
else:
request_content += f'"{row[0]}","{row[1]}","{row[2]}"' + '\n'

contents = json.dumps({
"message" : message,
"content" : content,
"sha" : sha
})
if count == 0:
request_content += f'"{exporter_id}","{name}","{description}"' + '\n'

content = base64.b64encode(request_content.encode('utf-8')).decode('utf-8')
contents = json.dumps({
"message" : message,
"content" : content,
"sha" : data['sha']
})

requests.put(url, data=contents, headers={'Content-Type':'application/json','Authorization': 'token ' + token})
result = requests.put(url, data=contents, headers={'Content-Type':'application/json','Authorization': 'token ' + token})

return result

elif result.status_code == 404:
request_content = '"exporter_id","exporter_name","description"' + '\n'
request_content += f'"{exporter_id}","{name}","{description}"' + '\n'
content = base64.b64encode(request_content.encode('utf-8')).decode('utf-8')

contents = json.dumps({
"message" : message,
"content" : content
})

result = requests.put(url, data=contents, headers={'Content-Type':'application/json','Authorization': 'token ' + token})

return result

@login_check
def get(self, request, exporter_id):
try:
Expand Down Expand Up @@ -525,15 +573,18 @@ def get(self, request, exporter_id):
else:
forked_repository_url = None

get_csv = self.get_csv(github_token)
if get_csv == 'GITHUB_URL_NON_EXISTED':
return JsonResponse({'message': 'GITHUB_URL_NON_EXISTED'}, status=400)

csv_file = get_csv['csv_file']
detail_description = ''
for row in csv_file:
if row[0] == str(exporter.id):
detail_description = row[2]
get_csv = self.get_csv(github_token)

if get_csv == 'GITHUB_GET_REPO_ERROR':
return JsonResponse({'message': 'GITHUB_API_FAIL'}, status=400)
elif not get_csv:
detail_description = ''
else:
csv_file = get_csv['csv_file']
for row in csv_file:
if row[0] == str(exporter.id):
detail_description = row[2]

data = {
'exporter_id' : exporter.id,
Expand Down Expand Up @@ -577,28 +628,28 @@ def post(self, request, exporter_id):
return JsonResponse({'message':'FILL_THE_BLANK'}, status = 400)

get_csv = self.get_csv(github_token)
if get_csv == 'GITHUB_URL_NON_EXISTED':
return JsonResponse({'message': 'GITHUB_URL_NON_EXISTED'}, status=400)
if get_csv == 'GITHUB_GET_REPO_ERROR':
return JsonResponse({'message': 'GITHUB_API_FAIL'}, status=400)
else:
result = self.push_to_github(
token = github_token,
message = message,
exporter_id = exporter.id,
name = exporter.name,
description = description,
)

if result.status_code == 201 or 200:
response = {
"exporter_id" : exporter.id,
"exporter_name" : exporter.name,
"description" : description
}

return JsonResponse(response, status = 201)

request_content = ''
csv_file = get_csv['csv_file']
for row in csv_file:
if row[0] == exporter.id:
return JsonResponse({'message':'EXIST_EXPORTER'}, status = 400)
else:
request_content += f'"{row[0]}","{row[1]}","{row[2]}"' + '\n'

request_content += f'"{exporter.id}","{exporter.name}","{description}"'
content = base64.b64encode(request_content.encode('utf-8')).decode('utf-8')
result = self.push_to_github(token=github_token, message=message, content=content, sha=get_csv['sha'])

response = {
"exporter_id" : exporter.id,
"exporter_name" : exporter.name,
"description" : description
}

return JsonResponse(response, status = 201)
return JsonResponse({'message':'GITHUB_API_ERROR'}, status=400)

except KeyError:
return JsonResponse({'message':'KEY_ERROR'}, status = 400)
Expand All @@ -612,31 +663,27 @@ def patch(self, request, exporter_id):
description = data["description"]
message = f"{exporter.name} description update"

get_csv = self.get_csv(github_token)
if get_csv == 'GITHUB_URL_NON_EXISTED':
return JsonResponse({'message': 'GITHUB_URL_NON_EXISTED'}, status=400)

request_content = ''
response_description = ''
csv_file = get_csv['csv_file']
for row in csv_file:
if row[0] == str(exporter.id) and description:
row[2] = description
response_description = row[2]
request_content += f'"{row[0]}","{row[1]}","{row[2]}"' + '\n'
elif row[0] != str(exporter.id):
request_content += f'"{row[0]}","{row[1]}","{row[2]}"' + '\n'

content = base64.b64encode(request_content.encode('utf-8')).decode('utf-8')
result = self.push_to_github(token=github_token, message=message, content=content, sha=get_csv['sha'])

response = {
"exporter_id" : exporter.id,
"exporter_name" : exporter.name,
"description" : response_description,
}

return JsonResponse(response, status = 200)
get_csv = self.get_csv(github_token)
if get_csv == 'GITHUB_GET_REPO_ERROR':
return JsonResponse({'message': 'GITHUB_API_FAIL'}, status=400)
else:
result = self.push_to_github(
token = github_token,
message = message,
exporter_id = exporter.id,
name = exporter.name,
description = description,
)
if result.status_code == 200:
response = {
"exporter_id" : exporter.id,
"exporter_name" : exporter.name,
"description" : description
}
return JsonResponse(response, status = 200)

else:
return JsonResponse({'message':'GITHUB_API_ERROR'}, status=400)

except KeyError:
return JsonResponse({'message':'KEY_ERROR'}, status = 400)
Expand Down Expand Up @@ -711,7 +758,8 @@ def get(self, request, exporter_id):

def code_to_github(self, app_name, file_name, token, content_type, content, file_type, sha, bf_file_name):
repo = f"{settings.ORGANIZATION}/exporterhub.io"
url = f"https://api.github.com/repos/{repo}/contents/{bf_file_name}"
old_file_name = bf_file_name.strip('"')
url = f"https://api.github.com/repos/{repo}/contents/{old_file_name}"
delete_content = json.dumps({'sha': sha, 'message': 'delete_old_file'})
delete = requests.delete(url, data=delete_content, headers={'Authorization': 'token ' + token, 'Content-Type':'application/vnd.github.v3+json'})

Expand All @@ -725,6 +773,11 @@ def code_to_github(self, app_name, file_name, token, content_type, content, file
})
result = requests.put(create_url, data=contents, headers={'Authorization': 'token ' + token, 'Content-Type':'application/vnd.github.v3+json'})

if result.status_code == 404:
return "GITHUB_REPO_API_ERROR"

return result

else:
create_url = f"https://api.github.com/repos/{repo}/contents/contents/{app_name}/{app_name}_{content_type}/{file_name}_{content_type}.{file_type}"
contents = json.dumps({
Expand Down Expand Up @@ -769,14 +822,13 @@ def csv_to_github(self, app_name, file_name, token, content_type, content, file_
yaml_id = len(content_list) + 1

for i, detail in enumerate(content_list):
if detail[0] == file_id:
if detail[0] == f'"{file_id}"':
bf_file_name = detail[2].strip()
content_list[i][1] = f"{content}"
content_list[i][2] = f'"./contents/{app_name}/{app_name}_{content_type}/{file_name}_{content_type}.{type[content_type]}", \n'
content_list[i][1] = f'"{content}"'
content_list[i][2] = f'"./contents/{app_name}/{app_name}_{content_type}/{file_name}_{content_type}.{type[content_type]}"'
count += 1
if count == 0:
content_list.append([f'"0{yaml_id}","{content}", "./contents/{app_name}/{app_name}_{content_type}/{file_name}_{content_type}.{type[content_type]}", \n'])

content_list.append([f'"0{yaml_id}","{content}", "./contents/{app_name}/{app_name}_{content_type}/{file_name}_{content_type}.{type[content_type]}",'])
for each_content in content_list:
response += ','.join(each_content) + '\n'
contents = json.dumps(
Expand Down Expand Up @@ -835,8 +887,8 @@ def post(self, request, exporter_id):

file_sha = data['file_sha']
file_name = data["file_name"]
file_content = data["file_content"]
csv_desc = data["csv_desc"]
file_content = data["file_content"]
csv_desc = data["csv_desc"] if data["csv_desc"] else '-'
csv_sha = data['csv_sha']
file_id = data.get('file_id')

Expand All @@ -856,10 +908,12 @@ def post(self, request, exporter_id):
return JsonResponse({'message': 'KEY_ERROR'}, status=400)

def code_file_delete(self, app_name, content_type, file_type, token, yaml_url):
repo = f"{settings.ORGANIZATION}/exporterhub.io"
yaml_url = yaml_url.strip()
url = f"https://api.github.com/repos/{repo}/contents/{yaml_url}"
data = requests.get(url, headers={'Content-Type': 'application/json', 'Authorization': 'token ' + token})
repo = f"{settings.ORGANIZATION}/exporterhub.io"
yaml_urls = yaml_url.replace('"','')
yaml_urls = yaml_urls.strip()
url = f"https://api.github.com/repos/{repo}/contents/{yaml_urls}"
data = requests.get(url, headers={'Content-Type': 'application/json', 'Authorization': 'token ' + token})


if data.status_code == 404:
return 'FILE_NOT_EXISTING'
Expand Down Expand Up @@ -907,7 +961,7 @@ def csv_file_delete(self, app_name, content_type, file_type, token, file_id):
content_list.append(csv_contents)

for i, detail in enumerate(content_list):
if detail[0] == file_id:
if detail[0] == f'"{file_id}"':
yaml_url = detail[2]
else:
results.append([detail[0], detail[1], detail[2], '\n'])
Expand All @@ -921,8 +975,8 @@ def csv_file_delete(self, app_name, content_type, file_type, token, file_id):

if csv_delete == 404:
return "GITHUB_REPO_API_ERROR"
return csv_delete

return {'result' : csv_delete, 'yaml_url' : yaml_url}

for content in results:
response += ','.join(content)
Expand Down
4 changes: 4 additions & 0 deletions api/exporter_list.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ title,project_name,github_url,offcial,category,
eBPF,eBPF Exporter,https://github.com/cloudflare/ebpf_exporter,0,Miscellaneous,
Ceph,Ceph Exporter,https://github.com/digitalocean/ceph_exporter,0,Storage,
Grok,Grok Exporter,https://github.com/fstab/grok_exporter,0,Logging,
cAdvisor,cAdvisor Exporter,https://github.com/google/cadvisor,0,Miscellaneous,
RabbitMQ,RabbitMQ Exporter,https://github.com/kbudde/rabbitmq_exporter,0,Messaging,
Kong API Gateway,Kong Prometheus Plugin,https://github.com/Kong/kong-plugin-prometheus,0,Software,
kube-state-metrics,kube-state-metrics,https://github.com/kubernetes/kube-state-metrics,0,Miscellaneous,
System Process,Process Exporter,https://github.com/ncabatoff/process-exporter,0,Miscellaneous,
NGINX,NGINX Exporter,https://github.com/nginxinc/nginx-prometheus-exporter,0,HTTP,
NVIDIA GPU Monitoring Tools,NVIDIA GPU Monitoring Tools,https://github.com/NVIDIA/gpu-monitoring-tools,0,Hardware,
Redis,Redis Metrics Exporter,https://github.com/oliver006/redis_exporter,0,Database,
OpenStack,OpenStack Exporter,https://github.com/openstack-exporter/openstack-exporter,0,Miscellaneous,
JSON,JSON Exporter,https://github.com/prometheus-community/json_exporter,0,Miscellaneous,
Google Stackdriver,Google Stackdriver Exporter,https://github.com/prometheus-community/stackdriver_exporter,0,Monitoring,
Windows system,Windows Exporter,https://github.com/prometheus-community/windows_exporter,0,Hardware,
Blackbox,Blackbox Exporter,https://github.com/prometheus/blackbox_exporter,1,Miscellaneous,
CloudWatch,CloudWatch Exporter,https://github.com/prometheus/cloudwatch_exporter,1,Monitoring,
Collectd,Collectd Exporter,https://github.com/prometheus/collectd_exporter,1,Monitoring,
Expand All @@ -21,6 +24,7 @@ InfluxDB,InfluxDB Exporter,https://github.com/prometheus/influxdb_exporter,1,Dat
JMX,JMX Exporter,https://github.com/prometheus/jmx_exporter,1,Monitoring,
Memcached,Memcached Exporter,https://github.com/prometheus/memcached_exporter,1,Database,
MySQL,MySQL Server Exporter,https://github.com/prometheus/mysqld_exporter,1,Database,
Node Exporter,Node Exporter,https://github.com/prometheus/node_exporter,1,Hardware,
SNMP,SNMP Exporter,https://github.com/prometheus/snmp_exporter,1,Monitoring,
Statsd,Statsd Exporter,https://github.com/prometheus/statsd_exporter,1,Monitoring,
Azure,Azure Metrics Exporter,https://github.com/RobustPerception/azure_metrics_exporter,0,Monitoring,
Expand Down
1 change: 0 additions & 1 deletion contents/Go-Client/Go-Client_helm/Go-Client_helm.csv

This file was deleted.

1 change: 0 additions & 1 deletion contents/Go-Client/Go-Client_helm/aaa_helm.yaml

This file was deleted.

1 change: 1 addition & 0 deletions contents/MongoDB/MongoDB_alert/MongoDB_alert.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"01","test", "./contents/MongoDB/MongoDB_alert/ddddddtest_alert.yaml",
1 change: 1 addition & 0 deletions contents/MongoDB/MongoDB_alert/ddddddtest_alert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tsssss
5 changes: 5 additions & 0 deletions contents/MongoDB/MongoDB_helm/MongoDB_helm.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"01","helm for deployment", "./contents/MongoDB/MongoDB_helm/deployment_helm.yaml",
"02","asdfasdf","./contents/MongoDB/MongoDB_helm/config_helm.yaml",
"03","ㄴ", "./contents/MongoDB/MongoDB_helm/v0.11.2_helm.yaml",
"04","a", "./contents/MongoDB/MongoDB_helm/deploysment v0.11.2_helm.yaml",
"05","-", "./contents/MongoDB/MongoDB_helm/deploysment v0.11.3_helm.yaml",
1 change: 1 addition & 0 deletions contents/MongoDB/MongoDB_helm/config_helm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdfasdfss1111ssss
3 changes: 3 additions & 0 deletions contents/MongoDB/MongoDB_helm/deployment_helm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test
sdfsdfsdf
sdfsdfsss111
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
Empty file.
1 change: 1 addition & 0 deletions contents/MongoDB/MongoDB_helm/v0.11.2_helm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4 changes: 3 additions & 1 deletion contents/exporter_description.csv
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exporter_id,exporter_name,description
"exporter_id","exporter_name","description"
"41","MongoDB Exporter","test"
"6","Kong Prometheus Plugin","ㅁㄴㅇㄹㅁㄴㅇㄹㅁㄴㅇㄹㅁㄴㅇㄹ"
Loading

0 comments on commit 3ab7d89

Please sign in to comment.