Skip to content

Commit 629b8bc

Browse files
committed
Merge pull request #4059 from Joshua-Anderson/add-domains
feat(client-go): add domains endpoint
2 parents 67dc142 + 3c9847e commit 629b8bc

6 files changed

Lines changed: 464 additions & 0 deletions

File tree

client-go/cmd/domains.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/deis/deis/client-go/controller/models/domains"
7+
)
8+
9+
// DomainsList lists domains registered with an app.
10+
func DomainsList(appID string) error {
11+
c, appID, err := load(appID)
12+
13+
if err != nil {
14+
return err
15+
}
16+
17+
domains, err := domains.List(c, appID)
18+
19+
if err != nil {
20+
return err
21+
}
22+
23+
fmt.Printf("=== %s Domains\n", appID)
24+
25+
for _, domain := range domains {
26+
fmt.Println(domain.Domain)
27+
}
28+
return nil
29+
}
30+
31+
// DomainsAdd adds a domain to an app.
32+
func DomainsAdd(appID, domain string) error {
33+
c, appID, err := load(appID)
34+
35+
if err != nil {
36+
return err
37+
}
38+
39+
fmt.Printf("Adding %s to %s... ", domain, appID)
40+
41+
quit := progress()
42+
_, err = domains.New(c, appID, domain)
43+
quit <- true
44+
<-quit
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
fmt.Println("done")
51+
return nil
52+
}
53+
54+
// DomainsRemove removes a domain registered with an app.
55+
func DomainsRemove(appID, domain string) error {
56+
c, appID, err := load(appID)
57+
58+
if err != nil {
59+
return err
60+
}
61+
62+
fmt.Printf("Removing %s from %s... ", domain, appID)
63+
64+
quit := progress()
65+
err = domains.Delete(c, appID, domain)
66+
quit <- true
67+
<-quit
68+
69+
if err != nil {
70+
return err
71+
}
72+
73+
fmt.Println("done")
74+
return nil
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package api
2+
3+
// Domain is the structure of the domain object.
4+
type Domain struct {
5+
App string `json:"app"`
6+
Created string `json:"created"`
7+
Domain string `json:"domain"`
8+
Owner string `json:"owner"`
9+
Updated string `json:"updated"`
10+
}
11+
12+
// Domains is the structure of GET /v1/app/<app id>/domains/.
13+
type Domains struct {
14+
Count int `json:"count"`
15+
Next int `json:"next"`
16+
Previous int `json:"previous"`
17+
Domains []Domain `json:"results"`
18+
}
19+
20+
// DomainCreateRequest is the structure of POST /v1/app/<app id>/domains/.
21+
type DomainCreateRequest struct {
22+
Domain string `json:"domain"`
23+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package domains
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/deis/deis/client-go/controller/api"
9+
"github.com/deis/deis/client-go/controller/client"
10+
)
11+
12+
// List domains registered with an app.
13+
func List(c *client.Client, appID string) ([]api.Domain, error) {
14+
u := fmt.Sprintf("/v1/apps/%s/domains/", appID)
15+
body, status, err := c.BasicRequest("GET", u, nil)
16+
17+
if err != nil {
18+
return []api.Domain{}, err
19+
}
20+
21+
if status != 200 {
22+
return []api.Domain{}, errors.New(body)
23+
}
24+
25+
domains := api.Domains{}
26+
if err = json.Unmarshal([]byte(body), &domains); err != nil {
27+
return []api.Domain{}, err
28+
}
29+
30+
return domains.Domains, nil
31+
}
32+
33+
// New adds a domain to an app.
34+
func New(c *client.Client, appID string, domain string) (api.Domain, error) {
35+
u := fmt.Sprintf("/v1/apps/%s/domains/", appID)
36+
37+
req := api.DomainCreateRequest{Domain: domain}
38+
39+
body, err := json.Marshal(req)
40+
41+
if err != nil {
42+
return api.Domain{}, err
43+
}
44+
45+
resBody, status, err := c.BasicRequest("POST", u, body)
46+
47+
if err != nil {
48+
return api.Domain{}, err
49+
}
50+
51+
if status != 201 {
52+
return api.Domain{}, errors.New(resBody)
53+
}
54+
55+
res := api.Domain{}
56+
if err = json.Unmarshal([]byte(resBody), &res); err != nil {
57+
return api.Domain{}, err
58+
}
59+
60+
return res, nil
61+
}
62+
63+
// Delete removes a domain from an app.
64+
func Delete(c *client.Client, appID string, domain string) error {
65+
u := fmt.Sprintf("/v1/apps/%s/domains/%s", appID, domain)
66+
body, status, err := c.BasicRequest("DELETE", u, nil)
67+
68+
if err != nil {
69+
return err
70+
}
71+
72+
if status != 204 {
73+
return errors.New(body)
74+
}
75+
76+
return nil
77+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package domains
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"net/http/httptest"
8+
"net/url"
9+
"reflect"
10+
"testing"
11+
12+
"github.com/deis/deis/client-go/controller/api"
13+
"github.com/deis/deis/client-go/controller/client"
14+
"github.com/deis/deis/version"
15+
)
16+
17+
const domainsFixture string = `
18+
{
19+
"count": 1,
20+
"next": null,
21+
"previous": null,
22+
"results": [
23+
{
24+
"app": "example-go",
25+
"created": "2014-01-01T00:00:00UTC",
26+
"domain": "example.example.com",
27+
"owner": "test",
28+
"updated": "2014-01-01T00:00:00UTC"
29+
}
30+
]
31+
}`
32+
33+
const domainFixture string = `
34+
{
35+
"app": "example-go",
36+
"created": "2014-01-01T00:00:00UTC",
37+
"domain": "example.example.com",
38+
"owner": "test",
39+
"updated": "2014-01-01T00:00:00UTC"
40+
}`
41+
42+
const domainCreateExpected string = `{"domain":"example.example.com"}`
43+
44+
type fakeHTTPServer struct{}
45+
46+
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
47+
res.Header().Add("DEIS_API_VERSION", version.APIVersion)
48+
49+
if req.URL.Path == "/v1/apps/example-go/domains/" && req.Method == "GET" {
50+
res.Write([]byte(domainsFixture))
51+
return
52+
}
53+
54+
if req.URL.Path == "/v1/apps/example-go/domains/" && req.Method == "POST" {
55+
body, err := ioutil.ReadAll(req.Body)
56+
57+
if err != nil {
58+
fmt.Println(err)
59+
res.WriteHeader(http.StatusInternalServerError)
60+
res.Write(nil)
61+
}
62+
63+
if string(body) != domainCreateExpected {
64+
fmt.Printf("Expected '%s', Got '%s'\n", domainCreateExpected, body)
65+
res.WriteHeader(http.StatusInternalServerError)
66+
res.Write(nil)
67+
return
68+
}
69+
70+
res.WriteHeader(http.StatusCreated)
71+
res.Write([]byte(domainFixture))
72+
return
73+
}
74+
75+
if req.URL.Path == "/v1/apps/example-go/domains/test.com" && req.Method == "DELETE" {
76+
res.WriteHeader(http.StatusNoContent)
77+
res.Write([]byte(domainsFixture))
78+
return
79+
}
80+
81+
fmt.Printf("Unrecognized URL %s\n", req.URL)
82+
res.WriteHeader(http.StatusNotFound)
83+
res.Write(nil)
84+
}
85+
86+
func TestDomainsList(t *testing.T) {
87+
t.Parallel()
88+
89+
expected := []api.Domain{
90+
api.Domain{
91+
App: "example-go",
92+
Created: "2014-01-01T00:00:00UTC",
93+
Domain: "example.example.com",
94+
Owner: "test",
95+
Updated: "2014-01-01T00:00:00UTC",
96+
},
97+
}
98+
99+
handler := fakeHTTPServer{}
100+
server := httptest.NewServer(handler)
101+
defer server.Close()
102+
103+
u, err := url.Parse(server.URL)
104+
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
109+
httpClient := client.CreateHTTPClient(false)
110+
111+
client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
112+
113+
actual, err := List(&client, "example-go")
114+
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
119+
if !reflect.DeepEqual(expected, actual) {
120+
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
121+
}
122+
}
123+
124+
func TestDomainsAdd(t *testing.T) {
125+
t.Parallel()
126+
127+
expected := api.Domain{
128+
App: "example-go",
129+
Created: "2014-01-01T00:00:00UTC",
130+
Domain: "example.example.com",
131+
Owner: "test",
132+
Updated: "2014-01-01T00:00:00UTC",
133+
}
134+
135+
handler := fakeHTTPServer{}
136+
server := httptest.NewServer(handler)
137+
defer server.Close()
138+
139+
u, err := url.Parse(server.URL)
140+
141+
if err != nil {
142+
t.Fatal(err)
143+
}
144+
145+
httpClient := client.CreateHTTPClient(false)
146+
147+
client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
148+
149+
actual, err := New(&client, "example-go", "example.example.com")
150+
151+
if err != nil {
152+
t.Fatal(err)
153+
}
154+
155+
if !reflect.DeepEqual(expected, actual) {
156+
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
157+
}
158+
}
159+
160+
func TestDomainsRemove(t *testing.T) {
161+
t.Parallel()
162+
163+
handler := fakeHTTPServer{}
164+
server := httptest.NewServer(handler)
165+
defer server.Close()
166+
167+
u, err := url.Parse(server.URL)
168+
169+
if err != nil {
170+
t.Fatal(err)
171+
}
172+
173+
httpClient := client.CreateHTTPClient(false)
174+
175+
client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
176+
177+
if err = Delete(&client, "example-go", "test.com"); err != nil {
178+
t.Fatal(err)
179+
}
180+
}

client-go/deis.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Use 'git push deis master' to deploy to an application.
8181
err = parser.Auth(argv)
8282
case "apps":
8383
err = parser.Apps(argv)
84+
case "domains":
85+
err = parser.Domains(argv)
8486
case "keys":
8587
err = parser.Keys(argv)
8688
case "users":

0 commit comments

Comments
 (0)