def _create_lb_service(self, context, service_client, tenant_id, router_id, nsx_router_id, lb_id, lb_size): router = self.core_plugin.get_router(context, router_id) if not router.get('external_gateway_info'): msg = (_('Tenant router %(router)s does not connect to ' 'external gateway') % { 'router': router['id'] }) raise n_exc.BadRequest(resource='lbaas-lbservice-create', msg=msg) lb_name = utils.get_name_and_uuid(router['name'] or 'router', router_id) tags = lb_utils.get_tags(self.core_plugin, router_id, lb_const.LR_ROUTER_TYPE, tenant_id, context.project_name) attachment = { 'target_id': nsx_router_id, 'target_type': 'LogicalRouter' } lb_service = service_client.create(display_name=lb_name, tags=tags, attachment=attachment, size=lb_size) # Update router to enable advertise_lb_vip flag self.core_plugin.nsxlib.logical_router.update_advertisement( nsx_router_id, advertise_lb_vip=True) return lb_service
def _get_listener_tags(self, context, listener): tags = lb_utils.get_tags(self.core_plugin, listener.id, lb_const.LB_LISTENER_TYPE, listener.tenant_id, context.project_name) tags.append({ 'scope': 'os-lbaas-lb-name', 'tag': listener.loadbalancer.name[:utils.MAX_TAG_LEN] }) tags.append({ 'scope': 'os-lbaas-lb-id', 'tag': listener.loadbalancer_id }) return tags
def create(self, context, pool): lb_id = pool.loadbalancer_id pool_client = self.core_plugin.nsxlib.load_balancer.pool vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server pool_name = utils.get_name_and_uuid(pool.name, pool.id) tags = lb_utils.get_tags(self.core_plugin, pool.id, lb_const.LB_POOL_TYPE, pool.tenant_id, context.project_name) lb_algorithm = lb_const.LB_POOL_ALGORITHM_MAP.get(pool.lb_algorithm) try: snat_translation = {'type': "LbSnatAutoMap"} lb_pool = pool_client.create(display_name=pool_name, tags=tags, algorithm=lb_algorithm, snat_translation=snat_translation) nsx_db.add_nsx_lbaas_pool_binding(context.session, lb_id, pool.id, lb_pool['id']) except nsxlib_exc.ManagerError: self.lbv2_driver.pool.failed_completion(context, pool) msg = (_('Failed to create pool on NSX backend: %(pool)s') % { 'pool': pool.id }) raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) # The pool object can be created with either --listener or # --loadbalancer option. If listener is present, the virtual server # will be updated with the pool. Otherwise, just return. The binding # will be added later when the pool is associated with layer7 rule. if pool.listener: listener_id = pool.listener.id binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if binding: vs_id = binding['lb_vs_id'] try: vs_client.update(vs_id, pool_id=lb_pool['id']) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): self.lbv2_driver.pool.failed_completion(context, pool) LOG.error( 'Failed to attach pool %s to virtual ' 'server %s', lb_pool['id'], vs_id) nsx_db.update_nsx_lbaas_pool_binding(context.session, lb_id, pool.id, vs_id) else: msg = (_("Couldn't find binding on the listener: %s") % listener_id) raise nsx_exc.NsxPluginException(err_msg=msg) self.lbv2_driver.pool.successful_completion(context, pool)
def _create_lb_service(self, context, service_client, tenant_id, router_id, nsx_router_id, lb_id, lb_size): router = self.core_plugin.get_router(context, router_id) lb_name = utils.get_name_and_uuid(router['name'], router_id) tags = lb_utils.get_tags(self.core_plugin, router_id, lb_const.LR_ROUTER_TYPE, tenant_id, context.project_name) attachment = { 'target_id': nsx_router_id, 'target_type': 'LogicalRouter' } lb_service = service_client.create(display_name=lb_name, tags=tags, attachment=attachment, size=lb_size) return lb_service
def create(self, context, listener, certificate=None): lb_id = listener.loadbalancer_id vip_address = listener.loadbalancer.vip_address load_balancer = self.core_plugin.nsxlib.load_balancer app_client = load_balancer.application_profile vs_client = load_balancer.virtual_server vs_name = utils.get_name_and_uuid(listener.name, listener.id) tags = lb_utils.get_tags(self.core_plugin, listener.id, lb_const.LB_LISTENER_TYPE, listener.tenant_id, context.project_name) if listener.protocol == 'HTTP' or listener.protocol == 'HTTPS': profile_type = lb_const.LB_HTTP_PROFILE elif listener.protocol == 'TCP': profile_type = lb_const.LB_TCP_PROFILE else: msg = (_('Cannot create listener %(listener)s with ' 'protocol %(protocol)s') % {'listener': listener.id, 'protocol': listener.protocol}) raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) try: app_profile = app_client.create( display_name=vs_name, resource_type=profile_type, tags=tags) app_profile_id = app_profile['id'] virtual_server = vs_client.create( display_name=vs_name, tags=tags, enabled=listener.admin_state_up, ip_address=vip_address, port=listener.protocol_port, application_profile_id=app_profile_id) except nsxlib_exc.ManagerError: self.lbv2_driver.listener.failed_completion(context, listener) msg = _('Failed to create virtual server at NSX backend') raise n_exc.BadRequest(resource='lbaas-listener', msg=msg) nsx_db.add_nsx_lbaas_listener_binding( context.session, lb_id, listener.id, app_profile_id, virtual_server['id']) self.lbv2_driver.listener.successful_completion( context, listener)
def create(self, context, rule): lb_id = rule.policy.listener.loadbalancer_id listener_id = rule.policy.listener_id vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server rule_client = self.core_plugin.nsxlib.load_balancer.rule tags = lb_utils.get_tags(self.core_plugin, rule.id, lb_const.LB_L7RULE_TYPE, rule.tenant_id, context.project_name) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if not binding: msg = _('Cannot find nsx lbaas binding for listener ' '%(listener_id)s') % { 'listener_id': listener_id } raise n_exc.BadRequest(resource='lbaas-l7rule-create', msg=msg) vs_id = binding['lb_vs_id'] rule_body = self._convert_l7policy_to_lb_rule(context, rule) try: lb_rule = rule_client.create(tags=tags, **rule_body) except nsxlib_exc.ManagerError: self.lbv2_driver.l7rule.failed_completion(context, rule) msg = _('Failed to create lb rule at NSX backend') raise n_exc.BadRequest(resource='lbaas-l7rule-create', msg=msg) try: vs_client.add_rule(vs_id, lb_rule['id']) except nsxlib_exc.ManagerError: self.lbv2_driver.l7rule.failed_completion(context, rule) msg = (_('Failed to add rule %(rule)% to virtual server ' '%(vs)s at NSX backend') % { 'rule': lb_rule['id'], 'vs': vs_id }) raise n_exc.BadRequest(resource='lbaas-l7rule-create', msg=msg) nsx_db.add_nsx_lbaas_l7rule_binding(context.session, lb_id, rule.l7policy_id, rule.id, lb_rule['id'], vs_id) self.lbv2_driver.l7rule.successful_completion(context, rule)
def create(self, context, policy): lb_id = policy.listener.loadbalancer_id listener_id = policy.listener_id rule_client = self.core_plugin.nsxlib.load_balancer.rule tags = lb_utils.get_tags(self.core_plugin, policy.id, lb_const.LB_L7POLICY_TYPE, policy.tenant_id, context.project_name) binding = nsx_db.get_nsx_lbaas_listener_binding( context.session, lb_id, listener_id) if not binding: self.lbv2_driver.l7policy.failed_completion(context, policy) msg = _('Cannot find nsx lbaas binding for listener ' '%(listener_id)s') % { 'listener_id': listener_id } raise n_exc.BadRequest(resource='lbaas-l7policy-create', msg=msg) vs_id = binding['lb_vs_id'] rule_body = lb_utils.convert_l7policy_to_lb_rule(context, policy) try: lb_rule = rule_client.create(tags=tags, **rule_body) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): self.lbv2_driver.l7policy.failed_completion(context, policy) LOG.error('Failed to create lb rule at NSX backend') try: self._update_policy_position(vs_id, lb_rule['id'], policy.position) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): self.lbv2_driver.l7policy.failed_completion(context, policy) LOG.error( 'Failed to add rule %(rule)% to virtual server ' '%(vs)s at NSX backend', { 'rule': lb_rule['id'], 'vs': vs_id }) nsx_db.add_nsx_lbaas_l7policy_binding(context.session, policy.id, lb_rule['id'], vs_id) self.lbv2_driver.l7policy.successful_completion(context, policy)
def create(self, context, hm): lb_id = hm.pool.loadbalancer_id pool_id = hm.pool.id pool_client = self.core_plugin.nsxlib.load_balancer.pool monitor_client = self.core_plugin.nsxlib.load_balancer.monitor monitor_name = utils.get_name_and_uuid(hm.name or 'monitor', hm.id) tags = lb_utils.get_tags(self.core_plugin, hm.id, lb_const.LB_HM_TYPE, hm.tenant_id, context.project_name) monitor_body = self._build_monitor_args(hm) try: lb_monitor = monitor_client.create(display_name=monitor_name, tags=tags, **monitor_body) except nsxlib_exc.ManagerError: with excutils.save_and_reraise_exception(): self.lbv2_driver.health_monitor.failed_completion(context, hm) binding = nsx_db.get_nsx_lbaas_pool_binding(context.session, lb_id, pool_id) if binding: lb_pool_id = binding['lb_pool_id'] try: pool_client.add_monitor_to_pool(lb_pool_id, lb_monitor['id']) except nsxlib_exc.ManagerError: self.lbv2_driver.health_monitor.failed_completion(context, hm) msg = _('Failed to attach monitor %(monitor)s to pool ' '%(pool)s') % { 'monitor': lb_monitor['id'], 'pool': lb_pool_id } raise n_exc.BadRequest(resource='lbaas-hm', msg=msg) nsx_db.add_nsx_lbaas_monitor_binding(context.session, lb_id, pool_id, hm.id, lb_monitor['id'], lb_pool_id) self.lbv2_driver.health_monitor.successful_completion(context, hm)
def _get_pool_tags(self, context, pool): return lb_utils.get_tags(self.core_plugin, pool.id, lb_const.LB_POOL_TYPE, pool.tenant_id, context.project_name)