Пример #1
0
// CreateFabricNetwork creates a new fabric network
func (c *CloudAPI) CreateFabricNetwork(vlanID int16, opts cloudapi.CreateFabricNetworkOpts) (*cloudapi.FabricNetwork, error) {
	id, err := localservices.NewUUID()
	if err != nil {
		return nil, err
	}

	vlan, err := c.getFabricWrapper(vlanID)
	if err != nil {
		return nil, err
	}

	vlan.Networks[id] = &cloudapi.FabricNetwork{
		Id:               id,
		Name:             opts.Name,
		Public:           false,
		Fabric:           true,
		Description:      opts.Description,
		Subnet:           opts.Subnet,
		ProvisionStartIp: opts.ProvisionStartIp,
		ProvisionEndIp:   opts.ProvisionEndIp,
		Gateway:          opts.Gateway,
		Routes:           opts.Routes,
		InternetNAT:      opts.InternetNAT,
		VLANId:           vlanID,
	}

	return vlan.Networks[id], 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,
	}
	c.machines = append(c.machines, newMachine)

	return newMachine, nil
}
Пример #3
0
// CreateFirewallRule creates a new firewall rule and returns it
func (c *CloudAPI) CreateFirewallRule(rule string, enabled bool) (*cloudapi.FirewallRule, error) {
	if err := c.ProcessFunctionHook(c, rule, enabled); err != nil {
		return nil, err
	}

	fwRuleID, err := localservices.NewUUID()
	if err != nil {
		return nil, fmt.Errorf("Error creating firewall rule: %q", err)
	}

	fwRule := &cloudapi.FirewallRule{Id: fwRuleID, Rule: rule, Enabled: enabled}
	c.firewallRules = append(c.firewallRules, fwRule)

	return fwRule, nil
}
Пример #4
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
}