Skip to content

Commit

Permalink
Merge pull request cloud-club#21 from yuridekim/main
Browse files Browse the repository at this point in the history
api 추가 구현
  • Loading branch information
yuridekim authored Jan 1, 2024
2 parents 7fe438f + bdec322 commit 75d8936
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 15 deletions.
25 changes: 21 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

pkg "github.com/cloud-club/Aviator-service/pkg"
server "github.com/cloud-club/Aviator-service/types/server"
types "github.com/cloud-club/Aviator-service/types/server"
)

func getImageProductList(ncp *pkg.NcpService) {
Expand Down Expand Up @@ -115,10 +116,12 @@ func list(ncp *pkg.NcpService) {

//Print response
fmt.Println(serverListResponse)
// fmt.Println("server: ", serverListResponse.ServerInstanceList[0])
// fmt.Println("status: ", serverListResponse.ServerInstanceList[0].ServerInstanceStatus.Code)
}

func update(ncp *pkg.NcpService) {
usr := &server.UpdateServerRequest{ServerInstanceNo: "21741451", ServerProductCode: "SVR.VSVR.HICPU.C004.M008.NET.HDD.B050.G002"}
usr := &server.UpdateServerRequest{ServerInstanceNo: "21741451", ServerProductCode: "SVR.VSVR.STAND.C032.M128.NET.HDD.B050.G002"}

updateServerResponse, err := ncp.Server.Update(pkg.API_URL+pkg.UPDATE_SERVER_INSTANCE_PATH, usr)
if err != nil {
Expand All @@ -143,6 +146,16 @@ func delete(ncp *pkg.NcpService) {
fmt.Println(deleteServerResponse)
}

func start(ncp *pkg.NcpService) {
ssr := &types.StartServerRequest{ServerNo: "21763788"}
_, err := ncp.Server.Start(pkg.API_URL+pkg.START_SERVER_INSTANCE_PATH, ssr)
if err != nil {
fmt.Println(err)
return
}

}

func main() {
ncp := pkg.NewNcpService("ncp service token")
ncp.Server = pkg.NewServerService("6CmrDJ4KaswJ10g25GEP", "OvZ7QHH0Bi3AwGn5rlsD7xoC986bEOiIjdbwMFCo")
Expand Down Expand Up @@ -174,11 +187,15 @@ func main() {
list(ncp)

// 7. Stop server instance
//stop(ncp)
stop(ncp)
// list(ncp)

// Start server instance
// start(ncp)

// list(ncp)
// time.Sleep(20 * time.Second)

// // Update server instance
// Update server instance
// update(ncp)
// time.Sleep(20 * time.Second)
// list(ncp)
Expand Down
4 changes: 2 additions & 2 deletions pkg/constant.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pkg

var (
API_URL = "https://ncloud.apigw.ntruss.com/vserver/v2/"

API_URL = "https://ncloud.apigw.ntruss.com/vserver/v2/"
GET_SERVER_INSTANCE_PATH = "getServerInstanceList"
CREATE_SERVER_INSTANCE_PATH = "createServerInstances"
UPDATE_SERVER_INSTANCE_PATH = "changeServerInstanceSpec"
START_SERVER_INSTANCE_PATH = "startServerInstances"
STOP_SERVER_INSTANCE_PATH = "stopServerInstances"
DELETE_SERVER_INSTANCE_PATH = "terminateServerInstances"

Expand Down
84 changes: 75 additions & 9 deletions pkg/server.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package pkg

import (
"errors"
"fmt"
"io"
"log"
"net/http"
"time"

types "github.com/cloud-club/Aviator-service/types/server"
)

var errorNotStopped = errors.New("Server is not stopped before update or deletion")

type ServerService struct {
accessKey string
secretKey string
Expand All @@ -23,10 +27,26 @@ type ServerInterface interface {
List(url string, request *types.ListServerRequest) (*types.ListServerResponse, error)
Create(url string, request *types.CreateServerRequest, params []int) (*types.CreateServerResponse, error)
Update(url string, request *types.UpdateServerRequest) (*types.UpdateServerResponse, error)
Start(url string, request *types.StartServerRequest) (*types.StartServerResponse, error)
Stop(url string, request *types.StopServerRequest) (*types.StopServerResponse, error)
Delete(url string, request *types.DeleteServerRequest) (*types.DeleteServerResponse, error)
}

func checkStatus(server *ServerService, condition string, repeat int) bool {
for i := 0; i < repeat; i++ {
lsr := &types.ListServerRequest{RegionCode: "KR"}
resp, _ := server.List(API_URL+GET_SERVER_INSTANCE_PATH, lsr)

serverStatus := resp.ServerInstanceList[0].ServerInstanceStatus.Code
if serverStatus == condition {
return true
}
time.Sleep(time.Second)
}

return false
}

func (server *ServerService) List(url string, request *types.ListServerRequest) (*types.ListServerResponse, error) {
requestParams := types.RequestString(request)

Expand Down Expand Up @@ -55,7 +75,6 @@ func (server *ServerService) List(url string, request *types.ListServerRequest)
}

responseByteData, err := io.ReadAll(resp.Body)
//println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
Expand Down Expand Up @@ -105,14 +124,11 @@ func (server *ServerService) Create(url string, request *types.CreateServerReque
}

responseByteData, err := io.ReadAll(resp.Body)
//println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
}

// fmt.Println("request:", requestParams)
// fmt.Println(string(responseByteData))
var csr *types.CreateServerResponse
responseInterface, err := types.MapResponse(responseByteData, &csr)

Expand All @@ -128,6 +144,10 @@ func (server *ServerService) Create(url string, request *types.CreateServerReque
}

func (server *ServerService) Update(url string, request *types.UpdateServerRequest) (*types.UpdateServerResponse, error) {
var usr *types.UpdateServerResponse
if serverStatus := checkStatus(server, "NSTOP", 25); !serverStatus {
return usr, errorNotStopped
}
requestParams := types.RequestString(request)

// Create an HTTP request
Expand Down Expand Up @@ -155,14 +175,12 @@ func (server *ServerService) Update(url string, request *types.UpdateServerReque
}

responseByteData, err := io.ReadAll(resp.Body)
//println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
}

var csr *types.UpdateServerResponse
responseInterface, err := types.MapResponse(responseByteData, &csr)
responseInterface, err := types.MapResponse(responseByteData, &usr)

if err != nil {
log.Fatal(err)
Expand All @@ -174,6 +192,52 @@ func (server *ServerService) Update(url string, request *types.UpdateServerReque
return *responseStruct, err
}

func (server *ServerService) Start(url string, request *types.StartServerRequest) (*types.StartServerResponse, error) {
requestParams := types.RequestString(request)

// Create an HTTP request
req, err := http.NewRequest(http.MethodGet, url+requestParams, nil)
if err != nil {
return nil, err
}
// Set HTTP header for NCP authorization
SetNCPHeader(req, server.accessKey, server.secretKey)

// Make the HTTP request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

// Check the response status
if resp.StatusCode != http.StatusOK {
// Read the response body and show the body message in error.
responseByteData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s", responseByteData)
}

responseByteData, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return nil, err
}

var ssr *types.StartServerResponse
responseInterface, err := types.MapResponse(responseByteData, &ssr)

if err != nil {
log.Fatal(err)
return nil, err
}

responseStruct := responseInterface.(**types.StartServerResponse)

return *responseStruct, err
}

func (server *ServerService) Stop(url string, request *types.StopServerRequest) (*types.StopServerResponse, error) {
requestParams := types.RequestString(request)

Expand Down Expand Up @@ -202,7 +266,6 @@ func (server *ServerService) Stop(url string, request *types.StopServerRequest)
}

responseByteData, err := io.ReadAll(resp.Body)
//println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
Expand All @@ -222,6 +285,10 @@ func (server *ServerService) Stop(url string, request *types.StopServerRequest)
}

func (server *ServerService) Delete(url string, request *types.DeleteServerRequest) (*types.DeleteServerResponse, error) {
var dsr *types.DeleteServerResponse
if serverStatus := checkStatus(server, "NSTOP", 20); !serverStatus {
return dsr, errorNotStopped
}
requestParams := types.RequestString(request)

// Create an HTTP request
Expand Down Expand Up @@ -249,7 +316,6 @@ func (server *ServerService) Delete(url string, request *types.DeleteServerReque
}

responseByteData, err := io.ReadAll(resp.Body)
//println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions types/server/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ type ListServerRequest struct {
RegionCode string `json:"regionCode"`
}

type StartServerRequest struct {
ServerNo string `json:"serverInstanceNoList.1"` // limiting only to a single server instance
}
type StopServerRequest struct {
ServerNo string `json:"serverInstanceNoList.1"` // limiting only to a single server instance
}
Expand Down
7 changes: 7 additions & 0 deletions types/server/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ type DeleteServerResponse struct {
ServerInstanceList []ServerInstance `xml:"serverInstanceList>serverInstance"`
}

type StartServerResponse struct {
ReturnCode int `xml:"returnCode"`
ReturnMessage string `xml:"returnMessage"`
TotalRows int `xml:"totalRows"`
ServerInstanceList []ServerInstance `xml:"serverInstanceList>serverInstance"`
}

type StopServerResponse struct {
ReturnCode int `xml:"returnCode"`
ReturnMessage string `xml:"returnMessage"`
Expand Down

0 comments on commit 75d8936

Please sign in to comment.