Skip to content

Commit b12b4d3

Browse files
authored
Make country optional for ISP and DC proxy types (#32)
1 parent c3ec0bb commit b12b4d3

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

cmd/proxies/create.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,21 @@ func (p ProxyCmd) Create(ctx context.Context, in ProxyCreateInput) error {
4040
// Build config based on type
4141
switch proxyType {
4242
case kernel.ProxyNewParamsTypeDatacenter:
43-
if in.Country == "" {
44-
return fmt.Errorf("--country is required for datacenter proxy type")
43+
config := kernel.ProxyNewParamsConfigDatacenterProxyConfig{}
44+
if in.Country != "" {
45+
config.Country = kernel.Opt(in.Country)
4546
}
4647
params.Config = kernel.ProxyNewParamsConfigUnion{
47-
OfProxyNewsConfigDatacenterProxyConfig: &kernel.ProxyNewParamsConfigDatacenterProxyConfig{
48-
Country: in.Country,
49-
},
48+
OfProxyNewsConfigDatacenterProxyConfig: &config,
5049
}
5150

5251
case kernel.ProxyNewParamsTypeIsp:
53-
if in.Country == "" {
54-
return fmt.Errorf("--country is required for ISP proxy type")
52+
config := kernel.ProxyNewParamsConfigIspProxyConfig{}
53+
if in.Country != "" {
54+
config.Country = kernel.Opt(in.Country)
5555
}
5656
params.Config = kernel.ProxyNewParamsConfigUnion{
57-
OfProxyNewsConfigIspProxyConfig: &kernel.ProxyNewParamsConfigIspProxyConfig{
58-
Country: in.Country,
59-
},
57+
OfProxyNewsConfigIspProxyConfig: &config,
6058
}
6159

6260
case kernel.ProxyNewParamsTypeResidential:

cmd/proxies/create_test.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestProxyCreate_Datacenter_Success(t *testing.T) {
2222
// Check config
2323
dcConfig := body.Config.OfProxyNewsConfigDatacenterProxyConfig
2424
assert.NotNil(t, dcConfig)
25-
assert.Equal(t, "US", dcConfig.Country)
25+
assert.Equal(t, "US", dcConfig.Country.Value)
2626

2727
return &kernel.ProxyNewResponse{
2828
ID: "dc-new",
@@ -48,19 +48,38 @@ func TestProxyCreate_Datacenter_Success(t *testing.T) {
4848
assert.Contains(t, output, "My DC Proxy")
4949
}
5050

51-
func TestProxyCreate_Datacenter_MissingCountry(t *testing.T) {
52-
_ = captureOutput(t)
53-
fake := &FakeProxyService{}
51+
func TestProxyCreate_Datacenter_WithoutCountry(t *testing.T) {
52+
buf := captureOutput(t)
53+
54+
fake := &FakeProxyService{
55+
NewFunc: func(ctx context.Context, body kernel.ProxyNewParams, opts ...option.RequestOption) (*kernel.ProxyNewResponse, error) {
56+
// Verify the request
57+
assert.Equal(t, kernel.ProxyNewParamsTypeDatacenter, body.Type)
58+
assert.Equal(t, "My DC Proxy", body.Name.Value)
59+
60+
// Check config - country should not be set (it should be zero/nil)
61+
dcConfig := body.Config.OfProxyNewsConfigDatacenterProxyConfig
62+
assert.NotNil(t, dcConfig)
63+
64+
return &kernel.ProxyNewResponse{
65+
ID: "dc-new",
66+
Name: "My DC Proxy",
67+
Type: kernel.ProxyNewResponseTypeDatacenter,
68+
}, nil
69+
},
70+
}
5471

5572
p := ProxyCmd{proxies: fake}
5673
err := p.Create(context.Background(), ProxyCreateInput{
5774
Name: "My DC Proxy",
5875
Type: "datacenter",
59-
// Missing required Country
76+
// Country is now optional
6077
})
6178

62-
assert.Error(t, err)
63-
assert.Contains(t, err.Error(), "--country is required for datacenter proxy type")
79+
assert.NoError(t, err)
80+
output := buf.String()
81+
assert.Contains(t, output, "Creating datacenter proxy")
82+
assert.Contains(t, output, "Successfully created proxy")
6483
}
6584

6685
func TestProxyCreate_Residential_Success(t *testing.T) {
@@ -315,7 +334,7 @@ func TestProxyCreate_ISP_Success(t *testing.T) {
315334
// Verify ISP config
316335
ispConfig := body.Config.OfProxyNewsConfigIspProxyConfig
317336
assert.NotNil(t, ispConfig)
318-
assert.Equal(t, "EU", ispConfig.Country)
337+
assert.Equal(t, "EU", ispConfig.Country.Value)
319338

320339
return &kernel.ProxyNewResponse{
321340
ID: "isp-new",
@@ -337,3 +356,33 @@ func TestProxyCreate_ISP_Success(t *testing.T) {
337356
assert.Contains(t, output, "Creating isp proxy")
338357
assert.Contains(t, output, "Successfully created proxy")
339358
}
359+
360+
func TestProxyCreate_ISP_WithoutCountry(t *testing.T) {
361+
buf := captureOutput(t)
362+
363+
fake := &FakeProxyService{
364+
NewFunc: func(ctx context.Context, body kernel.ProxyNewParams, opts ...option.RequestOption) (*kernel.ProxyNewResponse, error) {
365+
// Verify ISP config
366+
ispConfig := body.Config.OfProxyNewsConfigIspProxyConfig
367+
assert.NotNil(t, ispConfig)
368+
369+
return &kernel.ProxyNewResponse{
370+
ID: "isp-new",
371+
Name: "ISP Proxy",
372+
Type: kernel.ProxyNewResponseTypeIsp,
373+
}, nil
374+
},
375+
}
376+
377+
p := ProxyCmd{proxies: fake}
378+
err := p.Create(context.Background(), ProxyCreateInput{
379+
Name: "ISP Proxy",
380+
Type: "isp",
381+
// Country is now optional
382+
})
383+
384+
assert.NoError(t, err)
385+
output := buf.String()
386+
assert.Contains(t, output, "Creating isp proxy")
387+
assert.Contains(t, output, "Successfully created proxy")
388+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/charmbracelet/fang v0.2.0
99
github.com/golang-jwt/jwt/v5 v5.2.2
1010
github.com/joho/godotenv v1.5.1
11-
github.com/onkernel/kernel-go-sdk v0.15.0
11+
github.com/onkernel/kernel-go-sdk v0.17.0
1212
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
1313
github.com/pterm/pterm v0.12.80
1414
github.com/samber/lo v1.51.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ github.com/muesli/mango-pflag v0.1.0 h1:UADqbYgpUyRoBja3g6LUL+3LErjpsOwaC9ywvBWe
9191
github.com/muesli/mango-pflag v0.1.0/go.mod h1:YEQomTxaCUp8PrbhFh10UfbhbQrM/xJ4i2PB8VTLLW0=
9292
github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8=
9393
github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig=
94-
github.com/onkernel/kernel-go-sdk v0.15.0 h1:gLeZixS9bhOy1WuEk/eFogHUBjG6UJKN2l+OJNNdE+4=
95-
github.com/onkernel/kernel-go-sdk v0.15.0/go.mod h1:MjUR92i8UPqjrmneyVykae6GuB3GGSmnQtnjf1v74Dc=
94+
github.com/onkernel/kernel-go-sdk v0.17.0 h1:3q7hrfiLTJbUwcJTtPhdnNIXUI4/TUnoklPjUGBoeas=
95+
github.com/onkernel/kernel-go-sdk v0.17.0/go.mod h1:MjUR92i8UPqjrmneyVykae6GuB3GGSmnQtnjf1v74Dc=
9696
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
9797
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
9898
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

0 commit comments

Comments
 (0)