Skip to content

Commit a971592

Browse files
committed
Added addons service
1 parent 342a2a6 commit a971592

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

Diff for: addon.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package eygo
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
// Addon is a data structure that models a platform addon on the Engine
8+
// Yard API.
9+
type Addon struct {
10+
ID int `json:"id,omitempty"`
11+
Name string `json:"name,omitempty"`
12+
SSOURL string `json:"sso_url,omitempty"`
13+
Vars map[string]string `json:"vars,omitempty"`
14+
}
15+
16+
// AddonService is a repository one can use to retrieve, enable, and disable
17+
// Addon records on the API.
18+
type AddonService struct {
19+
Driver Driver
20+
}
21+
22+
// NewAddonService returns a AddonService configured with the provided
23+
// Driver.
24+
func NewAddonService(driver Driver) *AddonService {
25+
return &AddonService{Driver: driver}
26+
}
27+
28+
// ForAccount returns an array of Addons that are both associated with the
29+
// given Account and matching the given Params.
30+
func (service *AddonService) ForAccount(account *Account, params Params) []*Addon {
31+
return service.collection("accounts/"+account.ID+"/addons", params)
32+
}
33+
34+
func (service *AddonService) collection(path string, params Params) []*Addon {
35+
addons := make([]*Addon, 0)
36+
response := service.Driver.Get(path, params)
37+
38+
if response.Okay() {
39+
for _, page := range response.Pages {
40+
wrapper := struct {
41+
Addons []*Addon `json:"addons,omitempty"`
42+
}{}
43+
44+
if err := json.Unmarshal(page, &wrapper); err == nil {
45+
addons = append(addons, wrapper.Addons...)
46+
}
47+
}
48+
}
49+
50+
return addons
51+
}
52+
53+
/*
54+
Copyright 2018 Dennis Walters
55+
56+
Licensed under the Apache License, Version 2.0 (the "License");
57+
you may not use this file except in compliance with the License.
58+
You may obtain a copy of the License at
59+
60+
http://www.apache.org/licenses/LICENSE-2.0
61+
62+
Unless required by applicable law or agreed to in writing, software
63+
distributed under the License is distributed on an "AS IS" BASIS,
64+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65+
See the License for the specific language governing permissions and
66+
limitations under the License.
67+
*/

Diff for: addon_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package eygo
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
func TestNewAddonService(t *testing.T) {
9+
driver := NewMockDriver()
10+
service := NewAddonService(driver)
11+
12+
t.Run("it is configured with the given driver", func(t *testing.T) {
13+
if service.Driver != driver {
14+
t.Errorf("Expected the service to use the given driver")
15+
}
16+
})
17+
}
18+
19+
func TestAddonService_ForAccount(t *testing.T) {
20+
account := &Account{ID: "1", Name: "Account 1"}
21+
driver := NewMockDriver()
22+
service := NewAddonService(driver)
23+
24+
t.Run("when there are matching addons", func(t *testing.T) {
25+
addon1 := &Addon{ID: 1}
26+
addon2 := &Addon{ID: 2}
27+
addon3 := &Addon{ID: 3}
28+
29+
stubAccountAddons(driver, account, addon1, addon2, addon3)
30+
31+
all := service.ForAccount(account, nil)
32+
33+
t.Run("it contains all matching addons", func(t *testing.T) {
34+
addons := []*Addon{addon1, addon2, addon3}
35+
36+
if len(all) != len(addons) {
37+
t.Errorf("Expected %d addons, got %d", len(addons), len(all))
38+
}
39+
40+
for _, addon := range addons {
41+
found := false
42+
43+
for _, other := range all {
44+
if addon.ID == other.ID {
45+
found = true
46+
}
47+
}
48+
49+
if !found {
50+
t.Errorf("Addon %d was not present", addon.ID)
51+
}
52+
}
53+
})
54+
55+
})
56+
57+
t.Run("when there are no matching addons", func(t *testing.T) {
58+
driver.Reset()
59+
60+
t.Run("it is empty", func(t *testing.T) {
61+
all := service.ForAccount(account, nil)
62+
63+
if len(all) != 0 {
64+
t.Errorf("Expected 0 addons, got")
65+
}
66+
})
67+
68+
})
69+
70+
}
71+
72+
func stubAddons(driver *MockDriver, addons ...*Addon) {
73+
pages := make([][]byte, 0)
74+
75+
wrapper := struct {
76+
Addons []*Addon `json:"addons,omitempty"`
77+
}{Addons: addons}
78+
79+
if encoded, err := json.Marshal(&wrapper); err == nil {
80+
pages = append(pages, encoded)
81+
driver.AddResponse("get", "addons", Response{Pages: pages})
82+
}
83+
}
84+
85+
func stubAccountAddons(driver *MockDriver, account *Account, addons ...*Addon) {
86+
pages := make([][]byte, 0)
87+
88+
wrapper := struct {
89+
Addons []*Addon `json:"addons,omitempty"`
90+
}{Addons: addons}
91+
92+
if encoded, err := json.Marshal(&wrapper); err == nil {
93+
pages = append(pages, encoded)
94+
driver.AddResponse("get", "accounts/"+account.ID+"/addons", Response{Pages: pages})
95+
}
96+
}

0 commit comments

Comments
 (0)