def test_detach_real_with_project_id(self, CloudStack):
     self.conf.update({'CLOUDSTACK_VIP_NETWORK_INDEX': '0'})
     vms = {
         "virtualmachine": [{
             "id": "abc123",
             "nic": [{
                 "id": "def456",
                 "networkid": "netid1"
             }]
         }]
     }
     CloudStack.return_value = cloudstack_client = mock.Mock()
     cloudstack_client.listVirtualMachines.return_value = vms
     manager = networkapi_cloudstack.NetworkApiCloudstackLB(self.conf)
     lb = load_balancer.LoadBalancer("500",
                                     "myapp",
                                     "192.168.1.1",
                                     ip_id="303",
                                     project_id="pid-xxx")
     h = host.Host('abc123', 'name.host')
     manager.detach_real(lb, h)
     list_data = {"id": "abc123", "projectid": "pid-xxx"}
     cloudstack_client.listVirtualMachines.assert_called_with(list_data)
     assoc_data = {
         "nicid": "def456",
         "vipid": "500",
         "projectid": "pid-xxx"
     }
     cloudstack_client.disassociateGloboNetworkRealFromVip.assert_called_with(
         assoc_data)
     CloudStack.assert_called_with(self.conf["CLOUDSTACK_API_URL"],
                                   self.conf["CLOUDSTACK_API_KEY"],
                                   self.conf["CLOUDSTACK_SECRET_KEY"])
Exemple #2
0
 def create_host(self, name=None, alternative_id=0):
     group = self.get_conf("CLOUDSTACK_GROUP", "")
     user_data = self.get_user_data()
     if group and name:
         name = "{}_{}".format(group, name)
     elif not name:
         name = group
     data = {
         "group":
         group,
         "displayname":
         name,
         "templateid":
         self._get_alternate_conf("CLOUDSTACK_TEMPLATE_ID", alternative_id),
         "zoneid":
         self._get_alternate_conf("CLOUDSTACK_ZONE_ID", alternative_id),
         "serviceofferingid":
         self._get_alternate_conf("CLOUDSTACK_SERVICE_OFFERING_ID",
                                  alternative_id),
     }
     if user_data:
         data["userdata"] = self.client.encode_user_data(user_data)
     project_id = self._get_alternate_conf("CLOUDSTACK_PROJECT_ID",
                                           alternative_id, None)
     if project_id:
         data["projectid"] = project_id
     network_ids = self._get_alternate_conf("CLOUDSTACK_NETWORK_IDS",
                                            alternative_id, None)
     if network_ids:
         data["networkids"] = network_ids
     vm_job = self.client.deployVirtualMachine(data)
     max_tries = int(self.get_conf("CLOUDSTACK_MAX_TRIES", 100))
     if not vm_job.get("jobid"):
         raise CloudStackException(
             "unexpected response from deployVirtualMachine({}), expected jobid key, got: {}"
             .format(repr(data), repr(vm_job)))
     vm = self._wait_for_unit(vm_job, max_tries, project_id)
     tags = self.get_conf("HOST_TAGS", "")
     if tags:
         self._tag_vm(tags.split(","), vm, project_id)
     return host.Host(id=vm["id"],
                      dns_name=self._get_dns_name(vm),
                      alternative_id=alternative_id)
Exemple #3
0
 def test_detach_real(self, cs_mock):
     cs_instance = cs_mock.return_value
     cs_instance.removeFromLoadBalancerRule.return_value = {'jobid': 'j1'}
     lb = load_balancer.LoadBalancer('lbid',
                                     'lbname',
                                     'lbaddr',
                                     ip_id='ip_id',
                                     project_id='projid')
     h = host.Host('hostid', 'hostaddr')
     manager = cloudstack.CloudstackLB(self.conf)
     manager.detach_real(lb, h)
     cs_instance.removeFromLoadBalancerRule.assert_called_once_with({
         'id':
         'lbid',
         'projectid':
         'projid',
         'virtualmachineids':
         'hostid',
     })
     cs_instance.wait_for_job.assert_called_with('j1', 100)
Exemple #4
0
    def test_attach_real(self, cs_mock):
        cs_instance = cs_mock.return_value
        cs_instance.make_request.return_value = {'jobid': 'j1'}
        cs_instance.assignToLoadBalancerRule.return_value = {'jobid': 'j2'}
        cs_instance.wait_for_job.return_value = {'jobresult': True}

        vms = {
            "virtualmachine": [{
                "id": "abc123",
                "nic": [{
                    "id": "def456",
                    "networkid": "netid1"
                }]
            }]
        }
        cs_instance.listVirtualMachines.return_value = vms

        lb = load_balancer.LoadBalancer('lbid',
                                        'lbname',
                                        'lbaddr',
                                        ip_id='ip_id',
                                        project_id='projid')
        h = host.Host('hostid', 'hostaddr')
        manager = cloudstack.CloudstackLB(self.conf)
        manager.attach_real(lb, h)
        cs_instance.make_request.assert_called_once_with(
            'assignNetwork', {
                'id': 'lbid',
                'networkids': 'netid1',
                'projectid': 'projid',
            })
        cs_instance.assignToLoadBalancerRule.assert_called_once_with({
            'id':
            'lbid',
            'projectid':
            'projid',
            'virtualmachineids':
            'hostid',
        })
    def test_attach_real_with_network_index(self, CloudStack):
        vms = {
            "virtualmachine": [{
                "id":
                "abc123",
                "nic": [{
                    "id": "def456",
                    "networkid": "netid1"
                }, {
                    "id": "second-id",
                    "networkid": "netid2"
                }]
            }]
        }
        CloudStack.return_value = cloudstack_client = mock.Mock()
        cloudstack_client.listVirtualMachines.return_value = vms

        manager = networkapi_cloudstack.NetworkApiCloudstackLB(self.conf)
        lb = load_balancer.LoadBalancer(
            "500",
            "myapp",
            "192.168.1.1",
            ip_id="303",
            project_id=None,
        )
        h = host.Host('abc123', 'name.host')
        manager.attach_real(lb, h)
        list_data = {"id": "abc123"}
        cloudstack_client.listVirtualMachines.assert_called_with(list_data)
        net_data = {"networkid": "netid2", "vipid": "500"}
        cloudstack_client.addGloboNetworkVipToAccount.assert_called_with(
            net_data)
        assoc_data = {"nicid": "second-id", "vipid": "500"}
        cloudstack_client.associateGloboNetworkRealToVip.assert_called_with(
            assoc_data)
        CloudStack.assert_called_with(self.conf["CLOUDSTACK_API_URL"],
                                      self.conf["CLOUDSTACK_API_KEY"],
                                      self.conf["CLOUDSTACK_SECRET_KEY"])