Ejemplo n.º 1
0
 def update_project(self, project_id, **kwargs):
     """Updates a Project."""
     resp, body = self.get_project(project_id)
     name = kwargs.get('name', body['name'])
     desc = kwargs.get('description', body['description'])
     en = kwargs.get('enabled', body['enabled'])
     domain_id = kwargs.get('domain_id', body['domain_id'])
     post_body = common.Element("project",
                                xmlns=XMLNS,
                                name=name,
                                description=desc,
                                enabled=str(en).lower(),
                                domain_id=domain_id)
     resp, body = self.patch('projects/%s' % project_id,
                             str(common.Document(post_body)))
     self.expected_success(200, resp.status)
     body = self._parse_body(etree.fromstring(body))
     return resp, body
Ejemplo n.º 2
0
    def update_quota_set(self, tenant_id, gigabytes=None, volumes=None,
                         snapshots=None):
        post_body = {}
        element = xml.Element("quota_set")

        if gigabytes is not None:
            post_body['gigabytes'] = gigabytes

        if volumes is not None:
            post_body['volumes'] = volumes

        if snapshots is not None:
            post_body['snapshots'] = snapshots

        xml.deep_dict_to_xml(element, post_body)
        resp, body = self.put('os-quota-sets/%s' % tenant_id,
                              str(xml.Document(element)))
        body = xml.xml_to_json(etree.fromstring(body))
        return resp, self._format_quota(body)
Ejemplo n.º 3
0
 def create_user(self, user_name, **kwargs):
     """Creates a user."""
     password = kwargs.get('password', None)
     email = kwargs.get('email', None)
     en = kwargs.get('enabled', 'true')
     project_id = kwargs.get('project_id', None)
     description = kwargs.get('description', None)
     domain_id = kwargs.get('domain_id', 'default')
     post_body = common.Element("user",
                                xmlns=XMLNS,
                                name=user_name,
                                password=password,
                                description=description,
                                email=email,
                                enabled=str(en).lower(),
                                project_id=project_id,
                                domain_id=domain_id)
     resp, body = self.post('users', str(common.Document(post_body)))
     body = self._parse_body(etree.fromstring(body))
     return resp, body
Ejemplo n.º 4
0
 def update_user(self, user_id, name, **kwargs):
     """Updates a user."""
     resp, body = self.get_user(user_id)
     email = kwargs.get('email', body['email'])
     en = kwargs.get('enabled', body['enabled'])
     project_id = kwargs.get('project_id', body['project_id'])
     description = kwargs.get('description', body['description'])
     domain_id = kwargs.get('domain_id', body['domain_id'])
     update_user = common.Element("user",
                                  xmlns=XMLNS,
                                  name=name,
                                  email=email,
                                  project_id=project_id,
                                  domain_id=domain_id,
                                  description=description,
                                  enabled=str(en).lower())
     resp, body = self.patch('users/%s' % user_id,
                             str(common.Document(update_user)))
     body = self._parse_body(etree.fromstring(body))
     return resp, body
Ejemplo n.º 5
0
 def create_user(self,
                 user_name,
                 password=None,
                 project_id=None,
                 email=None,
                 domain_id='default',
                 **kwargs):
     """Creates a user."""
     en = kwargs.get('enabled', 'true')
     description = kwargs.get('description', None)
     post_body = common.Element("user",
                                xmlns=XMLNS,
                                name=user_name,
                                password=password,
                                description=description,
                                email=email,
                                enabled=str(en).lower(),
                                project_id=project_id,
                                domain_id=domain_id)
     resp, body = self.post('users', str(common.Document(post_body)))
     self.expected_success(201, resp.status)
     body = self._parse_body(etree.fromstring(body))
     return resp, body
Ejemplo n.º 6
0
    def update_endpoint(self,
                        endpoint_id,
                        service_id=None,
                        interface=None,
                        url=None,
                        region=None,
                        enabled=None,
                        **kwargs):
        """Updates an endpoint with given parameters.

        Normally this function wouldn't allow setting values that are not
        allowed for 'enabled'. Use `force_enabled` to set a non-boolean.

        """
        doc = common.Document()
        endpoint = common.Element("endpoint")
        doc.append(endpoint)

        if service_id:
            endpoint.add_attr("service_id", service_id)
        if interface:
            endpoint.add_attr("interface", interface)
        if url:
            endpoint.add_attr("url", url)
        if region:
            endpoint.add_attr("region", region)

        if 'force_enabled' in kwargs:
            endpoint.add_attr("enabled", kwargs['force_enabled'])
        elif enabled is not None:
            endpoint.add_attr("enabled", str(enabled).lower())

        resp, body = self.patch('endpoints/%s' % str(endpoint_id), str(doc))
        self.expected_success(200, resp.status)
        body = self._parse_body(etree.fromstring(body))
        return resp, body
Ejemplo n.º 7
0
 def test_parse_resp_body_item(self):
     body_item = xml.Element("item", **self.item_expected)
     body = self.rest_client._parse_resp(str(xml.Document(body_item)))
     self.assertEqual(self.item_expected, body)
Ejemplo n.º 8
0
    def create_server(self, name, image_ref, flavor_ref, **kwargs):
        """
        Creates an instance of a server.
        name (Required): The name of the server.
        image_ref (Required): Reference to the image used to build the server.
        flavor_ref (Required): The flavor used to build the server.
        Following optional keyword arguments are accepted:
        adminPass: Sets the initial root password.
        key_name: Key name of keypair that was created earlier.
        meta: A dictionary of values to be used as metadata.
        personality: A list of dictionaries for files to be injected into
        the server.
        security_groups: A list of security group dicts.
        networks: A list of network dicts with UUID and fixed_ip.
        user_data: User data for instance.
        availability_zone: Availability zone in which to launch instance.
        accessIPv4: The IPv4 access address for the server.
        accessIPv6: The IPv6 access address for the server.
        min_count: Count of minimum number of instances to launch.
        max_count: Count of maximum number of instances to launch.
        disk_config: Determines if user or admin controls disk configuration.
        block_device_mapping: Block device mapping for the server.
        """
        server = xml_utils.Element("server",
                                   xmlns=xml_utils.XMLNS_11,
                                   imageRef=image_ref,
                                   flavorRef=flavor_ref,
                                   name=name)

        for attr in [
                "adminPass", "accessIPv4", "accessIPv6", "key_name",
                "user_data", "availability_zone", "min_count", "max_count",
                "return_reservation_id", "block_device_mapping"
        ]:
            if attr in kwargs:
                server.add_attr(attr, kwargs[attr])

        if 'disk_config' in kwargs:
            server.add_attr(
                'xmlns:OS-DCF', "http://docs.openstack.org/"
                "compute/ext/disk_config/api/v1.1")
            server.add_attr('OS-DCF:diskConfig', kwargs['disk_config'])

        if 'security_groups' in kwargs:
            secgroups = xml_utils.Element("security_groups")
            server.append(secgroups)
            for secgroup in kwargs['security_groups']:
                s = xml_utils.Element("security_group", name=secgroup['name'])
                secgroups.append(s)

        if 'networks' in kwargs:
            networks = xml_utils.Element("networks")
            server.append(networks)
            for network in kwargs['networks']:
                s = xml_utils.Element("network",
                                      uuid=network['uuid'],
                                      fixed_ip=network['fixed_ip'])
                networks.append(s)

        if 'meta' in kwargs:
            metadata = xml_utils.Element("metadata")
            server.append(metadata)
            for k, v in kwargs['meta'].items():
                meta = xml_utils.Element("meta", key=k)
                meta.append(xml_utils.Text(v))
                metadata.append(meta)

        if 'personality' in kwargs:
            personality = xml_utils.Element('personality')
            server.append(personality)
            for k in kwargs['personality']:
                temp = xml_utils.Element('file', path=k['path'])
                temp.append(xml_utils.Text(k['contents']))
                personality.append(temp)

        if 'sched_hints' in kwargs:
            sched_hints = kwargs.get('sched_hints')
            hints = xml_utils.Element("os:scheduler_hints")
            hints.add_attr('xmlns:os', xml_utils.XMLNS_11)
            for attr in sched_hints:
                p1 = xml_utils.Element(attr)
                p1.append(sched_hints[attr])
                hints.append(p1)
            server.append(hints)
        resp, body = self.post('servers', str(xml_utils.Document(server)))
        server = self._parse_server(etree.fromstring(body))
        return resp, server
Ejemplo n.º 9
0
    def auth(self,
             user=None,
             password=None,
             tenant=None,
             user_type='id',
             domain=None,
             token=None):
        """
        :param user: user id or name, as specified in user_type
        :param domain: the user and tenant domain
        :param token: a token to re-scope.

        Accepts different combinations of credentials. Restrictions:
        - tenant and domain are only name (no id)
        - user domain and tenant domain are assumed identical
        - domain scope is not supported here
        Sample sample valid combinations:
        - token
        - token, tenant, domain
        - user_id, password
        - username, password, domain
        - username, password, tenant, domain
        Validation is left to the server side.
        """

        methods = common.Element('methods')
        identity = common.Element('identity')

        if token:
            method = common.Element('method')
            method.append(common.Text('token'))
            methods.append(method)

            token = common.Element('token', id=token)
            identity.append(token)

        if user and password:
            if user_type == 'id':
                _user = common.Element('user', id=user, password=password)
            else:
                _user = common.Element('user', name=user, password=password)
            if domain is not None:
                _domain = common.Element('domain', name=domain)
                _user.append(_domain)

            password = common.Element('password')
            password.append(_user)
            method = common.Element('method')
            method.append(common.Text('password'))
            methods.append(method)
            identity.append(password)

        identity.append(methods)

        auth = common.Element('auth')
        auth.append(identity)

        if tenant is not None:
            project = common.Element('project', name=tenant)
            _domain = common.Element('domain', name=domain)
            project.append(_domain)
            scope = common.Element('scope')
            scope.append(project)
            auth.append(scope)

        resp, body = self.post(self.auth_url, body=str(common.Document(auth)))
        return resp, body
Ejemplo n.º 10
0
 def add_dhcp_agent_to_network(self, agent_id, network_id):
     uri = '%s/agents/%s/dhcp-networks' % (self.uri_prefix, agent_id)
     network = common.Element("network_id", network_id)
     resp, body = self.post(uri, str(common.Document(network)))
     body = _root_tag_fetcher_and_xml_to_json_parse(body)
     return resp, body
Ejemplo n.º 11
0
 def add_router_to_l3_agent(self, agent_id, router_id):
     uri = '%s/agents/%s/l3-routers' % (self.uri_prefix, agent_id)
     router = (common.Element("router_id", router_id))
     resp, body = self.post(uri, str(common.Document(router)))
     body = _root_tag_fetcher_and_xml_to_json_parse(body)
     return resp, body
Ejemplo n.º 12
0
 def create_role(self, name):
     """Create a Role."""
     post_body = common.Element("role", xmlns=XMLNS, name=name)
     resp, body = self.post('roles', str(common.Document(post_body)))
     body = self._parse_body(etree.fromstring(body))
     return resp, body
Ejemplo n.º 13
0
 def create_role(self, name):
     """Create a role."""
     create_role = xml.Element("role", xmlns=XMLNS, name=name)
     resp, body = self.post('OS-KSADM/roles',
                            str(xml.Document(create_role)))
     return resp, self._parse_resp(body)
Ejemplo n.º 14
0
 def enable_disable_user(self, user_id, enabled):
     """Enables or disables a user."""
     enable_user = xml.Element("user", enabled=str(enabled).lower())
     resp, body = self.put('users/%s/enabled' % user_id,
                           str(xml.Document(enable_user)))
     return resp, self._parse_resp(body)
Ejemplo n.º 15
0
 def update_user_password(self, user_id, new_pass):
     """Update User Password."""
     put_body = xml.Element("user", id=user_id, password=new_pass)
     resp, body = self.put('users/%s/OS-KSADM/password' % user_id,
                           str(xml.Document(put_body)))
     return resp, self._parse_resp(body)
Ejemplo n.º 16
0
    def update_quota_set(self,
                         tenant_id,
                         user_id=None,
                         force=None,
                         injected_file_content_bytes=None,
                         metadata_items=None,
                         ram=None,
                         floating_ips=None,
                         fixed_ips=None,
                         key_pairs=None,
                         instances=None,
                         security_group_rules=None,
                         injected_files=None,
                         cores=None,
                         injected_file_path_bytes=None,
                         security_groups=None):
        """
        Updates the tenant's quota limits for one or more resources
        """
        post_body = xml_utils.Element("quota_set", xmlns=xml_utils.XMLNS_11)

        if force is not None:
            post_body.add_attr('force', force)

        if injected_file_content_bytes is not None:
            post_body.add_attr('injected_file_content_bytes',
                               injected_file_content_bytes)

        if metadata_items is not None:
            post_body.add_attr('metadata_items', metadata_items)

        if ram is not None:
            post_body.add_attr('ram', ram)

        if floating_ips is not None:
            post_body.add_attr('floating_ips', floating_ips)

        if fixed_ips is not None:
            post_body.add_attr('fixed_ips', fixed_ips)

        if key_pairs is not None:
            post_body.add_attr('key_pairs', key_pairs)

        if instances is not None:
            post_body.add_attr('instances', instances)

        if security_group_rules is not None:
            post_body.add_attr('security_group_rules', security_group_rules)

        if injected_files is not None:
            post_body.add_attr('injected_files', injected_files)

        if cores is not None:
            post_body.add_attr('cores', cores)

        if injected_file_path_bytes is not None:
            post_body.add_attr('injected_file_path_bytes',
                               injected_file_path_bytes)

        if security_groups is not None:
            post_body.add_attr('security_groups', security_groups)

        if user_id:
            resp, body = self.put(
                'os-quota-sets/%s?user_id=%s' % (str(tenant_id), str(user_id)),
                str(xml_utils.Document(post_body)))
        else:
            resp, body = self.put('os-quota-sets/%s' % str(tenant_id),
                                  str(xml_utils.Document(post_body)))

        body = xml_utils.xml_to_json(etree.fromstring(body))
        body = format_quota(body)
        return resp, body