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