You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ambari.apache.org by "Tom Beerbower (JIRA)" <ji...@apache.org> on 2014/11/12 14:59:34 UTC

[jira] [Updated] (AMBARI-8295) Views: support validation

     [ https://issues.apache.org/jira/browse/AMBARI-8295?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Tom Beerbower updated AMBARI-8295:
----------------------------------
    Attachment: AMBARI-8295.patch

> Views: support validation
> -------------------------
>
>                 Key: AMBARI-8295
>                 URL: https://issues.apache.org/jira/browse/AMBARI-8295
>             Project: Ambari
>          Issue Type: Bug
>            Reporter: Tom Beerbower
>            Assignee: Tom Beerbower
>             Fix For: 2.0.0
>
>         Attachments: AMBARI-8295.patch
>
>
> Proposal:
> The view framework SPI exposes a new Validator interface that could optionally be implemented by the view developer. The Validator interface allows the view developer to define the validation logic that gets applied when a new view instance is created or updated. The interface specifies a svalidate method that is passed a ViewInstanceDefinition. The given view instance is the instance that is being created or updated. The validator has full access to the instance definition as well as the owning view definition so it can check things like parameters and properties. If the validator successfully validates the given instance it simply returns. If the validation fails then the validator should throw an ValidationException with a message that describes the failure.
> {code}
> public interface Validator {
>   /**
>    * Validate the given view instance definition.  Return {@code null} to indicate that
>    * no validation was performed.
>    *
>    * @param definition  the view instance definition
>    * @param mode        the validation mode
>    *
>    * @return the instance validation result; may be {@code null}
>    */
>   public ValidationResult validateInstance(ViewInstanceDefinition definition, ValidationContext mode);
>   /**
>    * Validate a property of the given view instance definition.  Return {@code null} to indicate that
>    * no validation was performed.
>    *
>    * @param property    the property name
>    * @param definition  the view instance definition
>    * @param mode        the validation mode
>    *
>    * @return the instance validation result; may be {@code null}
>    */
>   public ValidationResult validateProperty(String property, ViewInstanceDefinition definition, ValidationContext mode);
>   /**
>    * The context in which a view instance validation check is performed.
>    */
>   public enum ValidationContext {
>     PRE_CREATE,  // validation prior to creation
>     PRE_UPDATE,  // validation prior to update
>     EXISTING     // validation of an existing view instance
>   }
> }
> {code}
> The context allows the framework a way to let the validator know at what point in the view instance lifecycle the validation is being performed.  If a validation fails during instance create (PRE_CREATE), the view framework will rollback the instance creation and will fail with an appropriate response (see below).  The same is true for instance update (PRE_UPDATE).  The validator (if specified) will also be called during a GET of view instance resources (EXISTING) and be reported back as properties of the resource in the normal response. 
> So, for example, a view instance may have a 'server' property.  During instance creation (PRE_CREATE), the validator for the view might check the 'server' property for invalid characters and fail the create if found.  After an instance is created and a request to get the instance resource is made (EXISTING), the validator might check to see if the server specified by the 'server' property is actually reachable and fail the verify if not.  In this case the view instance resource is still reachable but the fact that the validation failed will show as a property in the instance resource (see below).
> The result of the validation for the view instance and each of its properties are returned from the validator to the view framework in the validation result classes ...
> {code}
> public interface ValidationResult {
>   /**
>    * Determine whether or not the result is valid.
>    *
>    * @return true if the result is valid
>    */
>   public boolean isValid();
>   /**
>    * Get the detail of the validation result.
>    *
>    * @return the validation result detail
>    */
>   public String getDetail();
>   /**
>    * Successful validation result.
>    */
>   public static final ValidationResult SUCCESS = new ValidationResult() {
>     @Override
>     public boolean isValid() {
>       return true;
>     }
>     @Override
>     public String getDetail() {
>       return "OK";
>     }
>   };
> }
> {code}
> If a POST or PUT for a view instance fails because of a validation check the results will look something like this (note in the example that the validation for one property passed while the other failed) ...
> {code}
> {
>   "status": 400,
>   "message": "{"propertyResults": {
>     "cities": {
>         "valid": true,
>         "detail": "OK"
>     },
>     "units": {
>         "valid": false,
>         "detail": "Units must be either metric or imperial."
>     }
> }, "valid": false, "detail": "The instance has invalid properties."}"
> }
> {code}
> When a view specifies a validator, the instance resource will include the properties 'validation_result' and 'property_validation_results' which will be reevaluated each time the resource is requested...
> {code}
> {
>   "href" : "http://c6401.ambari.apache.org:8080/api/v1/views/WEATHER/versions/1.0.0/instances/EUROPE",
>   "ViewInstanceInfo" : {
>     "instance_name" : "EUROPE",
>     ...
>     "validation_result" : {
>       "valid" : true,
>       "detail" : "OK"
>     },
>     "version" : "1.0.0",
>     "view_name" : "WEATHER",
>     ...
>     "properties" : {
>       "cities" : "London, UK;Paris;Munich",
>       "units" : "imperial"
>     },
>     "property_validation_results" : {
>       "cities" : {
>         "valid" : true,
>         "detail" : "OK"
>       },
>       "units" : {
>         "valid" : true,
>         "detail" : "OK"
>       }
>     }
>   },
> ...
> {code}
> Note that in the examples above, the detail messages are provided by the view Validator implementation and are specific to the view (it can be any text that makes sense for that view).



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)