def populate_cloudids_choices(self, request, context): try: cloudid = [] cloudlist = tenantclouds.objects(tenantid=self.request.user.tenantid.id) roles = roledetail.objects(id = context['id']).first() for cloud in cloudlist: if roles.policy: for a in roles.policy: if cloud.name == a.cloudid.name: if a.cloudid.platform != "Cnext": cloud.__dict__['allowed'] = a.allowed else: pass cloudid.append((cloud.id,cloud)) else: cloudid.append((cloud.id,cloud)) else: cloudid.append((cloud.id,cloud)) cloudid = set(cloudid) cloudid = list(cloudid) cloudid.insert(0, ("", _("Select Cloud"))) except Exception,e: messages.error(request,_(e.message)) LOG.error(e.message) cloudid = []
def populate_cloudid_choices(self, request, context): try: cloudid = [] cloudlist = tenantclouds.objects(tenantid=self.request.user.tenantid.id) cloudid = [(cloud.id,cloud) for cloud in cloudlist] cloudid.insert(0, ("", _("Select Cloud"))) except Exception, e: messages.error(request,_(e.message)) LOG.error(e.message) cloudid = []
def populate_cloudslist_choices(self, request, context): try: roles = [] id = context.get('id', None) cloud = tenantclouds.objects(id = id).first() roles.append((cloud.id,cloud)) except Exception: exceptions.handle(request, _('Unable to retrieve list of Cloud Details')) roles = [] return roles
def populate_cloudid_choices(self, request, context): try: clouds =[] id = context['id'] roles = roledetail.objects(id =id).first() for policy in roles.policy: cloudlist = tenantclouds.objects(id = policy.cloudid.id).first() if (cloudlist.id,cloudlist.name) in clouds: pass else: clouds.append((cloudlist.id,cloudlist.name)) except Exception, e: messages.error(request,_(e.message)) LOG.error(e.message) clouds = []
def __init__(self, *args, **kwargs): super(CloudEditAction, self).__init__(*args, **kwargs) try: region_list = get_regions_wo_connection() tenantcloud_id = args[-1]["id"] cloud = tenantclouds.objects(id = tenantcloud_id).first() cloud_obj = clouds.objects(id=cloud.cloudid.id).first() self.fields['username1'].label = _(cloud_obj.credential_fields[0]) self.fields["password1"].label = _(cloud_obj.credential_fields[1]) if cloud_obj.name == "Amazon": self.fields["endpoint1"] = forms.ChoiceField(label=_("Default Region"), choices = region_list, help_text=_("Select default region")) self.fields["endpoint1"].label = _(cloud_obj.credential_fields[2]) else: self.fields["endpoint1"].label = _(cloud_obj.credential_fields[2]) except Exception, e: messages.error(self.request,_(e.message)) LOG.error(e.message)
def switch_cnext_tenants(request, tenant_id, redirect_field_name=REDIRECT_FIELD_NAME): """ Switches an authenticated user from one project to another. """ LOG.debug('Switching to tenant %s for user "%s".' % (tenant_id, request.user.username)) user = get_user_document().objects(username=request.user.username).first() cnext_tenant = tenantclouds.objects(id=tenant_id).first() user.cnextpublickey = cnext_tenant["cloud_meta"]["publickey"] user.cnextprivatekey = cnext_tenant["cloud_meta"]["privatekey"] user.cnextendpoint = cnext_tenant["cloud_meta"]["endpoint"] user.cnextname = cnext_tenant["name"] user.save() redirect_to = request.REQUEST.get(redirect_field_name, '') if not is_safe_url(url=redirect_to, host=request.get_host()): redirect_to = settings.LOGIN_REDIRECT_URL return shortcuts.redirect(redirect_to)
def handle(self, request, data): user = get_user_document().objects(username=request.user.username).first() openstack_clouds = sum([[y.cloudid for y in i.policy if y.cloudid.platform == "Openstack"] for i in request.user.roles], []) for cloud in openstack_clouds: if str(cloud.id) == str(data['account_name']): os_cloud = tenantclouds.objects(id = cloud.id).first() openstack_user = openstack_authenticate.authenticate(user_domain_name=None, username=os_cloud.cloud_meta['publickey'], password=encode_decode(os_cloud.cloud_meta['privatekey'],'decode'), auth_url= os_cloud.cloud_meta['endpoint']) utoken = openstack_user.token if utoken: delete_token(user.endpoint,user.token.id) otoken = trail.DocToken(user=utoken.user, user_domain_id=utoken.user_domain_id, id=utoken.id, project=utoken.project, tenant=utoken.project, domain=utoken.domain, roles=utoken.roles, serviceCatalog=utoken.serviceCatalog ) user.token = otoken user.authorized_tenants = [remove_tenants(d.__dict__) for d in openstack_user.authorized_tenants] user.service_catalog = openstack_user.service_catalog user.services_region = openstack_user.services_region user.project_name = openstack_user.project_name user.tenant_name = openstack_user.tenant_name user.tenant_id = openstack_user.tenant_id user.project_id = openstack_user.project_id user.endpoint = os_cloud.cloud_meta['endpoint'] + "" user.openstackname = os_cloud.name user.save() return True return False
def handle(self, request, context): id = context.get('id', None) username = context.get('username', None) cloudname = context.get('cloudname', None) password = context.get('password', None) endpoint = context.get('endpoint', None) if (username == '') & (password == ''): username = context.get('publickey', None) password = context.get('secretkey', None) try: user = get_user_document().objects(username=request.user.username).first() cloud = tenantclouds.objects(id = id).first() if cloud.platform =="Amazon": status = test(endpoint,username,password) if status == True: if user.awsname == cloud.name: user.awspublickey = username user.awsprivatekey = trail.encode_decode(password,"encode") user.awsendpoint = endpoint user.awsname = cloudname else: return False if cloud.platform =="Cnext": httpInst = httplib2.Http() httpInst.add_credentials(name = username, \ password = password) url = endpoint + ":8130/apiv2/instance" resp, body = httpInst.request(url) if resp.status == 200 : if user.cnextname == cloud.name: user.cnextpublickey = username user.cnextprivatekey = trail.encode_decode(password,"encode") user.cnextendpoint = endpoint user.cnextname = cloudname else: return False if cloud.platform =="Hpcloud": openstack_user = openstack_authenticate.authenticate(user_domain_name=None,username=username, password=password, auth_url= endpoint) utoken = openstack_user.token if utoken: hp = Hpclouddata.objects(id = request.user.hp_attr.id).first() hpcloud = tenantclouds.objects(id = hp.hpcloudid.id).first() if hpcloud.name == cloud.name: otoken = trail.DocToken(user=utoken.user, user_domain_id=utoken.user_domain_id, id=utoken.id, project=utoken.project, tenant=utoken.project, domain=utoken.domain, roles=utoken.roles, serviceCatalog=utoken.serviceCatalog ) hp.token = otoken hp.authorized_tenants = [remove_tenants(d.__dict__) for d in openstack_user.authorized_tenants] hp.service_catalog = openstack_user.service_catalog hp.services_region = openstack_user.services_region hp.project_name = openstack_user.project_name hp.tenant_name = openstack_user.tenant_name hp.tenant_id = openstack_user.tenant_id hp.project_id = openstack_user.project_id hp.endpoint = endpoint hp.save() user.hpname = cloudname else: return False if cloud.platform =="Openstack": openstack_user = openstack_authenticate.authenticate(user_domain_name=None,username=username, password=password, auth_url= endpoint) utoken = openstack_user.token if utoken: if user.openstackname == cloud.name: delete_token(user.endpoint,user.token.id) otoken = trail.DocToken(user=utoken.user, user_domain_id=utoken.user_domain_id, id=utoken.id, project=utoken.project, tenant=utoken.project, domain=utoken.domain, roles=utoken.roles, serviceCatalog=utoken.serviceCatalog ) user.token = otoken user.authorized_tenants = [remove_tenants(d.__dict__) for d in openstack_user.authorized_tenants] user.service_catalog = openstack_user.service_catalog user.services_region = openstack_user.services_region user.project_name = openstack_user.project_name user.tenant_name = openstack_user.tenant_name user.tenant_id = openstack_user.tenant_id user.project_id = openstack_user.project_id user.endpoint = endpoint + "" user.openstackname = cloudname else: delete_token(endpoint,utoken.id) else: return False clouds = tenantclouds.objects(id = id).update(set__name = cloudname,set__cloud_meta ={"publickey":username,"privatekey":trail.encode_decode(password,"encode"),"endpoint":endpoint}) user.save() refresh_session_policies(request, request.user) return True except Exception,e: messages.error(request,_(e.message)) LOG.error(e.message)