forked from fpt-corp/terraform-provider-fptcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
5,432 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package commons | ||
|
||
import "fmt" | ||
|
||
var ApiPath = struct { | ||
SSH string | ||
Storage func(vpcId string) string | ||
StorageUpdateAttached func(vpcId string, storageId string) string | ||
StoragePolicy func(vpcId string) string | ||
Flavor func(vpcId string) string | ||
Image func(vpcId string) string | ||
SecurityGroup func(vpcId string) string | ||
RenameSecurityGroup func(vpcId string, securityGroupId string) string | ||
UpdateApplyToSecurityGroup func(vpcId string, securityGroupId string) string | ||
SecurityGroupRule func(vpcId string, securityGroupRuleId string) string | ||
CreateSecurityGroupRule func(vpcId string) string | ||
}{ | ||
SSH: "/v1/user/sshs", | ||
Storage: func(vpcId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/storage", vpcId) | ||
}, | ||
StorageUpdateAttached: func(vpcId string, storageId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/storage/%s/update-attached", vpcId, storageId) | ||
}, | ||
StoragePolicy: func(vpcId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/storage-policies", vpcId) | ||
}, | ||
Flavor: func(vpcId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/flavors", vpcId) | ||
}, | ||
Image: func(vpcId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/images", vpcId) | ||
}, | ||
SecurityGroup: func(vpcId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/security-group", vpcId) | ||
}, | ||
RenameSecurityGroup: func(vpcId string, securityGroupId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/security-group/%s/rename", vpcId, securityGroupId) | ||
}, | ||
UpdateApplyToSecurityGroup: func(vpcId string, securityGroupId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/security-group/%s/apply-to", vpcId, securityGroupId) | ||
}, | ||
SecurityGroupRule: func(vpcId string, securityGroupRuleId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/security-group-rule/%s", vpcId, securityGroupRuleId) | ||
}, | ||
CreateSecurityGroupRule: func(vpcId string) string { | ||
return fmt.Sprintf("/v1/terraform/vpc/%s/security-group-rule", vpcId) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package commons | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestNewClientWithURL_ValidURL(t *testing.T) { | ||
client, err := NewClientWithURL("apiKey", "https://api.example.com", "region", "tenant") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, client) | ||
assert.Equal(t, "https://api.example.com", client.BaseURL.String()) | ||
} | ||
|
||
func TestNewClientWithURL_InvalidURL(t *testing.T) { | ||
client, err := NewClientWithURL("apiKey", ":", "region", "tenant") | ||
assert.Error(t, err) | ||
assert.Nil(t, client) | ||
} | ||
|
||
func TestSendGetRequest_ValidResponse(t *testing.T) { | ||
client, server, err := NewClientForTesting(map[string]string{ | ||
"/test": `{"data": "success"}`, | ||
}) | ||
assert.NoError(t, err) | ||
defer server.Close() | ||
|
||
resp, err := client.SendGetRequest("/test") | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(resp), "success") | ||
} | ||
|
||
func TestSendGetRequest_InvalidURL(t *testing.T) { | ||
client, err := NewClientWithURL("apiKey", "https://api.example.com", "region", "tenant") | ||
assert.NoError(t, err) | ||
|
||
resp, err := client.SendGetRequest("://invalid-url") | ||
assert.Error(t, err) | ||
assert.Nil(t, resp) | ||
} | ||
|
||
func TestSendPostRequest_ValidResponse(t *testing.T) { | ||
client, server, err := NewClientForTesting(map[string]string{ | ||
"/test": `{"data": "success"}`, | ||
}) | ||
assert.NoError(t, err) | ||
defer server.Close() | ||
|
||
resp, err := client.SendPostRequest("/test", map[string]string{"key": "value"}) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(resp), "success") | ||
} | ||
|
||
func TestSendPostRequest_InvalidJSON(t *testing.T) { | ||
client, err := NewClientWithURL("apiKey", "https://api.example.com", "region", "tenant") | ||
assert.NoError(t, err) | ||
|
||
resp, err := client.SendPostRequest("/test", make(chan int)) | ||
assert.Error(t, err) | ||
assert.Nil(t, resp) | ||
} | ||
|
||
func TestSendDeleteRequest_ValidResponse(t *testing.T) { | ||
client, server, err := NewClientForTesting(map[string]string{ | ||
"/test": `{"data": "deleted"}`, | ||
}) | ||
assert.NoError(t, err) | ||
defer server.Close() | ||
|
||
resp, err := client.SendDeleteRequest("/test") | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(resp), "deleted") | ||
} | ||
|
||
func TestSendDeleteRequestWithBody_ValidResponse(t *testing.T) { | ||
client, server, err := NewClientForTesting(map[string]string{ | ||
"/test": `{"data": "deleted"}`, | ||
}) | ||
assert.NoError(t, err) | ||
defer server.Close() | ||
|
||
resp, err := client.SendDeleteRequestWithBody("/test", map[string]string{"key": "value"}) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(resp), "deleted") | ||
} | ||
|
||
func TestSetUserAgent_SetsCorrectly(t *testing.T) { | ||
client, err := NewClientWithURL("apiKey", "https://api.example.com", "region", "tenant") | ||
assert.NoError(t, err) | ||
|
||
component := &Component{ID: "123", Name: "TestComponent", Version: "1.0"} | ||
client.SetUserAgent(component) | ||
assert.Contains(t, client.UserAgent, "TestComponent/1.0-123") | ||
} | ||
|
||
func TestDecodeSimpleResponse_ValidResponse(t *testing.T) { | ||
client, err := NewClientWithURL("apiKey", "https://api.example.com", "region", "tenant") | ||
assert.NoError(t, err) | ||
|
||
resp := []byte(`{"Data": "success", "Status": "ok"}`) | ||
simpleResp, err := client.DecodeSimpleResponse(resp) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "success", simpleResp.Data) | ||
assert.Equal(t, "ok", simpleResp.Status) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package commons | ||
|
||
const ( | ||
DefaultApiUrl = "https://console-api.fptcloud.com/api" | ||
) |
Oops, something went wrong.