On Thu, 2013-09-05 at 12:19 +0200, Petr Viktorin wrote:
> On 09/05/2013 06:38 AM, Nathaniel McCallum wrote:
> > 3. I had to make the 'id' option optional to make the uuid
> > autogeneration work in otp-add. However, this has the side-effect that
> > 'id' is now optional in all the other commands. This is particularly bad
> > in the case of otp-del, where calling this command with no ID
> > transparently removes all tokens. How can I make this optional for
> > otp-add but required for all other commands?
> 
> You'll need to add a new option flag.
> 
> 1. Add a 'optional_create' flag to the comment in ipalib.parameters.Param.
> 2. Handle the flag in ipalib.crud.Create.get_options (clone with 
> attribute=attribute, required=False)
> 
> See the handling of 'ask_create' for exapmles.

I spent part of yesterday and all day today working on this and I can't
make the attached patch work... No matter what I do, the Param with the
'optional_create' flag is never actually optional. There is no failure,
it just doesn't work. Any thoughts?

As a hint, the code in cli.py and frontend.py seems to call cmd.args()
rather than cmd.get_args(). Though, when I changed this it seemed to
break stuff. There seems to be lots of Python magic going on here via
the NameSpace class and I can't quite follow the logic.

Nathaniel
>From 71e04ef60696d3a58f6fc405d84fb27d6e528ef6 Mon Sep 17 00:00:00 2001
From: Nathaniel McCallum <npmccal...@redhat.com>
Date: Tue, 1 Oct 2013 13:57:24 -0400
Subject: [PATCH] Add optional_create flag

This permits IDs to be automatically generated if not specified.
---
 ipalib/crud.py       | 10 ++++++++--
 ipalib/parameters.py |  2 ++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ipalib/crud.py b/ipalib/crud.py
index d54b91fd7a927dda363f983d68fe732616663a82..84d6a43201fb34b4c59e270b141cd609eb32492d 100644
--- a/ipalib/crud.py
+++ b/ipalib/crud.py
@@ -132,7 +132,10 @@ class Create(Method):
 
     def get_args(self):
         if self.obj.primary_key:
-            yield self.obj.primary_key.clone(attribute=True)
+            kw = { 'attribute': True }
+            if 'optional_create' in self.obj.primary_key.flags:
+                kw['required'] = False
+            yield self.obj.primary_key.clone(**kw)
 
     def get_options(self):
         if self.extra_options_first:
@@ -148,7 +151,10 @@ class Create(Method):
                     autofill=False, alwaysask=True
                 )
             else:
-                yield option.clone(attribute=attribute)
+                kw = { 'attribute': attribute }
+                if 'optional_create' in option.flags:
+                    kw['required'] = False
+                yield option.clone(**kw)
         if not self.extra_options_first:
             for option in super(Create, self).get_options():
                 yield option
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index 30b9c920db95a1ec891d1a71553f11d3db645ed5..e645b02a1d442e10c315b05e7bcda3469d33de4d 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -351,6 +351,8 @@ class Param(ReadOnly):
               not be given at all. All crud.Update commands automatically
               convert required parameters to `nonempty` ones, so the value
               can be unspecified (unchanged) but cannot be deleted.
+            * optional_create: do not require the parameter for crud.Create
+              based commands
       - hint: this attribute is currently not used
       - alwaysask: when enabled, CLI asks for parameter value even when the
         parameter is not `required`
-- 
1.8.3.1

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to