File: live_example.go

package info (click to toggle)
golang-github-juju-gomaasapi 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 908 kB
  • sloc: makefile: 17
file content (172 lines) | stat: -rw-r--r-- 5,095 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2012-2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

/*
This is an example 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 example and that real code should probably do something more sensible with errors than ignoring them or panicking.
*/
package main

import (
	"bytes"
	"fmt"
	"net/url"

	"github.com/juju/gomaasapi/v2"
)

var apiKey string
var apiURL string
var apiVersion string

func getParams() {
	fmt.Println("Warning: this will create a node on the MAAS server; it should be deleted at the end of the run but if something goes wrong, that test node might be left over.  You've been warned.")
	fmt.Print("Enter API key: ")
	_, err := fmt.Scanf("%s", &apiKey)
	if err != nil {
		panic(err)
	}
	fmt.Print("Enter API URL: ")
	_, err = fmt.Scanf("%s", &apiURL)
	if err != nil {
		panic(err)
	}

	fmt.Print("Enter API version: ")
	_, err = fmt.Scanf("%s", &apiVersion)
	if err != nil {
		panic(err)
	}
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	getParams()

	// Create API server endpoint.
	authClient, err := gomaasapi.NewAuthenticatedClient(
		gomaasapi.AddAPIVersionToURL(apiURL, apiVersion), apiKey)
	checkError(err)
	maas := gomaasapi.NewMAAS(*authClient)

	// Exercise the API.
	ManipulateNodes(maas)
	ManipulateFiles(maas)

	fmt.Println("All done.")
}

// 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 ManipulateFiles(maas *gomaasapi.MAASObject) {
	files := maas.GetSubObject("files")
	fileContent := []byte("test file content")
	fileName := "filename"
	filesToUpload := map[string][]byte{"file": fileContent}

	// Upload a file.
	fmt.Println("Uploading a file...")
	_, err := files.CallPostFiles("add", url.Values{"filename": {fileName}}, filesToUpload)
	checkError(err)
	fmt.Println("File sent.")

	// Fetch the file.
	fmt.Println("Fetching the file...")
	fileResult, err := files.CallGet("get", url.Values{"filename": {fileName}})
	checkError(err)
	receivedFileContent, err := fileResult.GetBytes()
	checkError(err)
	if bytes.Compare(receivedFileContent, fileContent) != 0 {
		panic("Received content differs from the content sent!")
	}
	fmt.Println("Got file.")

	// Fetch list of files.
	listFiles, err := files.CallGet("list", url.Values{})
	checkError(err)
	listFilesArray, err := listFiles.GetArray()
	checkError(err)
	fmt.Printf("We've got %v file(s)\n", len(listFilesArray))

	// Delete the file.
	fmt.Println("Deleting the file...")
	fileObject, err := listFilesArray[0].GetMAASObject()
	checkError(err)
	errDelete := fileObject.Delete()
	checkError(errDelete)

	// Count the files.
	listFiles, err = files.CallGet("list", url.Values{})
	checkError(err)
	listFilesArray, err = listFiles.GetArray()
	checkError(err)
	fmt.Printf("We've got %v file(s)\n", len(listFilesArray))
}

// ManipulateFiles exercises the /api/1.0/nodes/ API endpoint.  Most precisely,
// it lists the existing nodes, creates a new node, updates it and then
// deletes it.
func ManipulateNodes(maas *gomaasapi.MAASObject) {
	nodeListing := maas.GetSubObject("nodes")

	// List nodes.
	fmt.Println("Fetching list of nodes...")
	listNodeObjects, err := nodeListing.CallGet("list", url.Values{})
	checkError(err)
	listNodes, err := listNodeObjects.GetArray()
	checkError(err)
	fmt.Printf("Got list of %v nodes\n", len(listNodes))
	for index, nodeObj := range listNodes {
		node, err := nodeObj.GetMAASObject()
		checkError(err)
		hostname, err := node.GetField("hostname")
		checkError(err)
		fmt.Printf("Node #%d is named '%v' (%v)\n", index, hostname, node.URL())
	}

	// Create a node.
	fmt.Println("Creating a new node...")
	params := url.Values{"architecture": {"i386/generic"}, "mac_addresses": {"AA:BB:CC:DD:EE:FF"}}
	newNodeObj, err := nodeListing.CallPost("new", params)
	checkError(err)
	newNode, err := newNodeObj.GetMAASObject()
	checkError(err)
	newNodeName, err := newNode.GetField("hostname")
	checkError(err)
	fmt.Printf("New node created: %s (%s)\n", newNodeName, newNode.URL())

	// Update the new node.
	fmt.Println("Updating the new node...")
	updateParams := url.Values{"hostname": {"mynewname"}}
	newNodeObj2, err := newNode.Update(updateParams)
	checkError(err)
	newNodeName2, err := newNodeObj2.GetField("hostname")
	checkError(err)
	fmt.Printf("New node updated, now named: %s\n", newNodeName2)

	// Count the nodes.
	listNodeObjects2, err := nodeListing.CallGet("list", url.Values{})
	checkError(err)
	listNodes2, err := listNodeObjects2.GetArray()
	checkError(err)
	fmt.Printf("We've got %v nodes\n", len(listNodes2))

	// Delete the new node.
	fmt.Println("Deleting the new node...")
	errDelete := newNode.Delete()
	checkError(errDelete)

	// Count the nodes.
	listNodeObjects3, err := nodeListing.CallGet("list", url.Values{})
	checkError(err)
	listNodes3, err := listNodeObjects3.GetArray()
	checkError(err)
	fmt.Printf("We've got %v nodes\n", len(listNodes3))
}