Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ user := cloudconnexa.User{
createdUser, err := client.Users.Create(user)
```

### Listing VPN Regions

```go
// List all VPN regions
regions, err := client.VPNRegions.List()
if err != nil {
log.Fatalf("error getting VPN regions: %v", err)
}

// Get specific region by ID
region, err := client.VPNRegions.GetByID("region-id")
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
23 changes: 22 additions & 1 deletion cloudconnexa/vpn_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,28 @@ type VpnRegion struct {

type VPNRegionsService service

func (c *VPNRegionsService) GetVpnRegion(regionID string) (*VpnRegion, error) {
// List retrieves all VPN regions
func (c *VPNRegionsService) List() ([]VpnRegion, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/regions", c.client.GetV1Url()), nil)
if err != nil {
return nil, err
}

body, err := c.client.DoRequest(req)
if err != nil {
return nil, err
}

var regions []VpnRegion
err = json.Unmarshal(body, &regions)
if err != nil {
return nil, err
}
return regions, nil
}

// GetByID retrieves a specific VPN region by ID
func (c *VPNRegionsService) GetByID(regionID string) (*VpnRegion, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/regions", c.client.GetV1Url()), nil)
if err != nil {
return nil, err
Expand Down
84 changes: 84 additions & 0 deletions cloudconnexa/vpn_regions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cloudconnexa

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

var testVpnRegion = VpnRegion{
ID: "test-region",
Country: "Test Country",
Continent: "Test Continent",
CountryISO: "TC",
RegionName: "Test Region",
}

func TestVPNRegionsService_List(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Handle auth token request
if r.URL.Path == "/api/v1/oauth/token" {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(map[string]string{
"access_token": "test-token",
})
assert.NoError(t, err)
return
}

// Handle VPN regions request
assert.Equal(t, "/api/v1/regions", r.URL.Path)

regions := []VpnRegion{testVpnRegion}

err := json.NewEncoder(w).Encode(regions)
assert.NoError(t, err)
}))
defer server.Close()

client, err := NewClient(server.URL, "test", "test")
assert.NoError(t, err)
regions, err := client.VPNRegions.List()

assert.NoError(t, err)
assert.Equal(t, 1, len(regions))
assert.Equal(t, testVpnRegion.ID, regions[0].ID)
}

func TestVPNRegionsService_GetByID(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Handle auth token request
if r.URL.Path == "/api/v1/oauth/token" {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(map[string]string{
"access_token": "test-token",
})
assert.NoError(t, err)
return
}

// Handle VPN regions request
assert.Equal(t, "/api/v1/regions", r.URL.Path)

err := json.NewEncoder(w).Encode([]VpnRegion{testVpnRegion})
assert.NoError(t, err)
}))
defer server.Close()

client, err := NewClient(server.URL, "test", "test")
assert.NoError(t, err)

// Test existing region
region, err := client.VPNRegions.GetByID(testVpnRegion.ID)
assert.NoError(t, err)
assert.NotNil(t, region)
assert.Equal(t, testVpnRegion.ID, region.ID)

// Test non-existent region
region, err = client.VPNRegions.GetByID("non-existent")
assert.NoError(t, err)
assert.Nil(t, region)
}
28 changes: 28 additions & 0 deletions e2e/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ func TestListConnectors(t *testing.T) {
fmt.Printf("found %d connectors\n", len(response.Content))
}

func TestVPNRegions(t *testing.T) {
c := setUpClient(t)

// Test List
regions, err := c.VPNRegions.List()
require.NoError(t, err)
require.NotNil(t, regions)
fmt.Printf("found %d VPN regions\n", len(regions))

// If regions exist, test GetByID
if len(regions) > 0 {
region := regions[0]
foundRegion, err := c.VPNRegions.GetByID(region.ID)
require.NoError(t, err)
require.NotNil(t, foundRegion)
require.Equal(t, region.ID, foundRegion.ID)
require.Equal(t, region.Country, foundRegion.Country)
require.Equal(t, region.Continent, foundRegion.Continent)
fmt.Printf("successfully found region %s in %s, %s\n",
foundRegion.ID, foundRegion.Country, foundRegion.Continent)
}

// Test GetByID with non-existent ID
nonExistentRegion, err := c.VPNRegions.GetByID("non-existent-id")
require.NoError(t, err)
require.Nil(t, nonExistentRegion)
}

func TestCreateNetwork(t *testing.T) {
c := setUpClient(t)
timestamp := time.Now().Unix()
Expand Down