SnapshotService.go 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. //
  2. // Copyright 2016, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package cloudstack
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net/url"
  21. "strconv"
  22. "strings"
  23. )
  24. type CreateSnapshotParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *CreateSnapshotParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["account"]; found {
  33. u.Set("account", v.(string))
  34. }
  35. if v, found := p.p["domainid"]; found {
  36. u.Set("domainid", v.(string))
  37. }
  38. if v, found := p.p["name"]; found {
  39. u.Set("name", v.(string))
  40. }
  41. if v, found := p.p["policyid"]; found {
  42. u.Set("policyid", v.(string))
  43. }
  44. if v, found := p.p["quiescevm"]; found {
  45. vv := strconv.FormatBool(v.(bool))
  46. u.Set("quiescevm", vv)
  47. }
  48. if v, found := p.p["volumeid"]; found {
  49. u.Set("volumeid", v.(string))
  50. }
  51. return u
  52. }
  53. func (p *CreateSnapshotParams) SetAccount(v string) {
  54. if p.p == nil {
  55. p.p = make(map[string]interface{})
  56. }
  57. p.p["account"] = v
  58. return
  59. }
  60. func (p *CreateSnapshotParams) SetDomainid(v string) {
  61. if p.p == nil {
  62. p.p = make(map[string]interface{})
  63. }
  64. p.p["domainid"] = v
  65. return
  66. }
  67. func (p *CreateSnapshotParams) SetName(v string) {
  68. if p.p == nil {
  69. p.p = make(map[string]interface{})
  70. }
  71. p.p["name"] = v
  72. return
  73. }
  74. func (p *CreateSnapshotParams) SetPolicyid(v string) {
  75. if p.p == nil {
  76. p.p = make(map[string]interface{})
  77. }
  78. p.p["policyid"] = v
  79. return
  80. }
  81. func (p *CreateSnapshotParams) SetQuiescevm(v bool) {
  82. if p.p == nil {
  83. p.p = make(map[string]interface{})
  84. }
  85. p.p["quiescevm"] = v
  86. return
  87. }
  88. func (p *CreateSnapshotParams) SetVolumeid(v string) {
  89. if p.p == nil {
  90. p.p = make(map[string]interface{})
  91. }
  92. p.p["volumeid"] = v
  93. return
  94. }
  95. // You should always use this function to get a new CreateSnapshotParams instance,
  96. // as then you are sure you have configured all required params
  97. func (s *SnapshotService) NewCreateSnapshotParams(volumeid string) *CreateSnapshotParams {
  98. p := &CreateSnapshotParams{}
  99. p.p = make(map[string]interface{})
  100. p.p["volumeid"] = volumeid
  101. return p
  102. }
  103. // Creates an instant snapshot of a volume.
  104. func (s *SnapshotService) CreateSnapshot(p *CreateSnapshotParams) (*CreateSnapshotResponse, error) {
  105. resp, err := s.cs.newRequest("createSnapshot", p.toURLValues())
  106. if err != nil {
  107. return nil, err
  108. }
  109. var r CreateSnapshotResponse
  110. if err := json.Unmarshal(resp, &r); err != nil {
  111. return nil, err
  112. }
  113. // If we have a async client, we need to wait for the async result
  114. if s.cs.async {
  115. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  116. if err != nil {
  117. if err == AsyncTimeoutErr {
  118. return &r, err
  119. }
  120. return nil, err
  121. }
  122. b, err = getRawValue(b)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if err := json.Unmarshal(b, &r); err != nil {
  127. return nil, err
  128. }
  129. }
  130. return &r, nil
  131. }
  132. type CreateSnapshotResponse struct {
  133. JobID string `json:"jobid,omitempty"`
  134. Account string `json:"account,omitempty"`
  135. Created string `json:"created,omitempty"`
  136. Domain string `json:"domain,omitempty"`
  137. Domainid string `json:"domainid,omitempty"`
  138. Id string `json:"id,omitempty"`
  139. Intervaltype string `json:"intervaltype,omitempty"`
  140. Name string `json:"name,omitempty"`
  141. Physicalsize int64 `json:"physicalsize,omitempty"`
  142. Project string `json:"project,omitempty"`
  143. Projectid string `json:"projectid,omitempty"`
  144. Revertable bool `json:"revertable,omitempty"`
  145. Snapshottype string `json:"snapshottype,omitempty"`
  146. State string `json:"state,omitempty"`
  147. Tags []struct {
  148. Account string `json:"account,omitempty"`
  149. Customer string `json:"customer,omitempty"`
  150. Domain string `json:"domain,omitempty"`
  151. Domainid string `json:"domainid,omitempty"`
  152. Key string `json:"key,omitempty"`
  153. Project string `json:"project,omitempty"`
  154. Projectid string `json:"projectid,omitempty"`
  155. Resourceid string `json:"resourceid,omitempty"`
  156. Resourcetype string `json:"resourcetype,omitempty"`
  157. Value string `json:"value,omitempty"`
  158. } `json:"tags,omitempty"`
  159. Volumeid string `json:"volumeid,omitempty"`
  160. Volumename string `json:"volumename,omitempty"`
  161. Volumetype string `json:"volumetype,omitempty"`
  162. Zoneid string `json:"zoneid,omitempty"`
  163. }
  164. type ListSnapshotsParams struct {
  165. p map[string]interface{}
  166. }
  167. func (p *ListSnapshotsParams) toURLValues() url.Values {
  168. u := url.Values{}
  169. if p.p == nil {
  170. return u
  171. }
  172. if v, found := p.p["account"]; found {
  173. u.Set("account", v.(string))
  174. }
  175. if v, found := p.p["domainid"]; found {
  176. u.Set("domainid", v.(string))
  177. }
  178. if v, found := p.p["id"]; found {
  179. u.Set("id", v.(string))
  180. }
  181. if v, found := p.p["intervaltype"]; found {
  182. u.Set("intervaltype", v.(string))
  183. }
  184. if v, found := p.p["isrecursive"]; found {
  185. vv := strconv.FormatBool(v.(bool))
  186. u.Set("isrecursive", vv)
  187. }
  188. if v, found := p.p["keyword"]; found {
  189. u.Set("keyword", v.(string))
  190. }
  191. if v, found := p.p["listall"]; found {
  192. vv := strconv.FormatBool(v.(bool))
  193. u.Set("listall", vv)
  194. }
  195. if v, found := p.p["name"]; found {
  196. u.Set("name", v.(string))
  197. }
  198. if v, found := p.p["page"]; found {
  199. vv := strconv.Itoa(v.(int))
  200. u.Set("page", vv)
  201. }
  202. if v, found := p.p["pagesize"]; found {
  203. vv := strconv.Itoa(v.(int))
  204. u.Set("pagesize", vv)
  205. }
  206. if v, found := p.p["projectid"]; found {
  207. u.Set("projectid", v.(string))
  208. }
  209. if v, found := p.p["snapshottype"]; found {
  210. u.Set("snapshottype", v.(string))
  211. }
  212. if v, found := p.p["tags"]; found {
  213. i := 0
  214. for k, vv := range v.(map[string]string) {
  215. u.Set(fmt.Sprintf("tags[%d].key", i), k)
  216. u.Set(fmt.Sprintf("tags[%d].value", i), vv)
  217. i++
  218. }
  219. }
  220. if v, found := p.p["volumeid"]; found {
  221. u.Set("volumeid", v.(string))
  222. }
  223. if v, found := p.p["zoneid"]; found {
  224. u.Set("zoneid", v.(string))
  225. }
  226. return u
  227. }
  228. func (p *ListSnapshotsParams) SetAccount(v string) {
  229. if p.p == nil {
  230. p.p = make(map[string]interface{})
  231. }
  232. p.p["account"] = v
  233. return
  234. }
  235. func (p *ListSnapshotsParams) SetDomainid(v string) {
  236. if p.p == nil {
  237. p.p = make(map[string]interface{})
  238. }
  239. p.p["domainid"] = v
  240. return
  241. }
  242. func (p *ListSnapshotsParams) SetId(v string) {
  243. if p.p == nil {
  244. p.p = make(map[string]interface{})
  245. }
  246. p.p["id"] = v
  247. return
  248. }
  249. func (p *ListSnapshotsParams) SetIntervaltype(v string) {
  250. if p.p == nil {
  251. p.p = make(map[string]interface{})
  252. }
  253. p.p["intervaltype"] = v
  254. return
  255. }
  256. func (p *ListSnapshotsParams) SetIsrecursive(v bool) {
  257. if p.p == nil {
  258. p.p = make(map[string]interface{})
  259. }
  260. p.p["isrecursive"] = v
  261. return
  262. }
  263. func (p *ListSnapshotsParams) SetKeyword(v string) {
  264. if p.p == nil {
  265. p.p = make(map[string]interface{})
  266. }
  267. p.p["keyword"] = v
  268. return
  269. }
  270. func (p *ListSnapshotsParams) SetListall(v bool) {
  271. if p.p == nil {
  272. p.p = make(map[string]interface{})
  273. }
  274. p.p["listall"] = v
  275. return
  276. }
  277. func (p *ListSnapshotsParams) SetName(v string) {
  278. if p.p == nil {
  279. p.p = make(map[string]interface{})
  280. }
  281. p.p["name"] = v
  282. return
  283. }
  284. func (p *ListSnapshotsParams) SetPage(v int) {
  285. if p.p == nil {
  286. p.p = make(map[string]interface{})
  287. }
  288. p.p["page"] = v
  289. return
  290. }
  291. func (p *ListSnapshotsParams) SetPagesize(v int) {
  292. if p.p == nil {
  293. p.p = make(map[string]interface{})
  294. }
  295. p.p["pagesize"] = v
  296. return
  297. }
  298. func (p *ListSnapshotsParams) SetProjectid(v string) {
  299. if p.p == nil {
  300. p.p = make(map[string]interface{})
  301. }
  302. p.p["projectid"] = v
  303. return
  304. }
  305. func (p *ListSnapshotsParams) SetSnapshottype(v string) {
  306. if p.p == nil {
  307. p.p = make(map[string]interface{})
  308. }
  309. p.p["snapshottype"] = v
  310. return
  311. }
  312. func (p *ListSnapshotsParams) SetTags(v map[string]string) {
  313. if p.p == nil {
  314. p.p = make(map[string]interface{})
  315. }
  316. p.p["tags"] = v
  317. return
  318. }
  319. func (p *ListSnapshotsParams) SetVolumeid(v string) {
  320. if p.p == nil {
  321. p.p = make(map[string]interface{})
  322. }
  323. p.p["volumeid"] = v
  324. return
  325. }
  326. func (p *ListSnapshotsParams) SetZoneid(v string) {
  327. if p.p == nil {
  328. p.p = make(map[string]interface{})
  329. }
  330. p.p["zoneid"] = v
  331. return
  332. }
  333. // You should always use this function to get a new ListSnapshotsParams instance,
  334. // as then you are sure you have configured all required params
  335. func (s *SnapshotService) NewListSnapshotsParams() *ListSnapshotsParams {
  336. p := &ListSnapshotsParams{}
  337. p.p = make(map[string]interface{})
  338. return p
  339. }
  340. // This is a courtesy helper function, which in some cases may not work as expected!
  341. func (s *SnapshotService) GetSnapshotID(name string, opts ...OptionFunc) (string, int, error) {
  342. p := &ListSnapshotsParams{}
  343. p.p = make(map[string]interface{})
  344. p.p["name"] = name
  345. for _, fn := range opts {
  346. if err := fn(s.cs, p); err != nil {
  347. return "", -1, err
  348. }
  349. }
  350. l, err := s.ListSnapshots(p)
  351. if err != nil {
  352. return "", -1, err
  353. }
  354. if l.Count == 0 {
  355. return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
  356. }
  357. if l.Count == 1 {
  358. return l.Snapshots[0].Id, l.Count, nil
  359. }
  360. if l.Count > 1 {
  361. for _, v := range l.Snapshots {
  362. if v.Name == name {
  363. return v.Id, l.Count, nil
  364. }
  365. }
  366. }
  367. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l)
  368. }
  369. // This is a courtesy helper function, which in some cases may not work as expected!
  370. func (s *SnapshotService) GetSnapshotByName(name string, opts ...OptionFunc) (*Snapshot, int, error) {
  371. id, count, err := s.GetSnapshotID(name, opts...)
  372. if err != nil {
  373. return nil, count, err
  374. }
  375. r, count, err := s.GetSnapshotByID(id, opts...)
  376. if err != nil {
  377. return nil, count, err
  378. }
  379. return r, count, nil
  380. }
  381. // This is a courtesy helper function, which in some cases may not work as expected!
  382. func (s *SnapshotService) GetSnapshotByID(id string, opts ...OptionFunc) (*Snapshot, int, error) {
  383. p := &ListSnapshotsParams{}
  384. p.p = make(map[string]interface{})
  385. p.p["id"] = id
  386. for _, fn := range opts {
  387. if err := fn(s.cs, p); err != nil {
  388. return nil, -1, err
  389. }
  390. }
  391. l, err := s.ListSnapshots(p)
  392. if err != nil {
  393. if strings.Contains(err.Error(), fmt.Sprintf(
  394. "Invalid parameter id value=%s due to incorrect long value format, "+
  395. "or entity does not exist", id)) {
  396. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  397. }
  398. return nil, -1, err
  399. }
  400. if l.Count == 0 {
  401. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  402. }
  403. if l.Count == 1 {
  404. return l.Snapshots[0], l.Count, nil
  405. }
  406. return nil, l.Count, fmt.Errorf("There is more then one result for Snapshot UUID: %s!", id)
  407. }
  408. // Lists all available snapshots for the account.
  409. func (s *SnapshotService) ListSnapshots(p *ListSnapshotsParams) (*ListSnapshotsResponse, error) {
  410. resp, err := s.cs.newRequest("listSnapshots", p.toURLValues())
  411. if err != nil {
  412. return nil, err
  413. }
  414. var r ListSnapshotsResponse
  415. if err := json.Unmarshal(resp, &r); err != nil {
  416. return nil, err
  417. }
  418. return &r, nil
  419. }
  420. type ListSnapshotsResponse struct {
  421. Count int `json:"count"`
  422. Snapshots []*Snapshot `json:"snapshot"`
  423. }
  424. type Snapshot struct {
  425. Account string `json:"account,omitempty"`
  426. Created string `json:"created,omitempty"`
  427. Domain string `json:"domain,omitempty"`
  428. Domainid string `json:"domainid,omitempty"`
  429. Id string `json:"id,omitempty"`
  430. Intervaltype string `json:"intervaltype,omitempty"`
  431. Name string `json:"name,omitempty"`
  432. Physicalsize int64 `json:"physicalsize,omitempty"`
  433. Project string `json:"project,omitempty"`
  434. Projectid string `json:"projectid,omitempty"`
  435. Revertable bool `json:"revertable,omitempty"`
  436. Snapshottype string `json:"snapshottype,omitempty"`
  437. State string `json:"state,omitempty"`
  438. Tags []struct {
  439. Account string `json:"account,omitempty"`
  440. Customer string `json:"customer,omitempty"`
  441. Domain string `json:"domain,omitempty"`
  442. Domainid string `json:"domainid,omitempty"`
  443. Key string `json:"key,omitempty"`
  444. Project string `json:"project,omitempty"`
  445. Projectid string `json:"projectid,omitempty"`
  446. Resourceid string `json:"resourceid,omitempty"`
  447. Resourcetype string `json:"resourcetype,omitempty"`
  448. Value string `json:"value,omitempty"`
  449. } `json:"tags,omitempty"`
  450. Volumeid string `json:"volumeid,omitempty"`
  451. Volumename string `json:"volumename,omitempty"`
  452. Volumetype string `json:"volumetype,omitempty"`
  453. Zoneid string `json:"zoneid,omitempty"`
  454. }
  455. type DeleteSnapshotParams struct {
  456. p map[string]interface{}
  457. }
  458. func (p *DeleteSnapshotParams) toURLValues() url.Values {
  459. u := url.Values{}
  460. if p.p == nil {
  461. return u
  462. }
  463. if v, found := p.p["id"]; found {
  464. u.Set("id", v.(string))
  465. }
  466. return u
  467. }
  468. func (p *DeleteSnapshotParams) SetId(v string) {
  469. if p.p == nil {
  470. p.p = make(map[string]interface{})
  471. }
  472. p.p["id"] = v
  473. return
  474. }
  475. // You should always use this function to get a new DeleteSnapshotParams instance,
  476. // as then you are sure you have configured all required params
  477. func (s *SnapshotService) NewDeleteSnapshotParams(id string) *DeleteSnapshotParams {
  478. p := &DeleteSnapshotParams{}
  479. p.p = make(map[string]interface{})
  480. p.p["id"] = id
  481. return p
  482. }
  483. // Deletes a snapshot of a disk volume.
  484. func (s *SnapshotService) DeleteSnapshot(p *DeleteSnapshotParams) (*DeleteSnapshotResponse, error) {
  485. resp, err := s.cs.newRequest("deleteSnapshot", p.toURLValues())
  486. if err != nil {
  487. return nil, err
  488. }
  489. var r DeleteSnapshotResponse
  490. if err := json.Unmarshal(resp, &r); err != nil {
  491. return nil, err
  492. }
  493. // If we have a async client, we need to wait for the async result
  494. if s.cs.async {
  495. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  496. if err != nil {
  497. if err == AsyncTimeoutErr {
  498. return &r, err
  499. }
  500. return nil, err
  501. }
  502. if err := json.Unmarshal(b, &r); err != nil {
  503. return nil, err
  504. }
  505. }
  506. return &r, nil
  507. }
  508. type DeleteSnapshotResponse struct {
  509. JobID string `json:"jobid,omitempty"`
  510. Displaytext string `json:"displaytext,omitempty"`
  511. Success bool `json:"success,omitempty"`
  512. }
  513. type CreateSnapshotPolicyParams struct {
  514. p map[string]interface{}
  515. }
  516. func (p *CreateSnapshotPolicyParams) toURLValues() url.Values {
  517. u := url.Values{}
  518. if p.p == nil {
  519. return u
  520. }
  521. if v, found := p.p["fordisplay"]; found {
  522. vv := strconv.FormatBool(v.(bool))
  523. u.Set("fordisplay", vv)
  524. }
  525. if v, found := p.p["intervaltype"]; found {
  526. u.Set("intervaltype", v.(string))
  527. }
  528. if v, found := p.p["maxsnaps"]; found {
  529. vv := strconv.Itoa(v.(int))
  530. u.Set("maxsnaps", vv)
  531. }
  532. if v, found := p.p["schedule"]; found {
  533. u.Set("schedule", v.(string))
  534. }
  535. if v, found := p.p["timezone"]; found {
  536. u.Set("timezone", v.(string))
  537. }
  538. if v, found := p.p["volumeid"]; found {
  539. u.Set("volumeid", v.(string))
  540. }
  541. return u
  542. }
  543. func (p *CreateSnapshotPolicyParams) SetFordisplay(v bool) {
  544. if p.p == nil {
  545. p.p = make(map[string]interface{})
  546. }
  547. p.p["fordisplay"] = v
  548. return
  549. }
  550. func (p *CreateSnapshotPolicyParams) SetIntervaltype(v string) {
  551. if p.p == nil {
  552. p.p = make(map[string]interface{})
  553. }
  554. p.p["intervaltype"] = v
  555. return
  556. }
  557. func (p *CreateSnapshotPolicyParams) SetMaxsnaps(v int) {
  558. if p.p == nil {
  559. p.p = make(map[string]interface{})
  560. }
  561. p.p["maxsnaps"] = v
  562. return
  563. }
  564. func (p *CreateSnapshotPolicyParams) SetSchedule(v string) {
  565. if p.p == nil {
  566. p.p = make(map[string]interface{})
  567. }
  568. p.p["schedule"] = v
  569. return
  570. }
  571. func (p *CreateSnapshotPolicyParams) SetTimezone(v string) {
  572. if p.p == nil {
  573. p.p = make(map[string]interface{})
  574. }
  575. p.p["timezone"] = v
  576. return
  577. }
  578. func (p *CreateSnapshotPolicyParams) SetVolumeid(v string) {
  579. if p.p == nil {
  580. p.p = make(map[string]interface{})
  581. }
  582. p.p["volumeid"] = v
  583. return
  584. }
  585. // You should always use this function to get a new CreateSnapshotPolicyParams instance,
  586. // as then you are sure you have configured all required params
  587. func (s *SnapshotService) NewCreateSnapshotPolicyParams(intervaltype string, maxsnaps int, schedule string, timezone string, volumeid string) *CreateSnapshotPolicyParams {
  588. p := &CreateSnapshotPolicyParams{}
  589. p.p = make(map[string]interface{})
  590. p.p["intervaltype"] = intervaltype
  591. p.p["maxsnaps"] = maxsnaps
  592. p.p["schedule"] = schedule
  593. p.p["timezone"] = timezone
  594. p.p["volumeid"] = volumeid
  595. return p
  596. }
  597. // Creates a snapshot policy for the account.
  598. func (s *SnapshotService) CreateSnapshotPolicy(p *CreateSnapshotPolicyParams) (*CreateSnapshotPolicyResponse, error) {
  599. resp, err := s.cs.newRequest("createSnapshotPolicy", p.toURLValues())
  600. if err != nil {
  601. return nil, err
  602. }
  603. var r CreateSnapshotPolicyResponse
  604. if err := json.Unmarshal(resp, &r); err != nil {
  605. return nil, err
  606. }
  607. return &r, nil
  608. }
  609. type CreateSnapshotPolicyResponse struct {
  610. Fordisplay bool `json:"fordisplay,omitempty"`
  611. Id string `json:"id,omitempty"`
  612. Intervaltype int `json:"intervaltype,omitempty"`
  613. Maxsnaps int `json:"maxsnaps,omitempty"`
  614. Schedule string `json:"schedule,omitempty"`
  615. Timezone string `json:"timezone,omitempty"`
  616. Volumeid string `json:"volumeid,omitempty"`
  617. }
  618. type UpdateSnapshotPolicyParams struct {
  619. p map[string]interface{}
  620. }
  621. func (p *UpdateSnapshotPolicyParams) toURLValues() url.Values {
  622. u := url.Values{}
  623. if p.p == nil {
  624. return u
  625. }
  626. if v, found := p.p["customid"]; found {
  627. u.Set("customid", v.(string))
  628. }
  629. if v, found := p.p["fordisplay"]; found {
  630. vv := strconv.FormatBool(v.(bool))
  631. u.Set("fordisplay", vv)
  632. }
  633. if v, found := p.p["id"]; found {
  634. u.Set("id", v.(string))
  635. }
  636. return u
  637. }
  638. func (p *UpdateSnapshotPolicyParams) SetCustomid(v string) {
  639. if p.p == nil {
  640. p.p = make(map[string]interface{})
  641. }
  642. p.p["customid"] = v
  643. return
  644. }
  645. func (p *UpdateSnapshotPolicyParams) SetFordisplay(v bool) {
  646. if p.p == nil {
  647. p.p = make(map[string]interface{})
  648. }
  649. p.p["fordisplay"] = v
  650. return
  651. }
  652. func (p *UpdateSnapshotPolicyParams) SetId(v string) {
  653. if p.p == nil {
  654. p.p = make(map[string]interface{})
  655. }
  656. p.p["id"] = v
  657. return
  658. }
  659. // You should always use this function to get a new UpdateSnapshotPolicyParams instance,
  660. // as then you are sure you have configured all required params
  661. func (s *SnapshotService) NewUpdateSnapshotPolicyParams() *UpdateSnapshotPolicyParams {
  662. p := &UpdateSnapshotPolicyParams{}
  663. p.p = make(map[string]interface{})
  664. return p
  665. }
  666. // Updates the snapshot policy.
  667. func (s *SnapshotService) UpdateSnapshotPolicy(p *UpdateSnapshotPolicyParams) (*UpdateSnapshotPolicyResponse, error) {
  668. resp, err := s.cs.newRequest("updateSnapshotPolicy", p.toURLValues())
  669. if err != nil {
  670. return nil, err
  671. }
  672. var r UpdateSnapshotPolicyResponse
  673. if err := json.Unmarshal(resp, &r); err != nil {
  674. return nil, err
  675. }
  676. // If we have a async client, we need to wait for the async result
  677. if s.cs.async {
  678. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  679. if err != nil {
  680. if err == AsyncTimeoutErr {
  681. return &r, err
  682. }
  683. return nil, err
  684. }
  685. b, err = getRawValue(b)
  686. if err != nil {
  687. return nil, err
  688. }
  689. if err := json.Unmarshal(b, &r); err != nil {
  690. return nil, err
  691. }
  692. }
  693. return &r, nil
  694. }
  695. type UpdateSnapshotPolicyResponse struct {
  696. JobID string `json:"jobid,omitempty"`
  697. Fordisplay bool `json:"fordisplay,omitempty"`
  698. Id string `json:"id,omitempty"`
  699. Intervaltype int `json:"intervaltype,omitempty"`
  700. Maxsnaps int `json:"maxsnaps,omitempty"`
  701. Schedule string `json:"schedule,omitempty"`
  702. Timezone string `json:"timezone,omitempty"`
  703. Volumeid string `json:"volumeid,omitempty"`
  704. }
  705. type DeleteSnapshotPoliciesParams struct {
  706. p map[string]interface{}
  707. }
  708. func (p *DeleteSnapshotPoliciesParams) toURLValues() url.Values {
  709. u := url.Values{}
  710. if p.p == nil {
  711. return u
  712. }
  713. if v, found := p.p["id"]; found {
  714. u.Set("id", v.(string))
  715. }
  716. if v, found := p.p["ids"]; found {
  717. vv := strings.Join(v.([]string), ",")
  718. u.Set("ids", vv)
  719. }
  720. return u
  721. }
  722. func (p *DeleteSnapshotPoliciesParams) SetId(v string) {
  723. if p.p == nil {
  724. p.p = make(map[string]interface{})
  725. }
  726. p.p["id"] = v
  727. return
  728. }
  729. func (p *DeleteSnapshotPoliciesParams) SetIds(v []string) {
  730. if p.p == nil {
  731. p.p = make(map[string]interface{})
  732. }
  733. p.p["ids"] = v
  734. return
  735. }
  736. // You should always use this function to get a new DeleteSnapshotPoliciesParams instance,
  737. // as then you are sure you have configured all required params
  738. func (s *SnapshotService) NewDeleteSnapshotPoliciesParams() *DeleteSnapshotPoliciesParams {
  739. p := &DeleteSnapshotPoliciesParams{}
  740. p.p = make(map[string]interface{})
  741. return p
  742. }
  743. // Deletes snapshot policies for the account.
  744. func (s *SnapshotService) DeleteSnapshotPolicies(p *DeleteSnapshotPoliciesParams) (*DeleteSnapshotPoliciesResponse, error) {
  745. resp, err := s.cs.newRequest("deleteSnapshotPolicies", p.toURLValues())
  746. if err != nil {
  747. return nil, err
  748. }
  749. var r DeleteSnapshotPoliciesResponse
  750. if err := json.Unmarshal(resp, &r); err != nil {
  751. return nil, err
  752. }
  753. return &r, nil
  754. }
  755. type DeleteSnapshotPoliciesResponse struct {
  756. Displaytext string `json:"displaytext,omitempty"`
  757. Success string `json:"success,omitempty"`
  758. }
  759. type ListSnapshotPoliciesParams struct {
  760. p map[string]interface{}
  761. }
  762. func (p *ListSnapshotPoliciesParams) toURLValues() url.Values {
  763. u := url.Values{}
  764. if p.p == nil {
  765. return u
  766. }
  767. if v, found := p.p["fordisplay"]; found {
  768. vv := strconv.FormatBool(v.(bool))
  769. u.Set("fordisplay", vv)
  770. }
  771. if v, found := p.p["id"]; found {
  772. u.Set("id", v.(string))
  773. }
  774. if v, found := p.p["keyword"]; found {
  775. u.Set("keyword", v.(string))
  776. }
  777. if v, found := p.p["page"]; found {
  778. vv := strconv.Itoa(v.(int))
  779. u.Set("page", vv)
  780. }
  781. if v, found := p.p["pagesize"]; found {
  782. vv := strconv.Itoa(v.(int))
  783. u.Set("pagesize", vv)
  784. }
  785. if v, found := p.p["volumeid"]; found {
  786. u.Set("volumeid", v.(string))
  787. }
  788. return u
  789. }
  790. func (p *ListSnapshotPoliciesParams) SetFordisplay(v bool) {
  791. if p.p == nil {
  792. p.p = make(map[string]interface{})
  793. }
  794. p.p["fordisplay"] = v
  795. return
  796. }
  797. func (p *ListSnapshotPoliciesParams) SetId(v string) {
  798. if p.p == nil {
  799. p.p = make(map[string]interface{})
  800. }
  801. p.p["id"] = v
  802. return
  803. }
  804. func (p *ListSnapshotPoliciesParams) SetKeyword(v string) {
  805. if p.p == nil {
  806. p.p = make(map[string]interface{})
  807. }
  808. p.p["keyword"] = v
  809. return
  810. }
  811. func (p *ListSnapshotPoliciesParams) SetPage(v int) {
  812. if p.p == nil {
  813. p.p = make(map[string]interface{})
  814. }
  815. p.p["page"] = v
  816. return
  817. }
  818. func (p *ListSnapshotPoliciesParams) SetPagesize(v int) {
  819. if p.p == nil {
  820. p.p = make(map[string]interface{})
  821. }
  822. p.p["pagesize"] = v
  823. return
  824. }
  825. func (p *ListSnapshotPoliciesParams) SetVolumeid(v string) {
  826. if p.p == nil {
  827. p.p = make(map[string]interface{})
  828. }
  829. p.p["volumeid"] = v
  830. return
  831. }
  832. // You should always use this function to get a new ListSnapshotPoliciesParams instance,
  833. // as then you are sure you have configured all required params
  834. func (s *SnapshotService) NewListSnapshotPoliciesParams() *ListSnapshotPoliciesParams {
  835. p := &ListSnapshotPoliciesParams{}
  836. p.p = make(map[string]interface{})
  837. return p
  838. }
  839. // This is a courtesy helper function, which in some cases may not work as expected!
  840. func (s *SnapshotService) GetSnapshotPolicyByID(id string, opts ...OptionFunc) (*SnapshotPolicy, int, error) {
  841. p := &ListSnapshotPoliciesParams{}
  842. p.p = make(map[string]interface{})
  843. p.p["id"] = id
  844. for _, fn := range opts {
  845. if err := fn(s.cs, p); err != nil {
  846. return nil, -1, err
  847. }
  848. }
  849. l, err := s.ListSnapshotPolicies(p)
  850. if err != nil {
  851. if strings.Contains(err.Error(), fmt.Sprintf(
  852. "Invalid parameter id value=%s due to incorrect long value format, "+
  853. "or entity does not exist", id)) {
  854. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  855. }
  856. return nil, -1, err
  857. }
  858. if l.Count == 0 {
  859. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  860. }
  861. if l.Count == 1 {
  862. return l.SnapshotPolicies[0], l.Count, nil
  863. }
  864. return nil, l.Count, fmt.Errorf("There is more then one result for SnapshotPolicy UUID: %s!", id)
  865. }
  866. // Lists snapshot policies.
  867. func (s *SnapshotService) ListSnapshotPolicies(p *ListSnapshotPoliciesParams) (*ListSnapshotPoliciesResponse, error) {
  868. resp, err := s.cs.newRequest("listSnapshotPolicies", p.toURLValues())
  869. if err != nil {
  870. return nil, err
  871. }
  872. var r ListSnapshotPoliciesResponse
  873. if err := json.Unmarshal(resp, &r); err != nil {
  874. return nil, err
  875. }
  876. return &r, nil
  877. }
  878. type ListSnapshotPoliciesResponse struct {
  879. Count int `json:"count"`
  880. SnapshotPolicies []*SnapshotPolicy `json:"snapshotpolicy"`
  881. }
  882. type SnapshotPolicy struct {
  883. Fordisplay bool `json:"fordisplay,omitempty"`
  884. Id string `json:"id,omitempty"`
  885. Intervaltype int `json:"intervaltype,omitempty"`
  886. Maxsnaps int `json:"maxsnaps,omitempty"`
  887. Schedule string `json:"schedule,omitempty"`
  888. Timezone string `json:"timezone,omitempty"`
  889. Volumeid string `json:"volumeid,omitempty"`
  890. }
  891. type RevertSnapshotParams struct {
  892. p map[string]interface{}
  893. }
  894. func (p *RevertSnapshotParams) toURLValues() url.Values {
  895. u := url.Values{}
  896. if p.p == nil {
  897. return u
  898. }
  899. if v, found := p.p["id"]; found {
  900. u.Set("id", v.(string))
  901. }
  902. return u
  903. }
  904. func (p *RevertSnapshotParams) SetId(v string) {
  905. if p.p == nil {
  906. p.p = make(map[string]interface{})
  907. }
  908. p.p["id"] = v
  909. return
  910. }
  911. // You should always use this function to get a new RevertSnapshotParams instance,
  912. // as then you are sure you have configured all required params
  913. func (s *SnapshotService) NewRevertSnapshotParams(id string) *RevertSnapshotParams {
  914. p := &RevertSnapshotParams{}
  915. p.p = make(map[string]interface{})
  916. p.p["id"] = id
  917. return p
  918. }
  919. // revert a volume snapshot.
  920. func (s *SnapshotService) RevertSnapshot(p *RevertSnapshotParams) (*RevertSnapshotResponse, error) {
  921. resp, err := s.cs.newRequest("revertSnapshot", p.toURLValues())
  922. if err != nil {
  923. return nil, err
  924. }
  925. var r RevertSnapshotResponse
  926. if err := json.Unmarshal(resp, &r); err != nil {
  927. return nil, err
  928. }
  929. // If we have a async client, we need to wait for the async result
  930. if s.cs.async {
  931. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  932. if err != nil {
  933. if err == AsyncTimeoutErr {
  934. return &r, err
  935. }
  936. return nil, err
  937. }
  938. b, err = getRawValue(b)
  939. if err != nil {
  940. return nil, err
  941. }
  942. if err := json.Unmarshal(b, &r); err != nil {
  943. return nil, err
  944. }
  945. }
  946. return &r, nil
  947. }
  948. type RevertSnapshotResponse struct {
  949. JobID string `json:"jobid,omitempty"`
  950. Account string `json:"account,omitempty"`
  951. Created string `json:"created,omitempty"`
  952. Domain string `json:"domain,omitempty"`
  953. Domainid string `json:"domainid,omitempty"`
  954. Id string `json:"id,omitempty"`
  955. Intervaltype string `json:"intervaltype,omitempty"`
  956. Name string `json:"name,omitempty"`
  957. Physicalsize int64 `json:"physicalsize,omitempty"`
  958. Project string `json:"project,omitempty"`
  959. Projectid string `json:"projectid,omitempty"`
  960. Revertable bool `json:"revertable,omitempty"`
  961. Snapshottype string `json:"snapshottype,omitempty"`
  962. State string `json:"state,omitempty"`
  963. Tags []struct {
  964. Account string `json:"account,omitempty"`
  965. Customer string `json:"customer,omitempty"`
  966. Domain string `json:"domain,omitempty"`
  967. Domainid string `json:"domainid,omitempty"`
  968. Key string `json:"key,omitempty"`
  969. Project string `json:"project,omitempty"`
  970. Projectid string `json:"projectid,omitempty"`
  971. Resourceid string `json:"resourceid,omitempty"`
  972. Resourcetype string `json:"resourcetype,omitempty"`
  973. Value string `json:"value,omitempty"`
  974. } `json:"tags,omitempty"`
  975. Volumeid string `json:"volumeid,omitempty"`
  976. Volumename string `json:"volumename,omitempty"`
  977. Volumetype string `json:"volumetype,omitempty"`
  978. Zoneid string `json:"zoneid,omitempty"`
  979. }
  980. type ListVMSnapshotParams struct {
  981. p map[string]interface{}
  982. }
  983. func (p *ListVMSnapshotParams) toURLValues() url.Values {
  984. u := url.Values{}
  985. if p.p == nil {
  986. return u
  987. }
  988. if v, found := p.p["account"]; found {
  989. u.Set("account", v.(string))
  990. }
  991. if v, found := p.p["domainid"]; found {
  992. u.Set("domainid", v.(string))
  993. }
  994. if v, found := p.p["isrecursive"]; found {
  995. vv := strconv.FormatBool(v.(bool))
  996. u.Set("isrecursive", vv)
  997. }
  998. if v, found := p.p["keyword"]; found {
  999. u.Set("keyword", v.(string))
  1000. }
  1001. if v, found := p.p["listall"]; found {
  1002. vv := strconv.FormatBool(v.(bool))
  1003. u.Set("listall", vv)
  1004. }
  1005. if v, found := p.p["name"]; found {
  1006. u.Set("name", v.(string))
  1007. }
  1008. if v, found := p.p["page"]; found {
  1009. vv := strconv.Itoa(v.(int))
  1010. u.Set("page", vv)
  1011. }
  1012. if v, found := p.p["pagesize"]; found {
  1013. vv := strconv.Itoa(v.(int))
  1014. u.Set("pagesize", vv)
  1015. }
  1016. if v, found := p.p["projectid"]; found {
  1017. u.Set("projectid", v.(string))
  1018. }
  1019. if v, found := p.p["state"]; found {
  1020. u.Set("state", v.(string))
  1021. }
  1022. if v, found := p.p["tags"]; found {
  1023. i := 0
  1024. for k, vv := range v.(map[string]string) {
  1025. u.Set(fmt.Sprintf("tags[%d].key", i), k)
  1026. u.Set(fmt.Sprintf("tags[%d].value", i), vv)
  1027. i++
  1028. }
  1029. }
  1030. if v, found := p.p["virtualmachineid"]; found {
  1031. u.Set("virtualmachineid", v.(string))
  1032. }
  1033. if v, found := p.p["vmsnapshotid"]; found {
  1034. u.Set("vmsnapshotid", v.(string))
  1035. }
  1036. return u
  1037. }
  1038. func (p *ListVMSnapshotParams) SetAccount(v string) {
  1039. if p.p == nil {
  1040. p.p = make(map[string]interface{})
  1041. }
  1042. p.p["account"] = v
  1043. return
  1044. }
  1045. func (p *ListVMSnapshotParams) SetDomainid(v string) {
  1046. if p.p == nil {
  1047. p.p = make(map[string]interface{})
  1048. }
  1049. p.p["domainid"] = v
  1050. return
  1051. }
  1052. func (p *ListVMSnapshotParams) SetIsrecursive(v bool) {
  1053. if p.p == nil {
  1054. p.p = make(map[string]interface{})
  1055. }
  1056. p.p["isrecursive"] = v
  1057. return
  1058. }
  1059. func (p *ListVMSnapshotParams) SetKeyword(v string) {
  1060. if p.p == nil {
  1061. p.p = make(map[string]interface{})
  1062. }
  1063. p.p["keyword"] = v
  1064. return
  1065. }
  1066. func (p *ListVMSnapshotParams) SetListall(v bool) {
  1067. if p.p == nil {
  1068. p.p = make(map[string]interface{})
  1069. }
  1070. p.p["listall"] = v
  1071. return
  1072. }
  1073. func (p *ListVMSnapshotParams) SetName(v string) {
  1074. if p.p == nil {
  1075. p.p = make(map[string]interface{})
  1076. }
  1077. p.p["name"] = v
  1078. return
  1079. }
  1080. func (p *ListVMSnapshotParams) SetPage(v int) {
  1081. if p.p == nil {
  1082. p.p = make(map[string]interface{})
  1083. }
  1084. p.p["page"] = v
  1085. return
  1086. }
  1087. func (p *ListVMSnapshotParams) SetPagesize(v int) {
  1088. if p.p == nil {
  1089. p.p = make(map[string]interface{})
  1090. }
  1091. p.p["pagesize"] = v
  1092. return
  1093. }
  1094. func (p *ListVMSnapshotParams) SetProjectid(v string) {
  1095. if p.p == nil {
  1096. p.p = make(map[string]interface{})
  1097. }
  1098. p.p["projectid"] = v
  1099. return
  1100. }
  1101. func (p *ListVMSnapshotParams) SetState(v string) {
  1102. if p.p == nil {
  1103. p.p = make(map[string]interface{})
  1104. }
  1105. p.p["state"] = v
  1106. return
  1107. }
  1108. func (p *ListVMSnapshotParams) SetTags(v map[string]string) {
  1109. if p.p == nil {
  1110. p.p = make(map[string]interface{})
  1111. }
  1112. p.p["tags"] = v
  1113. return
  1114. }
  1115. func (p *ListVMSnapshotParams) SetVirtualmachineid(v string) {
  1116. if p.p == nil {
  1117. p.p = make(map[string]interface{})
  1118. }
  1119. p.p["virtualmachineid"] = v
  1120. return
  1121. }
  1122. func (p *ListVMSnapshotParams) SetVmsnapshotid(v string) {
  1123. if p.p == nil {
  1124. p.p = make(map[string]interface{})
  1125. }
  1126. p.p["vmsnapshotid"] = v
  1127. return
  1128. }
  1129. // You should always use this function to get a new ListVMSnapshotParams instance,
  1130. // as then you are sure you have configured all required params
  1131. func (s *SnapshotService) NewListVMSnapshotParams() *ListVMSnapshotParams {
  1132. p := &ListVMSnapshotParams{}
  1133. p.p = make(map[string]interface{})
  1134. return p
  1135. }
  1136. // This is a courtesy helper function, which in some cases may not work as expected!
  1137. func (s *SnapshotService) GetVMSnapshotID(name string, opts ...OptionFunc) (string, int, error) {
  1138. p := &ListVMSnapshotParams{}
  1139. p.p = make(map[string]interface{})
  1140. p.p["name"] = name
  1141. for _, fn := range opts {
  1142. if err := fn(s.cs, p); err != nil {
  1143. return "", -1, err
  1144. }
  1145. }
  1146. l, err := s.ListVMSnapshot(p)
  1147. if err != nil {
  1148. return "", -1, err
  1149. }
  1150. if l.Count == 0 {
  1151. return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
  1152. }
  1153. if l.Count == 1 {
  1154. return l.VMSnapshot[0].Id, l.Count, nil
  1155. }
  1156. if l.Count > 1 {
  1157. for _, v := range l.VMSnapshot {
  1158. if v.Name == name {
  1159. return v.Id, l.Count, nil
  1160. }
  1161. }
  1162. }
  1163. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l)
  1164. }
  1165. // List virtual machine snapshot by conditions
  1166. func (s *SnapshotService) ListVMSnapshot(p *ListVMSnapshotParams) (*ListVMSnapshotResponse, error) {
  1167. resp, err := s.cs.newRequest("listVMSnapshot", p.toURLValues())
  1168. if err != nil {
  1169. return nil, err
  1170. }
  1171. var r ListVMSnapshotResponse
  1172. if err := json.Unmarshal(resp, &r); err != nil {
  1173. return nil, err
  1174. }
  1175. return &r, nil
  1176. }
  1177. type ListVMSnapshotResponse struct {
  1178. Count int `json:"count"`
  1179. VMSnapshot []*VMSnapshot `json:"vmsnapshot"`
  1180. }
  1181. type VMSnapshot struct {
  1182. Account string `json:"account,omitempty"`
  1183. Created string `json:"created,omitempty"`
  1184. Current bool `json:"current,omitempty"`
  1185. Description string `json:"description,omitempty"`
  1186. Displayname string `json:"displayname,omitempty"`
  1187. Domain string `json:"domain,omitempty"`
  1188. Domainid string `json:"domainid,omitempty"`
  1189. Id string `json:"id,omitempty"`
  1190. Name string `json:"name,omitempty"`
  1191. Parent string `json:"parent,omitempty"`
  1192. ParentName string `json:"parentName,omitempty"`
  1193. Project string `json:"project,omitempty"`
  1194. Projectid string `json:"projectid,omitempty"`
  1195. State string `json:"state,omitempty"`
  1196. Type string `json:"type,omitempty"`
  1197. Virtualmachineid string `json:"virtualmachineid,omitempty"`
  1198. Zoneid string `json:"zoneid,omitempty"`
  1199. }
  1200. type CreateVMSnapshotParams struct {
  1201. p map[string]interface{}
  1202. }
  1203. func (p *CreateVMSnapshotParams) toURLValues() url.Values {
  1204. u := url.Values{}
  1205. if p.p == nil {
  1206. return u
  1207. }
  1208. if v, found := p.p["description"]; found {
  1209. u.Set("description", v.(string))
  1210. }
  1211. if v, found := p.p["name"]; found {
  1212. u.Set("name", v.(string))
  1213. }
  1214. if v, found := p.p["quiescevm"]; found {
  1215. vv := strconv.FormatBool(v.(bool))
  1216. u.Set("quiescevm", vv)
  1217. }
  1218. if v, found := p.p["snapshotmemory"]; found {
  1219. vv := strconv.FormatBool(v.(bool))
  1220. u.Set("snapshotmemory", vv)
  1221. }
  1222. if v, found := p.p["virtualmachineid"]; found {
  1223. u.Set("virtualmachineid", v.(string))
  1224. }
  1225. return u
  1226. }
  1227. func (p *CreateVMSnapshotParams) SetDescription(v string) {
  1228. if p.p == nil {
  1229. p.p = make(map[string]interface{})
  1230. }
  1231. p.p["description"] = v
  1232. return
  1233. }
  1234. func (p *CreateVMSnapshotParams) SetName(v string) {
  1235. if p.p == nil {
  1236. p.p = make(map[string]interface{})
  1237. }
  1238. p.p["name"] = v
  1239. return
  1240. }
  1241. func (p *CreateVMSnapshotParams) SetQuiescevm(v bool) {
  1242. if p.p == nil {
  1243. p.p = make(map[string]interface{})
  1244. }
  1245. p.p["quiescevm"] = v
  1246. return
  1247. }
  1248. func (p *CreateVMSnapshotParams) SetSnapshotmemory(v bool) {
  1249. if p.p == nil {
  1250. p.p = make(map[string]interface{})
  1251. }
  1252. p.p["snapshotmemory"] = v
  1253. return
  1254. }
  1255. func (p *CreateVMSnapshotParams) SetVirtualmachineid(v string) {
  1256. if p.p == nil {
  1257. p.p = make(map[string]interface{})
  1258. }
  1259. p.p["virtualmachineid"] = v
  1260. return
  1261. }
  1262. // You should always use this function to get a new CreateVMSnapshotParams instance,
  1263. // as then you are sure you have configured all required params
  1264. func (s *SnapshotService) NewCreateVMSnapshotParams(virtualmachineid string) *CreateVMSnapshotParams {
  1265. p := &CreateVMSnapshotParams{}
  1266. p.p = make(map[string]interface{})
  1267. p.p["virtualmachineid"] = virtualmachineid
  1268. return p
  1269. }
  1270. // Creates snapshot for a vm.
  1271. func (s *SnapshotService) CreateVMSnapshot(p *CreateVMSnapshotParams) (*CreateVMSnapshotResponse, error) {
  1272. resp, err := s.cs.newRequest("createVMSnapshot", p.toURLValues())
  1273. if err != nil {
  1274. return nil, err
  1275. }
  1276. var r CreateVMSnapshotResponse
  1277. if err := json.Unmarshal(resp, &r); err != nil {
  1278. return nil, err
  1279. }
  1280. // If we have a async client, we need to wait for the async result
  1281. if s.cs.async {
  1282. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  1283. if err != nil {
  1284. if err == AsyncTimeoutErr {
  1285. return &r, err
  1286. }
  1287. return nil, err
  1288. }
  1289. b, err = getRawValue(b)
  1290. if err != nil {
  1291. return nil, err
  1292. }
  1293. if err := json.Unmarshal(b, &r); err != nil {
  1294. return nil, err
  1295. }
  1296. }
  1297. return &r, nil
  1298. }
  1299. type CreateVMSnapshotResponse struct {
  1300. JobID string `json:"jobid,omitempty"`
  1301. Account string `json:"account,omitempty"`
  1302. Created string `json:"created,omitempty"`
  1303. Current bool `json:"current,omitempty"`
  1304. Description string `json:"description,omitempty"`
  1305. Displayname string `json:"displayname,omitempty"`
  1306. Domain string `json:"domain,omitempty"`
  1307. Domainid string `json:"domainid,omitempty"`
  1308. Id string `json:"id,omitempty"`
  1309. Name string `json:"name,omitempty"`
  1310. Parent string `json:"parent,omitempty"`
  1311. ParentName string `json:"parentName,omitempty"`
  1312. Project string `json:"project,omitempty"`
  1313. Projectid string `json:"projectid,omitempty"`
  1314. State string `json:"state,omitempty"`
  1315. Type string `json:"type,omitempty"`
  1316. Virtualmachineid string `json:"virtualmachineid,omitempty"`
  1317. Zoneid string `json:"zoneid,omitempty"`
  1318. }
  1319. type DeleteVMSnapshotParams struct {
  1320. p map[string]interface{}
  1321. }
  1322. func (p *DeleteVMSnapshotParams) toURLValues() url.Values {
  1323. u := url.Values{}
  1324. if p.p == nil {
  1325. return u
  1326. }
  1327. if v, found := p.p["vmsnapshotid"]; found {
  1328. u.Set("vmsnapshotid", v.(string))
  1329. }
  1330. return u
  1331. }
  1332. func (p *DeleteVMSnapshotParams) SetVmsnapshotid(v string) {
  1333. if p.p == nil {
  1334. p.p = make(map[string]interface{})
  1335. }
  1336. p.p["vmsnapshotid"] = v
  1337. return
  1338. }
  1339. // You should always use this function to get a new DeleteVMSnapshotParams instance,
  1340. // as then you are sure you have configured all required params
  1341. func (s *SnapshotService) NewDeleteVMSnapshotParams(vmsnapshotid string) *DeleteVMSnapshotParams {
  1342. p := &DeleteVMSnapshotParams{}
  1343. p.p = make(map[string]interface{})
  1344. p.p["vmsnapshotid"] = vmsnapshotid
  1345. return p
  1346. }
  1347. // Deletes a vmsnapshot.
  1348. func (s *SnapshotService) DeleteVMSnapshot(p *DeleteVMSnapshotParams) (*DeleteVMSnapshotResponse, error) {
  1349. resp, err := s.cs.newRequest("deleteVMSnapshot", p.toURLValues())
  1350. if err != nil {
  1351. return nil, err
  1352. }
  1353. var r DeleteVMSnapshotResponse
  1354. if err := json.Unmarshal(resp, &r); err != nil {
  1355. return nil, err
  1356. }
  1357. // If we have a async client, we need to wait for the async result
  1358. if s.cs.async {
  1359. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  1360. if err != nil {
  1361. if err == AsyncTimeoutErr {
  1362. return &r, err
  1363. }
  1364. return nil, err
  1365. }
  1366. if err := json.Unmarshal(b, &r); err != nil {
  1367. return nil, err
  1368. }
  1369. }
  1370. return &r, nil
  1371. }
  1372. type DeleteVMSnapshotResponse struct {
  1373. JobID string `json:"jobid,omitempty"`
  1374. Displaytext string `json:"displaytext,omitempty"`
  1375. Success bool `json:"success,omitempty"`
  1376. }
  1377. type RevertToVMSnapshotParams struct {
  1378. p map[string]interface{}
  1379. }
  1380. func (p *RevertToVMSnapshotParams) toURLValues() url.Values {
  1381. u := url.Values{}
  1382. if p.p == nil {
  1383. return u
  1384. }
  1385. if v, found := p.p["vmsnapshotid"]; found {
  1386. u.Set("vmsnapshotid", v.(string))
  1387. }
  1388. return u
  1389. }
  1390. func (p *RevertToVMSnapshotParams) SetVmsnapshotid(v string) {
  1391. if p.p == nil {
  1392. p.p = make(map[string]interface{})
  1393. }
  1394. p.p["vmsnapshotid"] = v
  1395. return
  1396. }
  1397. // You should always use this function to get a new RevertToVMSnapshotParams instance,
  1398. // as then you are sure you have configured all required params
  1399. func (s *SnapshotService) NewRevertToVMSnapshotParams(vmsnapshotid string) *RevertToVMSnapshotParams {
  1400. p := &RevertToVMSnapshotParams{}
  1401. p.p = make(map[string]interface{})
  1402. p.p["vmsnapshotid"] = vmsnapshotid
  1403. return p
  1404. }
  1405. // Revert VM from a vmsnapshot.
  1406. func (s *SnapshotService) RevertToVMSnapshot(p *RevertToVMSnapshotParams) (*RevertToVMSnapshotResponse, error) {
  1407. resp, err := s.cs.newRequest("revertToVMSnapshot", p.toURLValues())
  1408. if err != nil {
  1409. return nil, err
  1410. }
  1411. var r RevertToVMSnapshotResponse
  1412. if err := json.Unmarshal(resp, &r); err != nil {
  1413. return nil, err
  1414. }
  1415. // If we have a async client, we need to wait for the async result
  1416. if s.cs.async {
  1417. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  1418. if err != nil {
  1419. if err == AsyncTimeoutErr {
  1420. return &r, err
  1421. }
  1422. return nil, err
  1423. }
  1424. b, err = getRawValue(b)
  1425. if err != nil {
  1426. return nil, err
  1427. }
  1428. if err := json.Unmarshal(b, &r); err != nil {
  1429. return nil, err
  1430. }
  1431. }
  1432. return &r, nil
  1433. }
  1434. type RevertToVMSnapshotResponse struct {
  1435. JobID string `json:"jobid,omitempty"`
  1436. Account string `json:"account,omitempty"`
  1437. Affinitygroup []struct {
  1438. Account string `json:"account,omitempty"`
  1439. Description string `json:"description,omitempty"`
  1440. Domain string `json:"domain,omitempty"`
  1441. Domainid string `json:"domainid,omitempty"`
  1442. Id string `json:"id,omitempty"`
  1443. Name string `json:"name,omitempty"`
  1444. Project string `json:"project,omitempty"`
  1445. Projectid string `json:"projectid,omitempty"`
  1446. Type string `json:"type,omitempty"`
  1447. VirtualmachineIds []string `json:"virtualmachineIds,omitempty"`
  1448. } `json:"affinitygroup,omitempty"`
  1449. Cpunumber int `json:"cpunumber,omitempty"`
  1450. Cpuspeed int `json:"cpuspeed,omitempty"`
  1451. Cpuused string `json:"cpuused,omitempty"`
  1452. Created string `json:"created,omitempty"`
  1453. Details map[string]string `json:"details,omitempty"`
  1454. Diskioread int64 `json:"diskioread,omitempty"`
  1455. Diskiowrite int64 `json:"diskiowrite,omitempty"`
  1456. Diskkbsread int64 `json:"diskkbsread,omitempty"`
  1457. Diskkbswrite int64 `json:"diskkbswrite,omitempty"`
  1458. Diskofferingid string `json:"diskofferingid,omitempty"`
  1459. Diskofferingname string `json:"diskofferingname,omitempty"`
  1460. Displayname string `json:"displayname,omitempty"`
  1461. Displayvm bool `json:"displayvm,omitempty"`
  1462. Domain string `json:"domain,omitempty"`
  1463. Domainid string `json:"domainid,omitempty"`
  1464. Forvirtualnetwork bool `json:"forvirtualnetwork,omitempty"`
  1465. Group string `json:"group,omitempty"`
  1466. Groupid string `json:"groupid,omitempty"`
  1467. Guestosid string `json:"guestosid,omitempty"`
  1468. Haenable bool `json:"haenable,omitempty"`
  1469. Hostid string `json:"hostid,omitempty"`
  1470. Hostname string `json:"hostname,omitempty"`
  1471. Hypervisor string `json:"hypervisor,omitempty"`
  1472. Id string `json:"id,omitempty"`
  1473. Instancename string `json:"instancename,omitempty"`
  1474. Isdynamicallyscalable bool `json:"isdynamicallyscalable,omitempty"`
  1475. Isodisplaytext string `json:"isodisplaytext,omitempty"`
  1476. Isoid string `json:"isoid,omitempty"`
  1477. Isoname string `json:"isoname,omitempty"`
  1478. Keypair string `json:"keypair,omitempty"`
  1479. Memory int `json:"memory,omitempty"`
  1480. Name string `json:"name,omitempty"`
  1481. Networkkbsread int64 `json:"networkkbsread,omitempty"`
  1482. Networkkbswrite int64 `json:"networkkbswrite,omitempty"`
  1483. Nic []struct {
  1484. Broadcasturi string `json:"broadcasturi,omitempty"`
  1485. Deviceid string `json:"deviceid,omitempty"`
  1486. Gateway string `json:"gateway,omitempty"`
  1487. Id string `json:"id,omitempty"`
  1488. Ip6address string `json:"ip6address,omitempty"`
  1489. Ip6cidr string `json:"ip6cidr,omitempty"`
  1490. Ip6gateway string `json:"ip6gateway,omitempty"`
  1491. Ipaddress string `json:"ipaddress,omitempty"`
  1492. Isdefault bool `json:"isdefault,omitempty"`
  1493. Isolationuri string `json:"isolationuri,omitempty"`
  1494. Macaddress string `json:"macaddress,omitempty"`
  1495. Netmask string `json:"netmask,omitempty"`
  1496. Networkid string `json:"networkid,omitempty"`
  1497. Networkname string `json:"networkname,omitempty"`
  1498. Secondaryip []struct {
  1499. Id string `json:"id,omitempty"`
  1500. Ipaddress string `json:"ipaddress,omitempty"`
  1501. } `json:"secondaryip,omitempty"`
  1502. Traffictype string `json:"traffictype,omitempty"`
  1503. Type string `json:"type,omitempty"`
  1504. Virtualmachineid string `json:"virtualmachineid,omitempty"`
  1505. } `json:"nic,omitempty"`
  1506. Ostypeid int64 `json:"ostypeid,omitempty"`
  1507. Password string `json:"password,omitempty"`
  1508. Passwordenabled bool `json:"passwordenabled,omitempty"`
  1509. Project string `json:"project,omitempty"`
  1510. Projectid string `json:"projectid,omitempty"`
  1511. Publicip string `json:"publicip,omitempty"`
  1512. Publicipid string `json:"publicipid,omitempty"`
  1513. Rootdeviceid int64 `json:"rootdeviceid,omitempty"`
  1514. Rootdevicetype string `json:"rootdevicetype,omitempty"`
  1515. Securitygroup []struct {
  1516. Account string `json:"account,omitempty"`
  1517. Description string `json:"description,omitempty"`
  1518. Domain string `json:"domain,omitempty"`
  1519. Domainid string `json:"domainid,omitempty"`
  1520. Egressrule []struct {
  1521. Account string `json:"account,omitempty"`
  1522. Cidr string `json:"cidr,omitempty"`
  1523. Endport int `json:"endport,omitempty"`
  1524. Icmpcode int `json:"icmpcode,omitempty"`
  1525. Icmptype int `json:"icmptype,omitempty"`
  1526. Protocol string `json:"protocol,omitempty"`
  1527. Ruleid string `json:"ruleid,omitempty"`
  1528. Securitygroupname string `json:"securitygroupname,omitempty"`
  1529. Startport int `json:"startport,omitempty"`
  1530. Tags []struct {
  1531. Account string `json:"account,omitempty"`
  1532. Customer string `json:"customer,omitempty"`
  1533. Domain string `json:"domain,omitempty"`
  1534. Domainid string `json:"domainid,omitempty"`
  1535. Key string `json:"key,omitempty"`
  1536. Project string `json:"project,omitempty"`
  1537. Projectid string `json:"projectid,omitempty"`
  1538. Resourceid string `json:"resourceid,omitempty"`
  1539. Resourcetype string `json:"resourcetype,omitempty"`
  1540. Value string `json:"value,omitempty"`
  1541. } `json:"tags,omitempty"`
  1542. } `json:"egressrule,omitempty"`
  1543. Id string `json:"id,omitempty"`
  1544. Ingressrule []struct {
  1545. Account string `json:"account,omitempty"`
  1546. Cidr string `json:"cidr,omitempty"`
  1547. Endport int `json:"endport,omitempty"`
  1548. Icmpcode int `json:"icmpcode,omitempty"`
  1549. Icmptype int `json:"icmptype,omitempty"`
  1550. Protocol string `json:"protocol,omitempty"`
  1551. Ruleid string `json:"ruleid,omitempty"`
  1552. Securitygroupname string `json:"securitygroupname,omitempty"`
  1553. Startport int `json:"startport,omitempty"`
  1554. Tags []struct {
  1555. Account string `json:"account,omitempty"`
  1556. Customer string `json:"customer,omitempty"`
  1557. Domain string `json:"domain,omitempty"`
  1558. Domainid string `json:"domainid,omitempty"`
  1559. Key string `json:"key,omitempty"`
  1560. Project string `json:"project,omitempty"`
  1561. Projectid string `json:"projectid,omitempty"`
  1562. Resourceid string `json:"resourceid,omitempty"`
  1563. Resourcetype string `json:"resourcetype,omitempty"`
  1564. Value string `json:"value,omitempty"`
  1565. } `json:"tags,omitempty"`
  1566. } `json:"ingressrule,omitempty"`
  1567. Name string `json:"name,omitempty"`
  1568. Project string `json:"project,omitempty"`
  1569. Projectid string `json:"projectid,omitempty"`
  1570. Tags []struct {
  1571. Account string `json:"account,omitempty"`
  1572. Customer string `json:"customer,omitempty"`
  1573. Domain string `json:"domain,omitempty"`
  1574. Domainid string `json:"domainid,omitempty"`
  1575. Key string `json:"key,omitempty"`
  1576. Project string `json:"project,omitempty"`
  1577. Projectid string `json:"projectid,omitempty"`
  1578. Resourceid string `json:"resourceid,omitempty"`
  1579. Resourcetype string `json:"resourcetype,omitempty"`
  1580. Value string `json:"value,omitempty"`
  1581. } `json:"tags,omitempty"`
  1582. Virtualmachinecount int `json:"virtualmachinecount,omitempty"`
  1583. Virtualmachineids []string `json:"virtualmachineids,omitempty"`
  1584. } `json:"securitygroup,omitempty"`
  1585. Serviceofferingid string `json:"serviceofferingid,omitempty"`
  1586. Serviceofferingname string `json:"serviceofferingname,omitempty"`
  1587. Servicestate string `json:"servicestate,omitempty"`
  1588. State string `json:"state,omitempty"`
  1589. Tags []struct {
  1590. Account string `json:"account,omitempty"`
  1591. Customer string `json:"customer,omitempty"`
  1592. Domain string `json:"domain,omitempty"`
  1593. Domainid string `json:"domainid,omitempty"`
  1594. Key string `json:"key,omitempty"`
  1595. Project string `json:"project,omitempty"`
  1596. Projectid string `json:"projectid,omitempty"`
  1597. Resourceid string `json:"resourceid,omitempty"`
  1598. Resourcetype string `json:"resourcetype,omitempty"`
  1599. Value string `json:"value,omitempty"`
  1600. } `json:"tags,omitempty"`
  1601. Templatedisplaytext string `json:"templatedisplaytext,omitempty"`
  1602. Templateid string `json:"templateid,omitempty"`
  1603. Templatename string `json:"templatename,omitempty"`
  1604. Userid string `json:"userid,omitempty"`
  1605. Username string `json:"username,omitempty"`
  1606. Vgpu string `json:"vgpu,omitempty"`
  1607. Zoneid string `json:"zoneid,omitempty"`
  1608. Zonename string `json:"zonename,omitempty"`
  1609. }