github.com/sacloud/libsacloud/v2@v2.32.3/helper/wait/simple_state_waiter.go (about)

     1  // Copyright 2016-2022 The Libsacloud Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package wait
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    23  )
    24  
    25  // ByFunc デフォルトのパラメータでSimpleStateWaiterを作成して返す
    26  func ByFunc(readStateFunc ReadStateFunc) sacloud.StateWaiter {
    27  	return &SimpleStateWaiter{ReadStateFunc: readStateFunc}
    28  }
    29  
    30  type ReadStateFunc func() (bool, error)
    31  
    32  // SimpleStateWaiter シンプルな待ち処理のためのsacloud.StateWaiterの実装
    33  //
    34  // sacloud.StatePollingWaiterをラップし、シンプルなfuncのみで待つべきかを判定する
    35  type SimpleStateWaiter struct {
    36  	// ReadStateFunc 待つべきかの判定func
    37  	// trueかつerrorが空の場合は待ち処理を完了させる
    38  	ReadStateFunc ReadStateFunc
    39  
    40  	// Timeout タイムアウト
    41  	Timeout time.Duration
    42  
    43  	// PollingInterval ポーリング間隔
    44  	PollingInterval time.Duration
    45  }
    46  
    47  func (s *SimpleStateWaiter) waiter() sacloud.StateWaiter {
    48  	return &sacloud.StatePollingWaiter{
    49  		ReadFunc: func() (interface{}, error) {
    50  			result, err := s.ReadStateFunc()
    51  			if err != nil {
    52  				return false, err
    53  			}
    54  			return &fakeState{available: result}, nil
    55  		},
    56  		TargetAvailability: []types.EAvailability{
    57  			types.Availabilities.Available,
    58  		},
    59  		PendingAvailability: []types.EAvailability{
    60  			types.Availabilities.Unknown,
    61  		},
    62  
    63  		PollingInterval: s.PollingInterval,
    64  		Timeout:         s.Timeout,
    65  	}
    66  }
    67  
    68  // WaitForState sacloud.StateWaiterの実装
    69  func (s *SimpleStateWaiter) WaitForState(ctx context.Context) (interface{}, error) {
    70  	return s.waiter().WaitForState(ctx)
    71  }
    72  
    73  // AsyncWaitForState sacloud.StateWaiterの実装
    74  func (s *SimpleStateWaiter) AsyncWaitForState(ctx context.Context) (compCh <-chan interface{}, progressCh <-chan interface{}, errorCh <-chan error) {
    75  	return s.waiter().AsyncWaitForState(ctx)
    76  }
    77  
    78  // SetPollingTimeout sacloud.StateWaiterの実装
    79  func (s *SimpleStateWaiter) SetPollingTimeout(d time.Duration) {
    80  	s.waiter().SetPollingTimeout(d)
    81  }
    82  
    83  // SetPollingInterval sacloud.StateWaiterの実装
    84  func (s *SimpleStateWaiter) SetPollingInterval(d time.Duration) {
    85  	s.waiter().SetPollingInterval(d)
    86  }
    87  
    88  type fakeState struct {
    89  	available bool
    90  }
    91  
    92  // GetAvailability accessor.Availabilityの実装
    93  func (f *fakeState) GetAvailability() types.EAvailability {
    94  	if f.available {
    95  		return types.Availabilities.Available
    96  	}
    97  	return types.Availabilities.Unknown
    98  }
    99  
   100  // SetAvailability accessor.Availabilityの実装
   101  func (f *fakeState) SetAvailability(v types.EAvailability) {
   102  	f.available = v == types.Availabilities.Available
   103  }