|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cloudstack |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "net/http/httptest" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +func TestListAccounts(t *testing.T) { |
| 30 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + resp := ` |
| 32 | +{ |
| 33 | + "listaccountsresponse": { |
| 34 | + "account": [ |
| 35 | + { |
| 36 | + "accountdetails": { |
| 37 | + "key0": "value0", |
| 38 | + "key1": "value1" |
| 39 | + }, |
| 40 | + "accounttype": 1, |
| 41 | + "cpuavailable": "Unlimited", |
| 42 | + "cpulimit": "Unlimited", |
| 43 | + "cputotal": 1, |
| 44 | + "domain": "ROOT", |
| 45 | + "domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a", |
| 46 | + "groups": [], |
| 47 | + "id": "62f61a73-5f5a-11e5-bc86-0242ac11180a", |
| 48 | + "ipavailable": "Unlimited", |
| 49 | + "iplimit": "Unlimited", |
| 50 | + "iptotal": 1, |
| 51 | + "isdefault": true, |
| 52 | + "memoryavailable": "Unlimited", |
| 53 | + "memorylimit": "Unlimited", |
| 54 | + "memorytotal": 512, |
| 55 | + "name": "admin", |
| 56 | + "networkavailable": "Unlimited", |
| 57 | + "networklimit": "Unlimited", |
| 58 | + "networktotal": 1, |
| 59 | + "primarystorageavailable": "Unlimited", |
| 60 | + "primarystoragelimit": "Unlimited", |
| 61 | + "primarystoragetotal": 2, |
| 62 | + "projectavailable": "Unlimited", |
| 63 | + "projectlimit": "Unlimited", |
| 64 | + "projecttotal": 1, |
| 65 | + "receivedbytes": 100, |
| 66 | + "secondarystorageavailable": "Unlimited", |
| 67 | + "secondarystoragelimit": "Unlimited", |
| 68 | + "secondarystoragetotal": 0, |
| 69 | + "sentbytes": 100, |
| 70 | + "snapshotavailable": "Unlimited", |
| 71 | + "snapshotlimit": "Unlimited", |
| 72 | + "snapshottotal": 0, |
| 73 | + "state": "enabled", |
| 74 | + "templateavailable": "Unlimited", |
| 75 | + "templatelimit": "Unlimited", |
| 76 | + "templatetotal": 0, |
| 77 | + "user": [ |
| 78 | + { |
| 79 | + "account": "admin", |
| 80 | + "accountid": "62f61a73-5f5a-11e5-bc86-0242ac11180a", |
| 81 | + "accounttype": 1, |
| 82 | + "apikey": "BFCyYeY0HhhrvNWC7FBwHTlso52ow3XWZUzNuiv03jmJOQn-5UH5BTVRVheiBnaRfYFE9yOzjwiqkbahq9P-Rw", |
| 83 | + "created": "2015-09-20T05:42:31+0000", |
| 84 | + "domain": "ROOT", |
| 85 | + "domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a", |
| 86 | + "email": "admin@mailprovider.com", |
| 87 | + "firstname": "Admin", |
| 88 | + "id": "62f6394b-5f5a-11e5-bc86-0242ac11180a", |
| 89 | + "iscallerchilddomain": false, |
| 90 | + "isdefault": true, |
| 91 | + "lastname": "User", |
| 92 | + "secretkey": "Em1UPdKTYEDaUxPXKtpqy_1uYluHWU2ZHotPi-VnBi_-MOEUAANoFHg3ycDProluDzYAXmT3YRssk367ylr_4A", |
| 93 | + "state": "enabled", |
| 94 | + "username": "admin" |
| 95 | + } |
| 96 | + ], |
| 97 | + "vmavailable": "Unlimited", |
| 98 | + "vmlimit": "Unlimited", |
| 99 | + "vmrunning": 1, |
| 100 | + "vmtotal": 1, |
| 101 | + "volumeavailable": "Unlimited", |
| 102 | + "volumelimit": "Unlimited", |
| 103 | + "volumetotal": 1, |
| 104 | + "vpcavailable": "Unlimited", |
| 105 | + "vpclimit": "Unlimited", |
| 106 | + "vpctotal": 0 |
| 107 | + } |
| 108 | + ], |
| 109 | + "count": 1 |
| 110 | + } |
| 111 | +} |
| 112 | + ` |
| 113 | + fmt.Fprintln(w, resp) |
| 114 | + })) |
| 115 | + defer server.Close() |
| 116 | + |
| 117 | + client := newClient(server.URL, "APIKEY", "SECRETKEY", true, true) |
| 118 | + p := client.Account.NewListAccountsParams() |
| 119 | + acc, _ := client.Account.ListAccounts(p) |
| 120 | + |
| 121 | + accounts := acc.Accounts |
| 122 | + if len(accounts) != 1 { |
| 123 | + t.Errorf("length: actual %d, expected 1", len(accounts)) |
| 124 | + } |
| 125 | + |
| 126 | + if accounts[0].Name != "admin" { |
| 127 | + t.Errorf("name: actual %s, expected admin", accounts[0].Name) |
| 128 | + } |
| 129 | + |
| 130 | + if len(accounts[0].User) != 1 { |
| 131 | + t.Errorf("length of user: actual %d, expected 1", len(accounts[0].User)) |
| 132 | + } |
| 133 | + |
| 134 | + if accounts[0].User[0].Username != "admin" { |
| 135 | + t.Errorf("username: actual %s, expected admin", accounts[0].User[0].Username) |
| 136 | + } |
| 137 | + |
| 138 | + if accounts[0].Accountdetails["key0"] != "value0" { |
| 139 | + t.Errorf("accountdetails[\"key0\"]: actual %s, expected value0", accounts[0].Accountdetails["key0"]) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestCreateAccounts(t *testing.T) { |
| 144 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 145 | + resp := ` |
| 146 | +{ |
| 147 | + "createaccountresponse": { |
| 148 | + "account": { |
| 149 | + "accountdetails": { |
| 150 | + "key0": "value0", |
| 151 | + "key1": "value1" |
| 152 | + }, |
| 153 | + "accounttype": 2, |
| 154 | + "cpuavailable": "40", |
| 155 | + "cpulimit": "40", |
| 156 | + "cputotal": 0, |
| 157 | + "domain": "ROOT", |
| 158 | + "domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a", |
| 159 | + "groups": [], |
| 160 | + "id": "c9cb9df8-dcd5-44c5-a40d-2e7d266669a6", |
| 161 | + "ipavailable": "20", |
| 162 | + "iplimit": "20", |
| 163 | + "iptotal": 0, |
| 164 | + "isdefault": false, |
| 165 | + "memoryavailable": "40960", |
| 166 | + "memorylimit": "40960", |
| 167 | + "memorytotal": 0, |
| 168 | + "name": "user01", |
| 169 | + "networkavailable": "20", |
| 170 | + "networklimit": "20", |
| 171 | + "networktotal": 0, |
| 172 | + "primarystorageavailable": "200", |
| 173 | + "primarystoragelimit": "200", |
| 174 | + "primarystoragetotal": 0, |
| 175 | + "projectavailable": "Unlimited", |
| 176 | + "projectlimit": "Unlimited", |
| 177 | + "projecttotal": 0, |
| 178 | + "secondarystorageavailable": "400", |
| 179 | + "secondarystoragelimit": "400", |
| 180 | + "secondarystoragetotal": 0, |
| 181 | + "snapshotavailable": "20", |
| 182 | + "snapshotlimit": "20", |
| 183 | + "snapshottotal": 0, |
| 184 | + "state": "enabled", |
| 185 | + "templateavailable": "20", |
| 186 | + "templatelimit": "20", |
| 187 | + "templatetotal": 0, |
| 188 | + "user": [ |
| 189 | + { |
| 190 | + "account": "user01", |
| 191 | + "accountid": "c9cb9df8-dcd5-44c5-a40d-2e7d266669a6", |
| 192 | + "accounttype": 2, |
| 193 | + "created": "2016-07-26T02:17:17+0000", |
| 194 | + "domain": "ROOT", |
| 195 | + "domainid": "62f5fe2e-5f5a-11e5-bc86-0242ac11180a", |
| 196 | + "email": "user01@example.com", |
| 197 | + "firstname": "user01", |
| 198 | + "id": "1d69ac45-a8f7-469c-ab24-d2b0382fa8be", |
| 199 | + "iscallerchilddomain": false, |
| 200 | + "isdefault": false, |
| 201 | + "lastname": "user01", |
| 202 | + "state": "enabled", |
| 203 | + "username": "user01" |
| 204 | + } |
| 205 | + ], |
| 206 | + "vmavailable": "20", |
| 207 | + "vmlimit": "20", |
| 208 | + "vmtotal": 0, |
| 209 | + "volumeavailable": "20", |
| 210 | + "volumelimit": "20", |
| 211 | + "volumetotal": 0, |
| 212 | + "vpcavailable": "20", |
| 213 | + "vpclimit": "20", |
| 214 | + "vpctotal": 0 |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + ` |
| 219 | + fmt.Fprintln(w, resp) |
| 220 | + })) |
| 221 | + defer server.Close() |
| 222 | + |
| 223 | + client := newClient(server.URL, "APIKEY", "SECRETKEY", true, true) |
| 224 | + email := "user01@example.com" |
| 225 | + firstname := "user01" |
| 226 | + lastname := "user01" |
| 227 | + password := "password" |
| 228 | + username := "user01" |
| 229 | + |
| 230 | + p := client.Account.NewCreateAccountParams(email, firstname, lastname, password, username) |
| 231 | + p.SetAccountdetails(map[string]string{"key0": "value0", "key1": "value1"}) |
| 232 | + account, _ := client.Account.CreateAccount(p) |
| 233 | + |
| 234 | + if account.Name != "user01" { |
| 235 | + t.Errorf("name: actual %s, expected user01", account.Name) |
| 236 | + } |
| 237 | + |
| 238 | + if len(account.User) != 1 { |
| 239 | + t.Errorf("length of user: actual %d, expected 1", len(account.User)) |
| 240 | + } |
| 241 | + |
| 242 | + if account.User[0].Username != "user01" { |
| 243 | + t.Errorf("username: actual %s, expected admin", account.User[0].Username) |
| 244 | + } |
| 245 | + |
| 246 | + if account.Accountdetails["key0"] != "value0" { |
| 247 | + t.Errorf("accountdetails[\"key0\"]: actual %s, expected value0", account.Accountdetails["key0"]) |
| 248 | + } |
| 249 | + |
| 250 | + if account.Accountdetails["key1"] != "value1" { |
| 251 | + t.Errorf("accountdetails[\"key1\"]: actual %s, expected value1", account.Accountdetails["key1"]) |
| 252 | + } |
| 253 | +} |
0 commit comments