def varray_allow_tenant(self, varray, tenant): ''' Makes a REST API call to retrieve details of a varray based on its UUID ''' uri = self.varray_query(varray) from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) tenanturi = tenant_obj.tenant_query(tenant) parms = { 'add': [{ 'privilege': ['USE'], 'tenant': tenanturi, }] } body = json.dumps(parms) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "PUT", self.URI_VIRTUALARRAY_ACLS.format(uri), body) return s
def cluster_create(self, label, tenant, project, datacenter, vcenter): tenant_obj = Tenant(self.__ipAddr, self.__port) vdatacenterobj = VcenterDatacenter(self.__ipAddr, self.__port) projectobj = Project(self.__ipAddr, self.__port) if(tenant == None): tenant_uri = tenant_obj.tenant_getid() else: tenant_uri = tenant_obj.tenant_query(tenant) parms = { 'name' : label } #project if(project): if(tenant): projectname = tenant + "/" + project else: projectname = "" + "/" + project #on failure, query raise exception parms['project'] = projectobj.project_query(projectname) #datacenter if(datacenter): #on failure, query raise exception parms['vcenter_data_center'] = vdatacenterobj.vcenterdatacenter_query(datacenter, vcenter) body = json.dumps(parms) (s, h) = common.service_json_request(self.__ipAddr, self.__port, "POST", Cluster.URI_TENANTS_CLUSTERS.format(tenant_uri), body) o = common.json_decode(s)
def project_query(self, name): ''' Retrieves UUID of project based on its name Parameters: name: name of project Returns: UUID of project Throws: SOSError - when project name is not found ''' if (common.is_uri(name)): return name (tenant_name, project_name) = common.get_parent_child_from_xpath(name) from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) try: tenant_uri = tenant_obj.tenant_query(tenant_name) projects = self.project_list(tenant_uri) if(projects and len(projects) > 0): for project in projects: if (project): project_detail = self.project_show_by_uri(project['id']) if(project_detail and project_detail['name'] == project_name): return project_detail['id'] raise SOSError(SOSError.NOT_FOUND_ERR, 'Project: ' + project_name + ' not found') except SOSError as e: raise e
def project_query(self, name): ''' Retrieves UUID of project based on its name Parameters: name: name of project Returns: UUID of project Throws: SOSError - when project name is not found ''' if (common.is_uri(name)): return name (tenant_name, project_name) = common.get_parent_child_from_xpath(name) from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) try: tenant_uri = tenant_obj.tenant_query(tenant_name) projects = self.project_list(tenant_uri) if(projects and len(projects) > 0): for project in projects: if (project): project_detail = self.project_show_by_uri( project['id']) if(project_detail and project_detail['name'] == project_name): return project_detail['id'] raise SOSError(SOSError.NOT_FOUND_ERR, 'Project: ' + project_name + ' not found') except SOSError as e: raise e
def cluster_create(self, label, tenant, datacenter, vcenter): tenant_obj = Tenant(self.__ipAddr, self.__port) vdatacenterobj = VcenterDatacenter(self.__ipAddr, self.__port) if(tenant is None): tenant_uri = tenant_obj.tenant_getid() else: tenant_uri = tenant_obj.tenant_query(tenant) parms = {'name': label} # datacenter if(datacenter): # on failure, query raise exception parms['vcenter_data_center'] = \ vdatacenterobj.vcenterdatacenter_query( datacenter, vcenter, tenant) body = json.dumps(parms) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", Cluster.URI_TENANTS_CLUSTERS.format(tenant_uri), body) o = common.json_decode(s)
def add_or_remove_vcenter_acls(self, vcenter_name, tenants, operation): vcenter_uri = self.vcenter_query(vcenter_name, VCenter.VCENTERS_FROM_ALL_TENANTS) tenants_array = [] tenants_array = tenants.split(',') from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) acl_entries = [] for tenant in tenants_array: acl_entry = dict() acl_entry['privilege'] = ['USE'] acl_entry['tenant'] = str(obj.tenant_query(tenant)) acl_entries.append(acl_entry) request = {operation: acl_entries} body = json.dumps(request) (s, h) = common.service_json_request(self.__ipAddr, self.__port, "PUT", VCenter.URI_VCENTER_ACL.format(vcenter_uri), body) o = common.json_decode(s) return o
def varray_disallow_tenant(self, varray, tenant): ''' Makes a REST API call to retrieve details of a varray based on its UUID ''' uri = self.varray_query(varray) from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) tenanturi = tenant_obj.tenant_query(tenant) parms = { 'remove': [{ 'privilege': ['USE'], 'tenant': tenanturi, }] } body = json.dumps(parms) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "PUT", self.URI_VIRTUALARRAY_ACLS.format(uri), body) return s
def varray_list(self, vdcname=None, tenant=None): ''' Returns all the varrays in a vdc Parameters: Returns: JSON payload of varray list ''' vdcuri = None vdcrestapi = None if(tenant != None): from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) tenanturi = tenant_obj.tenant_query(tenant) if(vdcname != None): vdcrestapi = VirtualArray.URI_VIRTUALARRAY_BY_VDC_ID_AND_TENANT_ID.format(vdcname, tenanturi) else: vdcrestapi = VirtualArray.URI_VIRTUALARRAY_BY_TENANT_ID.format(tenanturi) else: if(vdcname != None): vdcrestapi = VirtualArray.URI_VIRTUALARRAY_BY_VDC_ID.format(vdcname) else: vdcrestapi = VirtualArray.URI_VIRTUALARRAY (s, h) = common.service_json_request( self.__ipAddr, self.__port, "GET", vdcrestapi, None) o = common.json_decode(s) returnlst = [] for item in o['varray']: returnlst.append(item['id']) return returnlst
def get_tenant_uri_from_name(self, tenantname): if (tenantname is None or tenantname == "" or tenantname.lower() == VCenter.VCENTERS_FROM_ALL_TENANTS.lower() or tenantname.lower() == VCenter.VCENTERS_WITH_NO_TENANTS.lower()): tenanturi = tenantname else: from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) tenanturi = obj.tenant_query(tenantname) return tenanturi
def policy_list(self, tenantname): tenant_obj = Tenant(self.__ipAddr, self.__port) tenant_uri = tenant_obj.tenant_query(tenantname) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "GET", Schedulepolicy.URI_SNAPSHOT_SCHEDULE_POLICY_CREATE.format( tenant_uri), None) res = common.json_decode(s) return res['schedule_policy']
def create( self, tenant, policyname, policytype, schedulefrequency, schedulerepeat, scheduletime, scheddom, scheddow, expiretype, expirevalue, ): tenant_obj = Tenant(self.__ipAddr, self.__port) tenant_uri = tenant_obj.tenant_query(tenant) schedule = dict() schedule = { "schedule_frequency": schedulefrequency, "schedule_repeat": schedulerepeat, "schedule_time": scheduletime, } if schedulefrequency == "weeks": schedule["schedule_day_of_week"] = scheddow elif schedulefrequency == "months": schedule["schedule_day_of_month"] = scheddom parms = {"policy_type": policytype, "policy_name": policyname} body = None parms["schedule"] = schedule snapshot_expire = dict() if expiretype or expirevalue: snapshot_expire = {"expire_type": expiretype, "expire_value": expirevalue} parms["snapshot_expire"] = snapshot_expire body = json.dumps(parms) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", Schedulepolicy.URI_SNAPSHOT_SCHEDULE_POLICY_CREATE.format(tenant_uri), body, ) return
def policy_list(self, tenantname): tenant_obj = Tenant(self.__ipAddr, self.__port) tenant_uri = tenant_obj.tenant_query(tenantname) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "GET", Schedulepolicy.URI_SNAPSHOT_SCHEDULE_POLICY_CREATE.format(tenant_uri), None, ) res = common.json_decode(s) return res["schedule_policy"]
def get_vcenter_uri_with_tenant_param(self, tenant): uri = "" if (tenant is None or tenant == "" or tenant.lower() == VCenter.VCENTERS_FROM_ALL_TENANTS.lower()): uri = VCenter.URI_VCENTERS_WITH_TENANT_PARAM.format(VCenter.VCENTERS_FROM_ALL_TENANTS) elif tenant.lower() == VCenter.VCENTERS_WITH_NO_TENANTS.lower(): uri = VCenter.URI_VCENTERS_WITH_TENANT_PARAM.format(tenant) else: from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) tenant_uri = obj.tenant_query(tenant) uri = VCenter.URI_VCENTERS_WITH_TENANT_PARAM.format(tenant_uri) return uri
def vcenterdatacenter_update(self, label, vcenter, tenantname, newtenantname): ''' updates a vcenterdatacenter parameters: label: label of the vcenterdatacenter Returns: JSON payload response ''' try: check = self.vcenterdatacenter_show(label, vcenter, tenantname) if check: raise SOSError(SOSError.ENTRY_ALREADY_EXISTS_ERR, "vcenterdatacenter " + label + ": found") except SOSError as e: if e.err_code == SOSError.ENTRY_ALREADY_EXISTS_ERR: uri = self.vcenterdatacenter_query( label, vcenter, VcenterDatacenter.DATACENTERS_FROM_ALL_TENANTS) params = dict() params['name'] = label if newtenantname is not None and newtenantname != 'null': from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) params['tenant'] = obj.tenant_query(newtenantname) elif newtenantname is not None: params['tenant'] = newtenantname body = json.dumps(params) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "PUT", VcenterDatacenter.URI_DATACENTER.format(uri), body) o = common.json_decode(s) return o else: raise e if not check: raise SOSError( SOSError.NOT_FOUND_ERR, "vcenterdatacenter with name " + label + " dost not exist")
def create( self, name, tenantname, type, hourOfDay, length, lengthType, dayOfWeek, dayOfMonth, minuteOfHour, lastDayOfMonth=False, ): new_request = { "tenant": tenantname, "hour_of_day_in_utc": hourOfDay, "minute_of_hour_in_utc": minuteOfHour, "execution_window_length": length, "execution_window_length_type": lengthType, "execution_window_type": type, "day_of_week": dayOfWeek, "day_of_month": dayOfMonth, "last_day_of_month": lastDayOfMonth, "name": name, } if minuteOfHour is not None: new_request["minute_of_hour_in_utc"] = minuteOfHour if tenantname is not None: from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) try: tenant_uri = tenant_obj.tenant_query(None) except SOSError as e: raise e new_request["tenant"] = tenant_uri body = json.dumps(new_request) command = singletonURIHelperInstance.getUri(self.COMPONENT_TYPE, "create") (s, h) = common.service_json_request(self.__ipAddr, self.__port, "POST", command, body) o = common.json_decode(s) return o
def vcenter_create(self, label, tenant, ipaddress, devport, username, password, osversion, usessl): ''' creates a vcenter parameters: label: label of the vcenter Returns: JSON payload response ''' try: check = self.vcenter_show(label, tenant) if (not check): raise SOSError(SOSError.NOT_FOUND_ERR, "vcenter " + label + ": not found") except SOSError as e: if (e.err_code == SOSError.NOT_FOUND_ERR): from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) uri = obj.tenant_query(tenant) var = dict() params = dict() params = { 'name': label, 'ip_address': ipaddress, 'os_version': osversion, 'port_number': devport, 'user_name': username, 'password': password, 'use_ssl': usessl } body = json.dumps(params) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", VCenter.URI_TENANTS_VCENTERS.format(uri), body) o = common.json_decode(s) return o else: raise e if (check): raise SOSError(SOSError.ENTRY_ALREADY_EXISTS_ERR, "vcenter with name " + label + " already exists")
def create(self, name, tenantname, type, hourOfDay, length, lengthType, dayOfWeek, dayOfMonth, minuteOfHour, lastDayOfMonth=False): new_request = { "tenant": tenantname, "hour_of_day_in_utc": hourOfDay, "minute_of_hour_in_utc": minuteOfHour, "execution_window_length": length, "execution_window_length_type": lengthType, "execution_window_type": type, "day_of_week": dayOfWeek, "day_of_month": dayOfMonth, "last_day_of_month": lastDayOfMonth, "name": name } if (minuteOfHour is not None): new_request["minute_of_hour_in_utc"] = minuteOfHour if (tenantname is not None): from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) try: tenant_uri = tenant_obj.tenant_query(None) except SOSError as e: raise e new_request["tenant"] = tenant_uri body = json.dumps(new_request) command = singletonURIHelperInstance.getUri(self.COMPONENT_TYPE, "create") (s, h) = common.service_json_request(self.__ipAddr, self.__port, "POST", command, body) o = common.json_decode(s) return o
def vcenter_create(self, label, tenant, ipaddress, devport, username, password, osversion, usessl): ''' creates a vcenter parameters: label: label of the vcenter Returns: JSON payload response ''' try: check = self.vcenter_show(label, tenant) if(not check): raise SOSError(SOSError.NOT_FOUND_ERR, "vcenter " + label + ": not found") except SOSError as e: if(e.err_code == SOSError.NOT_FOUND_ERR): from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) uri = obj.tenant_query(tenant) var = dict() params = dict() params = {'name': label, 'ip_address': ipaddress, 'os_version': osversion, 'port_number': devport, 'user_name': username, 'password': password, 'use_ssl': usessl } body = json.dumps(params) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", VCenter.URI_TENANTS_VCENTERS.format(uri), body) o = common.json_decode(s) return o else: raise e if(check): raise SOSError(SOSError.ENTRY_ALREADY_EXISTS_ERR, "vcenter with name " + label + " already exists")
def vcenterdatacenter_update(self, label, vcenter, tenantname, newtenantname): ''' updates a vcenterdatacenter parameters: label: label of the vcenterdatacenter Returns: JSON payload response ''' try: check = self.vcenterdatacenter_show(label, vcenter, tenantname) if check: raise SOSError(SOSError.NOT_FOUND_ERR, "vcenterdatacenter " + label + ": found") except SOSError as e: if e.err_code == SOSError.NOT_FOUND_ERR: uri = self.vcenterdatacenter_query(label, vcenter, VcenterDatacenter.DATACENTERS_FROM_ALL_TENANTS) params = dict() params['name'] = label if newtenantname is not None and newtenantname != 'null': from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) params['tenant'] = obj.tenant_query(newtenantname) elif newtenantname is not None: params['tenant'] = newtenantname body = json.dumps(params) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "PUT", VcenterDatacenter.URI_DATACENTER.format(uri), body) o = common.json_decode(s) return o else: raise e if not check: raise SOSError(SOSError.ENTRY_ALREADY_EXISTS_ERR, "vcenterdatacenter with name " + label + " dost not exist")
def create(self, tenant, policyname, policytype, schedulefrequency, schedulerepeat,scheduletime, scheddom, scheddow, expiretype, expirevalue): tenant_obj = Tenant(self.__ipAddr, self.__port) tenant_uri = tenant_obj.tenant_query(tenant) schedule = dict() schedule = { 'schedule_frequency' : schedulefrequency, 'schedule_repeat' : schedulerepeat, 'schedule_time' : scheduletime } if schedulefrequency == 'weeks': schedule['schedule_day_of_week'] = scheddow elif schedulefrequency == 'months': schedule['schedule_day_of_month'] = scheddom parms = { 'policy_type' : policytype, 'policy_name' : policyname} body = None parms['schedule'] = schedule snapshot_expire = dict() if expiretype or expirevalue : snapshot_expire = { 'expire_type' : expiretype, 'expire_value' : expirevalue} parms['snapshot_expire'] = snapshot_expire body = json.dumps(parms) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", Schedulepolicy.URI_SNAPSHOT_SCHEDULE_POLICY_CREATE.format( tenant_uri), body) return
def vcenter_query(self, name, tenantname): ''' Returns the UID of the vcenter specified by the name ''' if (common.is_uri(name)): return name from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) tenanturi = obj.tenant_query(tenantname) vcenters = self.vcenter_list(tenanturi) for vcenter in vcenters: if (vcenter['name'] == name): return vcenter['id'] raise SOSError(SOSError.NOT_FOUND_ERR, "vcenter " + name + ": not found")
def vcenter_list(self, tenant): ''' Returns all the vcenters associated with a tenant Parameters: Returns: JSON payload of vcenter list ''' from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) uri = obj.tenant_query(tenant) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "GET", VCenter.URI_TENANTS_VCENTERS.format(uri), None) o = common.json_decode(s) return o['vcenter']
def project_list(self, tenant_name): ''' Makes REST API call and retrieves projects based on tenant UUID Parameters: None Returns: List of project UUIDs in JSON response payload ''' from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) try: tenant_uri = tenant_obj.tenant_query(tenant_name) except SOSError as e: raise e (s, h) = common.service_json_request(self.__ipAddr, self.__port, "GET", Project.URI_PROJECT_LIST.format(tenant_uri), None) o = common.json_decode(s) if("project" in o): return common.get_list(o, 'project') return []
def project_create(self, name, tenant_name): ''' Makes REST API call to create project under a tenant Parameters: name: name of project tenant_name: name of the tenant under which project is to be created Returns: Created project details in JSON response payload ''' from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) try: tenant_uri = tenant_obj.tenant_query(tenant_name) except SOSError as e: raise e project_already_exists = True try: if(not tenant_name): tenant_name = "" self.project_query(tenant_name + "/" + name) except SOSError as e: if(e.err_code == SOSError.NOT_FOUND_ERR): project_already_exists = False else: raise e if(project_already_exists): raise SOSError(SOSError.ENTRY_ALREADY_EXISTS_ERR, "Project with name: " + name + " already exists") body = common.json_encode('name', name) (s, h) = common.service_json_request(self.__ipAddr, self.__port, "POST", Project.URI_PROJECT_LIST.format(tenant_uri), body) o = common.json_decode(s) return o
def varray_list(self, vdcname=None, tenant=None): ''' Returns all the varrays in a vdc Parameters: Returns: JSON payload of varray list ''' vdcuri = None vdcrestapi = None if (tenant != None): from tenant import Tenant tenant_obj = Tenant(self.__ipAddr, self.__port) tenanturi = tenant_obj.tenant_query(tenant) if (vdcname != None): vdcrestapi = VirtualArray.URI_VIRTUALARRAY_BY_VDC_ID_AND_TENANT_ID.format( vdcname, tenanturi) else: vdcrestapi = VirtualArray.URI_VIRTUALARRAY_BY_TENANT_ID.format( tenanturi) else: if (vdcname != None): vdcrestapi = VirtualArray.URI_VIRTUALARRAY_BY_VDC_ID.format( vdcname) else: vdcrestapi = VirtualArray.URI_VIRTUALARRAY (s, h) = common.service_json_request(self.__ipAddr, self.__port, "GET", vdcrestapi, None) o = common.json_decode(s) returnlst = [] for item in o['varray']: returnlst.append(item['id']) return returnlst
def vcenter_create(self, label, tenant, ipaddress, devport, username, password, osversion, usessl, cascade_tenancy): ''' creates a vcenter parameters: label: label of the vcenter Returns: JSON payload response ''' try: check = self.vcenter_show(label, tenant) if(not check): raise SOSError(SOSError.NOT_FOUND_ERR, "vcenter " + label + ": not found") except SOSError as e: if(e.err_code == SOSError.NOT_FOUND_ERR): (s, h) = common.service_json_request( self.__ipAddr, self.__port, "GET", VCenter.URI_WHO_AM_I, None) user_info = common.json_decode(s) sys_admin = False vdc_roles = [] if user_info['vdc_roles']: vdc_roles = user_info['vdc_roles'] if VCenter.USER_ROLE_SYSTEM_ADMIN in vdc_roles: sys_admin = True var = dict() params = dict() params = {'name': label, 'ip_address': ipaddress, 'os_version': osversion, 'port_number': devport, 'user_name': username, 'password': password, 'use_ssl': usessl } if sys_admin: params['cascade_tenancy'] = cascade_tenancy; discover = 'false'; if tenant is None: discover = 'true'; body = json.dumps(params) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", VCenter.URI_VCENTERS_WITH_DISCOVER_PARAM.format(discover), body) else: body = json.dumps(params) from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) tenant_uri = obj.tenant_query(tenant) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "POST", VCenter.URI_TENANTS_VCENTERS.format(str(tenant_uri)), body) o = common.json_decode(s) if sys_admin and tenant is not None: tenant_uri = None if tenant is not None: from tenant import Tenant obj = Tenant(self.__ipAddr, self.__port) tenant_uri = obj.tenant_query(tenant) vcenter_id = (o['resource'])['id'] acls_params = dict() acls_params = {'add' : [{ 'tenant' : str(tenant_uri), 'privilege' : ['USE'] }] } body = json.dumps(acls_params) (s, h) = common.service_json_request( self.__ipAddr, self.__port, "PUT", VCenter.URI_VCENTER_ACL.format(vcenter_id), body) o = common.json_decode(s) return o else: raise e if(check): raise SOSError(SOSError.ENTRY_ALREADY_EXISTS_ERR, "vcenter with name " + label + " already exists")