diff --git a/api/api.go b/api/api.go index 0ee522a..2c233c2 100644 --- a/api/api.go +++ b/api/api.go @@ -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" @@ -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 { diff --git a/config/config.go b/config/config.go index ba6dc0e..5d326d4 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/config/config_test.go b/config/config_test.go deleted file mode 100644 index aed625d..0000000 --- a/config/config_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package config - -import ( - "testing" -) - -func TestConfig(t *testing.T) { -} diff --git a/scv.sample.json b/scv.sample.json index db453ee..c403810 100644 --- a/scv.sample.json +++ b/scv.sample.json @@ -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" } ] }