-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev'
- Loading branch information
Showing
13 changed files
with
623 additions
and
26 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
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,89 @@ | ||
package endpoints | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/models" | ||
) | ||
|
||
type Service string | ||
|
||
const ( | ||
GlobalSearch Service = "global-search" | ||
GlobalTagging Service = "global-tagging" | ||
AccountManagement Service = "account-management" | ||
UserManagement Service = "user-management" | ||
Billing Service = "billing" | ||
Enterprise Service = "enterprise" | ||
ResourceController Service = "resource-controller" | ||
ResourceCatalog Service = "global-catalog" | ||
) | ||
|
||
func (s Service) String() string { | ||
return string(s) | ||
} | ||
|
||
var endpointsMapping = map[Service]models.Endpoints{ | ||
GlobalSearch: models.Endpoints{ | ||
PublicEndpoint: "https://api.global-search-tagging.<cloud_domain>", | ||
PrivateEndpoint: "https://api.private.<region>.global-search-tagging.<cloud_domain>", | ||
}, | ||
GlobalTagging: models.Endpoints{ | ||
PublicEndpoint: "https://tags.global-search-tagging.<cloud_domain>", | ||
PrivateEndpoint: "https://tags.private.<region>.global-search-tagging.<cloud_domain>", | ||
}, | ||
AccountManagement: models.Endpoints{ | ||
PublicEndpoint: "https://accounts.<cloud_domain>", | ||
PrivateEndpoint: "https://private.<region>.accounts.<cloud_domain>", | ||
}, | ||
UserManagement: models.Endpoints{ | ||
PublicEndpoint: "https://user-management.<cloud_domain>", | ||
PrivateEndpoint: "https://private.<region>.user-management.<cloud_domain>", | ||
}, | ||
Billing: models.Endpoints{ | ||
PublicEndpoint: "https://billing.<cloud_domain>", | ||
PrivateEndpoint: "https://private.<region>.billing.<cloud_domain>", | ||
}, | ||
Enterprise: models.Endpoints{ | ||
PublicEndpoint: "https://enterprise.<cloud_domain>", | ||
PrivateEndpoint: "https://private.<region>.enterprise.<cloud_domain>", | ||
}, | ||
ResourceController: models.Endpoints{ | ||
PublicEndpoint: "https://resource-controller.<cloud_domain>", | ||
PrivateEndpoint: "https://private.<region>.resource-controller.<cloud_domain>", | ||
}, | ||
ResourceCatalog: models.Endpoints{ | ||
PublicEndpoint: "https://globalcatalog.<cloud_domain>", | ||
PrivateEndpoint: "https://private.<region>.globalcatalog.<cloud_domain>", | ||
}, | ||
} | ||
|
||
func Endpoint(svc Service, cloudDomain, region string, private bool) (string, error) { | ||
var endpoint string | ||
if endpoints, found := endpointsMapping[svc]; found { | ||
if private { | ||
endpoint = endpoints.PrivateEndpoint | ||
} else { | ||
endpoint = endpoints.PublicEndpoint | ||
} | ||
} | ||
if endpoint == "" { | ||
return "", fmt.Errorf("the endpoint of service '%s' was unknown", svc) | ||
} | ||
|
||
// replace <cloud_domain> | ||
if cloudDomain == "" { | ||
return "", fmt.Errorf("the cloud domain is empty") | ||
} | ||
endpoint = strings.ReplaceAll(endpoint, "<cloud_domain>", cloudDomain) | ||
|
||
// replace <region> | ||
if region != "" { | ||
endpoint = strings.ReplaceAll(endpoint, "<region>", region) | ||
} else if strings.Index(endpoint, "<region>") >= 0 { | ||
return "", fmt.Errorf("region is required to get the endpoint of service '%s'", svc) | ||
} | ||
|
||
return endpoint, nil | ||
} |
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,68 @@ | ||
package endpoints_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/endpoints" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEndpointUnknownService(t *testing.T) { | ||
_, err := Endpoint(Service("unknown"), "cloud.ibm.com", "us-south", false) | ||
assert.Error(t, err, "an error is expected") | ||
} | ||
|
||
func TestEndpointEmptyCloudDomain(t *testing.T) { | ||
_, err := Endpoint(AccountManagement, "", "", false) | ||
assert.Error(t, err, "an error is expected") | ||
} | ||
|
||
func TestEndpointPublic(t *testing.T) { | ||
cloudDomain := "cloud.ibm.com" | ||
region := "" | ||
private := false | ||
|
||
endpoints := map[Service]string{ | ||
GlobalSearch: fmt.Sprintf("https://api.global-search-tagging.%s", cloudDomain), | ||
GlobalTagging: fmt.Sprintf("https://tags.global-search-tagging.%s", cloudDomain), | ||
AccountManagement: fmt.Sprintf("https://accounts.%s", cloudDomain), | ||
UserManagement: fmt.Sprintf("https://user-management.%s", cloudDomain), | ||
Billing: fmt.Sprintf("https://billing.%s", cloudDomain), | ||
Enterprise: fmt.Sprintf("https://enterprise.%s", cloudDomain), | ||
ResourceController: fmt.Sprintf("https://resource-controller.%s", cloudDomain), | ||
ResourceCatalog: fmt.Sprintf("https://globalcatalog.%s", cloudDomain), | ||
} | ||
for svc, expected := range endpoints { | ||
actual, err := Endpoint(svc, cloudDomain, region, private) | ||
assert.NoError(t, err, "public endpoint of service '%s'", svc) | ||
assert.Equal(t, expected, actual, "public endpoint of service '%s'", svc) | ||
} | ||
} | ||
|
||
func TestEndpointPrivate(t *testing.T) { | ||
cloudDomain := "cloud.ibm.com" | ||
region := "us-south" | ||
private := true | ||
|
||
endpoints := map[Service]string{ | ||
GlobalSearch: fmt.Sprintf("https://api.private.%s.global-search-tagging.%s", region, cloudDomain), | ||
GlobalTagging: fmt.Sprintf("https://tags.private.%s.global-search-tagging.%s", region, cloudDomain), | ||
AccountManagement: fmt.Sprintf("https://private.%s.accounts.%s", region, cloudDomain), | ||
UserManagement: fmt.Sprintf("https://private.%s.user-management.%s", region, cloudDomain), | ||
Billing: fmt.Sprintf("https://private.%s.billing.%s", region, cloudDomain), | ||
Enterprise: fmt.Sprintf("https://private.%s.enterprise.%s", region, cloudDomain), | ||
ResourceController: fmt.Sprintf("https://private.%s.resource-controller.%s", region, cloudDomain), | ||
ResourceCatalog: fmt.Sprintf("https://private.%s.globalcatalog.%s", region, cloudDomain), | ||
} | ||
for svc, expected := range endpoints { | ||
actual, err := Endpoint(svc, cloudDomain, region, private) | ||
assert.NoError(t, err, "private endpoint of service '%s'", svc) | ||
assert.Equal(t, expected, actual, "private endpoint of service '%s'", svc) | ||
} | ||
} | ||
|
||
func TestEndpointPrivateNoRegion(t *testing.T) { | ||
_, err := Endpoint(AccountManagement, "cloud.ibm.com", "", true) | ||
assert.Error(t, err, "an error is expected") | ||
} |
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
Oops, something went wrong.