def merge_file(self, kitchen, recipe, file_path, file_contents, orig_head, last_file_sha): """ Returns the result of merging a local file with the latest version on the remote. This does not cause any side-effects on the server, and no actual merge is performed in the remote repo. /v2/file/merge/<string:kitchenname>/<string:recipename>/<path:filepath>, methods=['POST'] :param kitchen: name of the kitchen where this file lives :param recipe: name of the recipe that owns this file :param file_path: path to the file, relative to the recipe :param file_contents: contents of the file :param orig_head: sha of commit head of the branch when this file was obtained. :param last_file_sha: The sha of the file when it was obtained from the server. :return: dict """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False or \ recipe is None or isinstance(recipe, basestring) is False or \ file_path is None or isinstance(file_path, basestring) is False or \ orig_head is None or isinstance(orig_head, basestring) is False or \ last_file_sha is None or isinstance(last_file_sha, basestring) is False or \ file_contents is None: rc.set(rc.DK_FAIL, 'One or more parameters is invalid. ') return rc params = dict() params['orig_head'] = orig_head params['last_file_sha'] = last_file_sha params['content'] = file_contents adjusted_file_path = file_path url = '%s/v2/file/merge/%s/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe, adjusted_file_path) try: response = requests.post(url, data=json.dumps(params), headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError, TypeError), c: print "merge_file: exception: %s" % str(c) return None
def orderrun_detail(self, kitchen, pdict, return_all_data=False): """ api.add_resource(OrderDetailsV2, '/v2/order/details/<string:kitchenname>', methods=['POST']) :param self: DKCloudAPI :param kitchen: string :param pdict: dict :param return_all_data: boolean :rtype: DKReturnCode """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen') return rc url = '%s/v2/order/details/%s' % (self.get_url_for_direct_rest_call(), kitchen) try: response = requests.post(url, data=json.dumps(pdict), headers=self._get_common_headers()) rdict = self._get_json(response) if False: import pickle pickle.dump(rdict, open("files/orderrun_detail.p", "wb")) pass except (RequestException, ValueError), c: s = "orderrun_detail: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def list_kitchen(self): rc = DKReturnCode() url = '%s/v2/kitchen/list' % (self.get_url_for_direct_rest_call()) try: response = requests.get(url, headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError, TypeError), c: rc.set(rc.DK_FAIL, 'list_kitchen: exception: %s' % str(c)) return rc
def delete_orderrun(self, orderrun_id): """ api.add_resource(ServingDeleteV2, '/v2/serving/delete/<string:servingid>', methods=['DELETE']) :param self: DKCloudAPI :param orderrun_id: string :rtype: DKReturnCode """ rc = DKReturnCode() if orderrun_id is None or isinstance(orderrun_id, basestring) is False: rc.set(rc.DK_FAIL, 'issue with orderrun_id') return rc orderrun_id2 = urllib.quote(orderrun_id) url = '%s/v2/serving/delete/%s' % (self.get_url_for_direct_rest_call(), orderrun_id2) try: response = requests.delete(url, headers=self._get_common_headers()) rdict = self._get_json(response) if DKCloudAPI._valid_response(response): rc.set(rc.DK_SUCCESS, None, None) return rc else: arc = DKAPIReturnCode(rdict, response) rc.set(rc.DK_FAIL, arc.get_message()) return rc except (RequestException, ValueError), c: s = "orderrun_delete: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def put_kitchen_settings(self, kitchen_name, kitchen_dict, msg): rc = DKReturnCode() try: kitchen_json = json.dumps(kitchen_dict) except ValueError as ve: # Make sure this is valid json rc.set(rc.DK_FAIL, ve.message) return rc d1 = dict() d1['kitchen.json'] = kitchen_dict d1['message'] = msg url = '%s/v2/kitchen/settings/%s' % (self.get_url_for_direct_rest_call(), kitchen_name) try: response = requests.put(url, headers=self._get_common_headers(), data=json.dumps(d1)) rdict = self._get_json(response) except (RequestException, ValueError, TypeError) as c: rc.set(rc.DK_FAIL, 'settings_kitchen: exception: %s' % str(c)) return rc if DKCloudAPI._valid_response(response): rc.set(rc.DK_SUCCESS, None, rdict) else: arc = DKAPIReturnCode(rdict, response) rc.set(rc.DK_FAIL, arc.get_message()) return rc
def modify_kitchen_settings(self, kitchen_name, add=(), unset=()): rc = self.get_kitchen_settings(kitchen_name) if not rc.ok(): return rc kitchen_json = rc.get_payload() overrides = kitchen_json['recipeoverrides'] msg = '' commit_message = '' if len(add) > 0: for add_this in add: matches = [existing_override for existing_override in overrides if existing_override['variable'] == add_this[0]] if len(matches) == 0: overrides.append({'variable': add_this[0], 'value': add_this[1], 'category':'from_command_line'}) else: matches[0]['value'] = add_this[1] msg += "{} added with value '{}'\n".format(add_this[0], add_this[1]) if len(commit_message) != 0: commit_message += " ; {} added".format(add_this[0]) else: commit_message += "{} added".format(add_this[0]) # tom_index = next(index for (index, d) in enumerate(lst) if d["name"] == "Tom") # might be a string? if len(unset) > 0: if isinstance(unset, list) or isinstance(unset, tuple): for unset_this in unset: match_index = next((index for (index, d) in enumerate(overrides) if d["variable"] == unset_this), None) if match_index is not None: del overrides[match_index] msg += "{} unset".format(unset_this) if len(commit_message) != 0: commit_message += " ; {} unset".format(unset_this) else: commit_message += "{} unset".format(unset_this) else: match_index = next((index for (index, d) in enumerate(overrides) if d["variable"] == unset), None) if match_index is not None: del overrides[match_index] msg += "{} unset".format(unset) if len(commit_message) != 0: commit_message += " ; {} unset".format(unset) else: commit_message += "{} unset".format(unset) rc = self.put_kitchen_settings(kitchen_name, kitchen_json, commit_message) if not rc.ok(): return rc rc = DKReturnCode() rc.set(rc.DK_SUCCESS, msg, overrides) return rc
def get_compiled_serving(self, kitchen, recipe_name, variation_name): """ get the compiled version of arecipe with variables applied for a specific variation in a kitchen returns a dictionary '/v2/servings/compiled/get/<string:kitchenname>/<string:recipename>/<string:variationname>', methods=['GET'] :param self: DKCloudAPI :param kitchen: basestring :param recipe_name: basestring -- kitchen name, basestring :param variation_name: basestring message -- name of variation, basestring :rtype: dict """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen') return rc if recipe_name is None or isinstance(recipe_name, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe_name') return rc if variation_name is None or isinstance(variation_name, basestring) is False: rc.set(rc.DK_FAIL, 'issue with variation_name') return rc url = '%s/v2/servings/compiled/get/%s/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe_name, variation_name) try: response = requests.get(url, headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: rc.set(rc.DK_FAIL, "get_compiled_serving: exception: %s" % str(c)) return rc
def recipe_tree(self, kitchen, recipe): """ gets the status of a recipe :param self: DKCloudAPI :param kitchen: string :param recipe: string :rtype: dict """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen parameter') return rc if recipe is None or isinstance(recipe, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe parameter') return rc url = '%s/v2/recipe/tree/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe) try: response = requests.get(url, headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: s = "recipe_tree: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def merge_kitchens_improved(self, from_kitchen, to_kitchen, resolved_conflicts=None): """ merges kitchens '/v2/kitchen/merge/<string:kitchenname>/<string:kitchenname>', methods=['POST'] :param resolved_conflicts: :param self: DKCloudAPI :param from_kitchen: string :param to_kitchen: string :rtype: dict """ rc = DKReturnCode() if from_kitchen is None or isinstance(from_kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with from kitchen') return rc if to_kitchen is None or isinstance(to_kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with to kitchen') return rc url = '%s/v2/kitchen/merge/%s/%s' % (self.get_url_for_direct_rest_call(), from_kitchen, to_kitchen) try: if resolved_conflicts is not None and len(resolved_conflicts) > 0: data = dict() data['resolved_conflicts'] = resolved_conflicts response = requests.post(url, data=json.dumps(data), headers=self._get_common_headers()) else: response = requests.post(url, headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError, TypeError), c: rc.set("merge_kitchens: exception: %s" % str(c)) return rc
def list_order(self, kitchen, save_to_file=None): """ List the orders for a kitchen or recipe """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen parameter') return rc url = '%s/v2/order/status/%s' % (self.get_url_for_direct_rest_call(), kitchen) try: response = requests.get(url, headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: s = "get_recipe: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def recipe_create(self, kitchen, name): rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen parameter') return rc url = '%s/v2/recipe/create/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen,name) try: start_time = time.time() response = requests.post(url, headers=self._get_common_headers()) elapsed_recipe_status = time.time() - start_time print 'list_recipe - elapsed: %d' % elapsed_recipe_status rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: s = "list_recipe: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def order_delete_all(self, kitchen): """ api.add_resource(OrderDeleteAllV2, '/v2/order/deleteall/<string:kitchenname>', methods=['DELETE']) :param self: DKCloudAPI :param kitchen: string :rtype: DKReturnCode """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen') return rc url = '%s/v2/order/deleteall/%s' % (self.get_url_for_direct_rest_call(), kitchen) try: response = requests.delete(url, headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError), c: s = "order_delete_all: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def order_delete_one(self, order_id): """ api.add_resource(OrderDeleteV2, '/v2/order/delete/<string:orderid>', methods=['DELETE']) :param self: DKCloudAPI :param order_id: string :rtype: DKReturnCode """ rc = DKReturnCode() if order_id is None or isinstance(order_id, basestring) is False: rc.set(rc.DK_FAIL, 'issue with order_id') return rc order_id2 = urllib.quote(order_id) url = '%s/v2/order/delete/%s' % (self.get_url_for_direct_rest_call(), order_id2) try: response = requests.delete(url, headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError), c: s = "order_delete_one: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def orderrun_stop(self, orderrun_id): """ api.add_resource(ServingStopV2, '/v2/serving/stop/<string:servingid>', methods=['Put']) :param self: DKCloudAPI :param orderrun_id: string :rtype: DKReturnCode """ rc = DKReturnCode() if orderrun_id is None or isinstance(orderrun_id, basestring) is False: rc.set(rc.DK_FAIL, 'issue with orderrun_id') return rc orderrun_id2 = urllib.quote(orderrun_id) url = '%s/v2/serving/stop/%s' % (self.get_url_for_direct_rest_call(), orderrun_id2) try: response = requests.put(url, headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError), c: s = "order_stop: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def get_kitchen_settings(self, kitchen_name): rc = DKReturnCode() url = '%s/v2/kitchen/settings/%s' % (self.get_url_for_direct_rest_call(), kitchen_name) try: response = requests.get(url, headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError, TypeError) as c: rc.set(rc.DK_FAIL, 'settings_kitchen: exception: %s' % str(c)) return rc if DKCloudAPI._valid_response(response): rc.set(rc.DK_SUCCESS, None, rdict) else: arc = DKAPIReturnCode(rdict, response) rc.set(rc.DK_FAIL, arc.get_message()) return rc
def create_order(self, kitchen, recipe_name, variation_name, node_name=None): """ Full graph '/v2/order/create/<string:kitchenname>/<string:recipename>/<string:variationname>', methods=['PUT'] Single node '/v2/order/create/onenode/<string:kitchenname>/<string:recipename>/<string:variationname>/<string:nodename', methods=['PUT'] """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen') return rc if recipe_name is None or isinstance(recipe_name, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe_name') return rc if variation_name is None or isinstance(variation_name, basestring) is False: rc.set(rc.DK_FAIL, 'issue with variation_name') return rc if node_name is None: url = '%s/v2/order/create/%s/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe_name, variation_name) else: url = '%s/v2/order/create/onenode/%s/%s/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe_name, variation_name, node_name) try: response = requests.put(url, headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError), c: s = "create_order: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def delete_kitchen(self, existing_kitchen_name, message): rc = DKReturnCode() if existing_kitchen_name is None: rc.set(rc.DK_FAIL, 'Need to supply an existing kitchen name') return rc if isinstance(existing_kitchen_name, basestring) is False: rc.set(rc.DK_FAIL, 'Kitchen name needs to be a string') return rc if message is None or isinstance(message, basestring) is False: message = 'delete_kitchen' pdict = dict() pdict[DKCloudAPI.MESSAGE] = message url = '%s/v2/kitchen/delete/%s' % (self.get_url_for_direct_rest_call(), existing_kitchen_name) try: response = requests.delete(url, data=json.dumps(pdict), headers=self._get_common_headers()) rdict = self._get_json(response) except (RequestException, ValueError, TypeError), c: rc.set(rc.DK_FAIL, 'delete_kitchens: exception: %s' % str(c)) return rc
def secret_delete(self,path): rc = DKReturnCode() path = path or '' url = '%s/v2/secret/%s' % (self.get_url_for_direct_rest_call(), path) try: start_time = time.time() response = requests.delete(url, headers=self._get_common_headers()) elapsed_recipe_status = time.time() - start_time print 'secret_write - elapsed: %d' % elapsed_recipe_status rdict = self._get_json(response) if DKCloudAPI._valid_response(response): rc.set(rc.DK_SUCCESS, None, None) else: arc = DKAPIReturnCode(rdict, response) rc.set(rc.DK_FAIL, arc.get_message()) return rc except (RequestException, ValueError, TypeError), c: s = "secret_delete: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def get_recipe(self, kitchen, recipe, list_of_files=None): rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen parameter') return rc if recipe is None or isinstance(recipe, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe parameter') return rc url = '%s/v2/recipe/get/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe) try: if list_of_files is not None: params = dict() params['recipe-files'] = list_of_files response = requests.post(url, data=json.dumps(params), headers=self._get_common_headers()) else: response = requests.post(url, headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: s = "get_recipe: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def delete_file(self, kitchen, recipe, message, recipe_file_key, recipe_file): rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen parameter') return rc if recipe is None or isinstance(recipe, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe parameter') return rc if recipe_file_key is None or isinstance(recipe_file_key, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe_file_key parameter') return rc if recipe_file is None or isinstance(recipe_file, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe_file parameter') return rc pdict = dict() pdict[self.MESSAGE] = message pdict[self.FILEPATH] = recipe_file_key pdict[self.FILE] = recipe_file url = '%s/v2/recipe/delete/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe) try: response = requests.delete(url, data=json.dumps(pdict), headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: s = "delete_file: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc
def add_file(self, kitchen, recipe, message, api_file_key, file_contents): """ returns True for success or False for failure '/v2/recipe/create/<string:kitchenname>/<string:recipename>', methods=['PUT'] :param self: DKCloudAPI :param kitchen: basestring :param recipe: basestring -- kitchen name, basestring :param message: basestring message -- commit message, basestring :param api_file_key: -- file name and path to the file name, relative to the recipe root :param file_contents: -- character string of the recipe file to update :rtype: boolean """ rc = DKReturnCode() if kitchen is None or isinstance(kitchen, basestring) is False: rc.set(rc.DK_FAIL, 'issue with kitchen parameter') return rc if recipe is None or isinstance(recipe, basestring) is False: rc.set(rc.DK_FAIL, 'issue with recipe parameter') return rc if api_file_key is None or isinstance(api_file_key, basestring) is False: rc.set(rc.DK_FAIL, 'issue with api_file_key parameter') return rc if file_contents is None or isinstance(file_contents, basestring) is False: rc.set(rc.DK_FAIL, 'issue with file_contents parameter') return rc pdict = dict() pdict[self.MESSAGE] = message pdict[self.FILEPATH] = api_file_key pdict[self.FILE] = file_contents url = '%s/v2/recipe/create/%s/%s' % (self.get_url_for_direct_rest_call(), kitchen, recipe) try: response = requests.put(url, data=json.dumps(pdict), headers=self._get_common_headers()) rdict = self._get_json(response) pass except (RequestException, ValueError, TypeError), c: s = "add_file: exception: %s" % str(c) rc.set(rc.DK_FAIL, s) return rc