示例#1
0
    def test_resource_save(self, mock_session):
        mock_session.configure_mock(base_url=BASE)
        mock_session.post_json.return_value = {
            'foo': {
                'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
            }
        }
        r = Resource('foo', fq_name='domain:bar:foo')
        r['foo'] = 'bar'
        r.save()
        mock_session.post_json.assert_called_with(
            BASE + '/foos',
            {'foo': {
                'foo': 'bar',
                'fq_name': ['domain', 'bar', 'foo']
            }},
            cls=ResourceEncoder)

        mock_session.put_json.return_value = {
            'foo': {
                'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
            }
        }
        r = Resource('foo', uuid='4c7dc905-3322-49c8-be5d-d4adb5571d41')
        r['foo'] = 'bar'
        r.save()
        mock_session.put_json.assert_called_with(
            BASE + '/foo/4c7dc905-3322-49c8-be5d-d4adb5571d41', {
                'foo': {
                    'foo': 'bar',
                    'uuid': '4c7dc905-3322-49c8-be5d-d4adb5571d41'
                }
            },
            cls=ResourceEncoder)
示例#2
0
    def test_resource_save(self, mock_session):
        mock_session.configure_mock(base_url=BASE)
        mock_session.post_json.return_value = {
            'foo': {
                'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
            }
        }
        r = Resource('foo', fq_name='domain:bar:foo')
        r['foo'] = 'bar'
        r.save()
        mock_session.post_json.assert_called_with(BASE + '/foos',
                                                  {'foo': {'foo': 'bar',
                                                           'fq_name': ['domain', 'bar', 'foo']}},
                                                  cls=ResourceEncoder)

        mock_session.put_json.return_value = {
            'foo': {
                'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
            }
        }
        r = Resource('foo', uuid='4c7dc905-3322-49c8-be5d-d4adb5571d41')
        r['foo'] = 'bar'
        r.save()
        mock_session.put_json.assert_called_with(BASE + '/foo/4c7dc905-3322-49c8-be5d-d4adb5571d41',
                                                 {'foo': {
                                                     'foo': 'bar',
                                                     'uuid': '4c7dc905-3322-49c8-be5d-d4adb5571d41'
                                                 }},
                                                 cls=ResourceEncoder)
    def __call__(self, virtual_network_fqname=None, cidrs=None):
        vn = Resource('virtual-network',
                      fq_name=virtual_network_fqname,
                      fetch=True)

        cidrs = [netaddr.IPNetwork(cidr) for cidr in cidrs]
        ipam_subnets = self._get_network_ipam_subnets(vn)
        ipam_subnets_current = [{
            'subnet': {
                'ip_prefix': s['subnet']['ip_prefix'],
                'ip_prefix_len': s['subnet']['ip_prefix_len']
            }
        } for s in ipam_subnets]
        ipam_subnets_wanted = [{
            'subnet': {
                'ip_prefix': text_type(c.network),
                'ip_prefix_len': c.prefixlen
            }
        } for c in cidrs]

        modified = False
        for idx, subnet in enumerate(ipam_subnets_current):
            if subnet not in ipam_subnets_wanted:
                del ipam_subnets[idx]
                modified = True
        for subnet in ipam_subnets_wanted:
            if subnet not in ipam_subnets_current:
                ipam_subnets.append(subnet)
                modified = True
        if modified:
            vn.save()
示例#4
0
 def _get_rt(self, fq_name, parent):
     rt = Resource('route-table', fq_name=fq_name, parent=parent)
     try:
         rt.fetch()
     except ResourceNotFound:
         rt['routes'] = {'route': []}
         if not self.dry_run:
             rt.save()
     return rt
 def __call__(self, appliance_set_name=None, driver=None):
     global_config = Resource('global-system-config',
                              fq_name='default-global-system-config')
     sas = Resource('service-appliance-set',
                    fq_name='default-global-system-config:%s' %
                    appliance_set_name,
                    parent=global_config,
                    service_appliance_driver=driver)
     sas.save()
    def __call__(self, analytics_name=None, analytics_ip=None):
        global_config = Resource('global-system-config',
                                 fq_name='default-global-system-config')
        analytics = Resource('analytics-node',
                             fq_name='default-global-system-config:%s' %
                             analytics_name,
                             parent=global_config,
                             analytics_node_ip_address=analytics_ip)

        analytics.save()
示例#7
0
    def __call__(self, config_name=None, config_ip=None):

        global_config = Resource('global-system-config',
                                 fq_name='default-global-system-config')
        config = Resource('config-node',
                          fq_name='default-global-system-config:%s' %
                          config_name,
                          parent=global_config,
                          config_node_ip_address=config_ip)
        config.save()
示例#8
0
class Linklocal(Command):
    service_name = Option(help='Linklocal service name', required=True)
    service_ip = Option(help='Linklocal service IP',
                        type=ip_type,
                        required=True)
    service_port = Option(help='Linklocal service port',
                          type=int,
                          required=True)
    fabric_dns_service_name = Option(help='DNS service name in the fabric')
    fabric_service_ip = Option(help='Service IP in the fabric', type=ip_type)
    fabric_service_port = Option(help='Service port in the fabric',
                                 type=int,
                                 required=True)

    def __call__(self,
                 service_name=None,
                 service_ip=None,
                 service_port=None,
                 fabric_dns_service_name=None,
                 fabric_service_ip=None,
                 fabric_service_port=None):

        if not fabric_dns_service_name and not fabric_service_ip:
            raise CommandError(
                '--fabric_dns_service_name or --fabric_service_ip required')

        self.linklocal_entry = {
            'ip_fabric_DNS_service_name': fabric_dns_service_name,
            'ip_fabric_service_ip': [],
            'ip_fabric_service_port': fabric_service_port,
            'linklocal_service_ip': service_ip,
            'linklocal_service_port': service_port,
            'linklocal_service_name': service_name
        }
        if fabric_service_ip:
            self.linklocal_entry['ip_fabric_service_ip'].append(
                fabric_service_ip)

        try:
            self.vrouter_config = Resource(
                'global-vrouter-config',
                fq_name=
                'default-global-system-config:default-global-vrouter-config',
                fetch=True)
        except ResourceNotFound:
            global_config = Resource('global-system-config',
                                     fq_name='default-global-system-config')
            self.vrouter_config = Resource(
                'global-vrouter-config',
                fq_name=
                'default-global-system-config:default-global-vrouter-config',
                parent=global_config)
            self.vrouter_config.save()
示例#9
0
    def __call__(self, vrouter_ip=None, vrouter_name=None, vrouter_type=None):

        global_config = Resource('global-system-config',
                                 fq_name='default-global-system-config')
        vrouter = Resource('virtual-router',
                           fq_name='default-global-system-config:%s' %
                           vrouter_name,
                           parent=global_config,
                           virtual_router_ip_address=vrouter_ip)
        if vrouter_type:
            vrouter['virtual_router_type'] = [vrouter_type]
        vrouter.save()
示例#10
0
    def __call__(self, modes=None):
        if len(modes) > 3:
            raise CommandError('Too much modes provided')
        try:
            vrouter_config = Resource(
                'global-vrouter-config',
                fq_name=
                'default-global-system-config:default-global-vrouter-config',
                fetch=True)
        except ResourceNotFound:
            global_config = Resource('global-system-config',
                                     fq_name='default-global-system-config')
            vrouter_config = Resource(
                'global-vrouter-config',
                fq_name=
                'default-global-system-config:default-global-vrouter-config',
                parent=global_config)

        vrouter_config['encapsulation_priorities'] = {'encapsulation': modes}
        vrouter_config.save()
    def test_resource_save(self, mock_session):
        mock_session.configure_mock(base_url=self.BASE)
        mock_session.fqname_to_id.return_value = '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
        mock_session.get_json.side_effect = [{
            'foo': {
                'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd',
                'attr': 'bar',
                'fq_name': ['domain', 'bar', 'foo']
            }
        }, {
            'foo': {
                'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd',
                'attr': 'foo',
                'fq_name': ['domain', 'bar', 'foo']
            }
        }]

        r1 = Resource('foo', fq_name='domain:bar:foo')
        r1['attr'] = 'bar'
        r1.save()
        mock_session.post_json.assert_called_with(
            self.BASE + '/foos',
            {'foo': {
                'attr': 'bar',
                'fq_name': ['domain', 'bar', 'foo']
            }},
            cls=ResourceEncoder)
        self.assertEqual(r1['attr'], 'bar')

        r1['attr'] = 'foo'
        r1.save()
        mock_session.put_json.assert_called_with(
            self.BASE + '/foo/8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd', {
                'foo': {
                    'attr': 'foo',
                    'fq_name': ['domain', 'bar', 'foo'],
                    'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
                }
            },
            cls=ResourceEncoder)
        self.assertEqual(r1['attr'], 'foo')
示例#12
0
    def test_resource_save(self, mock_session):
        mock_session.configure_mock(base_url=BASE)
        mock_session.fqname_to_id.return_value = '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
        mock_session.get_json.side_effect = [
            {
                'foo': {
                    'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd',
                    'attr': 'bar',
                    'fq_name': ['domain', 'bar', 'foo']
                }
            },
            {
                'foo': {
                    'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd',
                    'attr': 'foo',
                    'fq_name': ['domain', 'bar', 'foo']
                }
            }
        ]

        r1 = Resource('foo', fq_name='domain:bar:foo')
        r1['attr'] = 'bar'
        r1.save()
        mock_session.post_json.assert_called_with(BASE + '/foos',
                                                  {'foo': {
                                                      'attr': 'bar',
                                                      'fq_name': ['domain', 'bar', 'foo']
                                                  }},
                                                  cls=ResourceEncoder)
        self.assertEqual(r1['attr'], 'bar')

        r1['attr'] = 'foo'
        r1.save()
        mock_session.put_json.assert_called_with(BASE + '/foo/8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd',
                                                 {'foo': {
                                                     'attr': 'foo',
                                                     'fq_name': ['domain', 'bar', 'foo'],
                                                     'uuid': '8240f9c7-0f28-4ca7-b92e-2f6441a0a6dd'
                                                 }},
                                                 cls=ResourceEncoder)
        self.assertEqual(r1['attr'], 'foo')
示例#13
0
    def __call__(self,
                 project_fqname=None,
                 virtual_network_name=None,
                 external=False,
                 shared=False):
        vn_fqname = '%s:%s' % (project_fqname, virtual_network_name)

        # fetch project to sync it from keystone if not already there
        project = Resource('project', fq_name=project_fqname, fetch=True)
        vn = Resource('virtual-network',
                      fq_name=vn_fqname,
                      parent=project,
                      router_external=external,
                      is_shared=shared)
        vn.save()

        if external:
            fip_pool = Resource('floating-ip-pool',
                                fq_name='%s:%s' %
                                (vn_fqname, 'floating-ip-pool'),
                                parent_type='virtual-network',
                                parent_uuid=vn.uuid)
            fip_pool.save()
    def __call__(self,
                 router_name=None,
                 router_ip=None,
                 router_port=None,
                 router_asn=None,
                 router_address_families=[],
                 router_type=None,
                 router_md5=None):

        default_ri = Resource('routing-instance',
                              fq_name=DEFAULT_RI_FQ_NAME,
                              check=True)
        router_fq_name = DEFAULT_RI_FQ_NAME + [router_name]

        bgp_router = Resource('bgp-router', fq_name=router_fq_name)
        if bgp_router.exists:
            raise CommandError("The BGP router %s already exists" %
                               FQName(router_fq_name))

        if router_type != 'contrail' and 'erm-vpn' in router_address_families:
            router_address_families.remove('erm-vpn')

        auth_data = None
        if router_md5:
            auth_data = {
                'key_items': [{
                    'key': router_md5,
                    'key_id': 0,
                }],
                'key_type': 'md5',
            }
        router_parameters = {
            'address': router_ip,
            'address_families': {
                'family': router_address_families,
            },
            'autonomous_system': router_asn,
            'identifier': router_ip,
            'port': router_port,
            'vendor': router_type,
            'auth_data': auth_data,
        }

        # full-mesh with existing BGP routers
        bgp_router_refs = []
        for bgp_router_neighbor in Collection('bgp-router',
                                              parent_uuid=default_ri.uuid,
                                              fetch=True):
            bgp_router_refs.append({
                'to': bgp_router_neighbor.fq_name,
                'attr': {
                    'session': [{
                        'attributes': [{
                            'address_families': {
                                'family': router_address_families,
                            },
                            'auth_data': auth_data,
                        }],
                    }],
                },
            })

        bgp_router = Resource('bgp-router',
                              fq_name=router_fq_name,
                              parent=default_ri,
                              bgp_router_parameters=router_parameters,
                              bgp_router_refs=bgp_router_refs)
        bgp_router.save()
 def __call__(self, asn=None):
     global_config = Resource('global-system-config',
                              fq_name='default-global-system-config',
                              check=True)
     global_config['autonomous_system'] = asn
     global_config.save()
示例#16
0
from contrail_api_cli.resource import Resource

d = Resource('domain', fq_name=['default-domain']).fetch()
p = Resource('project', fq_name=['default-domain', 'service'], parent=d)
p.save()