/
blueprints.go
101 lines (81 loc) · 2.47 KB
/
blueprints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package nelson
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type BlueprintResponse struct {
Name string `json:"name"`
Description string `json:"description"`
Revision string `json:"revision"`
Sha256 string `json:"sha256"`
Template string `json:"template"`
CreatedAt int64 `json:"created_at"`
}
type CreateBlueprintRequest struct {
// Example: gpu-general-deployment
Name string `json:"name"`
// Example: a blueprint for intensive graphics consumption
Description string `json:"description"`
// Sha256 sum of the template for versioning purposes
Sha256 string `json:"sha256"`
// Base64 Encoding of the blueprint template
Template string `json:"template"`
}
func (n *Nelson) CreateBlueprint(cbr CreateBlueprintRequest) error {
requestBody, err := json.Marshal(cbr)
if err != nil {
return err
}
endpoint := []string{n.NelsonConfig.Endpoint, n.apiVersion, "blueprints"}
req, _ := http.NewRequest(http.MethodPost, strings.Join(endpoint, "/"), bytes.NewBuffer(requestBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", LibraryVersion())
response, err := n.Client.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
var blueprintResponse BlueprintResponse
json.NewDecoder(response.Body).Decode(&blueprintResponse)
if response.StatusCode != 200 {
bodyBytes, _ := ioutil.ReadAll(response.Body)
bodyString := string(bodyBytes)
return fmt.Errorf("nelson server error: %s", bodyString)
}
return nil
}
func (n *Nelson) GetBlueprints() ([]*BlueprintResponse, error) {
var b []byte
requestBody := bytes.NewBuffer(b)
endpoint := []string{n.NelsonConfig.Endpoint, n.apiVersion, "blueprints"}
req, _ := http.NewRequest(http.MethodGet, strings.Join(endpoint, "/"), requestBody)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", LibraryVersion())
response, err := n.Client.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, fmt.Errorf("nelson server error")
}
var list []*BlueprintResponse
json.NewDecoder(response.Body).Decode(&list)
return list, nil
}
func (n *Nelson) GetBlueprint(blueprintName string) (*BlueprintResponse, error) {
blueprints, err := n.GetBlueprints()
if err != nil {
return nil, err
}
for _, blueprint := range blueprints {
if blueprint.Name == blueprintName {
return blueprint, nil
}
}
return nil, fmt.Errorf("blueprint not found")
}