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
11 changes: 8 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type Client struct {
apiKey string
baseURL string
httpClient *http.Client
version string
}

func New(apiKey string) *Client {
func New(apiKey, version string) *Client {
baseURL := "https://customer.cloudamqp.com/api"
if envURL := os.Getenv("CLOUDAMQP_URL"); envURL != "" {
baseURL = envURL
Expand All @@ -28,24 +29,27 @@ func New(apiKey string) *Client {
apiKey: apiKey,
baseURL: baseURL,
httpClient: &http.Client{},
version: version,
}
}

func NewWithBaseURL(apiKey, baseURL string) *Client {
func NewWithBaseURL(apiKey, baseURL, version string) *Client {
return &Client{
apiKey: apiKey,
baseURL: baseURL,
httpClient: &http.Client{},
version: version,
}
}

// NewWithHTTPClient creates a new client with a custom HTTP client.
// This is useful for testing with tools like go-vcr.
func NewWithHTTPClient(apiKey, baseURL string, httpClient *http.Client) *Client {
func NewWithHTTPClient(apiKey, baseURL, version string, httpClient *http.Client) *Client {
return &Client{
apiKey: apiKey,
baseURL: baseURL,
httpClient: httpClient,
version: version,
}
}

Expand Down Expand Up @@ -77,6 +81,7 @@ func (c *Client) makeRequest(method, endpoint string, body any) ([]byte, error)
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
req.Header.Set("User-Agent", fmt.Sprintf("cloudamqp-cli/%s", c.version))

resp, err := c.httpClient.Do(req)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestNew(t *testing.T) {
apiKey := "test-api-key"
client := New(apiKey)
client := New(apiKey, "test")

assert.NotNil(t, client)
assert.Equal(t, apiKey, client.apiKey)
Expand All @@ -29,7 +29,7 @@ func TestNew_WithEnvironmentVariable(t *testing.T) {
os.Setenv("CLOUDAMQP_URL", customURL)

apiKey := "test-api-key"
client := New(apiKey)
client := New(apiKey, "test")

assert.NotNil(t, client)
assert.Equal(t, apiKey, client.apiKey)
Expand All @@ -38,7 +38,7 @@ func TestNew_WithEnvironmentVariable(t *testing.T) {

// Test with empty environment variable (should use default)
os.Setenv("CLOUDAMQP_URL", "")
client = New(apiKey)
client = New(apiKey, "test")

assert.NotNil(t, client)
assert.Equal(t, "https://customer.cloudamqp.com/api", client.baseURL)
Expand All @@ -64,7 +64,7 @@ func TestMakeRequest_GET_Success(t *testing.T) {
defer server.Close()

// Create client with test server URL
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

// Test request
resp, err := client.makeRequest("GET", "/test", nil)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestMakeRequest_POST_FormData(t *testing.T) {
defer server.Close()

// Create client with test server URL
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

// Test request with form data
formData := url.Values{}
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestMakeRequest_POST_JSON(t *testing.T) {
defer server.Close()

// Create client with test server URL
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

// Test request with JSON data
jsonData := map[string]string{"test": "value"}
Expand All @@ -143,7 +143,7 @@ func TestMakeRequest_APIError_JSON(t *testing.T) {
defer server.Close()

// Create client with test server URL
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

// Test request
_, err := client.makeRequest("GET", "/test", nil)
Expand All @@ -161,7 +161,7 @@ func TestMakeRequest_APIError_Plain(t *testing.T) {
defer server.Close()

// Create client with test server URL
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

// Test request
_, err := client.makeRequest("GET", "/test", nil)
Expand All @@ -172,7 +172,7 @@ func TestMakeRequest_APIError_Plain(t *testing.T) {

func TestMakeRequest_NetworkError(t *testing.T) {
// Create client with invalid URL
client := NewWithBaseURL("test-api-key", "http://invalid-url-that-does-not-exist")
client := NewWithBaseURL("test-api-key", "http://invalid-url-that-does-not-exist", "test")

// Test request
_, err := client.makeRequest("GET", "/test", nil)
Expand All @@ -182,7 +182,7 @@ func TestMakeRequest_NetworkError(t *testing.T) {
}

func TestMakeRequest_InvalidJSON(t *testing.T) {
client := New("test-api-key")
client := New("test-api-key", "test")

// Test with invalid JSON data
invalidData := make(chan int) // channels can't be marshaled to JSON
Expand Down
8 changes: 4 additions & 4 deletions client/common_operations_vcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestListInstancesVCR(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

instances, err := client.ListInstances()

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestGetInstanceVCR(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

// Use an existing instance ID (should match cassette)
instanceID := 359563
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestListRegionsVCR(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

regions, err := client.ListRegions("")

Expand Down Expand Up @@ -135,7 +135,7 @@ func TestListPlansVCR(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

plans, err := client.ListPlans("")

Expand Down
6 changes: 3 additions & 3 deletions client/instances_bunny_vcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCreateInstanceBunny1(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

// Create instance with bunny-1 plan
req := &InstanceCreateRequest{
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestUpdateInstanceBunny1ToHare1(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

// Use a real instance ID from a bunny-1 instance
// This should be an instance that is already created and fully configured
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestDeleteInstanceBunny1(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

// Use an instance ID that exists (from a previous test or manual creation)
instanceID := 359559
Expand Down
18 changes: 9 additions & 9 deletions client/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestListInstances(t *testing.T) {
defer server.Close()

// Test
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

instances, err := client.ListInstances()

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestGetInstance(t *testing.T) {
defer server.Close()

// Test
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

instance, err := client.GetInstance(1234)

Expand Down Expand Up @@ -97,7 +97,7 @@ func TestCreateInstance(t *testing.T) {
defer server.Close()

// Test
client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

req := &InstanceCreateRequest{
Name: "test-instance",
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestCreateInstance_WithTags(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

req := &InstanceCreateRequest{
Name: "test-instance",
Expand All @@ -154,7 +154,7 @@ func TestCreateInstance_WithVPC(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

vpcID := 5678
req := &InstanceCreateRequest{
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestUpdateInstance(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

req := &InstanceUpdateRequest{
Name: "updated-name",
Expand All @@ -203,7 +203,7 @@ func TestDeleteInstance(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

err := client.DeleteInstance(1234)
assert.NoError(t, err)
Expand All @@ -223,7 +223,7 @@ func TestResizeInstanceDisk(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

req := &DiskResizeRequest{
ExtraDiskSize: 100,
Expand All @@ -246,7 +246,7 @@ func TestResizeInstanceDisk_NoDowntime(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

req := &DiskResizeRequest{
ExtraDiskSize: 50,
Expand Down
2 changes: 1 addition & 1 deletion client/instances_vcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestCreateInstanceVCR(t *testing.T) {
httpClient := &http.Client{Transport: r}

// Create client with VCR HTTP client
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

// Create instance request
req := &InstanceCreateRequest{
Expand Down
8 changes: 4 additions & 4 deletions client/regions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestListRegions(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

regions, err := client.ListRegions("")

Expand Down Expand Up @@ -63,7 +63,7 @@ func TestListRegions_WithProvider(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

regions, err := client.ListRegions("amazon-web-services")

Expand Down Expand Up @@ -97,7 +97,7 @@ func TestListPlans(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

plans, err := client.ListPlans("")

Expand Down Expand Up @@ -125,7 +125,7 @@ func TestListPlans_WithBackend(t *testing.T) {
}))
defer server.Close()

client := NewWithBaseURL("test-api-key", server.URL)
client := NewWithBaseURL("test-api-key", server.URL, "test")

plans, err := client.ListPlans("rabbitmq")

Expand Down
4 changes: 2 additions & 2 deletions client/vpc_vcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestListVPCsVCR(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

vpcs, err := client.ListVPCs()

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestVPCLifecycleVCR(t *testing.T) {
}

httpClient := &http.Client{Transport: r}
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient)
client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", "test", httpClient)

// Step 1: Create VPC
t.Log("Step 1: Creating VPC")
Expand Down
Loading
Loading