Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame^] | 12 | const singleInstanceJson = ` |
| 13 | { |
| 14 | "instance": { |
| 15 | "created": "2014-02-13T21:47:13", |
| 16 | "datastore": { |
| 17 | "type": "mysql", |
| 18 | "version": "5.6" |
| 19 | }, |
| 20 | "flavor": { |
| 21 | "id": "1", |
| 22 | "links": [ |
| 23 | { |
| 24 | "href": "https://my-openstack.com/v1.0/1234/flavors/1", |
| 25 | "rel": "self" |
| 26 | }, |
| 27 | { |
| 28 | "href": "https://my-openstack.com/v1.0/1234/flavors/1", |
| 29 | "rel": "bookmark" |
| 30 | } |
| 31 | ] |
| 32 | }, |
| 33 | "links": [ |
| 34 | { |
| 35 | "href": "https://my-openstack.com/v1.0/1234/instances/1", |
| 36 | "rel": "self" |
| 37 | } |
| 38 | ], |
| 39 | "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com", |
| 40 | "id": "d4603f69-ec7e-4e9b-803f-600b9205576f", |
| 41 | "name": "json_rack_instance", |
| 42 | "status": "BUILD", |
| 43 | "updated": "2014-02-13T21:47:13", |
| 44 | "volume": { |
| 45 | "size": 2 |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | ` |
| 50 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 51 | func HandleCreateInstanceSuccessfully(t *testing.T) { |
| 52 | th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) { |
| 53 | th.TestMethod(t, r, "POST") |
| 54 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 55 | |
| 56 | w.Header().Set("Content-Type", "application/json") |
| 57 | w.WriteHeader(http.StatusOK) |
| 58 | |
| 59 | th.TestJSONRequest(t, r, ` |
| 60 | { |
| 61 | "instance": { |
| 62 | "databases": [ |
| 63 | { |
| 64 | "character_set": "utf8", |
| 65 | "collate": "utf8_general_ci", |
| 66 | "name": "sampledb" |
| 67 | }, |
| 68 | { |
| 69 | "name": "nextround" |
| 70 | } |
| 71 | ], |
| 72 | "flavorRef": "1", |
| 73 | "name": "json_rack_instance", |
| 74 | "users": [ |
| 75 | { |
| 76 | "databases": [ |
| 77 | { |
| 78 | "name": "sampledb" |
| 79 | } |
| 80 | ], |
| 81 | "name": "demouser", |
| 82 | "password": "demopassword" |
| 83 | } |
| 84 | ], |
| 85 | "volume": { |
| 86 | "size": 2 |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | `) |
| 91 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame^] | 92 | fmt.Fprintf(w, singleInstanceJson) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 93 | }) |
| 94 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 95 | |
| 96 | func HandleListInstanceSuccessfully(t *testing.T) { |
| 97 | th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) { |
| 98 | th.TestMethod(t, r, "GET") |
| 99 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 100 | |
| 101 | w.Header().Add("Content-Type", "application/json") |
| 102 | |
| 103 | fmt.Fprintf(w, ` |
| 104 | { |
| 105 | "instances": [ |
| 106 | { |
| 107 | "name": "xml_rack_instance", |
| 108 | "status": "ACTIVE", |
| 109 | "volume": { |
| 110 | "size": 2 |
| 111 | }, |
| 112 | "flavor": { |
| 113 | "id": "1", |
| 114 | "links": [ |
| 115 | { |
| 116 | "href": "https://openstack.example.com/v1.0/1234/flavors/1", |
| 117 | "rel": "self" |
| 118 | }, |
| 119 | { |
| 120 | "href": "https://openstack.example.com/flavors/1", |
| 121 | "rel": "bookmark" |
| 122 | } |
| 123 | ] |
| 124 | }, |
| 125 | "id": "8fb081af-f237-44f5-80cc-b46be1840ca9", |
| 126 | "links": [ |
| 127 | { |
| 128 | "href": "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", |
| 129 | "rel": "self" |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 130 | } |
| 131 | ] |
| 132 | } |
| 133 | ] |
| 134 | } |
| 135 | `) |
| 136 | }) |
| 137 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame^] | 138 | |
| 139 | func HandleGetInstanceSuccessfully(t *testing.T, id string) { |
| 140 | th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) { |
| 141 | th.TestMethod(t, r, "GET") |
| 142 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 143 | |
| 144 | w.Header().Add("Content-Type", "application/json") |
| 145 | |
| 146 | fmt.Fprintf(w, singleInstanceJson) |
| 147 | }) |
| 148 | } |