/
api_service.go
74 lines (66 loc) · 1.93 KB
/
api_service.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package managementv2
import (
gohttp "net/http"
bluemix "github.com/IBM-Cloud/bluemix-go"
"github.com/IBM-Cloud/bluemix-go/authentication"
"github.com/IBM-Cloud/bluemix-go/client"
"github.com/IBM-Cloud/bluemix-go/http"
"github.com/IBM-Cloud/bluemix-go/rest"
"github.com/IBM-Cloud/bluemix-go/session"
)
//ResourceManagementAPI is the resource client ...
type ResourceManagementAPIv2 interface {
ResourceQuota() ResourceQuotaRepository
ResourceGroup() ResourceGroupRepository
}
//ErrCodeAPICreation ...
const ErrCodeAPICreation = "APICreationError"
//resourceManagementService holds the client
type resourceManagementService struct {
*client.Client
}
//New ...
func New(sess *session.Session) (ResourceManagementAPIv2, error) {
config := sess.Config.Copy()
err := config.ValidateConfigForService(bluemix.ResourceManagementService)
if err != nil {
return nil, err
}
if config.HTTPClient == nil {
config.HTTPClient = http.NewHTTPClient(config)
}
tokenRefreher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
DefaultHeader: gohttp.Header{
"X-Original-User-Agent": []string{config.UserAgent},
"User-Agent": []string{http.UserAgent()},
},
HTTPClient: config.HTTPClient,
})
if err != nil {
return nil, err
}
if config.IAMAccessToken == "" {
err := authentication.PopulateTokens(tokenRefreher, config)
if err != nil {
return nil, err
}
}
if config.Endpoint == nil {
ep, err := config.EndpointLocator.ResourceManagementEndpoint()
if err != nil {
return nil, err
}
config.Endpoint = &ep
}
return &resourceManagementService{
Client: client.New(config, bluemix.ResourceManagementService, tokenRefreher),
}, nil
}
//ResourceQuota API
func (a *resourceManagementService) ResourceQuota() ResourceQuotaRepository {
return newResourceQuotaAPI(a.Client)
}
//ResourceGroup API
func (a *resourceManagementService) ResourceGroup() ResourceGroupRepository {
return newResourceGroupAPI(a.Client)
}