def _test_conflict_dep(self, packages, from_file, dep_exists_action=''): packages.create = mock.MagicMock( side_effect=[common_exceptions.HTTPConflict("Conflict"), common_exceptions.HTTPConflict("Conflict"), None]) packages.filter.return_value = [mock.Mock(id='test_id')] pkg1 = make_pkg( {'FullName': 'first_app', 'Require': {'second_app': '1.0'}, }) pkg2 = make_pkg({'FullName': 'second_app', }) def side_effect(name): if 'first_app' in name: return mc_utils.Package(mc_utils.File(pkg1)) if 'second_app' in name: return mc_utils.Package(mc_utils.File(pkg2)) from_file.side_effect = side_effect arglist = ['first_app', '--exists-action', 's', '--dep-exists-action', dep_exists_action] parsed_args = self.check_parser(self.cmd, arglist, []) self.cmd.take_action(parsed_args)
def update(self, app_id, props_to_remove=None, **new_props): new_keywords = [] new_name = new_props.get('display_name') new_tags = new_props.get('tags') if new_name: new_keywords.extend(self._keywords_from_display_name(new_name)) if new_tags: new_keywords.extend(new_tags) if new_keywords: new_props['keywords'] = new_keywords visibility = new_props.get('visibility') if visibility == 'public': package = self.client.artifacts.get(app_id) # NOTE(ativelkov): this is very racy, but until we have a chance to # enforce uniqueness right in glance this is the only way to do it existing = self.list(name=package.name, version=package.version, visibility='public') try: while True: package = next(existing) if package.id == app_id: continue else: raise exc.HTTPConflict("Package already exists") except StopIteration: pass return self.client.artifacts.update(app_id, remove_props=props_to_remove, **new_props)
def create(self, fqn, data, **kwargs): package = utils.Package.from_file(data) manifest = package.manifest package_draft = { 'name': manifest.get('FullName', fqn), 'version': manifest.get('Version', '0.0.0'), 'description': manifest.get('Description'), 'display_name': manifest.get('Name', fqn), 'type': manifest.get('Type', 'Application'), 'author': manifest.get('Author'), 'tags': manifest.get('Tags', []), 'class_definitions': package.classes.keys() } for k, v in six.iteritems(kwargs): package_draft[k] = v inherits = self._get_local_inheritance(package.classes) package_draft['inherits'] = inherits keywords = self._keywords_from_display_name( package_draft['display_name']) keywords.extend(package_draft['tags']) package_draft['keywords'] = keywords # NOTE(ativelkov): this is very racy, but until we have a chance to # enforce uniqueness right in glance this is the only way to do it is_public = package_draft.get('visibility', 'private') if is_public: filters = {} else: filters = {'owner': self.tenant} existing = self.list(name=package_draft['name'], version=package_draft['version'], **filters) try: next(existing) raise exc.HTTPConflict("Package already exists") except StopIteration: pass res = self.client.artifacts.create(**package_draft) app_id = res.id self.client.artifacts.upload_blob(app_id, 'archive', package.file()) if package.logo is not None: self.client.artifacts.upload_blob(app_id, 'logo', package.logo) if package.ui is not None: self.client.artifacts.upload_blob(app_id, 'ui_definition', package.ui) package.file().close() self.client.artifacts.active(app_id) return self.client.artifacts.get(app_id)
def _test_conflict(self, packages, from_file, raw_input_mock, input_action, exists_action=''): packages.create = mock.MagicMock( side_effect=[common_exceptions.HTTPConflict("Conflict"), None]) packages.filter.return_value = [mock.Mock(id='test_id')] raw_input_mock.return_value = input_action args = TestArgs() args.exists_action = exists_action with tempfile.NamedTemporaryFile() as f: args.filename = [f.name] pkg = make_pkg({'FullName': f.name}) from_file.return_value = utils.Package(utils.File(pkg)) v1_shell.do_package_import(self.client, args) return f.name
def _test_conflict(self, packages, from_file, raw_input_mock, input_action, exists_action=''): packages.create = mock.MagicMock( side_effect=[common_exceptions.HTTPConflict("Conflict"), None]) packages.filter.return_value = [mock.Mock(id='test_id')] raw_input_mock.return_value = input_action with tempfile.NamedTemporaryFile() as f: pkg = make_pkg({'FullName': f.name}) from_file.return_value = mc_utils.Package(mc_utils.File(pkg)) if exists_action: arglist = [f.name, '--exists-action', exists_action] else: arglist = [f.name] parsed_args = self.check_parser(self.cmd, arglist, []) self.cmd.take_action(parsed_args) return f.name
def create(self, fqn, data, **kwargs): package = utils.Package.from_file(data) manifest = package.manifest package_draft = { 'name': manifest.get('FullName', fqn), 'version': manifest.get('Version', '0.0.0'), 'description': manifest.get('Description'), 'display_name': manifest.get('Name', fqn), 'type': manifest.get('Type', 'Application'), 'author': manifest.get('Author'), 'tags': manifest.get('Tags', []), 'class_definitions': package.classes.keys() } for k, v in kwargs.items(): package_draft[k] = v inherits = self._get_local_inheritance(package.classes, package.resolvers) # check for global inheritance ancestor_queue = collections.deque(inherits.keys()) while ancestor_queue: ancestor_name = ancestor_queue.popleft() child_classes = inherits[ancestor_name] ancestors = self.list(class_definitions=ancestor_name) for ancestor in ancestors: # check if ancestor inherits anything ancestor_inherits = \ ancestor.type_specific_properties.get('inherits', {}) for name, value in ancestor_inherits.items(): # check if this is the class we actually inherit if ancestor_name in value: ancestor_queue.append(name) inherits[name] = child_classes package_draft['inherits'] = inherits keywords = self._keywords_from_display_name( package_draft['display_name']) keywords.extend(package_draft['tags']) package_draft['keywords'] = keywords # NOTE(ativelkov): this is very racy, but until we have a chance to # enforce uniqueness right in glance this is the only way to do it visibility = package_draft.get('visibility', 'private') if visibility == 'public': filters = {} else: filters = {'owner': self.tenant} existing = self.list(name=package_draft['name'], version=package_draft['version'], **filters) try: next(existing) raise exc.HTTPConflict("Package already exists") except StopIteration: pass res = self.client.artifacts.create(**package_draft) app_id = res.id self.client.artifacts.upload_blob(app_id, 'archive', package.file()) if package.logo is not None: self.client.artifacts.upload_blob(app_id, 'logo', package.logo) if package.ui is not None: self.client.artifacts.upload_blob(app_id, 'ui_definition', package.ui) package.file().close() self.client.artifacts.active(app_id) return self.client.artifacts.get(app_id)