@@ -18,7 +18,7 @@ type serverProductListResponse struct {
1818}
1919
2020func (r * serverProductListResponse ) apiError () error {
21- return r .Response .responseMeta . apiError ()
21+ return r .Response .apiError ()
2222}
2323
2424type productList struct {
@@ -47,58 +47,86 @@ func (c *NaverClient) GetInstanceTypePollTime() time.Duration {
4747}
4848
4949func (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+
102130func (p naverProduct ) toInstanceType (location string ) cloud.InstanceType {
103131 storage := cloud.Storage {
104132 Type : firstNonEmpty (p .DiskType .Code , "NET" ),
0 commit comments