Skip to content

Commit

Permalink
serviceパッケージ (#39)
Browse files Browse the repository at this point in the history
* service: archiveパッケージ (#22, #23, #24, #25, #26, #28, #30, #31, #33, #34, #35, #36, #37, #38)
* libsacloudからsetupパッケージの移植 (#29)
* service/iaasをモジュール化 (#32)
* タグ名をrequestからserviceへ変更
  • Loading branch information
yamamoto-febc authored Mar 17, 2022
1 parent 431beab commit e514788
Show file tree
Hide file tree
Showing 751 changed files with 40,721 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ default: gen fmt set-license go-licenses-check goimports lint test
.PHONY: test
test:
(cd pkg; TESTACC= go test ./... $(TESTARGS) -v -timeout=120m -parallel=8 -race);
(cd service/iaas; TESTACC= go test ./... $(TESTARGS) -v -timeout=120m -parallel=8 -race);
TESTACC= go test ./... $(TESTARGS) -v -timeout=120m -parallel=8 -race;

.PHONY: testacc
testacc:
(cd pkg; TESTACC= go test ./... $(TESTARGS) -v -timeout=120m -parallel=8 -race);
(cd service/iaas; TESTACC= go test ./... $(TESTARGS) -v -timeout=120m -parallel=8 -race);
TESTACC=1 go test ./... $(TESTARGS) --tags=acctest -v -timeout=120m -parallel=8 ;

.PHONY: tools
Expand All @@ -48,6 +50,8 @@ gen: _gen fmt goimports set-license

.PHONY: _gen
_gen:
(cd pkg; go generate ./...)
(cd service/iaas; go generate ./...)
go generate ./...

.PHONY: goimports gosimports
Expand Down
15 changes: 12 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ module github.com/sacloud/sacloud-go

go 1.17

require github.com/stretchr/testify v1.7.0
require (
github.com/go-playground/validator/v10 v10.10.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
49 changes: 46 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.1 h1:uA0+amWMiglNZKZ9FJRKUAe9U3RX91eVn1JYXMWt7ig=
github.com/go-playground/validator/v10 v10.10.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 5 additions & 5 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ const (
CharSetNumber = "012346789"
)

const defaultResourceNamePrefix = "sacloud-go-testutil-"
// Random ランダムな文字列を生成して返す
func Random(strlen int, charSet string) string {
return RandomName("", strlen, charSet)
}

// RandomName ランダムな文字列を生成して返す
func RandomName(prefix string, strlen int, charSet string) string {
if prefix == "" {
prefix = defaultResourceNamePrefix
}
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = charSet[rand.Intn(len(charSet))]
}
return string(result)
return prefix + string(result)
}

// IsAccTest TESTACC環境変数が指定されているか
Expand Down
31 changes: 31 additions & 0 deletions service/iaas/archive/builder/api_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 The sacloud/sacloud-go Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import "github.com/sacloud/iaas-api-go"

// APIClient builderが利用するAPIクライアント
type APIClient struct {
Archive iaas.ArchiveAPI
Zone iaas.ZoneAPI
}

// NewAPIClient builderが利用するAPIクライアントを返す
func NewAPIClient(caller iaas.APICaller) *APIClient {
return &APIClient{
Archive: iaas.NewArchiveOp(caller),
Zone: iaas.NewZoneOp(caller),
}
}
93 changes: 93 additions & 0 deletions service/iaas/archive/builder/blank_archive_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2022 The sacloud/sacloud-go Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import (
"context"
"errors"
"fmt"
"io"

"github.com/sacloud/ftps"
"github.com/sacloud/iaas-api-go"
"github.com/sacloud/iaas-api-go/types"
"github.com/sacloud/sacloud-go/pkg/size"
)

// BlankArchiveBuilder ブランクアーカイブの作成〜FTPSでのファイルアップロードを行う
type BlankArchiveBuilder struct {
Name string
Description string
Tags types.Tags
IconID types.ID
SizeGB int
SourceReader io.Reader

NoWait bool

Client *APIClient
}

// Validate 設定値の検証
func (b *BlankArchiveBuilder) Validate(ctx context.Context, zone string) error {
if b.NoWait {
return errors.New("NoWait=true is not supported when uploading files and creating archives")
}
requiredValues := map[string]bool{
"Name": b.Name == "",
"SizeGB": b.SizeGB == 0,
"SourceReader": b.SourceReader == nil,
}
for key, empty := range requiredValues {
if empty {
return fmt.Errorf("%s is required", key)
}
}
return nil
}

// Build ブランクアーカイブの作成〜FTPSでのファイルアップロードを行う
func (b *BlankArchiveBuilder) Build(ctx context.Context, zone string) (*iaas.Archive, error) {
if err := b.Validate(ctx, zone); err != nil {
return nil, err
}

archive, ftpServer, err := b.Client.Archive.CreateBlank(ctx, zone,
&iaas.ArchiveCreateBlankRequest{
Name: b.Name,
Description: b.Description,
Tags: b.Tags,
IconID: b.IconID,
SizeMB: b.SizeGB * size.GiB,
})
if err != nil {
return nil, err
}

// upload sources via FTPS
ftpsClient := ftps.NewClient(ftpServer.User, ftpServer.Password, ftpServer.HostName)

if err := ftpsClient.UploadReader("data.raw", b.SourceReader); err != nil {
return archive, fmt.Errorf("uploading file via FTPS is failed: %s", err)
}

// close FTP
if err := b.Client.Archive.CloseFTP(ctx, zone, archive.ID); err != nil {
return archive, err
}

// reload
return b.Client.Archive.Read(ctx, zone, archive.ID)
}
86 changes: 86 additions & 0 deletions service/iaas/archive/builder/blank_archive_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2022 The sacloud/sacloud-go Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import (
"bytes"
"context"
"testing"

"github.com/sacloud/iaas-api-go"
"github.com/sacloud/iaas-api-go/testutil"
"github.com/sacloud/iaas-api-go/types"
"github.com/stretchr/testify/require"
)

func TestBlankArchiveBuilder_Validate(t *testing.T) {
builder := &BlankArchiveBuilder{
Name: "test",
SizeGB: 20,
SourceReader: bytes.NewBufferString(""),
NoWait: true,
Client: NewAPIClient(testutil.SingletonAPICaller()),
}

err := builder.Validate(context.Background(), testutil.TestZone())
require.EqualError(t, err, "NoWait=true is not supported when uploading files and creating archives")
}

func TestBlankArchiveBuilder_Build(t *testing.T) {
if !testutil.IsAccTest() {
t.Skip("TestBlankArchiveBuilder_Build only exec when running an Acceptance Test")
}

testZone := testutil.TestZone()
testutil.RunCRUD(t, &testutil.CRUDTestCase{
SetupAPICallerFunc: func() iaas.APICaller {
return testutil.SingletonAPICaller()
},
Parallel: true,
IgnoreStartupWait: true,
Create: &testutil.CRUDTestFunc{
Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
source := bytes.NewBufferString("dummy")

builder := &BlankArchiveBuilder{
Name: testutil.ResourceName("archive-from-shared-builder"),
Description: "description",
Tags: types.Tags{"tag1", "tag2"},
SizeGB: 20,
SourceReader: source,
Client: NewAPIClient(caller),
}
return builder.Build(ctx, testZone)
},
},
Read: &testutil.CRUDTestFunc{
Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
return iaas.NewArchiveOp(caller).Read(ctx, testZone, ctx.ID)
},
CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, value interface{}) error {
archive := value.(*iaas.Archive)
return testutil.DoAsserts(
testutil.AssertNotNilFunc(t, archive, "Archive"),
testutil.AssertTrueFunc(t, archive.Availability.IsAvailable(), "Archive.Availability.IsAvailable"),
)
},
},
Delete: &testutil.CRUDTestDeleteFunc{
Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
return iaas.NewArchiveOp(caller).Delete(ctx, testZone, ctx.ID)
},
},
})
}
Loading

0 comments on commit e514788

Please sign in to comment.