blob: af73dcff7735d8afe81680bff781336f16b9a954 [file] [log] [blame]
Jon Perritt9600eb42015-01-21 15:04:14 -07001// +build acceptance
2
3package v1
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 os "github.com/rackspace/gophercloud/openstack/cdn/v1/services"
Jon Perrittea764ef2015-01-23 10:59:32 -070010 "github.com/rackspace/gophercloud/pagination"
Jon Perritt9600eb42015-01-21 15:04:14 -070011 "github.com/rackspace/gophercloud/rackspace/cdn/v1/services"
12 th "github.com/rackspace/gophercloud/testhelper"
13)
14
15func TestService(t *testing.T) {
16 client := newClient(t)
17
18 t.Log("Creating Service")
19 loc := testServiceCreate(t, client)
20 t.Logf("Created service at location: %s", loc)
21
22 defer testServiceDelete(t, client, loc)
23
24 t.Log("Updating Service")
25 testServiceUpdate(t, client, loc)
26
27 t.Log("Retrieving Service")
28 testServiceGet(t, client, loc)
29
Jon Perrittea764ef2015-01-23 10:59:32 -070030 t.Log("Listing Services")
31 testServiceList(t, client)
Jon Perritt9600eb42015-01-21 15:04:14 -070032}
33
34func testServiceCreate(t *testing.T, client *gophercloud.ServiceClient) string {
35 createOpts := os.CreateOpts{
36 Name: "gophercloud-test-service",
37 Domains: []os.Domain{
38 os.Domain{
39 Domain: "www.gophercloud-test-service.com",
40 },
41 },
42 Origins: []os.Origin{
43 os.Origin{
44 Origin: "gophercloud-test-service.com",
45 Port: 80,
46 SSL: false,
47 },
48 },
49 FlavorID: "cdn",
50 }
51 l, err := services.Create(client, createOpts).Extract()
52 th.AssertNoErr(t, err)
53 return l
54}
55
56func testServiceGet(t *testing.T, client *gophercloud.ServiceClient, id string) {
57 s, err := services.Get(client, id).Extract()
58 th.AssertNoErr(t, err)
59 t.Logf("Retrieved service: %+v", *s)
60}
61
62func testServiceUpdate(t *testing.T, client *gophercloud.ServiceClient, id string) {
63 updateOpts := os.UpdateOpts{
64 os.UpdateOpt{
65 Op: os.Add,
66 Path: "/domains/-",
67 Value: map[string]interface{}{
68 "domain": "newDomain.com",
69 "protocol": "http",
70 },
71 },
72 }
73 loc, err := services.Update(client, id, updateOpts).Extract()
74 th.AssertNoErr(t, err)
75 t.Logf("Successfully updated service at location: %s", loc)
76}
77
Jon Perritt9600eb42015-01-21 15:04:14 -070078func testServiceList(t *testing.T, client *gophercloud.ServiceClient) {
Jon Perrittea764ef2015-01-23 10:59:32 -070079 err := services.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
80 serviceList, err := os.ExtractServices(page)
81 th.AssertNoErr(t, err)
82
83 for _, service := range serviceList {
84 t.Logf("Listing service: %+v", service)
85 }
86
87 return true, nil
88 })
89
Jon Perritt9600eb42015-01-21 15:04:14 -070090 th.AssertNoErr(t, err)
Jon Perritt9600eb42015-01-21 15:04:14 -070091}
Jon Perritt9600eb42015-01-21 15:04:14 -070092
93func testServiceDelete(t *testing.T, client *gophercloud.ServiceClient, id string) {
94 err := services.Delete(client, id).ExtractErr()
95 th.AssertNoErr(t, err)
96 t.Logf("Successfully deleted service (%s)", id)
97}