This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
functions_delete_package.py
121 lines (101 loc) · 5.97 KB
/
functions_delete_package.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Copyright 2018 IBM Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os, json, sys, argparse, requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from cfgCommons import Cfg
from wawCommons import setLoggerConfig, getScriptLogger, getFilesAtPath, openFile, getRequiredParameter, getOptionalParameter, getParametersCombination, convertApikeyToUsernameAndPassword, errorsInResponse
import urllib3
from urllib.parse import quote
import logging
logger = getScriptLogger(__file__)
def main(argv):
"""Deletes the cloudfunctions package specified in the configuration file or as CLI argument."""
parser = argparse.ArgumentParser(description="Deletes cloud functions package.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-v', '--verbose', required=False, help='verbosity', action='store_true')
parser.add_argument('-c', '--common_configFilePaths', help="configuration file", action='append')
parser.add_argument('--common_functions', required=False, help="directory where the cloud functions are located")
parser.add_argument('--cloudfunctions_namespace', required=False, help="cloud functions namespace")
parser.add_argument('--cloudfunctions_apikey', required=False, help="cloud functions apikey")
parser.add_argument('--cloudfunctions_username', required=False, help="cloud functions user name")
parser.add_argument('--cloudfunctions_password', required=False, help="cloud functions password")
parser.add_argument('--cloudfunctions_package', required=False, help="cloud functions package name")
parser.add_argument('--cloudfunctions_url', required=False, help="url of cloud functions API")
parser.add_argument('--log', type=str.upper, default=None, choices=list(logging._levelToName.values()))
args = parser.parse_args(argv)
if __name__ == '__main__':
setLoggerConfig(args.log, args.verbose)
def handleResponse(response):
"""Get response code and show an error if it's not OK"""
code = response.status_code
if code != requests.codes.ok:
if code == 401:
logger.error("Authorization error. Check your credentials. (Error code " + str(code) + ")")
elif code == 403:
logger.error("Access is forbidden. Check your credentials and permissions. (Error code " + str(code) + ")")
elif code == 404:
logger.error("The resource could not be found. Check your cloudfunctions url and namespace. (Error code " + str(code) + ")")
elif code >= 500:
logger.error("Internal server error. (Error code " + str(code) + ")")
else:
logger.error("Unexpected error code: " + str(code))
errorsInResponse(response.json())
return False
return True
def isActionSequence(action):
for annotation in action['annotations']:
if 'key' in annotation and annotation['key'] == 'exec':
if 'value' in annotation and annotation['value'] == 'sequence':
return True;
return False
config = Cfg(args)
logger.info('STARTING: '+ os.path.basename(__file__))
namespace = getRequiredParameter(config, 'cloudfunctions_namespace')
urlNamespace = quote(namespace)
auth = getParametersCombination(config, 'cloudfunctions_apikey', ['cloudfunctions_password', 'cloudfunctions_username'])
package = getRequiredParameter(config, 'cloudfunctions_package')
cloudfunctionsUrl = getRequiredParameter(config, 'cloudfunctions_url')
functionDir = getRequiredParameter(config, 'common_functions')
if 'cloudfunctions_apikey' in auth:
username, password = convertApikeyToUsernameAndPassword(auth['cloudfunctions_apikey'])
else:
username = auth['cloudfunctions_username']
password = auth['cloudfunctions_password']
logger.info("Will delete cloud functions in package '" + package + "'.")
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
packageUrl = cloudfunctionsUrl + '/' + urlNamespace + '/packages/' + package
response = requests.get(packageUrl, auth=(username, password), headers={'Content-Type': 'application/json'})
if not handleResponse(response):
logger.critical("Unable to get information about package '" + package + "'.")
sys.exit(1)
actions = response.json()['actions']
# put the sequences at the beggining
actions.sort(key=lambda action: isActionSequence(action))
for action in actions:
name = action['name']
actionUrl = cloudfunctionsUrl + '/' + urlNamespace + '/actions/' + package + '/' + name
logger.verbose("Deleting action '" + name + "' at " + actionUrl)
response = requests.delete(actionUrl, auth=(username, password), headers={'Content-Type': 'application/json'})
if not handleResponse(response):
logger.critical("Unable to delete action " + name + "' at " + actionUrl)
sys.exit(1)
logger.verbose("Action deleted.")
logger.verbose("Deleting package '" + package + "' at " + packageUrl)
response = requests.delete(packageUrl, auth=(username, password), headers={'Content-Type': 'application/json'})
if not handleResponse(response):
logger.critical("Unable to delete package '" + package + "' at " + packageUrl)
sys.exit(1)
logger.verbose("Package deleted.")
logger.info("Cloud functions in package successfully deleted.")
if __name__ == '__main__':
main(sys.argv[1:])