def import_resource(self, request, uuid=None): offering = self.get_object() marketplace_serializer = self.get_serializer(data=request.data) marketplace_serializer.is_valid(raise_exception=True) plan = marketplace_serializer.validated_data.get('plan', None) project = marketplace_serializer.validated_data['project'] backend_id = marketplace_serializer.validated_data['backend_id'] service_model = plugins.manager.get_service_model(offering.type) service = service_model.objects.get( settings=offering.scope, customer=project.customer ) spl_model = plugins.manager.get_spl_model(offering.type) spl = spl_model.objects.get(project=project, service=service) spl_url = reverse('{}-detail'.format(spl.get_url_name()), kwargs={'pk': spl.pk}) resource_data = { 'backend_id': backend_id, 'service_project_link': spl_url, } resource_viewset = plugins.manager.get_resource_viewset(offering.type) serializer_class = resource_viewset.import_resource_serializer_class serializer = serializer_class( data=resource_data, context=self.get_serializer_context() ) serializer.is_valid(raise_exception=True) try: resource = serializer.save() except IntegrityError: raise rf_exceptions.ValidationError(_('Resource is already registered.')) else: resource_imported.send( sender=resource.__class__, instance=resource, plan=plan, offering=offering, ) if resource_viewset.import_resource_executor: transaction.on_commit( lambda: resource_viewset.import_resource_executor.execute(resource) ) marketplace_resource = models.Resource.objects.get(scope=resource) resource_serializer = serializers.ResourceSerializer( marketplace_resource, context=self.get_serializer_context() ) return Response(data=resource_serializer.data, status=status.HTTP_201_CREATED)
def restore(self, request, uuid=None): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) restoration = serializer.save() executors.SnapshotRestorationExecutor().execute(restoration) serialized_volume = serializers.VolumeSerializer( restoration.volume, context={'request': self.request} ) resource_imported.send( sender=models.Volume, instance=restoration.volume, ) return response.Response(serialized_volume.data, status=status.HTTP_201_CREATED)
def execute(self, instance, user_id): node = instance content_type = ContentType.objects.get_for_model( openstack_tenant_models.Instance) flavor = node.initial_data['flavor'] system_volume_size = node.initial_data['system_volume_size'] system_volume_type = node.initial_data.get('system_volume_type') data_volumes = node.initial_data.get('data_volumes', []) image = node.initial_data['image'] subnet = node.initial_data['subnet'] group = node.initial_data['group'] tenant_spl = node.initial_data['tenant_service_project_link'] user = auth.get_user_model().objects.get(pk=user_id) post_data = { 'name': node.name, 'flavor': reverse('openstacktenant-flavor-detail', kwargs={'uuid': flavor}), 'image': reverse('openstacktenant-image-detail', kwargs={'uuid': image}), 'service_project_link': reverse('openstacktenant-spl-detail', kwargs={'pk': tenant_spl}), 'system_volume_size': system_volume_size, 'system_volume_type': system_volume_type and reverse( 'openstacktenant-volume-type-detail', kwargs={'uuid': system_volume_type}, ), 'data_volumes': [{ 'size': volume['size'], 'volume_type': volume.get('volume_type') and reverse( 'openstacktenant-volume-type-detail', kwargs={'uuid': volume.get('volume_type')}, ), } for volume in data_volumes], 'security_groups': [{ 'url': reverse('openstacktenant-sgp-detail', kwargs={'uuid': group}) }], 'internal_ips_set': [{ 'subnet': reverse('openstacktenant-subnet-detail', kwargs={'uuid': subnet}) }], 'user_data': utils.format_node_cloud_config(node), } view = InstanceViewSet.as_view({'post': 'create'}) response = common_utils.create_request(view, user, post_data) if response.status_code != status.HTTP_201_CREATED: raise exceptions.RancherException(response.data) instance_uuid = response.data['uuid'] instance = openstack_tenant_models.Instance.objects.get( uuid=instance_uuid) node.content_type = content_type node.object_id = instance.id node.state = models.Node.States.CREATING node.save() resource_imported.send( sender=instance.__class__, instance=instance, )
def execute(self, instance, node, user_id): cluster = instance content_type = ContentType.objects.get_for_model( openstack_tenant_models.Instance) flavor = node['flavor'] storage = node['storage'] image = node['image'] subnet = node['subnet'] roles = node['roles'] group = node['group'] tenant_spl = node['tenant_service_project_link'] user = auth.get_user_model().objects.get(pk=user_id) roles_command = [] if 'controlplane' in roles: roles_command.append('--controlplane') if 'etcd' in roles: roles_command.append('--etcd') if 'worker' in roles: roles_command.append('--worker') node_command = cluster.node_command + ' ' + ' '.join(roles_command) post_data = { 'name': cluster.name + '_rancher_node', 'flavor': reverse('openstacktenant-flavor-detail', kwargs={'uuid': flavor}), 'image': reverse('openstacktenant-image-detail', kwargs={'uuid': image}), 'service_project_link': reverse('openstacktenant-spl-detail', kwargs={'pk': tenant_spl}), 'system_volume_size': storage, 'security_groups': [{ 'url': reverse('openstacktenant-sgp-detail', kwargs={'uuid': group}) }], 'internal_ips_set': [{ 'subnet': reverse('openstacktenant-subnet-detail', kwargs={'uuid': subnet}) }], 'user_data': settings.WALDUR_RANCHER['RANCHER_NODE_CLOUD_INIT_TEMPLATE'].format( command=node_command) } view = InstanceViewSet.as_view({'post': 'create'}) response = common_utils.create_request(view, user, post_data) if response.status_code != status.HTTP_201_CREATED: six.reraise(exceptions.RancherException, response.data) instance_uuid = response.data['uuid'] instance = openstack_tenant_models.Instance.objects.get( uuid=instance_uuid) models.Node.objects.create( cluster=cluster, object_id=instance.id, content_type=content_type, controlplane_role='controlplane' in node['roles'], etcd_role='etcd' in node['roles'], worker_role='worker' in node['roles'], ) resource_imported.send( sender=instance.__class__, instance=instance, )