github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/standard_archive_builder.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  	"fmt"
    20  
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    23  )
    24  
    25  // StandardArchiveBuilder 同一アカウント/同一ゾーンのディスク/アーカイブからアーカイブの作成を行う
    26  type StandardArchiveBuilder struct {
    27  	Name            string
    28  	Description     string
    29  	Tags            types.Tags
    30  	IconID          types.ID
    31  	SourceDiskID    types.ID
    32  	SourceArchiveID types.ID
    33  
    34  	NoWait bool
    35  	Client *APIClient
    36  }
    37  
    38  // Validate 設定値の検証
    39  func (b *StandardArchiveBuilder) Validate(ctx context.Context, zone string) error {
    40  	requiredValues := map[string]bool{
    41  		"Name":                            b.Name == "",
    42  		"SourceDiskID or SourceArchiveID": b.SourceArchiveID.IsEmpty() && b.SourceDiskID.IsEmpty(),
    43  	}
    44  	for key, empty := range requiredValues {
    45  		if empty {
    46  			return fmt.Errorf("%s is required", key)
    47  		}
    48  	}
    49  	return nil
    50  }
    51  
    52  // Build 同一アカウント/同一ゾーンのディスク/アーカイブからアーカイブの作成を行う
    53  func (b *StandardArchiveBuilder) Build(ctx context.Context, zone string) (*sacloud.Archive, error) {
    54  	if err := b.Validate(ctx, zone); err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	archive, err := b.Client.Archive.Create(ctx, zone,
    59  		&sacloud.ArchiveCreateRequest{
    60  			Name:            b.Name,
    61  			Description:     b.Description,
    62  			Tags:            b.Tags,
    63  			IconID:          b.IconID,
    64  			SourceDiskID:    b.SourceDiskID,
    65  			SourceArchiveID: b.SourceArchiveID,
    66  		})
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	if b.NoWait {
    72  		return archive, nil
    73  	}
    74  
    75  	lastState, err := sacloud.WaiterForReady(func() (interface{}, error) {
    76  		return b.Client.Archive.Read(ctx, zone, archive.ID)
    77  	}).WaitForState(ctx)
    78  
    79  	var ret *sacloud.Archive
    80  	if lastState != nil {
    81  		ret = lastState.(*sacloud.Archive)
    82  	}
    83  	return ret, err
    84  }