Debian Codegraph @

photon/flavors.go

raw symbols SCIP
1 // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
2 //
3 // This product is licensed to you under the Apache License, Version 2.0 (the "License").
4 // You may not use this product except in compliance with the License.
5 //
6 // This product may include a number of subcomponents with separate copyright notices and
7 // license terms. Your use of these subcomponents is subject to the terms and conditions
8 // of the subcomponent's license, as noted in the LICENSE file.
9
10 package photon
11
12 import (
13 "bytes"
14 "encoding/json"
15 )
16
17 // Contains functionality for flavors API.
18 type FlavorsAPI struct {
19 client *Client
20 }
21
22 // Options used for find/get APIs
23 type FlavorGetOptions struct {
24 Name string `urlParam:"name"`
25 Kind string `urlParam:"kind"`
26 }
27
28 var flavorUrl string = rootUrl + "/flavors"
29
30 // Creates a flavor.
31 func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) {
32 body, err := json.Marshal(spec)
33 if err != nil {
34 return
35 }
36 res, err := api.client.restClient.Post(
37 api.client.Endpoint+flavorUrl,
38 "application/json",
39 bytes.NewReader(body),
40 api.client.options.TokenOptions)
41 if err != nil {
42 return
43 }
44 defer res.Body.Close()
45 task, err = getTask(getError(res))
46 return
47 }
48
49 // Gets details of flavor with specified ID.
50 func (api *FlavorsAPI) Get(flavorID string) (flavor *Flavor, err error) {
51 res, err := api.client.restClient.Get(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions)
52 if err != nil {
53 return
54 }
55 defer res.Body.Close()
56 res, err = getError(res)
57 if err != nil {
58 return
59 }
60 flavor = &Flavor{}
61 err = json.NewDecoder(res.Body).Decode(flavor)
62 return
63 }
64
65 // Gets flavors using options to filter results. Returns all flavors if options is nil.
66 func (api *FlavorsAPI) GetAll(options *FlavorGetOptions) (flavors *FlavorList, err error) {
67 uri := api.client.Endpoint + flavorUrl
68 if options != nil {
69 uri += getQueryString(options)
70 }
71 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
72 if err != nil {
73 return
74 }
75
76 flavors = &FlavorList{}
77 err = json.Unmarshal(res, flavors)
78 return
79 }
80
81 // Deletes flavor with specified ID.
82 func (api *FlavorsAPI) Delete(flavorID string) (task *Task, err error) {
83 res, err := api.client.restClient.Delete(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions)
84 if err != nil {
85 return
86 }
87 defer res.Body.Close()
88 task, err = getTask(getError(res))
89 return
90 }
91
92 // Gets all tasks with the specified flavor ID, using options to filter the results.
93 // If options is nil, no filtering will occur.
94 func (api *FlavorsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
95 uri := api.client.Endpoint + flavorUrl + "/" + id + "/tasks"
96 if options != nil {
97 uri += getQueryString(options)
98 }
99
100 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
101 if err != nil {
102 return
103 }
104
105 result = &TaskList{}
106 err = json.Unmarshal(res, result)
107 return
108 }