コード例 #1
0
func HasDisk(c config.Cpi, extInput bosh.MethodArguments) (bool, error) {
	var diskCID string

	if reflect.TypeOf(extInput[0]) != reflect.TypeOf(diskCID) {
		return false, errors.New("Received unexpected type for disk cid")
	}

	diskCID = extInput[0].(string)

	if diskCID == "" {
		return false, nil
	}

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return false, err
	}

	for _, node := range nodes {
		if node.PersistentDisk.DiskCID == diskCID {
			return true, nil
		}
	}

	return false, nil
}
コード例 #2
0
func SelectNodeFromRackHD(c config.Cpi, diskCID string) (string, error) {
	if diskCID != "" {
		node, err := rackhdapi.GetNodeByDiskCID(c, diskCID)

		if err != nil {
			return "", err
		}

		log.Info(fmt.Sprintf("selected node %s", node.ID))
		return node.ID, nil
	}

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return "", err
	}

	nodeID, err := randomSelectAvailableNode(c, nodes)
	if err != nil || nodeID == "" {
		return "", err
	}

	log.Info(fmt.Sprintf("selected node %s", nodeID))
	return nodeID, nil
}
コード例 #3
0
func SelectNodeFromRackHD(c config.Cpi, nodeID string, filter Filter) (rackhdapi.Node, error) {
	if nodeID != "" {
		node, err := rackhdapi.GetNode(c, nodeID)

		if err != nil {
			return rackhdapi.Node{}, err
		}

		log.Info(fmt.Sprintf("selected node %s", node.ID))
		return node, nil
	}

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return rackhdapi.Node{}, err
	}

	node, err := randomSelectAvailableNode(c, nodes, filter)
	if err != nil || node.ID == "" {
		return rackhdapi.Node{}, err
	}

	log.Info(fmt.Sprintf("selected node %s", node.ID))
	return node, nil
}
コード例 #4
0
func DeleteDisk(c config.Cpi, extInput bosh.MethodArguments) error {
	var diskCID string

	if reflect.TypeOf(extInput[0]) != reflect.TypeOf(diskCID) {
		return errors.New("Received unexpected type for disk cid")
	}

	diskCID = extInput[0].(string)

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return err
	}

	for _, node := range nodes {
		if node.PersistentDisk.DiskCID == diskCID {
			if node.PersistentDisk.IsAttached {
				return fmt.Errorf("Disk: %s is attached\n", diskCID)
			}

			container := rackhdapi.PersistentDiskSettingsContainer{
				PersistentDisk: rackhdapi.PersistentDiskSettings{},
			}
			bodyBytes, err := json.Marshal(container)
			if err != nil {
				return err
			}

			rackhdapi.PatchNode(c, node.ID, bodyBytes)
			return nil
		}
	}

	return fmt.Errorf("Disk: %s not found\n", diskCID)
}
コード例 #5
0
func DetachDisk(c config.Cpi, extInput bosh.MethodArguments) error {
	var vmCID string
	var diskCID string

	if reflect.TypeOf(extInput[0]) != reflect.TypeOf(vmCID) {
		return errors.New("Received unexpected type for vm cid")
	}

	if reflect.TypeOf(extInput[1]) != reflect.TypeOf(diskCID) {
		return errors.New("Received unexpected type for disk cid")
	}

	vmCID = extInput[0].(string)
	diskCID = extInput[1].(string)

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return err
	}

	for _, node := range nodes {
		if node.PersistentDisk.DiskCID == diskCID {
			if !node.PersistentDisk.IsAttached {
				return fmt.Errorf("Disk: %s is detached\n", diskCID)
			}

			if node.CID != vmCID {
				return fmt.Errorf("Disk %s does not belong to VM %s\n", diskCID, vmCID)
			}

			container := rackhdapi.PersistentDiskSettingsContainer{
				PersistentDisk: node.PersistentDisk,
			}
			container.PersistentDisk.IsAttached = false

			bodyBytes, err := json.Marshal(container)
			if err != nil {
				return err
			}

			rackhdapi.PatchNode(c, node.ID, bodyBytes)
			return nil
		}
	}

	return fmt.Errorf("Disk: %s not found\n", diskCID)
}
コード例 #6
0
func HasVM(c config.Cpi, extInput bosh.MethodArguments) (bool, error) {
	var cid string
	if reflect.TypeOf(extInput[0]) != reflect.TypeOf(cid) {
		return false, errors.New("Received unexpected type for vm cid")
	}

	cid = extInput[0].(string)

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return false, err
	}

	for _, node := range nodes {
		if node.CID == cid {
			return true, nil
		}
	}

	return false, nil
}
コード例 #7
0
func DetachDisk(c config.Cpi, extInput bosh.MethodArguments) error {
	var vmCID string
	var diskCID string

	if reflect.TypeOf(extInput[0]) != reflect.TypeOf(vmCID) {
		return errors.New("Received unexpected type for vm cid")
	}

	if reflect.TypeOf(extInput[1]) != reflect.TypeOf(diskCID) {
		return errors.New("Received unexpected type for disk cid")
	}

	vmCID = extInput[0].(string)
	diskCID = extInput[1].(string)

	nodes, err := rackhdapi.GetNodes(c)
	if err != nil {
		return err
	}

	for _, node := range nodes {
		if node.PersistentDisk.DiskCID == diskCID {
			if !node.PersistentDisk.IsAttached {
				return fmt.Errorf("Disk: %s is detached\n", diskCID)
			}

			if node.CID != vmCID {
				return fmt.Errorf("Disk %s does not belong to VM %s\n", diskCID, vmCID)
			}

			return rackhdapi.MakeDiskRequest(c, node, false)
		}
	}

	return fmt.Errorf("Disk: %s not found\n", diskCID)
}
コード例 #8
0
		server.Close()
	})

	Describe("Getting nodes", func() {
		It("return expected nodes' fields", func() {
			expectedNodes := helpers.LoadNodes("../spec_assets/dummy_two_node_response.json")
			expectedNodesData, err := json.Marshal(expectedNodes)
			Expect(err).ToNot(HaveOccurred())
			server.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/api/common/nodes"),
					ghttp.RespondWith(http.StatusOK, expectedNodesData),
				),
			)

			nodes, err := rackhdapi.GetNodes(cpiConfig)

			Expect(err).ToNot(HaveOccurred())
			Expect(server.ReceivedRequests()).To(HaveLen(1))
			Expect(nodes).To(Equal(expectedNodes))
		})
	})

	Describe("Getting a single node by CID", func() {
		It("returns expected node's fields", func() {
			expectedNodes := helpers.LoadNodes("../spec_assets/dummy_all_nodes_are_vms.json")
			expectedNodesData, err := json.Marshal(expectedNodes)
			Expect(err).ToNot(HaveOccurred())
			server.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/api/common/nodes"),
コード例 #9
0
			CallTryReservation := func(c config.Cpi, nodes []rackhdapi.Node) {
				defer GinkgoRecover()
				_, err := TryReservation(
					c,
					"",
					SelectNodeFromRackHD,
					ReserveNodeFromRackHD,
				)
				Expect(err).ToNot(HaveOccurred())
				defer wg.Done()
			}

			apiServer, err := helpers.GetRackHDHost()
			Expect(err).ToNot(HaveOccurred())

			c := config.Cpi{ApiServer: apiServer, MaxReserveNodeAttempts: 5, RunWorkflowTimeoutSeconds: 4 * 60}

			nodes, err := rackhdapi.GetNodes(c)
			Expect(err).ToNot(HaveOccurred())

			times := 3
			for i := 0; i < times; i++ {
				wg.Add(1)
				go CallTryReservation(c, nodes)
			}

			wg.Wait()
		})
	})
})