forked from maximp/gosigma
-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoint.go
52 lines (41 loc) · 1.15 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 ALTOROS
// Licensed under the AGPLv3, see LICENSE file for details.
package gosigma
import (
"errors"
"fmt"
"net/url"
)
// Default CloudSigma region
const DefaultRegion string = "zrh"
var errEmptyEndpoint = errors.New("endpoint are not allowed to be empty")
var errHttpsRequired = errors.New("endpoint must use https scheme")
var errInvalidAuth = errors.New("auth information is not allowed in the endpoint string")
var errEndpointWithQuery = errors.New("query information is not allowed in the endpoint string")
// ResolveEndpoint returns endpoint for given region code
func ResolveEndpoint(endpoint string) string {
if err := VerifyEndpoint(endpoint); err == nil {
return endpoint
}
return fmt.Sprintf("https://%s.cloudsigma.com/api/2.0/", endpoint)
}
// VerifyEndpoint verifies CloudSigma endpoint URL
func VerifyEndpoint(e string) error {
if len(e) == 0 {
return errEmptyEndpoint
}
u, err := url.Parse(e)
if err != nil {
return err
}
if u.Scheme != "https" {
return errHttpsRequired
}
if u.User != nil {
return errInvalidAuth
}
if len(u.RawQuery) > 0 || len(u.Fragment) > 0 {
return errEndpointWithQuery
}
return nil
}