func SetVMMetadata(c config.Cpi, extInput bosh.MethodArguments) error {

	var cid string
	if reflect.TypeOf(extInput[0]) != reflect.TypeOf(cid) {
		return fmt.Errorf("Cannot set VM metadata: received unexpected value for vm cid: %s", cid)
	}
	cid = extInput[0].(string)

	metadata, err := json.Marshal(extInput[1])
	if err != nil {
		return fmt.Errorf("Cannot set VM metadata: metadata is not valid JSON")
	} else {
		for _, v := range extInput[1].(map[string]interface{}) {
			switch v.(type) {
			case string, int:
			default:
				fmt.Println("Cannot set VM metadata: metadata must be a hash with string or integer values")
			}
		}
	}

	node, err := rackhdapi.GetNodeByVMCID(c, cid)
	nodeID := node.ID
	if err != nil {
		return err
	}

	return rackhdapi.SetNodeMetadata(c, nodeID, string(metadata))
}
Пример #2
0
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("PATCH", fmt.Sprintf("/api/common/nodes/%s", nodes[0].ID)),
					ghttp.VerifyJSON(fmt.Sprintf(`{"status": "%s", "status_reason": "%s"}`, "blocked", "Node has missing disks")),
				),
			)

			rackhdapi.BlockNode(cpiConfig, nodes[0].ID)
			Expect(server.ReceivedRequests()).To(HaveLen(1))
		})
	})

	Describe("Setting node metadata", func() {
		It("Adds metadata to the node", func() {
			nodeID := "node_id"
			metadata := "{}"

			server.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("PATCH", fmt.Sprintf("/api/common/nodes/%s", nodeID)),
					ghttp.VerifyJSON(fmt.Sprintf(`{"metadata": %s}`, metadata)),
				),
			)

			err := rackhdapi.SetNodeMetadata(cpiConfig, nodeID, metadata)
			Expect(err).ToNot(HaveOccurred())

			Expect(server.ReceivedRequests()).To(HaveLen(1))
		})
	})
})