~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/metadataserver/api.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-27 14:49:56 UTC
  • mto: (20.1.1 quantal) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20120327144956-z5stunhc83bnnwsi
Tags: upstream-0.1+bzr363+dfsg
ImportĀ upstreamĀ versionĀ 0.1+bzr363+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    PermissionDenied,
24
24
    Unauthorized,
25
25
    )
 
26
from maasserver.models import SSHKey
26
27
from metadataserver.models import (
27
28
    NodeKey,
28
29
    NodeUserData,
99
100
class MetaDataHandler(VersionIndexHandler):
100
101
    """Meta-data listing for a given version."""
101
102
 
102
 
    fields = ('instance-id', 'local-hostname',)
 
103
    fields = ('instance-id', 'local-hostname', 'public-keys')
103
104
 
104
105
    def get_attribute_producer(self, item):
105
106
        """Return a callable to deliver a given metadata item.
119
120
        producers = {
120
121
            'local-hostname': self.local_hostname,
121
122
            'instance-id': self.instance_id,
 
123
            'public-keys': self.public_keys,
122
124
        }
123
125
 
124
126
        return producers[field]
125
127
 
126
128
    def read(self, request, version, item=None):
 
129
        check_version(version)
 
130
        node = get_node_for_request(request)
 
131
 
 
132
        # Requesting the list of attributes, not any particular
 
133
        # attribute.
127
134
        if item is None or len(item) == 0:
128
 
            # Requesting the list of attributes, not any particular
129
 
            # attribute.
130
 
            return make_list_response(sorted(self.fields))
 
135
            fields = list(self.fields)
 
136
            # Add public-keys to the list of attributes, if the
 
137
            # node has registered SSH keys.
 
138
            keys = SSHKey.objects.get_keys_for_user(user=node.owner)
 
139
            if not keys:
 
140
                fields.remove('public-keys')
 
141
            return make_list_response(sorted(fields))
131
142
 
132
 
        check_version(version)
133
 
        node = get_node_for_request(request)
134
143
        producer = self.get_attribute_producer(item)
135
144
        return producer(node, version, item)
136
145
 
142
151
        """Produce instance-id attribute."""
143
152
        return make_text_response(node.system_id)
144
153
 
 
154
    def public_keys(self, node, version, item):
 
155
        """ Produce public-keys attribute."""
 
156
        keys = SSHKey.objects.get_keys_for_user(user=node.owner)
 
157
        if not keys:
 
158
            raise MAASAPINotFound("No registered public keys")
 
159
        return make_list_response(keys)
 
160
 
145
161
 
146
162
class UserDataHandler(MetadataViewHandler):
147
163
    """User-data blob for a given version."""