Skip to content

Commit

Permalink
Merge pull request #9 from blp1526/zone_name
Browse files Browse the repository at this point in the history
Rename zone to zone_name
  • Loading branch information
blp1526 authored Jul 9, 2017
2 parents 42beaa4 + 163be73 commit d1c9b09
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
35 changes: 23 additions & 12 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,30 @@ type Body struct {
Port string `json:"Port"`
}

func Request(body interface{}, zoneName string, serverId string) error {
client := &http.Client{Timeout: 10 * time.Second}
func Request(body interface{}, zoneName string, serverName string) error {
scv := &config.Scv{}
current, _ := user.Current()
dir := filepath.Join(current.HomeDir, "scv.json")
config.Load(scv, dir)

if scv.AccessToken == "" || scv.AccessTokenSecret == "" {
message := fmt.Sprintf("Check scv.json, AccessToken is %s, AccessTokenSecret is %s", scv.AccessToken, scv.AccessTokenSecret)
return errors.New(message)
}

serverId := ""
for i := 0; i < len(scv.Servers); i++ {
if scv.Servers[i].ZoneName == zoneName && scv.Servers[i].Name == serverName {
serverId = scv.Servers[i].ID
}
}
logger.Debug(fmt.Sprintf("Found ServerID is %s", serverId))

if serverId == "" {
return errors.New(fmt.Sprintf("ServerID is not found by ZoneName %s and ServerName %s", zoneName, serverName))
}

client := &http.Client{Timeout: 10 * time.Second}
scheme := "https"
host := "secure.sakura.ad.jp"
path := "/cloud/zone/" + zoneName + "/api/cloud/1.1/server/" + serverId + "/vnc/proxy"
Expand All @@ -33,16 +54,6 @@ func Request(body interface{}, zoneName string, serverId string) error {
return err
}

scv := &config.Scv{}
current, _ := user.Current()
dir := filepath.Join(current.HomeDir, "scv.json")
config.Load(scv, dir)

if scv.AccessToken == "" || scv.AccessTokenSecret == "" {
message := fmt.Sprintf("Check scv.json, AccessToken is %s, AccessTokenSecret is %s", scv.AccessToken, scv.AccessTokenSecret)
return errors.New(message)
}

req.SetBasicAuth(scv.AccessToken, scv.AccessTokenSecret)
resp, err := client.Do(req)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type Scv struct {
}

type Server struct {
Name string `json:"name"`
Zone string `json:"zone"`
ID string `json:"id"`
Name string `json:"name"`
ZoneName string `json:"zone_name"`
ID string `json:"id"`
}

func Load(scv *Scv, filePath string) error {
Expand Down
8 changes: 0 additions & 8 deletions config/config_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions scv.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"access_token": "xxxx",
"access_token_secret": "xxxx",
"servers": [
{ "name": "ubuntu16.04", "zone": "is1a", "id": "999900000001" },
{ "name": "centos6.9", "zone": "is1b", "id": "999900000002" }
{ "zone_name": "is1a", "id": "1129XXXXXXX1", "name": "ubuntu16.04" },
{ "zone_name": "is1b", "id": "1129XXXXXXX2", "name": "centos6.9" }
]
}

0 comments on commit d1c9b09

Please sign in to comment.