Revision 445d7262

b/lib/rapi/client.py
1625 1625
                             ("/%s/nodes/%s/tags" %
1626 1626
                              (GANETI_RAPI_VERSION, node)), query, None)
1627 1627

  
1628
  def GetNetworks(self, bulk=False):
1629
    """Gets all networks in the cluster.
1630

  
1631
    @type bulk: bool
1632
    @param bulk: whether to return all information about the networks
1633

  
1634
    @rtype: list of dict or str
1635
    @return: if bulk is true, a list of dictionaries with info about all
1636
        networks in the cluster, else a list of names of those networks
1637

  
1638
    """
1639
    query = []
1640
    _AppendIf(query, bulk, ("bulk", 1))
1641

  
1642
    networks = self._SendRequest(HTTP_GET, "/%s/networks" % GANETI_RAPI_VERSION,
1643
                               query, None)
1644
    if bulk:
1645
      return networks
1646
    else:
1647
      return [n["name"] for n in networks]
1648

  
1649
  def GetNetwork(self, network):
1650
    """Gets information about a network.
1651

  
1652
    @type group: str
1653
    @param group: name of the network whose info to return
1654

  
1655
    @rtype: dict
1656
    @return: info about the network
1657

  
1658
    """
1659
    return self._SendRequest(HTTP_GET,
1660
                             "/%s/networks/%s" % (GANETI_RAPI_VERSION, network),
1661
                             None, None)
1662

  
1663
  def CreateNetwork(self, network_name, network, gateway=None, network6=None,
1664
                    gateway6=None, mac_prefix=None, network_type=None,
1665
                    reserved_ips=None, dry_run=False):
1666
    """Creates a new network.
1667

  
1668
    @type name: str
1669
    @param name: the name of network to create
1670
    @type dry_run: bool
1671
    @param dry_run: whether to peform a dry run
1672

  
1673
    @rtype: string
1674
    @return: job id
1675

  
1676
    """
1677
    query = []
1678
    _AppendDryRunIf(query, dry_run)
1679

  
1680
    body = {
1681
      "network_name": network_name,
1682
      "gateway": gateway,
1683
      "network": network,
1684
      "gateway6": gateway6,
1685
      "network6": network6,
1686
      "mac_prefix": mac_prefix,
1687
      "network_type": network_type,
1688
      "reserved_ips": reserved_ips
1689
      }
1690

  
1691
    return self._SendRequest(HTTP_POST, "/%s/networks" % GANETI_RAPI_VERSION,
1692
                             query, body)
1693

  
1694
  def ConnectNetwork(self, network_name, group_name, mode, link):
1695
    """Connects a Network to a NodeGroup with the given netparams
1696

  
1697
    """
1698
    body = {
1699
      "group_name": group_name,
1700
      "network_mode": mode,
1701
      "network_link": link
1702
      }
1703

  
1704
    return self._SendRequest(HTTP_PUT,
1705
                             ("/%s/networks/%s/connect" %
1706
                             (GANETI_RAPI_VERSION, network_name)), None, body)
1707

  
1708
  def DisconnectNetwork(self, network_name, group_name):
1709
    """Connects a Network to a NodeGroup with the given netparams
1710

  
1711
    """
1712
    body = {
1713
      "group_name": group_name
1714
      }
1715
    return self._SendRequest(HTTP_PUT,
1716
                             ("/%s/networks/%s/disconnect" %
1717
                             (GANETI_RAPI_VERSION, network_name)), None, body)
1718

  
1719
  def ConnectNetworkAll(self, network_name, mode, link):
1720
    """Connects a Network to a NodeGroup with the given netparams
1721

  
1722
    """
1723
    body = {
1724
      "network_mode": mode,
1725
      "network_link": link
1726
      }
1727

  
1728
    return self._SendRequest(HTTP_PUT,
1729
                             ("/%s/networks/%s/connectall" %
1730
                             (GANETI_RAPI_VERSION, network_name)), None, body)
1731

  
1732
  def DisconnectNetworkAll(self, network_name):
1733
    """Connects a Network to a NodeGroup with the given netparams
1734

  
1735
    """
1736
    return self._SendRequest(HTTP_PUT,
1737
                             ("/%s/networks/%s/disconnectall" %
1738
                             (GANETI_RAPI_VERSION, network_name)), None, None)
1739

  
1740
  def DeleteNetwork(self, network, dry_run=False):
1741
    """Deletes a network.
1742

  
1743
    @type group: str
1744
    @param group: the network to delete
1745
    @type dry_run: bool
1746
    @param dry_run: whether to peform a dry run
1747

  
1748
    @rtype: string
1749
    @return: job id
1750

  
1751
    """
1752
    query = []
1753
    _AppendDryRunIf(query, dry_run)
1754

  
1755
    return self._SendRequest(HTTP_DELETE,
1756
                             ("/%s/networks/%s" %
1757
                              (GANETI_RAPI_VERSION, network)), query, None)
1758

  
1628 1759
  def GetGroups(self, bulk=False):
1629 1760
    """Gets all node groups in the cluster.
1630 1761

  
b/lib/rapi/connector.py
89 89

  
90 90

  
91 91
def GetHandlers(node_name_pattern, instance_name_pattern,
92
                group_name_pattern, job_id_pattern, disk_pattern,
92
                group_name_pattern, network_name_pattern,
93
                job_id_pattern, disk_pattern,
93 94
                query_res_pattern):
94 95
  """Returns all supported resources and their handlers.
95 96

  
......
167 168
    re.compile(r"^/2/instances/(%s)/console$" % instance_name_pattern):
168 169
      rlib2.R_2_instances_name_console,
169 170

  
171
    "/2/networks": rlib2.R_2_networks,
172
    re.compile(r"^/2/networks/(%s)$" % network_name_pattern):
173
      rlib2.R_2_networks_name,
174
    re.compile(r"^/2/networks/(%s)/connect$" % network_name_pattern):
175
      rlib2.R_2_networks_name_connect,
176
    re.compile(r"^/2/networks/(%s)/disconnect$" % network_name_pattern):
177
      rlib2.R_2_networks_name_disconnect,
178
    re.compile(r"^/2/networks/(%s)/connectall$" % network_name_pattern):
179
      rlib2.R_2_networks_name_connectall,
180
    re.compile(r"^/2/networks/(%s)/disconnectall$" % network_name_pattern):
181
      rlib2.R_2_networks_name_disconnectall,
182

  
170 183
    "/2/groups": rlib2.R_2_groups,
171 184
    re.compile(r"^/2/groups/(%s)$" % group_name_pattern):
172 185
      rlib2.R_2_groups_name,
......
197 210
    }
198 211

  
199 212

  
200
CONNECTOR.update(GetHandlers(_NAME_PATTERN, _NAME_PATTERN, _NAME_PATTERN,
213
CONNECTOR.update(GetHandlers(_NAME_PATTERN, _NAME_PATTERN,
214
                             _NAME_PATTERN, _NAME_PATTERN,
201 215
                             constants.JOB_ID_TEMPLATE, _DISK_PATTERN,
202 216
                             _NAME_PATTERN))
b/lib/rapi/rlib2.py
70 70
I_FIELDS = ["name", "admin_state", "os",
71 71
            "pnode", "snodes",
72 72
            "disk_template",
73
            "nic.ips", "nic.macs", "nic.modes", "nic.links", "nic.bridges",
73
            "nic.ips", "nic.macs", "nic.modes",
74
            "nic.links", "nic.networks", "nic.bridges",
74 75
            "network_port",
75 76
            "disk.sizes", "disk_usage",
76 77
            "beparams", "hvparams",
......
90 91
            "group.uuid",
91 92
            ] + _COMMON_FIELDS
92 93

  
94
NET_FIELDS = ["name", "network", "gateway",
95
              "network6", "gateway6",
96
              "mac_prefix", "network_type",
97
              "free_count", "reserved_count",
98
              "map", "group_list", "inst_list",
99
              "external_reservations",
100
             ] 
101

  
93 102
G_FIELDS = [
94 103
  "alloc_policy",
95 104
  "name",
......
642 651
      })
643 652

  
644 653

  
654
class R_2_networks(baserlib.OpcodeResource):
655
  """/2/networks resource.
656

  
657
  """
658
  GET_OPCODE = opcodes.OpNetworkQuery
659
  POST_OPCODE = opcodes.OpNetworkAdd
660
  POST_RENAME = {
661
    "name": "network_name",
662
    }
663

  
664
  def GetPostOpInput(self):
665
    """Create a network.
666

  
667
    """
668
    assert not self.items
669
    return (self.request_body, {
670
      "dry_run": self.dryRun(),
671
      })
672

  
673
  def GET(self):
674
    """Returns a list of all networks.
675

  
676
    """
677
    client = self.GetClient()
678

  
679
    if self.useBulk():
680
      bulkdata = client.QueryNetworks([], NET_FIELDS, False)
681
      return baserlib.MapBulkFields(bulkdata, NET_FIELDS)
682
    else:
683
      data = client.QueryNetworks([], ["name"], False)
684
      networknames = [row[0] for row in data]
685
      return baserlib.BuildUriList(networknames, "/2/networks/%s",
686
                                   uri_fields=("name", "uri"))
687

  
688

  
689
class R_2_networks_name(baserlib.OpcodeResource):
690
  """/2/network/[network_name] resource.
691

  
692
  """
693
  DELETE_OPCODE = opcodes.OpNetworkRemove
694

  
695
  def GET(self):
696
    """Send information about a network.
697

  
698
    """
699
    network_name = self.items[0]
700
    client = self.GetClient()
701

  
702
    result = baserlib.HandleItemQueryErrors(client.QueryNetworks,
703
                                            names=[network_name],
704
                                            fields=NET_FIELDS,
705
                                            use_locking=self.useLocking())
706

  
707
    return baserlib.MapFields(NET_FIELDS, result[0])
708

  
709
  def GetDeleteOpInput(self):
710
    """Delete a network.
711

  
712
    """
713
    assert len(self.items) == 1
714
    return (self.request_body, {
715
      "network_name": self.items[0],
716
      "dry_run": self.dryRun(),
717
      })
718

  
719
class R_2_networks_name_connect(baserlib.OpcodeResource):
720
  """/2/network/[network_name]/connect.
721

  
722
  """
723
  PUT_OPCODE = opcodes.OpNetworkConnect
724

  
725
  def GetPutOpInput(self):
726
    """Changes some parameters of node group.
727

  
728
    """
729
    assert self.items
730
    return (self.request_body, {
731
      "network_name": self.items[0],
732
      })
733

  
734
class R_2_networks_name_connectall(baserlib.OpcodeResource):
735
  """/2/network/[network_name]/connectall.
736

  
737
  """
738
  PUT_OPCODE = opcodes.OpNetworkConnectAll
739

  
740
  def GetPutOpInput(self):
741
    """Changes some parameters of node group.
742

  
743
    """
744
    assert self.items
745
    return (self.request_body, {
746
      "network_name": self.items[0]
747
      })
748

  
749
class R_2_networks_name_disconnect(baserlib.OpcodeResource):
750
  """/2/network/[network_name]/disconnect.
751

  
752
  """
753
  PUT_OPCODE = opcodes.OpNetworkDisconnect
754

  
755
  def GetPutOpInput(self):
756
    """Changes some parameters of node group.
757

  
758
    """
759
    assert self.items
760
    return (self.request_body, {
761
      "network_name": self.items[0],
762
      })
763

  
764
class R_2_networks_name_disconnectall(baserlib.OpcodeResource):
765
  """/2/network/[network_name]/disconnectall.
766

  
767
  """
768
  PUT_OPCODE = opcodes.OpNetworkDisconnectAll
769

  
770
  def GetPutOpInput(self):
771
    """Changes some parameters of node group.
772

  
773
    """
774
    assert self.items
775
    return (self.request_body, {
776
      "network_name": self.items[0],
777
      })
778

  
645 779
class R_2_groups(baserlib.OpcodeResource):
646 780
  """/2/groups resource.
647 781

  
......
655 789
  def GetPostOpInput(self):
656 790
    """Create a node group.
657 791

  
792

  
658 793
    """
659 794
    assert not self.items
660 795
    return (self.request_body, {

Also available in: Unified diff