~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/gomaasapi/example/live_example.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012-2016 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
/*
 
5
This is an example on how the Go library gomaasapi can be used to interact with
 
6
a real MAAS server.
 
7
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.
 
8
*/
 
9
package main
 
10
 
 
11
import (
 
12
        "bytes"
 
13
        "fmt"
 
14
        "net/url"
 
15
 
 
16
        "github.com/juju/gomaasapi"
 
17
)
 
18
 
 
19
var apiKey string
 
20
var apiURL string
 
21
var apiVersion string
 
22
 
 
23
func getParams() {
 
24
        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.")
 
25
        fmt.Print("Enter API key: ")
 
26
        _, err := fmt.Scanf("%s", &apiKey)
 
27
        if err != nil {
 
28
                panic(err)
 
29
        }
 
30
        fmt.Print("Enter API URL: ")
 
31
        _, err = fmt.Scanf("%s", &apiURL)
 
32
        if err != nil {
 
33
                panic(err)
 
34
        }
 
35
 
 
36
        fmt.Print("Enter API version: ")
 
37
        _, err = fmt.Scanf("%s", &apiVersion)
 
38
        if err != nil {
 
39
                panic(err)
 
40
        }
 
41
}
 
42
 
 
43
func checkError(err error) {
 
44
        if err != nil {
 
45
                panic(err)
 
46
        }
 
47
}
 
48
 
 
49
func main() {
 
50
        getParams()
 
51
 
 
52
        // Create API server endpoint.
 
53
        authClient, err := gomaasapi.NewAuthenticatedClient(apiURL, apiKey, apiVersion)
 
54
        checkError(err)
 
55
        maas := gomaasapi.NewMAAS(*authClient)
 
56
 
 
57
        // Exercise the API.
 
58
        ManipulateNodes(maas)
 
59
        ManipulateFiles(maas)
 
60
 
 
61
        fmt.Println("All done.")
 
62
}
 
63
 
 
64
// ManipulateFiles exercises the /api/1.0/files/ API endpoint.  Most precisely,
 
65
// it uploads a files and then fetches it, making sure the received content
 
66
// is the same as the one that was sent.
 
67
func ManipulateFiles(maas *gomaasapi.MAASObject) {
 
68
        files := maas.GetSubObject("files")
 
69
        fileContent := []byte("test file content")
 
70
        fileName := "filename"
 
71
        filesToUpload := map[string][]byte{"file": fileContent}
 
72
 
 
73
        // Upload a file.
 
74
        fmt.Println("Uploading a file...")
 
75
        _, err := files.CallPostFiles("add", url.Values{"filename": {fileName}}, filesToUpload)
 
76
        checkError(err)
 
77
        fmt.Println("File sent.")
 
78
 
 
79
        // Fetch the file.
 
80
        fmt.Println("Fetching the file...")
 
81
        fileResult, err := files.CallGet("get", url.Values{"filename": {fileName}})
 
82
        checkError(err)
 
83
        receivedFileContent, err := fileResult.GetBytes()
 
84
        checkError(err)
 
85
        if bytes.Compare(receivedFileContent, fileContent) != 0 {
 
86
                panic("Received content differs from the content sent!")
 
87
        }
 
88
        fmt.Println("Got file.")
 
89
 
 
90
        // Fetch list of files.
 
91
        listFiles, err := files.CallGet("list", url.Values{})
 
92
        checkError(err)
 
93
        listFilesArray, err := listFiles.GetArray()
 
94
        checkError(err)
 
95
        fmt.Printf("We've got %v file(s)\n", len(listFilesArray))
 
96
 
 
97
        // Delete the file.
 
98
        fmt.Println("Deleting the file...")
 
99
        fileObject, err := listFilesArray[0].GetMAASObject()
 
100
        checkError(err)
 
101
        errDelete := fileObject.Delete()
 
102
        checkError(errDelete)
 
103
 
 
104
        // Count the files.
 
105
        listFiles, err = files.CallGet("list", url.Values{})
 
106
        checkError(err)
 
107
        listFilesArray, err = listFiles.GetArray()
 
108
        checkError(err)
 
109
        fmt.Printf("We've got %v file(s)\n", len(listFilesArray))
 
110
}
 
111
 
 
112
// ManipulateFiles exercises the /api/1.0/nodes/ API endpoint.  Most precisely,
 
113
// it lists the existing nodes, creates a new node, updates it and then
 
114
// deletes it.
 
115
func ManipulateNodes(maas *gomaasapi.MAASObject) {
 
116
        nodeListing := maas.GetSubObject("nodes")
 
117
 
 
118
        // List nodes.
 
119
        fmt.Println("Fetching list of nodes...")
 
120
        listNodeObjects, err := nodeListing.CallGet("list", url.Values{})
 
121
        checkError(err)
 
122
        listNodes, err := listNodeObjects.GetArray()
 
123
        checkError(err)
 
124
        fmt.Printf("Got list of %v nodes\n", len(listNodes))
 
125
        for index, nodeObj := range listNodes {
 
126
                node, err := nodeObj.GetMAASObject()
 
127
                checkError(err)
 
128
                hostname, err := node.GetField("hostname")
 
129
                checkError(err)
 
130
                fmt.Printf("Node #%d is named '%v' (%v)\n", index, hostname, node.URL())
 
131
        }
 
132
 
 
133
        // Create a node.
 
134
        fmt.Println("Creating a new node...")
 
135
        params := url.Values{"architecture": {"i386/generic"}, "mac_addresses": {"AA:BB:CC:DD:EE:FF"}}
 
136
        newNodeObj, err := nodeListing.CallPost("new", params)
 
137
        checkError(err)
 
138
        newNode, err := newNodeObj.GetMAASObject()
 
139
        checkError(err)
 
140
        newNodeName, err := newNode.GetField("hostname")
 
141
        checkError(err)
 
142
        fmt.Printf("New node created: %s (%s)\n", newNodeName, newNode.URL())
 
143
 
 
144
        // Update the new node.
 
145
        fmt.Println("Updating the new node...")
 
146
        updateParams := url.Values{"hostname": {"mynewname"}}
 
147
        newNodeObj2, err := newNode.Update(updateParams)
 
148
        checkError(err)
 
149
        newNodeName2, err := newNodeObj2.GetField("hostname")
 
150
        checkError(err)
 
151
        fmt.Printf("New node updated, now named: %s\n", newNodeName2)
 
152
 
 
153
        // Count the nodes.
 
154
        listNodeObjects2, err := nodeListing.CallGet("list", url.Values{})
 
155
        checkError(err)
 
156
        listNodes2, err := listNodeObjects2.GetArray()
 
157
        checkError(err)
 
158
        fmt.Printf("We've got %v nodes\n", len(listNodes2))
 
159
 
 
160
        // Delete the new node.
 
161
        fmt.Println("Deleting the new node...")
 
162
        errDelete := newNode.Delete()
 
163
        checkError(errDelete)
 
164
 
 
165
        // Count the nodes.
 
166
        listNodeObjects3, err := nodeListing.CallGet("list", url.Values{})
 
167
        checkError(err)
 
168
        listNodes3, err := listNodeObjects3.GetArray()
 
169
        checkError(err)
 
170
        fmt.Printf("We've got %v nodes\n", len(listNodes3))
 
171
}