# $Id: $ # # Copyright (c) 2013 NetApp, Inc., All Rights Reserved # Any use, modification, or distribution is prohibited # without prior written consent from NetApp, Inc. # ## @summary VserverExportPolicy Task Module ## @author kathar.hidayath@netapp.com, dl-nacl-dev ## @status shared ## @pod here package NACL::STask::VserverExportPolicy; use strict; use warnings; use base qw(NACL::C::VserverExportPolicy NACL::STask::STask); use NATE::Log qw(log_global); my $Log = log_global(); my $may_enter = $Log->may_enter(); my $may_exit = $Log->may_exit(); use Params::Validate qw(validate SCALAR BOOLEAN SCALARREF); use NACL::C::Exceptions::VserverExportPolicy::AlreadyExists (); use NACL::C::Exceptions::VserverExportPolicy::DoesNotExist (); use NACL::C::Exceptions::VserverExportPolicy::CannotDeleteDefault (); use NATE::Exceptions::Argument qw(:try); use NACL::APISet::Exceptions::ResponseException (); use NACL::Exceptions::VerifyFailure (); =head1 NAME NACL::STask::VserverExportPolicy =head1 DESCRIPTION C provide methods to create, purge vserver export policy in ONTAP. It is created on top of C component Since it is derived class of C, we can use all the methods of C from the object of this task. =head1 ATTRIBUTES =head2 command_interface (Required) A component object that represents the node to which to send commands See L. =head2 vserver (Required) The name of the Vserver to be used =head2 policyname (Required) Name of the policy to be created =head1 METHODS =head2 create my $policy_obj = NACL::STask::VserverExportPolicy->create( 'command_interface' => $ci, 'vserver' => $vserver, 'policyname' => $policy, 'nacltask_if_exists' => $action # default die %other_options, ); (Class Method) This method is used to create an export policy. If policy already exists, it will perform the action based on "nacltask_if_exists" parameter. Default behavior would be "die". It returns the Task object for the created Policy. Uses CMode CLI/ZAPI. =over =item Options =over =item C<< command_interface => $command_interface >> (Required) See L =item C<< 'vserver' => $vserver >> (Required) Name of the vserver to be used. =item C<< 'policyname' => $policy >> (Required) Name of the policy to be created =item C<< apiset_must => $ruleset >> (Optional) See L =item C<< apiset_should => $ruleset >> (Optional) See L =item C<< "nacltask_if_exists => $action" >> (Optional) What to do if the policy to be created already exists. If $action is "die", then fail with an exception. If action is "purge", then purge the export policy(see the "purge" method, below) before creating a new one. If $action is "reuse", It will return the object of existing export policy. =item C<< "nacltask_verify => $action" >> (Optional) The user of this library can specify to verify whether the policy is created or not. If the action is 0, which is default, it wont verify the creation. If the user sets the action to 1, it will verify the it using the component state of this library. =item C<< "_was_created => \$scalar" >> (Optional) When this option is provided with a reference to a scalar variable, the variable gets filled in with a boolean value describing whether the policy was available (value will be 0; this scenario is possible when if_exists => "reuse") or whether the policy was created (value will be 1). This is necessary to determine whether the export policy needs to be cleaned up later. my $was_created; my $policy_obj = NACL::STask::VserverExportPolicy->create( vserver => $vserver_name, policyname => $policy, $nacltask_if_exists => 'reuse', _was_created => \$was_created, %other_opts ); # Operate on $policy_obj here # ... # Now determine whether to clean up the policy, since we're not sure # whether we reused an existing policy or created a new one if ($was_created) { # New export policy was created. Clean it up. $policy_obj->purge(); } command_interface, apiset_must, apiset_should, mode, etc. All of the other various options to L<< NACL::C::VserverExportPolicy->create|lib-NACL-C-VserverExportPolicy-pm/create >> =back =over =item Exceptions =over =item C This type of exception is thrown when an attempt is made to create vserver export policy that already exists. =item C This type of exception is thrown when verification fails for the created vserver policy. =back =cut sub create { $Log->enter() if $may_enter; my $pkg = shift; my %opts = $pkg->_common_validate_with( params => \@_, additional_spec => { nacltask_if_exists => $pkg->_if_exists_validate_spec(), nacltask_verify => { type => BOOLEAN, default => 0 }, _was_created => { type => SCALARREF, optional => 1 }, $pkg->_cleanup_validate_spec(), }, ); # Transform %opts from the options we received into the options to # pass to the base class method. my ( $self, %common_opts, %nacltask_opts ); my ( %opts_for_cleanup, %opts_for_register, $nacltask_to_cleanup ); $pkg->_copy_common_component_params_with_ci( source => \%opts, target => \%common_opts ); $pkg->_move_common_cleanup_opts( source => \%opts, target => \%opts_for_cleanup, ); $pkg->_move_nacltask_options( source => \%opts, target => \%nacltask_opts ); my $was_created = delete $opts{_was_created}; $$was_created = 0; my $if_exists = $nacltask_opts{'nacltask_if_exists'}; my $verify = $nacltask_opts{'nacltask_verify'}; CREATE: { use warnings; try { $self = $pkg->SUPER::create(%opts); $$was_created = 1; } catch NACL::C::Exceptions::VserverExportPolicy::AlreadyExists with { my $exception = shift; $Log->debug("The Policy already Exists!"); $self = $pkg->_element_exists_handler( create_opts => \%opts, nacltask_if_exists => $if_exists, exception => $exception ); if ( !$self ) { no warnings qw(exiting); redo CREATE; } }; } if ($verify) { $pkg->verify_state(%opts); } # Register this resource with the Cleanup Manager for cleanup $pkg->_copy_common_opts_for_cleanup( 'source' => {%opts, %opts_for_cleanup}, 'target' => \%opts_for_register, 'nacltask_to_cleanup' => \$nacltask_to_cleanup, 'to_cleanup' => 'purge' ); $pkg->_register_for_cleanup(%opts_for_register) if ($nacltask_to_cleanup && $$was_created); $Log->exit() if $may_exit; return $self; } ## end sub create =head2 purge $policy_obj->purge(); (or) NACL::STask::VserverExportPolicy->purge( 'command_interface' => $ci, 'vserver' => $vserver_name, 'policyname' => $policy, 'nacltask_verify' => 1, ); (Class or instance method) This method is used to delete the export policy. Uses a CMode CLI/ZAPI APISet. =over =item Options =over =item C<< command_interface => $ci >> (Required for class method, Not Applicable for instance method) A component object that represents the host which to send commands. See NACL::C::Component::command_interface =item C<< 'vserver' => $vserver >> (Required for class method, Not Applicable for instance method) Name of the vserver to be used. =item C<< 'policyname' => $policy >> (Required for class method, Not Applicable for instance method) Name of the policy to be created =item C<< nacltask_verify => $nacltask_verify_boolean >> (Optional) If '0' (default), verification will not be performed. If '1', verification will be performed to ensure that the deletion did happen successfully. =item C<< _was_deleted => \$scalar >> (Optional) When this option is provided a reference to a scalar variable, the variable gets filled in with a boolean value describing whether the policy did not exist (value will be 0; this scenario is possible when nacltask_if_purged => "pass") or whether the policy was deleted (value will be 1). =item C<< nacltask_if_purged => $nacltask_if_purged >> (Optional) If 'pass' (default), It will pass if the policy is already deleted. If 'fail', It will fail if the policy is already deleted. All of the other various options to L<< NACL::C::VserverExportPolicy->delete|lib-NACL-C-VserverExportPolicy-pm/delete >> =back =over =item Exceptions =over =item C This type of exception is thrown when an attempt is made to delete vserver export policy that does not exists. =item C This type of exception is thrown when an attempt is made to delete the default export policy. =item C This type of exception is thrown when verification fails for the deleted vserver export policy. =back =cut sub purge { $Log->enter() if $may_enter; my $pkg_or_obj = shift; my %orig_opts = @_; my $additional_spec = { nacltask_verify => { type => BOOLEAN, default => 0 }, nacltask_if_purged => $pkg_or_obj->_if_action_completed_already_validate_spec(), nacltask_ignore_default => { type => BOOLEAN, default => 0 }, _was_deleted => { type => SCALARREF, optional => 1 }, }; # Throw away options specific to the task $pkg_or_obj->_hash_move( source => \%orig_opts, target => {}, move => [ keys %{$additional_spec} ] ); my %opts = $pkg_or_obj->_common_validate_with( params => \@_, additional_spec => $additional_spec, ); my %common_opts; $pkg_or_obj->_copy_common_component_params_with_ci( source => \%opts, target => \%common_opts ); my $if_purged = delete $opts{nacltask_if_purged}; my $was_deleted = delete $opts{_was_deleted}; $$was_deleted = 0; try { $pkg_or_obj->SUPER::delete( %common_opts, %orig_opts ); $$was_deleted = 1; } catch NACL::C::Exceptions::VserverExportPolicy::DoesNotExist with { my $exception = shift; $Log->debug("Entry Does Not Exists!"); if ( $if_purged !~ /pass/i ) { $Log->exit() if $may_exit; $exception->throw(); } } catch NACL::C::Exceptions::VserverExportPolicy::CannotDeleteDefault with { my $exception = shift; if ( $opts{nacltask_ignore_default} ) { $Log->comment("Cannot delete default export policy."); } else { $Log->exit() if $may_exit; $exception->throw(); } }; if ( $opts{nacltask_verify} ) { $pkg_or_obj->_generic_purge_verify(%orig_opts); } $Log->exit() if $may_exit; } ## end sub purge 1;