github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/create.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package commands
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/Sirupsen/logrus"
    12  	"github.com/scaleway/scaleway-cli/pkg/api"
    13  )
    14  
    15  // CreateArgs are arguments passed to `RunCreate`
    16  type CreateArgs struct {
    17  	Volumes        []string
    18  	Tags           []string
    19  	Name           string
    20  	Bootscript     string
    21  	Image          string
    22  	IP             string
    23  	CommercialType string
    24  	TmpSSHKey      bool
    25  	IPV6           bool
    26  }
    27  
    28  // RunCreate is the handler for 'scw create'
    29  func RunCreate(ctx CommandContext, args CreateArgs) error {
    30  	if args.TmpSSHKey {
    31  		err := AddSSHKeyToTags(ctx, &args.Tags, args.Image)
    32  		if err != nil {
    33  			return err
    34  		}
    35  	}
    36  
    37  	env := strings.Join(args.Tags, " ")
    38  	volume := strings.Join(args.Volumes, " ")
    39  	config := api.ConfigCreateServer{
    40  		ImageName:         args.Image,
    41  		Name:              args.Name,
    42  		Bootscript:        args.Bootscript,
    43  		Env:               env,
    44  		AdditionalVolumes: volume,
    45  		DynamicIPRequired: false,
    46  		IP:                args.IP,
    47  		CommercialType:    args.CommercialType,
    48  		EnableIPV6:        args.IPV6,
    49  	}
    50  	if args.IP == "dynamic" || args.IP == "" {
    51  		config.DynamicIPRequired = true
    52  		config.IP = ""
    53  	} else if args.IP == "none" || args.IP == "no" {
    54  		config.IP = ""
    55  	}
    56  	serverID, err := api.CreateServer(ctx.API, &config)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	logrus.Debugf("Server created: %s", serverID)
    61  	logrus.Debugf("PublicDNS %s", serverID+api.URLPublicDNS)
    62  	logrus.Debugf("PrivateDNS %s", serverID+api.URLPrivateDNS)
    63  	fmt.Fprintln(ctx.Stdout, serverID)
    64  	return nil
    65  }