github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/director.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 archive
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    23  )
    24  
    25  // Builder アーカイブビルダーが持つ共通インターフェース
    26  type Builder interface {
    27  	Build(ctx context.Context, zone string) (*sacloud.Archive, error)
    28  	Validate(ctx context.Context, zone string) error
    29  }
    30  
    31  // Director パラメータに応じて適切なアーカイブビルダーを返す
    32  type Director struct {
    33  	Name        string
    34  	Description string
    35  	Tags        types.Tags
    36  	IconID      types.ID
    37  	SizeGB      int
    38  
    39  	// for blank builder
    40  	SourceReader io.Reader
    41  
    42  	// for standard builder
    43  	SourceDiskID    types.ID
    44  	SourceArchiveID types.ID
    45  
    46  	// transfer archive builder
    47  	SourceArchiveZone string
    48  
    49  	// for shared archive builder
    50  	SourceSharedKey types.ArchiveShareKey
    51  
    52  	// trueの場合アーカイブ作成完了まで待たずにreturnする。SourceReaderを指定する場合(BlankArchiveBuilder)にNoWaitをtrueにするとエラーとする
    53  	NoWait bool
    54  
    55  	Client *APIClient
    56  }
    57  
    58  // パラメータに応じて適切なアーカイブビルダーを返す
    59  //
    60  // Note: 他ゾーンからの転送の場合、転送元/先でゾーンが同一でもエラーとならない。
    61  // このためDirectorでは転送元/先ゾーンを意識せずにSourceArchiveZoneが指定されていた場合は
    62  // 一律でTransferArchiveBuilderを返す。
    63  //
    64  // もしこの挙動で問題が発生する場合は呼び出し側で適切にビルダーを切り替える実装を行う必要がある。
    65  func (d *Director) Builder() Builder {
    66  	if d.SourceReader != nil {
    67  		return &BlankArchiveBuilder{
    68  			Name:         d.Name,
    69  			Description:  d.Description,
    70  			Tags:         d.Tags,
    71  			IconID:       d.IconID,
    72  			SizeGB:       d.SizeGB,
    73  			SourceReader: d.SourceReader,
    74  			NoWait:       d.NoWait,
    75  			Client:       d.Client,
    76  		}
    77  	}
    78  	if d.SourceSharedKey.String() != "" {
    79  		return &FromSharedArchiveBuilder{
    80  			Name:            d.Name,
    81  			Description:     d.Description,
    82  			Tags:            d.Tags,
    83  			IconID:          d.IconID,
    84  			SourceSharedKey: d.SourceSharedKey,
    85  			NoWait:          d.NoWait,
    86  			Client:          d.Client,
    87  		}
    88  	}
    89  
    90  	if d.SourceArchiveZone != "" {
    91  		return &TransferArchiveBuilder{
    92  			Name:              d.Name,
    93  			Description:       d.Description,
    94  			Tags:              d.Tags,
    95  			IconID:            d.IconID,
    96  			SourceArchiveID:   d.SourceArchiveID,
    97  			SourceArchiveZone: d.SourceArchiveZone,
    98  			NoWait:            d.NoWait,
    99  			Client:            d.Client,
   100  		}
   101  	}
   102  
   103  	return &StandardArchiveBuilder{
   104  		Name:            d.Name,
   105  		Description:     d.Description,
   106  		Tags:            d.Tags,
   107  		IconID:          d.IconID,
   108  		SourceDiskID:    d.SourceDiskID,
   109  		SourceArchiveID: d.SourceArchiveID,
   110  		NoWait:          d.NoWait,
   111  		Client:          d.Client,
   112  	}
   113  }