github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/archive/blank_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  	"errors"
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/sacloud/ftps"
    24  	"github.com/sacloud/libsacloud/v2/pkg/size"
    25  	"github.com/sacloud/libsacloud/v2/sacloud"
    26  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    27  )
    28  
    29  // BlankArchiveBuilder ブランクアーカイブの作成〜FTPSでのファイルアップロードを行う
    30  type BlankArchiveBuilder struct {
    31  	Name         string
    32  	Description  string
    33  	Tags         types.Tags
    34  	IconID       types.ID
    35  	SizeGB       int
    36  	SourceReader io.Reader
    37  
    38  	NoWait bool
    39  
    40  	Client *APIClient
    41  }
    42  
    43  // Validate 設定値の検証
    44  func (b *BlankArchiveBuilder) Validate(ctx context.Context, zone string) error {
    45  	if b.NoWait {
    46  		return errors.New("NoWait=true is not supported when uploading files and creating archives")
    47  	}
    48  	requiredValues := map[string]bool{
    49  		"Name":         b.Name == "",
    50  		"SizeGB":       b.SizeGB == 0,
    51  		"SourceReader": b.SourceReader == nil,
    52  	}
    53  	for key, empty := range requiredValues {
    54  		if empty {
    55  			return fmt.Errorf("%s is required", key)
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  // Build ブランクアーカイブの作成〜FTPSでのファイルアップロードを行う
    62  func (b *BlankArchiveBuilder) Build(ctx context.Context, zone string) (*sacloud.Archive, error) {
    63  	if err := b.Validate(ctx, zone); err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	archive, ftpServer, err := b.Client.Archive.CreateBlank(ctx, zone,
    68  		&sacloud.ArchiveCreateBlankRequest{
    69  			Name:        b.Name,
    70  			Description: b.Description,
    71  			Tags:        b.Tags,
    72  			IconID:      b.IconID,
    73  			SizeMB:      b.SizeGB * size.GiB,
    74  		})
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	// upload sources via FTPS
    80  	ftpsClient := ftps.NewClient(ftpServer.User, ftpServer.Password, ftpServer.HostName)
    81  
    82  	if err := ftpsClient.UploadReader("data.raw", b.SourceReader); err != nil {
    83  		return archive, fmt.Errorf("uploading file via FTPS is failed: %s", err)
    84  	}
    85  
    86  	// close FTP
    87  	if err := b.Client.Archive.CloseFTP(ctx, zone, archive.ID); err != nil {
    88  		return archive, err
    89  	}
    90  
    91  	// reload
    92  	return b.Client.Archive.Read(ctx, zone, archive.ID)
    93  }