Exemple #1
0
func create(client *clc.Client) cli.Command {
	return cli.Command{
		Name:        "create",
		Aliases:     []string{"c"},
		Usage:       "create server",
		Description: "example: clc server create -n 'my cool server' -c 1 -m 1 -g [group id] -t standard --ubuntu-14",
		Flags:       append(createFlags(), templateFlags()...),
		Before: func(c *cli.Context) error {
			err := util.CheckStringFlag(c, "name", "cpu", "memory", "group")
			if err == nil {
				err = util.CheckForEitherBooleanFlag(c, "standard", "hyperscale")
			}
			return err
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			source, err := findTemplateInContext(c)
			if err != nil {
				return
			}
			server := server.Server{
				Name:           c.String("name"),
				CPU:            c.Int("cpu"),
				MemoryGB:       c.Int("memory"),
				GroupID:        c.String("group"),
				SourceServerID: source,
				Type:           deriveServerType(c),
			}
			server.Password = c.String("password")
			server.Description = c.String("description")
			server.IPaddress = c.String("ip")
			server.IsManagedOS = c.Bool("managed")
			server.PrimaryDNS = c.String("primaryDNS")
			server.SecondaryDNS = c.String("secondaryDNS")
			server.NetworkID = c.String("network")
			server.Storagetype = deriveStorageType(c)

			resp, err := client.Server.Create(server)
			if err != nil {
				fmt.Printf("failed to create %s", server.Name)
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
func resourceCLCServerCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*clc.Client)
	spec := server.Server{
		Name:           d.Get("name_template").(string),
		Password:       d.Get("password").(string),
		Description:    d.Get("description").(string),
		GroupID:        d.Get("group_id").(string),
		CPU:            d.Get("cpu").(int),
		MemoryGB:       d.Get("memory_mb").(int) / 1024,
		SourceServerID: d.Get("source_server_id").(string),
		Type:           d.Get("type").(string),
		IPaddress:      d.Get("private_ip_address").(string),
		NetworkID:      d.Get("network_id").(string),
		Storagetype:    d.Get("storage_type").(string),
	}

	var err error
	disks, err := parseAdditionalDisks(d)
	if err != nil {
		return fmt.Errorf("Failed parsing disks: %v", err)
	}
	spec.Additionaldisks = disks
	fields, err := parseCustomFields(d)
	if err != nil {
		return fmt.Errorf("Failed setting customfields: %v", err)
	}
	spec.Customfields = fields

	resp, err := client.Server.Create(spec)
	if err != nil || !resp.IsQueued {
		return fmt.Errorf("Failed creating server: %v", err)
	}
	// server's UUID returned under rel=self link
	_, uuid := resp.Links.GetID("self")

	ok, st := resp.GetStatusID()
	if !ok {
		return fmt.Errorf("Failed extracting status to poll on %v: %v", resp, err)
	}
	err = waitStatus(client, st)
	if err != nil {
		return err
	}

	s, err := client.Server.Get(uuid)
	d.SetId(strings.ToUpper(s.Name))
	log.Printf("[INFO] Server created. id: %v", s.Name)
	return resourceCLCServerRead(d, meta)
}