예제 #1
0
func (c *CloudAPI) AddNIC(machineID, networkID string) (*cloudapi.NIC, error) {
	machine, err := c.getMachineWrapper(machineID)
	if err != nil {
		return nil, err
	}

	// make sure that we're getting a real network
	_, err = c.GetNetwork(networkID)
	if err != nil {
		return nil, err
	}

	found := false
	for _, network := range machine.Networks {
		if network == networkID {
			found = true
		}
	}
	if found {
		return nil, fmt.Errorf("Machine %s is already in network %s", machineID, networkID)
	}

	mac, err := localservices.NewMAC()
	if err != nil {
		return nil, err
	}

	machine.NICs[mac] = &cloudapi.NIC{
		IP:      "10.88.88.100",
		MAC:     mac,
		Primary: false,
		Netmask: "255.255.255.0",
		Gateway: "10.88.88.2",
		State:   cloudapi.NICStateRunning,
		Network: networkID,
	}
	machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")
	machine.Networks = append(machine.Networks, networkID)
	machine.NetworkNICs[mac] = networkID

	return machine.NICs[mac], nil
}
예제 #2
0
// CreateMachine creates a new machine in the double. It will be running immediately.
func (c *CloudAPI) CreateMachine(name, pkg, image string, networks []string, metadata, tags map[string]string) (*cloudapi.Machine, error) {
	if err := c.ProcessFunctionHook(c, name, pkg, image); err != nil {
		return nil, err
	}

	machineID, err := localservices.NewUUID()
	if err != nil {
		return nil, err
	}

	mPkg, err := c.GetPackage(pkg)
	if err != nil {
		return nil, err
	}

	mImg, err := c.GetImage(image)
	if err != nil {
		return nil, err
	}

	mNetworks := []string{}
	for _, network := range networks {
		mNetwork, err := c.GetNetwork(network)
		if err != nil {
			return nil, err
		}

		mNetworks = append(mNetworks, mNetwork.Id)
	}

	publicIP := generatePublicIPAddress()

	newMachine := cloudapi.Machine{
		Id:        machineID,
		Name:      name,
		Type:      mImg.Type,
		State:     "running",
		Memory:    mPkg.Memory,
		Disk:      mPkg.Disk,
		IPs:       []string{publicIP, generatePrivateIPAddress()},
		Created:   time.Now().Format("2013-11-26T19:47:13.448Z"),
		Package:   pkg,
		Image:     image,
		Metadata:  metadata,
		Tags:      tags,
		PrimaryIP: publicIP,
		Networks:  mNetworks,
	}

	nics := map[string]*cloudapi.NIC{}
	nicNetworks := map[string]string{}
	for i, network := range mNetworks {
		mac, err := localservices.NewMAC()
		if err != nil {
			return nil, err
		}

		nics[mac] = &cloudapi.NIC{
			IP:      fmt.Sprintf("10.88.88.%d", i),
			MAC:     mac,
			Primary: i == 0,
			Netmask: "255.255.255.0",
			Gateway: "10.88.88.2",
			State:   cloudapi.NICStateRunning,
			Network: network,
		}
		nicNetworks[mac] = network
	}

	c.machines = append(c.machines, &machine{newMachine, nics, nicNetworks})

	return &newMachine, nil
}