Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
yuridekim committed Jan 1, 2024
2 parents b5bc35f + 7fe438f commit 3552831
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 76 deletions.
1 change: 0 additions & 1 deletion create/root.go

This file was deleted.

1 change: 0 additions & 1 deletion delete/root.go

This file was deleted.

1 change: 0 additions & 1 deletion get/root.go

This file was deleted.

211 changes: 158 additions & 53 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,188 @@ package main

import (
"fmt"
"time"

//"time"

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

func main() {
vserver := pkg.NewServerService("access key", "secret key")
serverNo := test_list(0, vserver)
func getImageProductList(ncp *pkg.NcpService) {
ncp.ServerImageProduct = pkg.NewImageProductService()
imageProductList, err := ncp.ServerImageProduct.Get(pkg.API_URL + pkg.GET_SERVER_IMAGE_PRODUCT_LIST_PATH)
if err != nil {
fmt.Println(err)
return
}

stopReturn := test_stop(serverNo, vserver)
fmt.Println("Is stop successful: ", stopReturn)
fmt.Println(imageProductList)
}

func getProductList(ncp *pkg.NcpService) {
ncp.ServerProduct = pkg.NewProductService()

time.Sleep(25 * time.Second) //wait for stop to take place
gpr := &server.GetProductRequest{ServerImageProductCode: "SW.VSVR.OS.LNX64.CNTOS.0703.B050"}

productCode := "SVR.VSVR.HICPU.C002.M004.NET.SSD.B050.G002"
updateReturn := test_update(serverNo, productCode, vserver)
fmt.Println("Is update successful: ", updateReturn)
time.Sleep(20 * time.Second) //wait for update to take place
productList, err := ncp.ServerProduct.Get(pkg.API_URL+pkg.GET_SERVER_PRODUCT_LIST_PATH, gpr)
if err != nil {
fmt.Println(err)
return
}

deleteReturn := test_delete(serverNo, vserver)
fmt.Println("Is deletion(termination) successful: ", deleteReturn)
fmt.Println(productList)
}

func test_list(server_index int, vserver *pkg.ServerService) string { // server index for choosing which server to target
gsr := types.ListServerRequest{RegionCode: "KR"}
response, err := vserver.CallApi(pkg.API_URL+pkg.GET_SERVER_INSTANCE_PATH, gsr)
responseStruct := response.(*types.ListServerResponse)
serverNo := responseStruct.ServerInstanceList[server_index].ServerInstanceNo
fmt.Println("Error for listing:", err)
fmt.Println("responsestruct for listing: ", responseStruct)
fmt.Println("serverno: ", serverNo)
func getNetworkInterfaceList(ncp *pkg.NcpService) {
ncp.Network = pkg.NewNetworkInterfaceService()
networkInterfaceList, err := ncp.Network.Get(pkg.API_URL + pkg.GET_NETWORKINTERFACE_LIST_PATH)
if err != nil {
fmt.Println(err)
return
}

return serverNo
fmt.Println(networkInterfaceList)
}

func test_stop(serverNo string, vserver *pkg.ServerService) string { // server index for choosing which server to target
ssr := types.StopServerRequest{ServerNo: serverNo}
response, err := vserver.CallApi(pkg.API_URL+pkg.STOP_SERVER_INSTANCE_PATH, ssr)
responseStruct := response.(*types.StopServerResponse)
fmt.Println("Error for stopping: ", err)
fmt.Println("responsestruct for stopping:", responseStruct)
func getAccessControlGroupList(ncp *pkg.NcpService) {
ncp.AccessControlGroup = pkg.NewAccessControlGroupService()
accessControlGroupList, err := ncp.AccessControlGroup.Get(pkg.API_URL + pkg.GET_ACG_LIST_PATH)
if err != nil {
fmt.Println(err)
return
}

return responseStruct.ReturnMessage
fmt.Println(accessControlGroupList)
}

func test_delete(serverNo string, vserver *pkg.ServerService) string { // server index for choosing which server to target
dsr := types.DeleteServerRequest{ServerNo: serverNo}
response, err := vserver.CallApi(pkg.API_URL+pkg.DELETE_SERVER_INSTANCE_PATH, dsr)
responseStruct := response.(*types.DeleteServerResponse)
fmt.Println("Error for stopping: ", err)
fmt.Println("responsestruct for stopping:", responseStruct)
func getSubnetList(ncp *pkg.NcpService) {
ncp.Subnet = pkg.NewSubnetService()
subnetList, err := ncp.Subnet.Get(pkg.VPC_API_URL + pkg.GET_SUBNET_LIST_PATH)
if err != nil {
fmt.Println(err)
return
}

return responseStruct.ReturnMessage
fmt.Println(subnetList)
}

func test_update(serverNo, serverProductCode string, vserver *pkg.ServerService) string { // server index for choosing which server to target
dsr := types.UpdateServerRequest{ServerInstanceNo: serverNo, ServerProductCode: serverProductCode}
response, err := vserver.CallApi(pkg.API_URL+pkg.UPDATE_SERVER_INSTANCE_PATH, dsr)
responseStruct := response.(*types.UpdateServerResponse)
fmt.Println("Error for stopping: ", err)
fmt.Println("responsestruct for stopping:", responseStruct)
func getVpcList(ncp *pkg.NcpService) {
ncp.Vpc = pkg.NewVpcService()
vpcList, err := ncp.Vpc.Get(pkg.VPC_API_URL + pkg.GET_VPC_LIST)
if err != nil {
fmt.Println(err)
return
}

return responseStruct.ReturnMessage
fmt.Println(vpcList)
}

func test_product(serverImageProductCode string, vserver *pkg.ServerService) []string {
gpr := types.GetProductRequest{ServerImageProductCode: serverImageProductCode}
response, err := vserver.CallApi(pkg.API_URL+pkg.GET_PRODUCT_LIST_PATH, gpr)
responseStruct := response.(*types.GetProductResponse)
func create(ncp *pkg.NcpService) {
csr := &server.CreateServerRequest{ServerImageProductCode: "SW.VSVR.OS.LNX64.CNTOS.0703.B050", VpcNo: "52833", SubnetNo: "120320", NetworkInterfaceOrder: 0, AccessControlGroupNoListN: "148207", ServerProductCode: "SVR.VSVR.HICPU.C002.M004.NET.HDD.B050.G002"}

productCodeList := []string{}
for _, product := range responseStruct.ProductList {
productCodeList = append(productCodeList, product.ProductCode)
response, err := ncp.Server.Create(pkg.API_URL+pkg.CREATE_SERVER_INSTANCE_PATH, csr, []int{1, 1})
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Error for getting product list: ", err)
//Print response
fmt.Println(response)
}

func stop(ncp *pkg.NcpService) {
ssr := &server.StopServerRequest{ServerNo: "21741451"}

stopServerResponse, err := ncp.Server.Stop(pkg.API_URL+pkg.STOP_SERVER_INSTANCE_PATH, ssr)
if err != nil {
fmt.Println(err)
return
}

//Print response
fmt.Println(stopServerResponse)
}

func list(ncp *pkg.NcpService) {
lsr := &server.ListServerRequest{RegionCode: "KR"}

serverListResponse, err := ncp.Server.List(pkg.API_URL+pkg.GET_SERVER_INSTANCE_PATH, lsr)
if err != nil {
fmt.Println(err)
return
}

//Print response
fmt.Println(serverListResponse)
}

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

updateServerResponse, err := ncp.Server.Update(pkg.API_URL+pkg.UPDATE_SERVER_INSTANCE_PATH, usr)
if err != nil {
fmt.Println(err)
return
}

//Print response
fmt.Println(updateServerResponse)
}

func delete(ncp *pkg.NcpService) {
dsr := &server.DeleteServerRequest{ServerNo: "21741451"}

deleteServerResponse, err := ncp.Server.Delete(pkg.API_URL+pkg.DELETE_SERVER_INSTANCE_PATH, dsr)
if err != nil {
fmt.Println(err)
return
}

//Print response
fmt.Println(deleteServerResponse)
}

func main() {
ncp := pkg.NewNcpService("ncp service token")
ncp.Server = pkg.NewServerService("6CmrDJ4KaswJ10g25GEP", "OvZ7QHH0Bi3AwGn5rlsD7xoC986bEOiIjdbwMFCo")

// Check image product list
//getImageProductList(ncp)

// Check product list
//getProductList(ncp)

// Get network interface list
//getNetworkInterfaceList(ncp)

// Get access control group list
//getAccessControlGroupList(ncp)

// Get subnet list
//getSubnetList(ncp)

// Get VPC list
//getVpcList(ncp)

list(ncp)

// Create server instance
create(ncp)

//6. Get server instance list
list(ncp)

// 7. Stop server instance
//stop(ncp)
// list(ncp)
// time.Sleep(20 * time.Second)

// // Update server instance
// update(ncp)
// time.Sleep(20 * time.Second)
// list(ncp)

// // Terminate server instance
// delete(ncp)

return productCodeList
}
17 changes: 9 additions & 8 deletions pkg/constant.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package pkg

var (
API_URL = "https://ncloud.apigw.ntruss.com/vserver/v2/"
GET_SERVER_INSTANCE_PATH = "getServerInstanceList"
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"
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"

GET_NETWORKINTERFACE_LIST_PATH = "getNetworkInterfaceList"
GET_ACG_LIST_PATH = "getAccessControlGroupList"

GET_SERVER_IMAGE_PRODUCT_LIST_PATH = "getServerImageProductList"
GET_SERVER_PRODUCT_LIST_PATH = "getServerProductList"

VPC_API_URL = "https://ncloud.apigw.ntruss.com/vpc/v2/"
GET_VPC_LIST = "getVpcList"
Expand Down
3 changes: 2 additions & 1 deletion pkg/ncp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type NcpService struct {
Subnet SubnetInterface
AccessControlGroup AccessControlGroupInterface
Vpc VpcInterface
ServerImageProduct ProductInterface
ServerImageProduct ImageProductInterface
ServerProduct ProductInterface
}

func NewNcpService(token string) *NcpService {
Expand Down
12 changes: 6 additions & 6 deletions pkg/serverimageproduct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
serverType "github.com/cloud-club/Aviator-service/types/server"
)

type ProductService struct{}
type ImageProductService struct{}

func NewProductService() ProductInterface {
return &ProductService{}
func NewImageProductService() ImageProductInterface {
return &ImageProductService{}
}

type ProductInterface interface {
type ImageProductInterface interface {
Get(url string) (*serverType.ProductList, error)
}

func (product *ProductService) Get(url string) (*serverType.ProductList, error) {
func (product *ImageProductService) Get(url string) (*serverType.ProductList, error) {
// Set url with query parameters
// However, there is no must required query parameters for this API, so we will comment this line right now.
//requestParams := serverType.CreateRequestString(request)
Expand All @@ -31,7 +31,7 @@ func (product *ProductService) Get(url string) (*serverType.ProductList, error)
}

// Set HTTP header for NCP authorization
SetNCPHeader(req, "YnCbljlTZfqRkFOXighj", "bDoQVXDGLJ9BhzpOYxnjSyxNB97dohAUPLeiQC0D")
SetNCPHeader(req, "6CmrDJ4KaswJ10g25GEP", "OvZ7QHH0Bi3AwGn5rlsD7xoC986bEOiIjdbwMFCo")

// Make the HTTP request
resp, err := http.DefaultClient.Do(req)
Expand Down
66 changes: 66 additions & 0 deletions pkg/serverproduct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package pkg

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

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

type ProductService struct{}

func NewProductService() ProductInterface {
return &ProductService{}
}

type ProductInterface interface {
Get(url string, request *serverType.GetProductRequest) (*serverType.ProductList, error)
}

func (product *ProductService) Get(url string, request *serverType.GetProductRequest) (*serverType.ProductList, error) {
// Set url with query parameters
// However, there is no must required query parameters for this API, so we will comment this line right now.
//requestParams := serverType.CreateRequestString(request)
requestParams := serverType.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, "6CmrDJ4KaswJ10g25GEP", "OvZ7QHH0Bi3AwGn5rlsD7xoC986bEOiIjdbwMFCo")

// 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 {
return nil, fmt.Errorf("unexpected response status: %s", resp.Status)
}

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

// Convert the response body to the struct
var sl *serverType.ProductList
responseInterface, err := serverType.MapResponse(responseByteData, &sl)
if err != nil {
return nil, err
}

// interface{} to *serverType.SubnetList
responseStruct := responseInterface.(**serverType.ProductList)

return *responseStruct, nil
}
1 change: 0 additions & 1 deletion pkg/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (vpc *VpcService) Get(url string) (*serverType.VpcList, error) {

// Read the response body
responseByteData, err := io.ReadAll(resp.Body)
println(string(responseByteData))
if err != nil {
log.Fatal(err)
return nil, err
Expand Down
File renamed without changes.
Loading

0 comments on commit 3552831

Please sign in to comment.