From f111bb6fd53071247476ee389caa32e04a5d919e Mon Sep 17 00:00:00 2001 From: yuridekim Date: Mon, 1 Jan 2024 13:49:57 +0900 Subject: [PATCH 1/3] Add start for server interface --- pkg/constant.go | 1 + pkg/server.go | 48 ++++++++++++++++++++++++++++++++++++++++ types/server/request.go | 3 +++ types/server/response.go | 7 ++++++ 4 files changed, 59 insertions(+) diff --git a/pkg/constant.go b/pkg/constant.go index ae21597..75030e2 100644 --- a/pkg/constant.go +++ b/pkg/constant.go @@ -6,6 +6,7 @@ var ( GET_PRODUCT_LIST_PATH = "getServerProductList" 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" GET_NETWORKINTERFACE_LIST_PATH = "getNetworkInterfaceList" diff --git a/pkg/server.go b/pkg/server.go index 24c4a42..179a2d2 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -23,6 +23,7 @@ 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) } @@ -174,6 +175,53 @@ 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) + println(string(responseByteData)) + if err != nil { + log.Fatal(err) + return nil, err + } + + var csr *types.StartServerResponse + responseInterface, err := types.MapResponse(responseByteData, &csr) + + 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) diff --git a/types/server/request.go b/types/server/request.go index 3890296..925f3b7 100644 --- a/types/server/request.go +++ b/types/server/request.go @@ -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 } diff --git a/types/server/response.go b/types/server/response.go index 08ffaf7..1fe9767 100644 --- a/types/server/response.go +++ b/types/server/response.go @@ -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"` From b5bc35fb6d46b8e81e2a029fe19221a4eb008dde Mon Sep 17 00:00:00 2001 From: yuridekim Date: Mon, 1 Jan 2024 14:47:15 +0900 Subject: [PATCH 2/3] Add checking server status before deletion and update --- pkg/server.go | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/pkg/server.go b/pkg/server.go index 179a2d2..7d8bd56 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -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 @@ -28,6 +32,21 @@ type ServerInterface interface { 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) @@ -56,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 @@ -106,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) @@ -129,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 @@ -156,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) @@ -203,14 +220,13 @@ func (server *ServerService) Start(url string, request *types.StartServerRequest } responseByteData, err := io.ReadAll(resp.Body) - println(string(responseByteData)) if err != nil { log.Fatal(err) return nil, err } - var csr *types.StartServerResponse - responseInterface, err := types.MapResponse(responseByteData, &csr) + var ssr *types.StartServerResponse + responseInterface, err := types.MapResponse(responseByteData, &ssr) if err != nil { log.Fatal(err) @@ -250,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 @@ -270,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 @@ -297,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 From bdec3223cfdb3f91416de7f3c63640d3abf7e602 Mon Sep 17 00:00:00 2001 From: yuridekim Date: Mon, 1 Jan 2024 15:19:00 +0900 Subject: [PATCH 3/3] Add start to main test and change product code for update testing --- main.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 3d54e63..ce1dcaf 100644 --- a/main.go +++ b/main.go @@ -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) { @@ -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 { @@ -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") @@ -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)