当前位置: 首页>>代码示例>>Python>>正文


Python ProfitBricksService.get_nic方法代码示例

本文整理汇总了Python中profitbricks.client.ProfitBricksService.get_nic方法的典型用法代码示例。如果您正苦于以下问题:Python ProfitBricksService.get_nic方法的具体用法?Python ProfitBricksService.get_nic怎么用?Python ProfitBricksService.get_nic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在profitbricks.client.ProfitBricksService的用法示例。


在下文中一共展示了ProfitBricksService.get_nic方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestNIC

# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import get_nic [as 别名]
class TestNIC(unittest.TestCase):
    def setUp(self):
        self.nic = ProfitBricksService(
            username='username', password='password')

    def test_list_nics(self):
        nics = self.nic.list_nics(
            datacenter_id=datacenter_id,
            server_id=server_id)

        self.assertEqual(len(nics), 4)
        self.assertEqual(nics['items'][0]['id'], nic_id)
        self.assertEqual(nics['items'][0]['properties']['name'], 'nic1')
        self.assertEqual(
            nics['items'][0]['properties']['mac'], 'AB:21:23:09:78:C2')
        self.assertEqual(nics['items'][0]['properties']['dhcp'], 'true')
        self.assertEqual(nics['items'][0]['properties']['lan'], 1)

    def test_get_nic(self):
        nic = self.nic.get_nic(
            datacenter_id=datacenter_id,
            server_id=server_id,
            nic_id=nic_id)

        self.assertEqual(nic['id'], nic_id)
        self.assertEqual(nic['properties']['name'], 'nic1')
        self.assertEqual(nic['properties']['mac'], 'AB:21:23:09:78:C2')
        self.assertEqual(nic['properties']['dhcp'], 'true')
        self.assertEqual(nic['properties']['lan'], 1)

    def test_delete_nic(self):
        nic = self.nic.delete_nic(
            datacenter_id=datacenter_id,
            server_id=server_id,
            nic_id=nic_id)

        self.assertTrue(nic)

    def test_update_nic(self):
        nic = self.nic.update_nic(
            datacenter_id=datacenter_id,
            server_id=server_id,
            nic_id=nic_id,
            ips=['10.2.2.3', '10.2.3.4'])

        self.assertEqual(nic['id'], nic_id)
        self.assertEqual(nic['properties']['name'], 'nic1')
        self.assertEqual(nic['properties']['mac'], 'AB:21:23:09:78:C2')
        self.assertEqual(nic['properties']['dhcp'], 'true')
        self.assertEqual(nic['properties']['lan'], 1)

    def test_create_complex(self):
        fwrule1 = FirewallRule(
            name='Open SSH port',
            protocol='TCP',
            source_mac='01:23:45:67:89:00',
            port_range_start=22
            )

        fwrule2 = FirewallRule(
            name='Allow PING',
            protocol='ICMP',
            icmp_type=8,
            icmp_code=0
            )

        fw_rules = [fwrule1, fwrule2]

        i = NIC(
            name='nic1',
            ips=['10.2.2.3', '10.2.3.4'],
            dhcp='true',
            lan=1,
            firewall_active=True,
            firewall_rules=fw_rules
            )

        response = self.nic.create_nic(
            datacenter_id=datacenter_id,
            server_id=server_id, nic=i)

        self.assertEqual(response['id'], nic_id)
        self.assertEqual(response['properties']['name'], 'nic1')
        self.assertEqual(
            response['properties']['mac'], 'AB:21:23:09:78:C2')
        self.assertEqual(response['properties']['dhcp'], 'true')
        self.assertEqual(response['properties']['lan'], 1)
        self.assertListEqual(
            response['properties']['ips'], ['10.2.2.3'])

    def test_create_simple(self):
        i = NIC(
            name='nic1',
            ips=['10.2.2.3', '10.2.3.4'],
            dhcp='true',
            lan=1,
            firewall_active=True
            )

        response = self.nic.create_nic(
#.........这里部分代码省略.........
开发者ID:grandvizier,项目名称:profitbricks-sdk-python,代码行数:103,代码来源:test_nic.py

示例2: main

# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import get_nic [as 别名]

#.........这里部分代码省略.........
            raise ValueError("user or password resolved to None")
        pbclient = ProfitBricksService(user, password)

        if args.metatype == 'OVF':
            metadata = OFVData(args.metafile)
            metadata.parse()
        else:
            sys.stderr.write("Metadata type '{}' is not supported"
                             .format(args.metatype))
            return 1

        # we need the DC first to have the location defined
        dc_id = None
        if args.datacenterid is None:
            if args.dcname is None or args.location is None:
                sys.stderr.write("Either '-d <id>' or '-D <name> -l <loc>'  must be specified")
                return 1
            # else: we will create the DC later after parsing the meta data
        else:
            dc_id = args.datacenterid

        if dc_id is None:
            location = args.location
            dc = Datacenter(name=args.dcname, location=location,
                            description="created by pb_importVM")
            print("create new DC {}".format(str(dc)))
            response = pbclient.create_datacenter(dc)
            dc_id = response['id']
            result = wait_for_request(pbclient, response['requestId'])
            print("wait loop returned {}".format(result))
        else:
            dc = pbclient.get_datacenter(dc_id)
            location = dc['properties']['location']
            print("use existing DC {} in location {}"
                  .format(dc['properties']['name'], location))

        # check if images exist
        for disk in metadata.disks:
            disk_name = disk['file']
            images = get_disk_image_by_name(pbclient, location, disk_name)
            if len(images) == 0:
                raise ValueError("No HDD image with name '{}' found in location {}"
                                 .format(disk_name, location))
            if len(images) > 1:
                raise ValueError("Ambigous image name '{}' in location {}"
                                 .format(disk_name, location))
            disk['image'] = images[0]['id']

        # now we're ready to create the VM
        # Server
        server = Server(name=metadata.name,
                        cores=metadata.cpus, ram=metadata.ram)
        print("create server {}".format(str(Server)))
        response = pbclient.create_server(dc_id, server)
        srv_id = response['id']
        result = wait_for_request(pbclient, response['requestId'])
        print("wait loop returned {}".format(str(result)))
        # NICs (note that createing LANs may be implicit)
        for nic in metadata.nics:
            dcnic = NIC(name=nic['nic'], lan=nic['lanid'])
            print("create NIC {}".format(str(dcnic)))
            response = pbclient.create_nic(dc_id, srv_id, dcnic)
            nic_id = response['id']
            result = wait_for_request(pbclient, response['requestId'])
            print("wait loop returned {}".format(str(result)))
            response = pbclient.get_nic(dc_id, srv_id, nic_id, 2)
            mac = response['properties']['mac']
            print("dcnic has MAC {} for {}".format(mac, nic_id))
        # end for(nics)
        # Volumes (we use the image name as volume name too
        requests = []
        for disk in metadata.disks:
            dcvol = Volume(name=disk['file'], size=disk['capacity'],
                           image=disk['image'],
                           licence_type=metadata.licenseType)
            print("create Volume {}".format(str(dcvol)))
            response = pbclient.create_volume(dc_id, dcvol)
            requests.append(response['requestId'])
            disk['volume_id'] = response['id']
        # end for(disks)
        if len(requests) != 0:
            result = wait_for_requests(pbclient, requests, initial_wait=10, scaleup=15)
            print("wait loop returned {}".format(str(result)))
        for disk in metadata.disks:
            print("attach volume {}".format(disk))
            response = pbclient.attach_volume(dc_id, srv_id, disk['volume_id'])
            result = wait_for_request(pbclient, response['requestId'])
            print("wait loop returned {}".format(str(result)))
        # end for(disks)

        print("import of VM succesfully finished")
        return 0

    except KeyboardInterrupt:
        ### handle keyboard interrupt ###
        return 0
    except Exception:
        traceback.print_exc()
        sys.stderr.write("\n" + program_name + ":  for help use --help\n")
        return 2
开发者ID:fbrehm,项目名称:profitbricks-sdk-python,代码行数:104,代码来源:pb_importVM.py

示例3: main

# 需要导入模块: from profitbricks.client import ProfitBricksService [as 别名]
# 或者: from profitbricks.client.ProfitBricksService import get_nic [as 别名]

#.........这里部分代码省略.........
    requests = []
    print("create Servers {}".format(str(dc)))
    # we do NOT consider dangling volumes, only server-attached ones
    for server in dcdef["entities"]["servers"]["items"]:
        print("- server {}".format(server["properties"]["name"]))
        if "custom" in server and "id" in server["custom"]:
            srv_id = server["custom"]["id"]
            print("using existing server w/ id {}".format(str(srv_id)))
        else:
            dcsrv = getServerObject(server)
            print("OBJ: {}".format(str(dcsrv)))
            response = pbclient.create_server(dc_id, dcsrv)
            server.update({"custom": {"id": response["id"]}})
            requests.append(response["requestId"])
    # end for(server)
    if len(requests) != 0:
        result = wait_for_requests(pbclient, requests, initial_wait=10, scaleup=15)
        print("wait loop returned {}".format(str(result)))
        tmpfile = usefile + ".tmp_postsrv"
        write_dc_definition(pbclient, dcdef, tmpfile)
    else:
        print("all servers existed already")

    # TODO: only do this if we have lan entities
    requests = []
    # Huuh, looks like we get unpredictable order for LANs!
    # Nope, order of creation determines the LAN id,
    # thus we better wait for each request
    print("create LANs {}".format(str(dc)))
    for lan in dcdef["entities"]["lans"]["items"]:
        print("- lan {}".format(lan["properties"]["name"]))
        dclan = getLANObject(lan)
        print("OBJ: {}".format(str(dclan)))
        response = pbclient.create_lan(dc_id, dclan)
        lan.update({"custom": {"id": response["id"]}})
        result = wait_for_request(pbclient, response["requestId"])
        print("wait loop returned {}".format(str(result)))
    # end for(lan)
    tmpfile = usefile + ".tmp_postlan"
    write_dc_definition(pbclient, dcdef, tmpfile)

    requests = []
    # Attention:
    # NICs appear in OS in the order, they are created.
    # But DCD rearranges the display by ascending MAC addresses.
    # This does not change the OS order.
    # MAC may not be available from create response,
    # thus we wait for each request :-(
    print("create NICs {}".format(str(dc)))
    for server in dcdef["entities"]["servers"]["items"]:
        print("- server {}".format(server["properties"]["name"]))
        srv_id = server["custom"]["id"]
        if "nics" not in server["entities"]:
            print(" server {} has no NICs".format(server["properties"]["name"]))
            continue
        macmap = dict()
        for nic in server["entities"]["nics"]["items"]:
            dcnic = getNICObject(nic)
            response = pbclient.create_nic(dc_id, srv_id, dcnic)
            # print("dcnic response {}".format(str(response)))
            # mac = response['properties']['mac'] # we don't get it here !?
            nic_id = response["id"]
            result = wait_for_request(pbclient, response["requestId"])
            print("wait loop returned {}".format(str(result)))
            response = pbclient.get_nic(dc_id, srv_id, nic_id, 2)
            mac = response["properties"]["mac"]
            print("dcnic has MAC {} for {}".format(mac, nic_id))
            macmap[mac] = nic_id
        # end for(nic)
        macs = sorted(macmap)
        print("macs will be displayed by DCD in th following order:")
        for mac in macs:
            print("mac {} -> id{}".format(mac, macmap[mac]))
    # end for(server)
    tmpfile = usefile + ".tmp_postnic"
    write_dc_definition(pbclient, dcdef, tmpfile)

    requests = []
    # don't know if we get a race here too, so better wait for each request :-/
    print("attach volumes {}".format(str(dc)))
    for server in dcdef["entities"]["servers"]["items"]:
        print("- server {}".format(server["properties"]["name"]))
        if "volumes" not in server["entities"]:
            print(" server {} has no volumes".format(server["properties"]["name"]))
            continue
        srv_id = server["custom"]["id"]
        for volume in server["entities"]["volumes"]["items"]:
            print("OBJ: {}".format(volume["properties"]["name"]))
            response = pbclient.attach_volume(dc_id, srv_id, volume["custom"]["id"])
            result = wait_for_request(pbclient, response["requestId"])
            print("wait loop returned {}".format(str(result)))
        # end for(volume)
    # end for(server)
    tmpfile = usefile + ".tmp_postatt"
    write_dc_definition(pbclient, dcdef, tmpfile)

    # TODO: do we need to set boot volume for each server?
    # looks like it's working without

    return 0
开发者ID:fbrehm,项目名称:profitbricks-sdk-python,代码行数:104,代码来源:pb_createDatacenter.py


注:本文中的profitbricks.client.ProfitBricksService.get_nic方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。