# # Copyright (c) 2014-2016 NetApp, Inc., All Rights Reserved # Any use, modification, or distribution is prohibited # without prior written consent from NetApp, Inc. # ## @summary ClusterDate ComponentState Module (auto-generated by CGT) ## @author dl-nacl-dev@netapp.com ## @status shared ## @pod here =head1 NAME NACL::CS::ClusterDate =head1 DESCRIPTION C is a derived class of L. It represents the state of an ONTAP ClusterDate. A related class is L, which represents access to an ONTAP ClusterDate. =head1 ATTRIBUTES The individual pieces of data that are part of the state of the ClusterDate element are the attributes of the ClusterDate ComponentState. Additionally, the command_interface used to obtain the ComponentState object is also an attribute of the object. This makes it easier to obtain the component object corresponding to the CS object, using L. =over =item command_interface The command_interface with which the CS object was obtained. (Available in all CS objects regardless of requested_fields and the mode/interface) =item C<< utc_date >> UTC Date and Time Filled in for CMode/7Mode CLI =item C<< alt_date >> *Alternative Date and Time Filled in for CMode CLI =item C<< timezone >> Time Zone Filled in for CMode/7Mode CLI =item C<< utcdateandtime >> UTC Date and Time Maps to: CMode ZAPI: $value Filled in for CMode CLI/ZAPI =item C<< date >> Date and Time Filled in for CMode CLI =item C<< dateandtime >> Date and Time Maps to: CMode ZAPI: $value Filled in for CMode CLI/ZAPI =item C<< node >> Node Filled in for CMode CLI =item C<< utc_epoch >> UTC epoch CMode ZAPI: $value Filled in for CModeZAPI =item C<< local_epoch >> Local epoch CMode ZAPI: $value Filled in for CMode ZAPI =back =cut package NACL::CS::ClusterDate; use strict; use warnings; use Params::Validate qw(validate validate_pos HASHREF); use NATE::Log qw(log_global); my $Log = log_global(); my $may_enter = $Log->may_enter(); my $may_exit = $Log->may_exit(); use NACL::ComponentUtils qw(convert_to_system_time enumerate_month enumerate_week); use NACL::Exceptions::NoElementsFound qw(:try); use base 'NACL::CS::ComponentState::ONTAP'; use Class::MethodMaker [ scalar => 'utc_date', scalar => 'alt_date', scalar => 'timezone', scalar => 'utcdateandtime', scalar => 'date', scalar => 'dateandtime', scalar => 'node', scalar => 'utc_epoch', scalar => 'local_epoch', ]; =head1 METHODS =head2 fetch my $ClusterDate_state = NACL::CS::ClusterDate->fetch(command_interface => $ci, ...); my @ClusterDate_states = NACL::CS::ClusterDate->fetch(command_interface => $ci, ...); (Class method) Discovers which elements are present and returns their state in ComponentState objects. Called in scalar context it returns only one state object, in list context it returns all state objects. See L for a more detailed description along with a complete explanation of the options it accepts. Supports CMode CLI/ZAPI and 7Mode CLI. Invokes "clock-get-clock" API for CMode ZAPI. =over =item Exceptions =over =item C When there are no elements matching the query specified or elements of that type doesn't exist, then this exception will be thrown. =back =back =cut sub fetch { $Log->enter() if $may_enter; my $pkg = shift; my @state_objs = $pkg->SUPER::fetch( @_, choices => [ { method => '_fetch_cmode_cli', interface => 'CLI', set => 'CMode' }, { method => '_fetch_cmode_zapi', interface => 'ZAPI', set => 'CMode' }, { method => '_fetch_7mode_cli', interface => 'CLI', set => '7Mode', } ], exception_text => 'No matching date(s) found', show_cmd => 'cluster date show', ); $Log->exit() if $may_exit; return wantarray ? @state_objs : $state_objs[0]; } sub _fetch_cmode_cli { $Log->enter() if $may_enter; my ($pkg, @args) = @_; ## w.r.t burt974474,789946 'cluster date show' ## fails intermittently with error ## "node_date_iterator::get_imp()" took longer than 25 seconds ## adding a retry logic here my $retry_attempts = 0; my @state_objs ; FETCH_DATE: { try{ @state_objs = $pkg->SUPER::_fetch_cmode_cli(@args, api => 'cluster_date_show'); }catch NACL::APISet::Exceptions::ResponseException with{ my $exception = shift; if($exception->text() =~ /node_date_iterator::get_imp|took longer than 25 seconds to complete/){ ++$retry_attempts ; if($retry_attempts > 3){ $exception->throw(); }else{ goto FETCH_DATE ; } }else{ $exception->throw(); } } } $Log->exit() if $may_exit; return @state_objs; } sub _fetch_cmode_zapi { $Log->enter() if $may_enter; my ($pkg, @args) = @_; my %opts = validate @args, $pkg->_fetch_backend_validate_spec(); my $apiset = $opts{apiset}; my $obj = $pkg->new(command_interface => $opts{command_interface}); my $timezone_resp = $apiset->clock_get_timezone(); my $timezone_output = $timezone_resp->get_parsed_output()->[0]; my $timezone = $timezone_output->{timezone}; $obj->timezone($timezone); my %fields; #Node wont be set on CServer command interface $obj->node($opts{command_interface}->node()) if ( $opts{command_interface}->isa('NACL::C::Node') ); if ($pkg->_want_any_field_of( requested_fields => $opts{requested_fields}, filter => $opts{filter}, fields_filled_by_api => [qw(date utc-date dateandtime utcdateandtime utc-epoch local-epoch)] ) ) { my $clock_response = $apiset->clock_get_clock(); my $clock_output = $clock_response->get_parsed_output()->[0]; if ($opts{filter}->{'want_epoch'}) { $fields{'utc-epoch'} = $clock_output->{'utc-time'}; $fields{'local-epoch'} = $clock_output->{'local-time'}; } else { #For same epoch values, convert_to_system_time() returns same date-time format values. #i.e. For timezone "Etc" or "Etc/UTC" zapi o/p does have #same epoch values for both 'utc-time' and 'local-time' fields hence #convert_to_system_time() does return same converted values for both the fields. # Returns a hash like this: # {'hour' => '06','minute' => '01','second' => '48','month' => '07', # 'day_of_week' => 3,'day' => '24','year' => '2013'} my $utc_hash = $pkg->convert_to_system_time( unixtime => $clock_output->{'utc-time'}, timezone => '+0000' ); $utc_hash->{month} =~ s/(\d)/$1/; $utc_hash->{day} =~ s/(\d)/$1/; my $local_hash = $pkg->convert_to_system_time( unixtime => $clock_output->{'local-time'}, timezone => '+0000' ); # for dateandtime field value generation my $dateandtime_hash = $local_hash; $dateandtime_hash->{month} =~ s/(\d)/$1/; $dateandtime_hash->{day} =~ s/(\d)/$1/; $local_hash->{month} =~ s/0(\d)/$1/; $local_hash->{day} =~ s/0(\d)/$1/; # hh:mm:ss my $construct_hh_mm_ss = sub { my ($hash) = validate_pos(@_, {type => HASHREF}); return "$hash->{hour}:$hash->{minute}:$hash->{second}"; }; # MM/DD/YYYY my $construct_mm_dd_yyyy = sub { my ($hash) = validate_pos(@_, {type => HASHREF}); return "$hash->{month}/$hash->{day}/$hash->{year}"; }; if ($timezone_output->{'timezone-UTC'} =~ /(\+|\-)(\d\d)(\d\d)/i) { $timezone_output->{'timezone-UTC'} = $1 . $2 . ":" . $3; } # "date" is in this form: 7/24/2013 06:05:05 -07:00 $fields{date} = $construct_mm_dd_yyyy->($local_hash) . ' ' . $construct_hh_mm_ss->($local_hash) . ' ' . $timezone_output->{'timezone-UTC'}; # "utc-date" is in this form: 7/24/2013 13:05:05 $fields{'utc-date'} = $construct_mm_dd_yyyy->($utc_hash) . ' ' . $construct_hh_mm_ss->($utc_hash); # "dateandtime" and "utcdateandtime" are in this form: Wed Jul 24 06:05:05 2013 my $construct_dateandtime = sub { my ($hash) = validate_pos(@_, {type => HASHREF}); # "wday" should be between 1 and 7, "day_of_week" field (within hash output returned from convert_to_system_time() ) # does maintain this value my $day = $pkg->enumerate_week( wday => ($hash->{day_of_week}), reverse => 1 ); my $mon = $pkg->enumerate_month( month => $hash->{month}, reverse => 1 ); my $hh_mm_ss = $construct_hh_mm_ss->($hash); return "$day $mon $hash->{day} $hh_mm_ss $hash->{year}"; }; $fields{dateandtime} = $construct_dateandtime->($dateandtime_hash); $fields{utcdateandtime} = $construct_dateandtime->($utc_hash); } } $obj->_set_fields(row => \%fields); $Log->exit() if $may_exit; return $obj; } ## end sub _fetch_cmode_zapi sub _fetch_7mode_cli { my $pkg = shift; my %opts = validate @_, $pkg->_fetch_backend_validate_spec(); my $apiset = delete $opts{apiset}; my $response = $apiset->date(); my $output = $response->get_parsed_output(); my $Node = $opts{command_interface}->node(); my ( @state_objs, @values, $timezone ); if ($pkg->_want_any_field_of( requested_fields => $opts{requested_fields}, filter => $opts{filter}, fields_filled_by_api => [qw(timezone utc-date)] ) ) { my $output = $apiset->timezone()->get_parsed_output(); $timezone = $output->[0]->{timezone}; } foreach my $row (@$output) { my $obj = $pkg->new(node => $Node); my %hash = (); $hash{dateandtime} = "$$row{'system_day_date'} $$row{'system_current_time'}"; my $time; @values = split(/\s+/, $$row{'system_day_date'}); my $month_string = $values[1]; my $DD = $values[2]; my $HHMMSS = $$row{'system_current_time_without_timezone'}; my $YYYY = $$row{'system_year'}; # Turn the month string into the MM format my $MM = $pkg->enumerate_month(month => $month_string); $hash{'date'} = "$MM/$DD/$YYYY $HHMMSS"; if ($timezone) { my ($hr, $min, $sec) = split /:/, $HHMMSS; my $dt = DateTime->new( year => $YYYY, month => $MM, day => $DD, hour => $hr, minute => $min, second => $sec, time_zone => $timezone, ); $dt->set_time_zone('UTC'); my $mdy = $dt->mdy('/'); my $hms = $dt->hms(); $hash{utc_date} = "$mdy $hms"; $hash{'timezone'} = $timezone; } $hash{'command_interface'} = $opts{'command_interface'}; $obj->_set_fields(row => \%hash); push @state_objs, $obj; } ## end foreach my $row (@$output) return @state_objs; } ## end sub _fetch_7mode_cli sub _extra_filter_fields { $Log->enter() if $may_enter; $Log->exit() if $may_exit; return [ qw( want_epoch ) ]; } 1;