/
shared_domain.go
151 lines (130 loc) · 4.02 KB
/
shared_domain.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package mccpv2
import (
"fmt"
"github.com/IBM-Cloud/bluemix-go/bmxerror"
"github.com/IBM-Cloud/bluemix-go/client"
"github.com/IBM-Cloud/bluemix-go/rest"
)
//ErrCodeSharedDomainDoesnotExist ...
var ErrCodeSharedDomainDoesnotExist = "SharedDomainDoesnotExist"
//SharedDomainRequest ...
type SharedDomainRequest struct {
Name string `json:"name"`
RouterGroupGUID string `json:"router_group_guid,omitempty"`
}
//SharedDomaineMetadata ...
type SharedDomainMetadata struct {
GUID string `json:"guid"`
URL string `json:"url"`
}
//SharedDomainEntity ...
type SharedDomainEntity struct {
Name string `json:"name"`
RouterGroupGUID string `json:"router_group_guid"`
RouterGroupType string `json:"router_group_type"`
}
//SharedDomainResource ...
type SharedDomainResource struct {
Resource
Entity SharedDomainEntity
}
//SharedDomainFields ...
type SharedDomainFields struct {
Metadata SharedDomainMetadata
Entity SharedDomainEntity
}
//ToFields ..
func (resource SharedDomainResource) ToFields() SharedDomain {
entity := resource.Entity
return SharedDomain{
GUID: resource.Metadata.GUID,
Name: entity.Name,
RouterGroupGUID: entity.RouterGroupGUID,
RouterGroupType: entity.RouterGroupType,
}
}
//SharedDomain model
type SharedDomain struct {
GUID string
Name string
RouterGroupGUID string
RouterGroupType string
}
//SharedDomains ...
type SharedDomains interface {
FindByName(domainName string) (*SharedDomain, error)
Create(req SharedDomainRequest, opts ...bool) (*SharedDomainFields, error)
Get(sharedDomainGUID string) (*SharedDomainFields, error)
Delete(sharedDomainGUID string, opts ...bool) error
}
type sharedDomain struct {
client *client.Client
}
func newSharedDomainAPI(c *client.Client) SharedDomains {
return &sharedDomain{
client: c,
}
}
func (d *sharedDomain) FindByName(domainName string) (*SharedDomain, error) {
rawURL := "/v2/shared_domains"
req := rest.GetRequest(rawURL).Query("q", "name:"+domainName)
httpReq, err := req.Build()
if err != nil {
return nil, err
}
path := httpReq.URL.String()
domain, err := listSharedDomainWithPath(d.client, path)
if err != nil {
return nil, err
}
if len(domain) == 0 {
return nil, bmxerror.New(ErrCodeSharedDomainDoesnotExist, fmt.Sprintf("Shared Domain: %q doesn't exist", domainName))
}
return &domain[0], nil
}
func listSharedDomainWithPath(c *client.Client, path string) ([]SharedDomain, error) {
var sharedDomain []SharedDomain
_, err := c.GetPaginated(path, NewCCPaginatedResources(SharedDomainResource{}), func(resource interface{}) bool {
if sharedDomainResource, ok := resource.(SharedDomainResource); ok {
sharedDomain = append(sharedDomain, sharedDomainResource.ToFields())
return true
}
return false
})
return sharedDomain, err
}
// opts is list of boolean parametes
// opts[0] - async - Will run the create request in a background job. Recommended: 'true'. Default to 'true'
func (d *sharedDomain) Create(req SharedDomainRequest, opts ...bool) (*SharedDomainFields, error) {
async := true
if len(opts) > 0 {
async = opts[0]
}
rawURL := fmt.Sprintf("/v2/shared_domains?async=%t", async)
sharedDomainFields := SharedDomainFields{}
_, err := d.client.Post(rawURL, req, &sharedDomainFields)
if err != nil {
return nil, err
}
return &sharedDomainFields, nil
}
func (d *sharedDomain) Get(sharedDomainGUID string) (*SharedDomainFields, error) {
rawURL := fmt.Sprintf("/v2/shared_domains/%s", sharedDomainGUID)
sharedDomainFields := SharedDomainFields{}
_, err := d.client.Get(rawURL, &sharedDomainFields, nil)
if err != nil {
return nil, err
}
return &sharedDomainFields, nil
}
// opts is list of boolean parametes
// opts[0] - async - Will run the delete request in a background job. Recommended: 'true'. Default to 'true'
func (d *sharedDomain) Delete(sharedDomainGUID string, opts ...bool) error {
async := true
if len(opts) > 0 {
async = opts[0]
}
rawURL := fmt.Sprintf("/v2/shared_domains/%s?async=%t", sharedDomainGUID, async)
_, err := d.client.Delete(rawURL)
return err
}