Skip to content

Commit

Permalink
Add document comments and fix all golint problems
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc committed Oct 14, 2016
1 parent c1148d0 commit f4ae7f0
Show file tree
Hide file tree
Showing 122 changed files with 3,398 additions and 1,004 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ install:

script:
- test -z "$(gofmt -s -l -w . | tee /dev/stderr)"
- test -z "$(golint ./... | grep -v 'should have comment' | tee /dev/stderr)"
- test -z "$(golint ./... | tee /dev/stderr)"
- go vet ./...
- go build -v ./...
- go test -v ./sacloud
Expand Down
65 changes: 29 additions & 36 deletions README.md
Expand Up @@ -5,7 +5,7 @@ on [`SAKURA CLOUD APIs`](http://developer.sakura.ad.jp/cloud/api/1.1/).

[![GoDoc](https://godoc.org/github.com/yamamoto-febc/libsacloud?status.svg)](https://godoc.org/github.com/yamamoto-febc/libsacloud)
[![Build Status](https://travis-ci.org/yamamoto-febc/libsacloud.svg?branch=master)](https://travis-ci.org/yamamoto-febc/libsacloud)

[![Go Report Card](https://goreportcard.com/badge/github.com/yamamoto-febc/libsacloud)](https://goreportcard.com/report/github.com/yamamoto-febc/libsacloud)
See list of implemented API clients [here](https://godoc.org/github.com/yamamoto-febc/libsacloud).

# Installation
Expand All @@ -23,7 +23,7 @@ import (
"fmt"
"github.com/yamamoto-febc/libsacloud/api"
"github.com/yamamoto-febc/libsacloud/builder"
"github.com/yamamoto-febc/libsacloud/sacloud"
"github.com/yamamoto-febc/libsacloud/sacloud/ostype"
)

func main() {
Expand All @@ -50,7 +50,7 @@ exit 0`
client := api.NewClient(token, secret, zone)

// Create server using CentOS public archive
result, err := builder.FromPublicArchiveUnix(client, sacloud.CentOS, serverName, password).
result, err := builder.FromPublicArchiveUnix(client, ostype.CentOS, serverName, password).
AddPublicNWConnectedNIC(). // connect shared segment
SetCore(core). // set cpu core
SetMemory(memory). // set memory size
Expand Down Expand Up @@ -84,7 +84,7 @@ package main

import (
"fmt"
API "github.com/yamamoto-febc/libsacloud/api"
"github.com/yamamoto-febc/libsacloud/api"
"os"
"time"
)
Expand All @@ -107,21 +107,15 @@ func main() {
)

// authorize
api := API.NewClient(token, secret, zone)
client := api.NewClient(token, secret, zone)

//search archives
fmt.Println("searching archives")
res, _ := api.Archive.
WithNameLike("CentOS 6.7 64bit").
WithSharedScope().
Limit(1).
Find()

archive := res.Archives[0]
archive, _ := client.Archive.FindLatestStableCentOS()

// search scripts
fmt.Println("searching scripts")
res, _ = api.Note.
res, _ := client.Note.
WithNameLike("WordPress").
WithSharedScope().
Limit(1).
Expand All @@ -130,77 +124,76 @@ func main() {

// create a disk
fmt.Println("creating a disk")
disk := api.Disk.New()
disk.Name = name
disk := client.Disk.New()
disk.Name = name
disk.Description = description
disk.Tags = []string{tag}
disk.SetDiskPlanToSSD()
disk.SetSourceArchive(archive.ID)

disk, _ = api.Disk.Create(disk)
disk, _ = client.Disk.Create(disk)

// create a server
fmt.Println("creating a server")
server := api.Server.New()
server := client.Server.New()
server.Name = name
server.Description = description
server.Tags = []string{tag}

// (set ServerPlan)
plan, _ := api.Product.Server.GetBySpec(cpu, mem)
server.SetServerPlanByID(plan.ID.String())
plan, _ := client.Product.Server.GetBySpec(cpu, mem)
server.SetServerPlanByID(plan.GetStrID())

server, _ = api.Server.Create(server)
server, _ = client.Server.Create(server)

// connect to shared segment

fmt.Println("connecting the server to shared segment")
iface, _ := api.Interface.CreateAndConnectToServer(server.ID)
api.Interface.ConnectToSharedSegment(iface.ID)
iface, _ := client.Interface.CreateAndConnectToServer(server.ID)
client.Interface.ConnectToSharedSegment(iface.ID)

// wait disk copy
err := api.Disk.SleepWhileCopying(disk.ID, 120*time.Second)
err := client.Disk.SleepWhileCopying(disk.ID, 120*time.Second)
if err != nil {
fmt.Println("failed")
os.Exit(1)
}

// config the disk
diskconf := api.Disk.NewCondig()
diskconf.HostName = hostName
diskconf.Password = password
diskconf.SSHKey.PublicKey = sshPublicKey
diskconf.AddNote(script.ID)
api.Disk.Config(disk.ID, diskconf)
diskConf := client.Disk.NewCondig()
diskConf.HostName = hostName
diskConf.Password = password
diskConf.SSHKey.PublicKey = sshPublicKey
diskConf.AddNote(script.ID)
client.Disk.Config(disk.ID, diskConf)

// boot
fmt.Println("booting the server")
api.Server.Boot(server.ID)
client.Server.Boot(server.ID)

// stop
time.Sleep(3 * time.Second)
fmt.Println("stopping the server")
api.Server.Stop(server.ID)
client.Server.Stop(server.ID)

err = api.Server.SleepUntilDown(server.ID, 120*time.Second)
// wait for server to down
err = client.Server.SleepUntilDown(server.ID, 120*time.Second)
if err != nil {
fmt.Println("failed")
os.Exit(1)
}

// disconnect the disk from the server
fmt.Println("disconnecting the disk")
api.Disk.DisconnectFromServer(disk.ID)
client.Disk.DisconnectFromServer(disk.ID)

// delete the server
fmt.Println("deleting the server")
api.Server.Delete(server.ID)
client.Server.Delete(server.ID)

// delete the disk
fmt.Println("deleting the disk")
api.Disk.Delete(disk.ID)

client.Disk.Delete(disk.ID)
}

```
Expand Down
64 changes: 42 additions & 22 deletions api/archive.go
Expand Up @@ -3,25 +3,28 @@ package api
import (
"fmt"
"github.com/yamamoto-febc/libsacloud/sacloud"
"github.com/yamamoto-febc/libsacloud/sacloud/ostype"
"strings"
"time"
)

// ArchiveAPI アーカイブAPI
type ArchiveAPI struct {
*baseAPI
FindFuncMapPerOSType map[sacloud.ArchiveOSTypes]func() (*sacloud.Archive, error)
findFuncMapPerOSType map[ostype.ArchiveOSTypes]func() (*sacloud.Archive, error)
}

var (
ArchiveLatestStableCentOSTags = []string{"current-stable", "distro-centos"}
ArchiveLatestStableUbuntuTags = []string{"current-stable", "distro-ubuntu"}
ArchiveLatestStableDebianTags = []string{"current-stable", "distro-debian"}
ArchiveLatestStableVyOSTags = []string{"current-stable", "distro-vyos"}
ArchiveLatestStableCoreOSTags = []string{"current-stable", "distro-coreos"}
ArchiveLatestStableKusanagiTags = []string{"current-stable", "pkg-kusanagi"}
archiveLatestStableCentOSTags = []string{"current-stable", "distro-centos"}
archiveLatestStableUbuntuTags = []string{"current-stable", "distro-ubuntu"}
archiveLatestStableDebianTags = []string{"current-stable", "distro-debian"}
archiveLatestStableVyOSTags = []string{"current-stable", "distro-vyos"}
archiveLatestStableCoreOSTags = []string{"current-stable", "distro-coreos"}
archiveLatestStableKusanagiTags = []string{"current-stable", "pkg-kusanagi"}
//ArchiveLatestStableSiteGuardTags = []string{"current-stable", "pkg-siteguard"} //tk1aではcurrent-stableタグが付いていないため絞り込めない
)

// NewArchiveAPI アーカイブAPI作成
func NewArchiveAPI(client *Client) *ArchiveAPI {
api := &ArchiveAPI{
baseAPI: &baseAPI{
Expand All @@ -32,18 +35,19 @@ func NewArchiveAPI(client *Client) *ArchiveAPI {
},
}

api.FindFuncMapPerOSType = map[sacloud.ArchiveOSTypes]func() (*sacloud.Archive, error){
sacloud.CentOS: api.FindLatestStableCentOS,
sacloud.Ubuntu: api.FindLatestStableUbuntu,
sacloud.Debian: api.FindLatestStableDebian,
sacloud.VyOS: api.FindLatestStableVyOS,
sacloud.CoreOS: api.FindLatestStableCoreOS,
sacloud.Kusanagi: api.FindLatestStableKusanagi,
api.findFuncMapPerOSType = map[ostype.ArchiveOSTypes]func() (*sacloud.Archive, error){
ostype.CentOS: api.FindLatestStableCentOS,
ostype.Ubuntu: api.FindLatestStableUbuntu,
ostype.Debian: api.FindLatestStableDebian,
ostype.VyOS: api.FindLatestStableVyOS,
ostype.CoreOS: api.FindLatestStableCoreOS,
ostype.Kusanagi: api.FindLatestStableKusanagi,
}

return api
}

// OpenFTP FTP接続開始
func (api *ArchiveAPI) OpenFTP(id int64) (*sacloud.FTPServer, error) {
var (
method = "PUT"
Expand All @@ -60,6 +64,7 @@ func (api *ArchiveAPI) OpenFTP(id int64) (*sacloud.FTPServer, error) {
return res.FTPServer, nil
}

// CloseFTP FTP接続終了
func (api *ArchiveAPI) CloseFTP(id int64) (bool, error) {
var (
method = "DELETE"
Expand All @@ -69,6 +74,7 @@ func (api *ArchiveAPI) CloseFTP(id int64) (bool, error) {

}

// SleepWhileCopying コピー終了まで待機
func (api *ArchiveAPI) SleepWhileCopying(id int64, timeout time.Duration) error {

current := 0 * time.Second
Expand All @@ -91,6 +97,7 @@ func (api *ArchiveAPI) SleepWhileCopying(id int64, timeout time.Duration) error
}
}

// CanEditDisk ディスクの修正が可能か判定
func (api *ArchiveAPI) CanEditDisk(id int64) (bool, error) {

archive, err := api.Read(id)
Expand Down Expand Up @@ -129,26 +136,39 @@ func (api *ArchiveAPI) CanEditDisk(id int64) (bool, error) {

}

// FindLatestStableCentOS 安定版最新のCentOSパブリックアーカイブを取得
func (api *ArchiveAPI) FindLatestStableCentOS() (*sacloud.Archive, error) {
return api.findByOSTags(ArchiveLatestStableCentOSTags)
return api.findByOSTags(archiveLatestStableCentOSTags)
}

// FindLatestStableDebian 安定版最新のDebianパブリックアーカイブを取得
func (api *ArchiveAPI) FindLatestStableDebian() (*sacloud.Archive, error) {
return api.findByOSTags(ArchiveLatestStableDebianTags)
return api.findByOSTags(archiveLatestStableDebianTags)
}

// FindLatestStableUbuntu 安定版最新のUbuntuパブリックアーカイブを取得
func (api *ArchiveAPI) FindLatestStableUbuntu() (*sacloud.Archive, error) {
return api.findByOSTags(ArchiveLatestStableUbuntuTags)
return api.findByOSTags(archiveLatestStableUbuntuTags)
}

// FindLatestStableVyOS 安定版最新のVyOSパブリックアーカイブを取得
func (api *ArchiveAPI) FindLatestStableVyOS() (*sacloud.Archive, error) {
return api.findByOSTags(ArchiveLatestStableVyOSTags)
return api.findByOSTags(archiveLatestStableVyOSTags)
}

// FindLatestStableCoreOS 安定版最新のCoreOSパブリックアーカイブを取得
func (api *ArchiveAPI) FindLatestStableCoreOS() (*sacloud.Archive, error) {
return api.findByOSTags(ArchiveLatestStableCoreOSTags)
return api.findByOSTags(archiveLatestStableCoreOSTags)
}

// FindLatestStableKusanagi 安定版最新のKusanagiパブリックアーカイブを取得
func (api *ArchiveAPI) FindLatestStableKusanagi() (*sacloud.Archive, error) {
return api.findByOSTags(ArchiveLatestStableKusanagiTags)
return api.findByOSTags(archiveLatestStableKusanagiTags)
}
func (api *ArchiveAPI) FindByOSType(os sacloud.ArchiveOSTypes) (*sacloud.Archive, error) {
if f, ok := api.FindFuncMapPerOSType[os]; ok {

// FindByOSType 指定のOS種別の安定版最新のパブリックアーカイブを取得
func (api *ArchiveAPI) FindByOSType(os ostype.ArchiveOSTypes) (*sacloud.Archive, error) {
if f, ok := api.findFuncMapPerOSType[os]; ok {
return f()
}

Expand Down

0 comments on commit f4ae7f0

Please sign in to comment.