Beispiel #1
0
// 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))
}
Beispiel #2
0
// 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))
}
// fetchNodes do a HTTP GET to the MAAS server to query all the nodes
func fetchNodes(client *maas.MAASObject) ([]MaasNode, error) {
	nodeListing := client.GetSubObject("nodes")
	listNodeObjects, err := nodeListing.CallGet("list", url.Values{})
	if checkWarn(err, "unable to get the list of all nodes: %s", err) {
		return nil, err
	}
	listNodes, err := listNodeObjects.GetArray()
	if checkWarn(err, "unable to get the node objects for the list: %s", err) {
		return nil, err
	}

	var nodes = make([]MaasNode, len(listNodes))
	for index, nodeObj := range listNodes {
		node, err := nodeObj.GetMAASObject()
		if !checkWarn(err, "unable to retrieve object for node: %s", err) {
			nodes[index] = MaasNode{node}
		}
	}
	return nodes, nil
}
Beispiel #4
0
// updateName - changes the name of the MAAS node based on the configuration file
func updateNodeName(client *maas.MAASObject, node MaasNode, options ProcessingOptions) error {
	macs := node.MACs()

	// Get current node name and strip off domain name
	current := node.Hostname()
	if i := strings.IndexRune(current, '.'); i != -1 {
		current = current[:i]
	}
	for _, mac := range macs {
		if entry, ok := options.Mappings[mac]; ok {
			if name, ok := entry.(map[string]interface{})["hostname"]; ok && current != name.(string) {
				nodesObj := client.GetSubObject("nodes")
				nodeObj := nodesObj.GetSubObject(node.ID())
				log.Printf("RENAME '%s' to '%s'\n", node.Hostname(), name.(string))

				if !options.Preview {
					nodeObj.Update(url.Values{"hostname": []string{name.(string)}})
				}
			}
		}
	}
	return nil
}