diff --git a/.gitignore b/.gitignore index 7437dfe..89bd77f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,8 @@ # End of https://www.gitignore.io/api/go .idea/ + +# Config VSCode IDE +.vscode + +build/ \ No newline at end of file diff --git a/client.go b/client.go index 34c5012..003a625 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,8 @@ import ( "net/url" "path" "strings" + + "github.com/bizflycloud/gobizfly/utils" ) const ( @@ -117,7 +119,11 @@ func WithHTTPClient(client *http.Client) Option { // WithRegionName sets the client region for Bizfly client. func WithRegionName(region string) Option { return func(c *Client) error { - c.regionName = region + regionName, err := utils.ParseRegionName(region) + if err != nil { + return err + } + c.regionName = regionName return nil } } diff --git a/client_test.go b/client_test.go index 6479d8c..ba2f0c8 100644 --- a/client_test.go +++ b/client_test.go @@ -22,7 +22,7 @@ func setup() { serverTest = httptest.NewServer(mux) var err error - testRegion := "HN" + testRegion := "HaNoi" client, err = NewClient(WithAPIUrl(serverTest.URL), WithRegionName(testRegion)) services := []*Service{ {Name: "Cloud Server", diff --git a/constants/constants.go b/constants/constants.go new file mode 100644 index 0000000..9d5c705 --- /dev/null +++ b/constants/constants.go @@ -0,0 +1,15 @@ +package constants + +const ( + HaNoiRegion = "HaNoi" + HoCHiMinhRegion = "HoChiMinh" + VcHaNoiRegion = "VC-HaNoi" +) + +var RegionMapping = map[string]string{ + "hn": HaNoiRegion, + "hanoi": HaNoiRegion, + "hcm": HoCHiMinhRegion, + "hochiminh": HoCHiMinhRegion, + "vc-hanoi": VcHaNoiRegion, +} diff --git a/errors/errors.go b/errors/errors.go new file mode 100644 index 0000000..977e1b9 --- /dev/null +++ b/errors/errors.go @@ -0,0 +1,49 @@ +package gobizflyErr + +import ( + "bytes" + "html/template" +) + +var tmpl = template.New("GobizflyTemplate") + +type GobizflyErr struct { + Message string + Code string + Metadata map[string]interface{} +} + +func (err GobizflyErr) Error() string { + message := err.GetMessage() + return message +} + +func (err GobizflyErr) String() string { + message := err.GetMessage() + return message +} + +func (err GobizflyErr) GetMessage() string { + newTmpl, tmplErr := tmpl.Parse(err.Message) + if tmplErr != nil { + return err.Message + } + var result bytes.Buffer + tmplErr = newTmpl.Execute(&result, err.Metadata) + if tmplErr != nil { + return err.Message + } + return result.String() +} + +func (err GobizflyErr) SetMetadata(metadata map[string]interface{}) GobizflyErr { + err.Metadata = metadata + return err +} + +var ( + InvalidRegion = GobizflyErr{ + Message: "Invalid region {{.Region}}", + Code: "InvalidRegion", + } +) diff --git a/utils/parser.go b/utils/parser.go new file mode 100644 index 0000000..fffd9bf --- /dev/null +++ b/utils/parser.go @@ -0,0 +1,17 @@ +package utils + +import ( + "strings" + + "github.com/bizflycloud/gobizfly/constants" + gobizflyErr "github.com/bizflycloud/gobizfly/errors" +) + +func ParseRegionName(region string) (string, error) { + lowerRegion := strings.ToLower(region) + regionName, ok := constants.RegionMapping[lowerRegion] + if !ok { + return "", gobizflyErr.InvalidRegion.SetMetadata(map[string]interface{}{"Region": region}) + } + return regionName, nil +}