Skip to content

Commit 8e52304

Browse files
committed
fix(naver): satisfy lint checks
1 parent 313966b commit 8e52304

6 files changed

Lines changed: 124 additions & 65 deletions

File tree

v1/providers/naver/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
const (
2020
CloudProviderID = "naver"
21+
architectureX8664 = "x86_64"
2122
defaultBaseURL = "https://ncloud.apigw.ntruss.com"
2223
defaultRegionCode = "KR"
2324
defaultServerImageProductCode = "SW.VSVR.OS.LNX64.UBNTU.SVR24.G003"

v1/providers/naver/image.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ type imageProductListResponse struct {
1515
}
1616

1717
func (r *imageProductListResponse) apiError() error {
18-
return r.Response.responseMeta.apiError()
18+
return r.Response.apiError()
1919
}
2020

2121
func (c *NaverClient) GetImages(ctx context.Context, args cloud.GetImageArgs) ([]cloud.Image, error) {
2222
params := url.Values{}
2323
params.Set("regionCode", c.location)
2424
if len(args.Architectures) > 0 {
2525
for i, arch := range args.Architectures {
26-
if arch == "x86_64" {
26+
if arch == architectureX8664 {
2727
params.Set(indexedParam("platformTypeCodeList", i+1), "LNX64")
2828
}
2929
}
@@ -59,7 +59,7 @@ func (c *NaverClient) GetImages(ctx context.Context, args cloud.GetImageArgs) ([
5959

6060
func architectureFromPlatform(platform string) string {
6161
if strings.Contains(platform, "64") {
62-
return "x86_64"
62+
return architectureX8664
6363
}
6464
if strings.Contains(platform, "32") {
6565
return "i386"

v1/providers/naver/image_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestGetImagesConvertsAndFiltersImageProducts(t *testing.T) {
2424

2525
client := newTestNaverClient(t, server.URL)
2626
images, err := client.GetImages(context.Background(), cloud.GetImageArgs{
27-
Architectures: []string{"x86_64"},
27+
Architectures: []string{architectureX8664},
2828
ImageIDs: []string{"UBUNTU24"},
2929
})
3030
if err != nil {
@@ -33,7 +33,7 @@ func TestGetImagesConvertsAndFiltersImageProducts(t *testing.T) {
3333
if len(images) != 1 {
3434
t.Fatalf("images len = %d, want 1", len(images))
3535
}
36-
if images[0].ID != "UBUNTU24" || images[0].Architecture != "x86_64" || images[0].Name != "ubuntu-24.04" {
36+
if images[0].ID != "UBUNTU24" || images[0].Architecture != architectureX8664 || images[0].Name != "ubuntu-24.04" {
3737
t.Fatalf("unexpected image: %+v", images[0])
3838
}
3939
}

v1/providers/naver/instance.go

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type importLoginKeyResponse struct {
8080
}
8181

8282
func (r *importLoginKeyResponse) apiError() error {
83-
return r.Response.responseMeta.apiError()
83+
return r.Response.apiError()
8484
}
8585

8686
type loginKeyList struct {
@@ -95,40 +95,69 @@ type naverLoginKey struct {
9595
}
9696

9797
func (c *NaverClient) CreateInstance(ctx context.Context, attrs cloud.CreateInstanceAttrs) (*cloud.Instance, error) {
98+
if err := validateCreateInstanceAttrs(attrs); err != nil {
99+
return nil, err
100+
}
101+
102+
location := firstNonEmpty(attrs.Location, c.location, defaultRegionCode)
103+
keyName, err := c.createInstanceLoginKey(ctx, attrs, location)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
params := createInstanceParams(attrs, location, keyName, c.refID)
109+
var resp serverInstanceListResponse
110+
if err := c.do(ctx, "createServerInstances", params, &resp); err != nil {
111+
return nil, err
112+
}
113+
114+
inst, err := c.createdInstance(resp.Create.ServerInstanceList, attrs)
115+
if err != nil {
116+
return nil, err
117+
}
118+
return inst, nil
119+
}
120+
121+
func validateCreateInstanceAttrs(attrs cloud.CreateInstanceAttrs) error {
98122
if attrs.VPCID == "" {
99-
return nil, fmt.Errorf("VPCID is required for NAVER VPC server creation")
123+
return fmt.Errorf("VPCID is required for NAVER VPC server creation")
100124
}
101125
if attrs.SubnetID == "" {
102-
return nil, fmt.Errorf("SubnetID is required for NAVER VPC server creation")
126+
return fmt.Errorf("SubnetID is required for NAVER VPC server creation")
103127
}
104128
if attrs.InstanceType == "" {
105-
return nil, fmt.Errorf("InstanceType is required")
129+
return fmt.Errorf("InstanceType is required")
106130
}
107131
if attrs.ImageID == "" {
108-
return nil, fmt.Errorf("ImageID is required")
132+
return fmt.Errorf("ImageID is required")
109133
}
134+
return nil
135+
}
110136

111-
location := firstNonEmpty(attrs.Location, c.location, defaultRegionCode)
137+
func (c *NaverClient) createInstanceLoginKey(ctx context.Context, attrs cloud.CreateInstanceAttrs, location string) (string, error) {
112138
keyName := ""
113139
if attrs.KeyPairName != nil {
114140
keyName = *attrs.KeyPairName
115141
}
116142
if attrs.PublicKey != "" {
117143
imported, err := c.importLoginKey(ctx, location, naverResourceName("brev-"+attrs.RefID), attrs.PublicKey)
118144
if err != nil {
119-
return nil, err
145+
return "", err
120146
}
121147
keyName = imported
122148
}
123149
if keyName == "" {
124-
return nil, fmt.Errorf("KeyPairName or PublicKey is required")
150+
return "", fmt.Errorf("KeyPairName or PublicKey is required")
125151
}
152+
return keyName, nil
153+
}
126154

155+
func createInstanceParams(attrs cloud.CreateInstanceAttrs, location, keyName, refID string) url.Values {
127156
params := url.Values{}
128157
params.Set("regionCode", location)
129158
params.Set("vpcNo", attrs.VPCID)
130159
params.Set("subnetNo", attrs.SubnetID)
131-
params.Set("serverName", naverResourceName(firstNonEmpty(attrs.Name, attrs.RefID, c.refID)))
160+
params.Set("serverName", naverResourceName(firstNonEmpty(attrs.Name, attrs.RefID, refID)))
132161
params.Set("serverCreateCount", "1")
133162
params.Set("networkInterfaceList.1.networkInterfaceOrder", "0")
134163
params.Set("networkInterfaceList.1.subnetNo", attrs.SubnetID)
@@ -139,15 +168,14 @@ func (c *NaverClient) CreateInstance(ctx context.Context, attrs cloud.CreateInst
139168
if attrs.UserDataBase64 != "" {
140169
params.Set("userData", attrs.UserDataBase64)
141170
}
171+
return params
172+
}
142173

143-
var resp serverInstanceListResponse
144-
if err := c.do(ctx, "createServerInstances", params, &resp); err != nil {
145-
return nil, err
146-
}
147-
if len(resp.Create.ServerInstanceList) != 1 {
148-
return nil, fmt.Errorf("expected 1 server instance, got %d", len(resp.Create.ServerInstanceList))
174+
func (c *NaverClient) createdInstance(servers []naverServerInstance, attrs cloud.CreateInstanceAttrs) (*cloud.Instance, error) {
175+
if len(servers) != 1 {
176+
return nil, fmt.Errorf("expected 1 server instance, got %d", len(servers))
149177
}
150-
inst := c.convertServerInstance(resp.Create.ServerInstanceList[0])
178+
inst := c.convertServerInstance(servers[0])
151179
inst.RefID = attrs.RefID
152180
inst.CloudCredRefID = c.refID
153181
inst.Tags = attrs.Tags

v1/providers/naver/instancetype.go

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type serverProductListResponse struct {
1818
}
1919

2020
func (r *serverProductListResponse) apiError() error {
21-
return r.Response.responseMeta.apiError()
21+
return r.Response.apiError()
2222
}
2323

2424
type productList struct {
@@ -47,58 +47,86 @@ func (c *NaverClient) GetInstanceTypePollTime() time.Duration {
4747
}
4848

4949
func (c *NaverClient) GetInstanceTypes(ctx context.Context, args cloud.GetInstanceTypeArgs) ([]cloud.InstanceType, error) {
50-
locations := args.Locations
51-
if len(locations) == 0 {
52-
locations = cloud.LocationsFilter{c.location}
53-
}
54-
if locations.IsAll() {
55-
locs, err := c.GetLocations(ctx, cloud.GetLocationsArgs{})
56-
if err != nil {
57-
return nil, err
58-
}
59-
locations = make(cloud.LocationsFilter, 0, len(locs))
60-
for _, loc := range locs {
61-
locations = append(locations, loc.Name)
62-
}
50+
locations, err := c.instanceTypeLocations(ctx, args.Locations)
51+
if err != nil {
52+
return nil, err
6353
}
6454

6555
var out []cloud.InstanceType
6656
for _, location := range locations {
67-
params := url.Values{}
68-
params.Set("regionCode", location)
69-
params.Set("serverImageProductCode", defaultServerImageProductCode)
70-
var resp serverProductListResponse
71-
if err := c.do(ctx, "getServerProductList", params, &resp); err != nil {
57+
instanceTypes, err := c.instanceTypesForLocation(ctx, location, args)
58+
if err != nil {
7259
return nil, err
7360
}
74-
for _, product := range resp.Response.ProductList {
75-
it := product.toInstanceType(location)
76-
if len(args.InstanceTypes) > 0 && !slices.Contains(args.InstanceTypes, it.Type) {
77-
continue
78-
}
79-
if args.CloudFilter != nil && !args.CloudFilter.IsAllowed(it.Cloud) {
80-
continue
81-
}
82-
if args.ArchitectureFilter != nil && !args.ArchitectureFilter.IsAllowed(cloud.ArchitectureX86_64) {
83-
continue
84-
}
85-
if args.GPUManufactererFilter != nil {
86-
allowed := len(it.SupportedGPUs) == 0
87-
for _, gpu := range it.SupportedGPUs {
88-
if args.GPUManufactererFilter.IsAllowed(gpu.Manufacturer) {
89-
allowed = true
90-
}
91-
}
92-
if !allowed {
93-
continue
94-
}
95-
}
61+
out = append(out, instanceTypes...)
62+
}
63+
return out, nil
64+
}
65+
66+
func (c *NaverClient) instanceTypeLocations(ctx context.Context, locations cloud.LocationsFilter) (cloud.LocationsFilter, error) {
67+
if len(locations) == 0 {
68+
return cloud.LocationsFilter{c.location}, nil
69+
}
70+
if !locations.IsAll() {
71+
return locations, nil
72+
}
73+
74+
locs, err := c.GetLocations(ctx, cloud.GetLocationsArgs{})
75+
if err != nil {
76+
return nil, err
77+
}
78+
resolved := make(cloud.LocationsFilter, 0, len(locs))
79+
for _, loc := range locs {
80+
resolved = append(resolved, loc.Name)
81+
}
82+
return resolved, nil
83+
}
84+
85+
func (c *NaverClient) instanceTypesForLocation(ctx context.Context, location string, args cloud.GetInstanceTypeArgs) ([]cloud.InstanceType, error) {
86+
params := url.Values{}
87+
params.Set("regionCode", location)
88+
params.Set("serverImageProductCode", defaultServerImageProductCode)
89+
90+
var resp serverProductListResponse
91+
if err := c.do(ctx, "getServerProductList", params, &resp); err != nil {
92+
return nil, err
93+
}
94+
95+
out := make([]cloud.InstanceType, 0, len(resp.Response.ProductList))
96+
for _, product := range resp.Response.ProductList {
97+
it := product.toInstanceType(location)
98+
if includeInstanceType(it, args) {
9699
out = append(out, it)
97100
}
98101
}
99102
return out, nil
100103
}
101104

105+
func includeInstanceType(it cloud.InstanceType, args cloud.GetInstanceTypeArgs) bool {
106+
if len(args.InstanceTypes) > 0 && !slices.Contains(args.InstanceTypes, it.Type) {
107+
return false
108+
}
109+
if args.CloudFilter != nil && !args.CloudFilter.IsAllowed(it.Cloud) {
110+
return false
111+
}
112+
if args.ArchitectureFilter != nil && !args.ArchitectureFilter.IsAllowed(cloud.ArchitectureX86_64) {
113+
return false
114+
}
115+
return allowsGPUManufacturer(it.SupportedGPUs, args.GPUManufactererFilter)
116+
}
117+
118+
func allowsGPUManufacturer(gpus []cloud.GPU, filter *cloud.GPUManufacturerFilter) bool {
119+
if filter == nil || len(gpus) == 0 {
120+
return true
121+
}
122+
for _, gpu := range gpus {
123+
if filter.IsAllowed(gpu.Manufacturer) {
124+
return true
125+
}
126+
}
127+
return false
128+
}
129+
102130
func (p naverProduct) toInstanceType(location string) cloud.InstanceType {
103131
storage := cloud.Storage{
104132
Type: firstNonEmpty(p.DiskType.Code, "NET"),

v1/providers/naver/location.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type regionListResponse struct {
1111
}
1212

1313
func (r *regionListResponse) apiError() error {
14-
return r.Response.responseMeta.apiError()
14+
return r.Response.apiError()
1515
}
1616

1717
type regionList struct {
@@ -26,6 +26,8 @@ type naverRegion struct {
2626
RegionName string `json:"regionName"`
2727
}
2828

29+
const countryJPN = "JPN"
30+
2931
func (c *NaverClient) GetLocations(ctx context.Context, _ cloud.GetLocationsArgs) ([]cloud.Location, error) {
3032
var resp regionListResponse
3133
if err := c.do(ctx, "getRegionList", nil, &resp); err != nil {
@@ -49,8 +51,8 @@ func countryForRegion(regionCode string) string {
4951
switch regionCode {
5052
case "KR":
5153
return "KOR"
52-
case "JPN":
53-
return "JPN"
54+
case countryJPN:
55+
return countryJPN
5456
case "SGN":
5557
return "SGP"
5658
case "USWN", "USEN":

0 commit comments

Comments
 (0)