ia64/xen-unstable

changeset 14545:af07c7b01893

Implement session.last_active, session.this_host, session.get_record,
session.get_uuid.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Mon Mar 26 00:08:13 2007 +0100 (2007-03-26)
parents e3e7c59cdba1
children eb52a7f22e72
files tools/libxen/include/xen_common.h tools/libxen/src/xen_common.c tools/libxen/test/test_bindings.c tools/python/xen/xend/XendAPI.py
line diff
     1.1 --- a/tools/libxen/include/xen_common.h	Sun Mar 25 22:17:30 2007 +0100
     1.2 +++ b/tools/libxen/include/xen_common.h	Mon Mar 26 00:08:13 2007 +0100
     1.3 @@ -51,6 +51,30 @@ typedef struct
     1.4  } xen_session;
     1.5  
     1.6  
     1.7 +typedef struct xen_session_record
     1.8 +{
     1.9 +    char *uuid;
    1.10 +    struct xen_host_record_opt *this_host;
    1.11 +    char *this_user;
    1.12 +    time_t last_active;
    1.13 +} xen_session_record;
    1.14 +
    1.15 +
    1.16 +/**
    1.17 + * Allocate a xen_session_record.
    1.18 + */
    1.19 +extern xen_session_record *
    1.20 +xen_session_record_alloc(void);
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + * Free the given xen_session_record, and all referenced values.  The
    1.25 + * given record must have been allocated by this library.
    1.26 + */
    1.27 +extern void
    1.28 +xen_session_record_free(xen_session_record *record);
    1.29 +
    1.30 +
    1.31  struct xen_task_;
    1.32  typedef struct xen_task_ * xen_task_id;
    1.33  
    1.34 @@ -136,10 +160,45 @@ xen_session_logout(xen_session *session)
    1.35  
    1.36  
    1.37  /**
    1.38 - * Set *result to be a handle to the host to which this session is connected.
    1.39 + * Get the UUID of the second given session.  Set *result to point at a
    1.40 + * string, yours to free.
    1.41 + */
    1.42 +extern bool
    1.43 +xen_session_get_uuid(xen_session *session, char **result,
    1.44 +                     xen_session *self_session);
    1.45 +
    1.46 +
    1.47 +/**
    1.48 + * Get the this_host field of the second given session.  Set *result to be a
    1.49 + * handle to that host.
    1.50   */
    1.51 -extern int
    1.52 -xen_session_get_this_host(xen_session *session, xen_host *result);
    1.53 +extern bool
    1.54 +xen_session_get_this_host(xen_session *session, xen_host *result,
    1.55 +                          xen_session *self_session);
    1.56 +
    1.57 +
    1.58 +/**
    1.59 + * Get the this_user field of the second given session.  Set *result to point
    1.60 + * at a string, yours to free.
    1.61 + */
    1.62 +extern bool
    1.63 +xen_session_get_this_user(xen_session *session, char **result,
    1.64 +                          xen_session *self_session);
    1.65 +
    1.66 +
    1.67 +/**
    1.68 + * Get the last_active field of the given session, and place it in *result.
    1.69 + */
    1.70 +extern bool
    1.71 +xen_session_get_last_active(xen_session *session, time_t *result,
    1.72 +                            xen_session *self_session);
    1.73 +
    1.74 +/**
    1.75 + * Get a record containing the current state of the second given session.
    1.76 + */
    1.77 +extern bool
    1.78 +xen_session_get_record(xen_session *session, xen_session_record **result,
    1.79 +                       xen_session *self_session);
    1.80  
    1.81  
    1.82  #endif
     2.1 --- a/tools/libxen/src/xen_common.c	Sun Mar 25 22:17:30 2007 +0100
     2.2 +++ b/tools/libxen/src/xen_common.c	Mon Mar 26 00:08:13 2007 +0100
     2.3 @@ -32,6 +32,7 @@
     2.4  #include <libxml/xpath.h>
     2.5  
     2.6  #include "xen_common.h"
     2.7 +#include "xen_host.h"
     2.8  #include "xen_internal.h"
     2.9  #include "xen_int_float_map.h"
    2.10  #include "xen_string_string_map.h"
    2.11 @@ -138,6 +139,20 @@ xen_fini(void)
    2.12  }
    2.13  
    2.14  
    2.15 +void
    2.16 +xen_session_record_free(xen_session_record *record)
    2.17 +{
    2.18 +    if (record == NULL)
    2.19 +    {
    2.20 +        return;
    2.21 +    }
    2.22 +    free(record->uuid);
    2.23 +    xen_host_record_opt_free(record->this_host);
    2.24 +    free(record->this_user);
    2.25 +    free(record);
    2.26 +}
    2.27 +
    2.28 +
    2.29  xen_session *
    2.30  xen_session_login_with_password(xen_call_func call_func, void *handle,
    2.31                                  const char *uname, const char *pwd)
    2.32 @@ -187,15 +202,111 @@ xen_session_logout(xen_session *session)
    2.33  }
    2.34  
    2.35  
    2.36 -int
    2.37 -xen_session_get_this_host(xen_session *session, xen_host *result)
    2.38 +bool
    2.39 +xen_session_get_uuid(xen_session *session, char **result,
    2.40 +                     xen_session *self_session)
    2.41 +{
    2.42 +    abstract_value params[] =
    2.43 +        {
    2.44 +	  { .type = &abstract_type_string,
    2.45 +	    .u.string_val = self_session->session_id }
    2.46 +        };
    2.47 +
    2.48 +    xen_call_(session, "session.get_uuid", params, 1,
    2.49 +              &abstract_type_string, result);
    2.50 +    return session->ok;
    2.51 +}
    2.52 +
    2.53 +
    2.54 +bool
    2.55 +xen_session_get_this_host(xen_session *session, xen_host *result,
    2.56 +                          xen_session *self_session)
    2.57 +{
    2.58 +    abstract_value params[] =
    2.59 +        {
    2.60 +	  { .type = &abstract_type_string,
    2.61 +	    .u.string_val = self_session->session_id }
    2.62 +        };
    2.63 +
    2.64 +    xen_call_(session, "session.get_this_host", params, 1,
    2.65 +              &abstract_type_string, result);
    2.66 +    return session->ok;
    2.67 +}
    2.68 +
    2.69 +
    2.70 +bool
    2.71 +xen_session_get_this_user(xen_session *session, char **result,
    2.72 +                          xen_session *self_session)
    2.73  {
    2.74      abstract_value params[] =
    2.75          {
    2.76 +	  { .type = &abstract_type_string,
    2.77 +	    .u.string_val = self_session->session_id }
    2.78 +        };
    2.79 +
    2.80 +    xen_call_(session, "session.get_this_user", params, 1,
    2.81 +              &abstract_type_string, result);
    2.82 +    return session->ok;
    2.83 +}
    2.84 +
    2.85 +
    2.86 +bool
    2.87 +xen_session_get_last_active(xen_session *session, time_t *result,
    2.88 +                            xen_session *self_session)
    2.89 +{
    2.90 +    abstract_value params[] =
    2.91 +        {
    2.92 +	  { .type = &abstract_type_string,
    2.93 +	    .u.string_val = self_session->session_id }
    2.94          };
    2.95  
    2.96 -    xen_call_(session, "session.get_this_host", params, 0,
    2.97 -              &abstract_type_string, result);
    2.98 +    xen_call_(session, "session.get_last_active", params, 1,
    2.99 +              &abstract_type_datetime, result);
   2.100 +    return session->ok;
   2.101 +}
   2.102 +
   2.103 +
   2.104 +static const struct_member xen_session_record_struct_members[] =
   2.105 +    {
   2.106 +        { .key = "uuid",
   2.107 +          .type = &abstract_type_string,
   2.108 +          .offset = offsetof(xen_session_record, uuid) },
   2.109 +        { .key = "this_host",
   2.110 +          .type = &abstract_type_ref,
   2.111 +          .offset = offsetof(xen_session_record, this_host) },
   2.112 +        { .key = "this_user",
   2.113 +          .type = &abstract_type_string,
   2.114 +          .offset = offsetof(xen_session_record, this_user) },
   2.115 +        { .key = "last_active",
   2.116 +          .type = &abstract_type_datetime,
   2.117 +          .offset = offsetof(xen_session_record, last_active) },
   2.118 +    };
   2.119 +
   2.120 +const abstract_type xen_session_record_abstract_type_ =
   2.121 +    {
   2.122 +       .typename = STRUCT,
   2.123 +       .struct_size = sizeof(xen_session_record),
   2.124 +       .member_count =
   2.125 +           sizeof(xen_session_record_struct_members) / sizeof(struct_member),
   2.126 +       .members = xen_session_record_struct_members
   2.127 +    };
   2.128 +
   2.129 +
   2.130 +bool
   2.131 +xen_session_get_record(xen_session *session, xen_session_record **result,
   2.132 +                       xen_session *self_session)
   2.133 +{
   2.134 +    abstract_value param_values[] =
   2.135 +        {
   2.136 +            { .type = &abstract_type_string,
   2.137 +              .u.string_val = self_session->session_id }
   2.138 +        };
   2.139 +
   2.140 +    abstract_type result_type = xen_session_record_abstract_type_;
   2.141 +
   2.142 +    *result = NULL;
   2.143 +    XEN_CALL_("session.get_record");
   2.144 +
   2.145      return session->ok;
   2.146  }
   2.147  
     3.1 --- a/tools/libxen/test/test_bindings.c	Sun Mar 25 22:17:30 2007 +0100
     3.2 +++ b/tools/libxen/test/test_bindings.c	Mon Mar 26 00:08:13 2007 +0100
     3.3 @@ -17,6 +17,7 @@
     3.4   */
     3.5  
     3.6  #define _GNU_SOURCE
     3.7 +#include <assert.h>
     3.8  #include <inttypes.h>
     3.9  #include <stdlib.h>
    3.10  #include <stdio.h>
    3.11 @@ -61,6 +62,7 @@ typedef struct
    3.12  
    3.13  
    3.14  static xen_vm create_new_vm(xen_session *session, bool hvm);
    3.15 +static void print_session_info(xen_session *session);
    3.16  static void print_vm_power_state(xen_session *session, xen_vm vm);
    3.17  static void print_vm_metrics(xen_session *session, xen_vm vm);
    3.18  
    3.19 @@ -144,6 +146,14 @@ int main(int argc, char **argv)
    3.20      xen_session *session =
    3.21          xen_session_login_with_password(call_func, NULL, username, password);
    3.22  
    3.23 +    print_session_info(session);
    3.24 +    if (!session->ok)
    3.25 +    {
    3.26 +        /* Error has been logged, just clean up. */
    3.27 +        CLEANUP;
    3.28 +        return 1;
    3.29 +    }
    3.30 +
    3.31      xen_vm vm;
    3.32      if (!xen_vm_get_by_uuid(session, &vm,
    3.33                              "00000000-0000-0000-0000-000000000000"))
    3.34 @@ -184,7 +194,7 @@ int main(int argc, char **argv)
    3.35      }
    3.36  
    3.37      xen_host host;
    3.38 -    if (!xen_session_get_this_host(session, &host))
    3.39 +    if (!xen_session_get_this_host(session, &host, session))
    3.40      {
    3.41          print_error(session);
    3.42          xen_vm_record_free(vm_record);
    3.43 @@ -583,6 +593,44 @@ static size_t my_strftime(char *s, size_
    3.44  
    3.45  
    3.46  /**
    3.47 + * Print some session details.
    3.48 + */
    3.49 +static void print_session_info(xen_session *session)
    3.50 +{
    3.51 +    xen_session_record *record;
    3.52 +    if (!xen_session_get_record(session, &record, session))
    3.53 +    {
    3.54 +        print_error(session);
    3.55 +        return;
    3.56 +    }
    3.57 +
    3.58 +    printf("Session UUID: %s.\n", record->uuid);
    3.59 +    printf("Session user: %s.\n", record->this_user);
    3.60 +    char time[256];
    3.61 +    struct tm *tm = localtime(&record->last_active);
    3.62 +    my_strftime(time, 256, "Session last active: %c, local time.\n", tm);
    3.63 +    printf(time);
    3.64 +
    3.65 +    char *uuid = NULL;
    3.66 +    char *this_user = NULL;
    3.67 +    xen_session_get_uuid(session, &uuid, session);
    3.68 +    xen_session_get_this_user(session, &this_user, session);
    3.69 +
    3.70 +    if (!session->ok)
    3.71 +    {
    3.72 +        xen_session_record_free(record);
    3.73 +        print_error(session);
    3.74 +        return;
    3.75 +    }
    3.76 +
    3.77 +    assert(!strcmp(record->uuid, uuid));
    3.78 +    assert(!strcmp(record->this_user, this_user));
    3.79 +
    3.80 +    xen_session_record_free(record);
    3.81 +}
    3.82 +
    3.83 +
    3.84 +/**
    3.85   * Print the metrics for the given VM.
    3.86   */
    3.87  static void print_vm_metrics(xen_session *session, xen_vm vm)
     4.1 --- a/tools/python/xen/xend/XendAPI.py	Sun Mar 25 22:17:30 2007 +0100
     4.2 +++ b/tools/python/xen/xend/XendAPI.py	Mon Mar 26 00:08:13 2007 +0100
     4.3 @@ -523,9 +523,12 @@ class XendAPI(object):
     4.4      # ----------------------------------------------------------------
     4.5      # NOTE: Left unwrapped by __init__
     4.6  
     4.7 -    session_attr_ro = ['this_host', 'this_user']
     4.8 +    session_attr_ro = ['this_host', 'this_user', 'last_active']
     4.9      session_methods = [('logout', None)]
    4.10  
    4.11 +    def session_get_all(self, session):
    4.12 +        return xen_api_success([session])
    4.13 +
    4.14      def session_login_with_password(self, *args):
    4.15          if len(args) != 2:
    4.16              return xen_api_error(
    4.17 @@ -534,8 +537,8 @@ class XendAPI(object):
    4.18          username = args[0]
    4.19          password = args[1]
    4.20          try:
    4.21 -            session = (self.auth == AUTH_NONE and
    4.22 -                       auth_manager().login_unconditionally(username) or
    4.23 +            session = ((self.auth == AUTH_NONE and
    4.24 +                        auth_manager().login_unconditionally(username)) or
    4.25                         auth_manager().login_with_password(username, password))
    4.26              return xen_api_success(session)
    4.27          except XendError, e:
    4.28 @@ -546,27 +549,41 @@ class XendAPI(object):
    4.29      def session_logout(self, session):
    4.30          auth_manager().logout(session)
    4.31          return xen_api_success_void()
    4.32 -    def session_get_record(self, session):
    4.33 -        record = {'uuid'     : session,
    4.34 -                  'this_host': XendNode.instance().uuid,
    4.35 -                  'this_user': auth_manager().get_user(session)}
    4.36 +
    4.37 +    def session_get_record(self, session, self_session):
    4.38 +        if self_session != session:
    4.39 +            return xen_api_error(['PERMISSION_DENIED'])
    4.40 +        record = {'uuid'       : session,
    4.41 +                  'this_host'  : XendNode.instance().uuid,
    4.42 +                  'this_user'  : auth_manager().get_user(session),
    4.43 +                  'last_active': now()}
    4.44          return xen_api_success(record)
    4.45  
    4.46 -    def session_get_uuid(self, session):
    4.47 -        return xen_api_success(session)
    4.48 +    def session_get_uuid(self, session, self_session):
    4.49 +        return xen_api_success(self_session)
    4.50  
    4.51 -    def session_get_by_uuid(self, session):
    4.52 -        return xen_api_success(session)
    4.53 +    def session_get_by_uuid(self, session, self_session):
    4.54 +        return xen_api_success(self_session)
    4.55  
    4.56      # attributes (ro)
    4.57 -    def session_get_this_host(self, session):
    4.58 +    def session_get_this_host(self, session, self_session):
    4.59 +        if self_session != session:
    4.60 +            return xen_api_error(['PERMISSION_DENIED'])
    4.61          return xen_api_success(XendNode.instance().uuid)
    4.62 -    def session_get_this_user(self, session):
    4.63 +
    4.64 +    def session_get_this_user(self, session, self_session):
    4.65 +        if self_session != session:
    4.66 +            return xen_api_error(['PERMISSION_DENIED'])
    4.67          user = auth_manager().get_user(session)
    4.68 -        if user:
    4.69 +        if user is not None:
    4.70              return xen_api_success(user)
    4.71          return xen_api_error(['SESSION_INVALID', session])
    4.72  
    4.73 +    def session_get_last_active(self, session, self_session):
    4.74 +        if self_session != session:
    4.75 +            return xen_api_error(['PERMISSION_DENIED'])
    4.76 +        return xen_api_success(now())
    4.77 +
    4.78  
    4.79      # Xen API: Class User
    4.80      # ----------------------------------------------------------------