-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
138 lines (108 loc) · 3.29 KB
/
sample.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2012-2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
/*
This is an cmd on how the Go library gomaasapi can be used to interact with
a real maas server.
Note that this is a provided only as an cmd and that real code should probably do something more sensible with errors than ignoring them or panicking.
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/alejandroEsc/golang-maas-client/pkg/api"
"github.com/alejandroEsc/golang-maas-client/pkg/api/v2"
"github.com/spf13/viper"
)
const (
apiKeyKey = "api_key"
maasURLKey = "api_url"
maasAPIVersionKey = "api_version"
)
var apiKey string
var apiURL string
var apiVersion string
// Init initializes the environment variables to be used by the app
func Init() {
viper.AutomaticEnv()
viper.SetEnvPrefix("maas")
viper.BindEnv(maasURLKey)
viper.BindEnv(apiKeyKey)
viper.BindEnv(maasAPIVersionKey)
}
func checkError(err error) {
if err != nil {
fmt.Println(err)
panic(err)
}
}
func main() {
Init()
apiKey := viper.GetString(apiKeyKey)
apiURL := viper.GetString(maasURLKey)
apiVersion := viper.GetString(maasAPIVersionKey)
fmt.Printf("values are: %s %s %s\n", apiKey, apiURL, apiVersion)
m, err := api.NewMASS(apiURL, apiVersion, apiKey)
checkError(err)
files(m)
machines(m)
nodes(m)
fmt.Println("All done.")
}
func machines(maas *api.MAAS) {
params := v2.MachinesParams(v2.MachinesArgs{})
rawMachines, err := maas.Get("machines", "", params.Values)
checkError(err)
var machines []v2.Machine
err = json.Unmarshal(rawMachines, &machines)
checkError(err)
fmt.Printf("\nGot list of %v machines\n", len(machines))
for index, machine := range machines {
fmt.Printf("Machine #%d is named '%v' (%v)\n", index, machine.Hostname, machine.ResourceURI)
}
}
func nodes(maas *api.MAAS) {
rawNodes, err := maas.Get("nodes", "", nil)
checkError(err)
fmt.Printf("\n%s\n", string(rawNodes))
}
// ManipulateFiles exercises the /api/1.0/files/ API endpoint. Most precisely,
// it uploads a files and then fetches it, making sure the received content
// is the same as the one that was sent.
func files(maas *api.MAAS) {
var err error
fileContent := []byte("test file content")
fileName := "myfilename"
// Upload a file.
fmt.Println("Uploading a file...")
params := v2.FileParams(v2.AddFileArgs{Filename: fileName})
_, err = maas.PostFile("files", "", params.Values, fileContent)
checkError(err)
fmt.Println("File sent.")
// Fetch the file.
fileBytes, err := maas.Get("files/"+fileName, "", nil)
checkError(err)
var file v2.File
err = json.Unmarshal(fileBytes, &file)
if bytes.Compare([]byte(file.Content), fileContent) != 0 {
maas.Delete(file.ResourceURI)
panic("Received content differs from the content sent!")
}
fmt.Println("Got file.")
// Fetch list of filesResource.
var listFiles []v2.File
listBytes, err := maas.Get("files", "", nil)
err = json.Unmarshal(listBytes, &listFiles)
checkError(err)
fmt.Printf("We've got %v file(s)\n", len(listFiles))
// Delete the file.
fmt.Println("Deleting the file...")
f := listFiles[0]
err = maas.Delete(f.ResourceURI)
checkError(err)
// Count the filesResource.
listBytes, err = maas.Get("files", "", nil)
checkError(err)
json.Unmarshal(listBytes, &listFiles)
fmt.Printf("We've got %v file(s)\n", len(listFiles))
}