def post(self): # lookup user's auth info user_info = User.get_by_id(long(self.user_id)) # load projects pulldown self.form.project.choices = [] # public + private projects = Project.get_available(user_info.key) for project in projects: self.form.project.choices.insert(0, (str(project.key.id()), project.name)) # insert images into list for wisp self.form.image.choices=[('custom', "Dynamic Image URL")] images = Image.get_all() for image in images: self.form.image.choices.insert(0, (str(image.key.id()), image.description)) # check what was returned from form validates if not self.form.validate(): self.add_message("The new wisp form did not validate.", "error") return self.get() # load form values name = self.form.name.data.strip() ssh_key = self.form.ssh_key.data.strip() dynamic_image_url = self.form.dynamic_image_url.data.strip() image_container_format = self.form.image_container_format.data.strip() image_disk_format = self.form.image_disk_format.data.strip() post_creation = self.form.post_creation.data.strip() callback_url = self.form.callback_url.data.strip() default = self.form.default.data # no strip cause bool # check if project is selected if self.form.wisp_type.data.strip() == 'project': project = Project.get_by_id(long(self.form.project.data.strip())).key else: project = None # hack up form to deal with custom image if self.form.image.data.strip() == "custom": image = None else: image = Image.get_by_id(long(self.form.image.data.strip())).key # hack up form to deal with custom callback if self.form.wisp_type.data.strip() == "custom": image = None ssh_key = None dynamic_image_url = None post_creation = None elif self.form.wisp_type.data.strip() == "project": image = None dynamic_image_url = None post_creation = None else: callback_url = None # check if we have it already if Wisp.get_by_user_name(user_info.key, name): self.add_message("A wisp with that name already exists in this account!", "error") return self.redirect_to('account-wisps') # check if we need to force default setting for first new wisp if not Wisp.get_by_user(user_info.key): default = True # save the new wisp in our database wisp = Wisp( name = name, owner = user_info.key, image = image, ssh_key = ssh_key, dynamic_image_url = dynamic_image_url, image_container_format = image_container_format, image_disk_format = image_disk_format, post_creation = post_creation, callback_url = callback_url, project = project ) wisp.put() # set default if true if default: Wisp.set_default(wisp) # log to alert self.add_message("Wisp %s successfully created!" % name, "success") # give it a few seconds to update db, then redirect time.sleep(1) return self.redirect_to('account-wisps-detail', wisp_id=wisp.key.id())
def post(self): # lookup user's auth info user_info = User.get_by_id(long(self.user_id)) # insert images into list for wisp self.form.image.choices=[('custom', "Dynamic Image URL")] images = Image.get_all() for image in images: self.form.image.choices.insert(0, (str(image.key.id()), image.description)) # check what was returned from form validates if not self.form.validate(): self.add_message("The new wisp form did not validate.", "error") return self.get() # load form values name = self.form.name.data.strip() ssh_key = self.form.ssh_key.data.strip() dynamic_image_url = self.form.dynamic_image_url.data.strip() post_creation = self.form.post_creation.data.strip() callback_url = self.form.callback_url.data.strip() default = self.form.default.data # no strip cause bool # hack up form to deal with custom image if self.form.image.data.strip() == "custom": image = None else: image = Image.get_by_id(int(self.form.image.data.strip())).key # hack up form to deal with custom callback if self.form.callback.data.strip() == "custom": image = None ssh_key = None dynamic_image_url = None post_creation = None else: callback_url = None # check if we have it already if Wisp.get_by_user_name(user_info.key, name): self.add_message("A wisp with that name already exists in this account!", "error") return self.redirect_to('account-wisps') # save the new wisp in our database wisp = Wisp( name = name, owner = user_info.key, image = image, ssh_key = ssh_key, dynamic_image_url = dynamic_image_url, post_creation = post_creation, callback_url = callback_url, ) wisp.put() # set default if true if default: Wisp.set_default(wisp) # log to alert self.add_message("Wisp %s successfully created!" % name, "success") # give it a few seconds to update db, then redirect time.sleep(1) return self.redirect_to('account-wisps')
def post(self): # try to pull get user info from a browser based session if self.user_id: user_info = User.get_by_id(long(self.user_id)) else: user_info = None # paramters, assume failure, response type params = {} params['response'] = "error" self.response.headers['Content-Type'] = "application/json" # response headers self.response.headers['Content-Type'] = "application/json" self.response.headers['Access-Control-Allow-Origin'] = '*' # load various variables body = json.loads(self.request.body) # SSH key (not required) try: ssh_key = body['ssh_key'] except: ssh_key = "" # load project if we have it try: project_id = body['project_id'] project = Project.get_by_id(long(project_id)) except: project = None # handle project wisps differently if project: wisp = Wisp().from_project( ssh_key, project, user_info ) else: try: post_creation = body['post_creation'] except: post_creation = "" try: image_id = body['image_id'] image = Image.get_by_id(long(image_id)) except: try: dynamic_image_url = body['dynamic_image_url'] except: # we don't have an image or a URL, so nothing can do params['message'] = "Wisps require an image to boot." self.response.set_status(401) return self.render_template('api/response.json', **params) # disk and container formats if they were sent (usually qcow/bare) try: image_disk_format = body['image_disk_format'] except: image_disk_format = "qcow2" try: image_container_format = body['image_container_format'] except: image_container_format = "bare" # create an anonymous wisp if we don't have it already wisp = Wisp().from_stock( ssh_key, post_creation, dynamic_image_url, image_disk_format, image_container_format, user_info ) if wisp: # return JSON response params['response'] = "success" params['wisp'] = wisp return self.render_template('api/wisp.json', **params) else: params['message'] = "Wisp creation failed." self.response.set_status(401) return self.render_template('api/response.json', **params)