github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/from_shared_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  // FromSharedArchiveBuilder 共有アーカイブからアーカイブの作成を行う
    27  type FromSharedArchiveBuilder struct {
    28  	Name            string
    29  	Description     string
    30  	Tags            types.Tags
    31  	IconID          types.ID
    32  	SourceSharedKey types.ArchiveShareKey
    33  
    34  	NoWait bool
    35  	Client *APIClient
    36  }
    37  
    38  // Validate 設定値の検証
    39  func (b *FromSharedArchiveBuilder) Validate(ctx context.Context, zone string) error {
    40  	requiredValues := map[string]bool{
    41  		"Name":            b.Name == "",
    42  		"SourceSharedKey": b.SourceSharedKey == "",
    43  	}
    44  	for key, empty := range requiredValues {
    45  		if empty {
    46  			return fmt.Errorf("%s is required", key)
    47  		}
    48  	}
    49  	if !b.SourceSharedKey.ValidFormat() {
    50  		return fmt.Errorf("archive shared key is invalid format: key:%q", b.SourceSharedKey)
    51  	}
    52  	return nil
    53  }
    54  
    55  // Build 共有アーカイブからアーカイブの作成を行う
    56  func (b *FromSharedArchiveBuilder) 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  	archive, err := b.Client.Archive.CreateFromShared(ctx, b.SourceSharedKey.Zone(), b.SourceSharedKey.SourceArchiveID(), zoneID,
    67  		&sacloud.ArchiveCreateRequestFromShared{
    68  			Name:            b.Name,
    69  			Description:     b.Description,
    70  			Tags:            b.Tags,
    71  			IconID:          b.IconID,
    72  			SourceSharedKey: b.SourceSharedKey,
    73  		})
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	if b.NoWait {
    79  		return archive, nil
    80  	}
    81  
    82  	lastState, err := sacloud.WaiterForReady(func() (interface{}, error) {
    83  		return b.Client.Archive.Read(ctx, zone, archive.ID)
    84  	}).WaitForState(ctx)
    85  
    86  	var ret *sacloud.Archive
    87  	if lastState != nil {
    88  		ret = lastState.(*sacloud.Archive)
    89  	}
    90  	return ret, err
    91  }