github.com/leeclow-ops/gophercloud@v1.2.1/openstack/db/v1/instances/requests.go (about)

     1  package instances
     2  
     3  import (
     4  	"github.com/leeclow-ops/gophercloud"
     5  	db "github.com/leeclow-ops/gophercloud/openstack/db/v1/databases"
     6  	"github.com/leeclow-ops/gophercloud/openstack/db/v1/users"
     7  	"github.com/leeclow-ops/gophercloud/pagination"
     8  )
     9  
    10  // CreateOptsBuilder is the top-level interface for create options.
    11  type CreateOptsBuilder interface {
    12  	ToInstanceCreateMap() (map[string]interface{}, error)
    13  }
    14  
    15  // DatastoreOpts represents the configuration for how an instance stores data.
    16  type DatastoreOpts struct {
    17  	Version string `json:"version"`
    18  	Type    string `json:"type"`
    19  }
    20  
    21  // ToMap converts a DatastoreOpts to a map[string]string (for a request body)
    22  func (opts DatastoreOpts) ToMap() (map[string]interface{}, error) {
    23  	return gophercloud.BuildRequestBody(opts, "")
    24  }
    25  
    26  // NetworkOpts is used within CreateOpts to control a new server's network attachments.
    27  type NetworkOpts struct {
    28  	// UUID of a nova-network to attach to the newly provisioned server.
    29  	// Required unless Port is provided.
    30  	UUID string `json:"net-id,omitempty"`
    31  
    32  	// Port of a neutron network to attach to the newly provisioned server.
    33  	// Required unless UUID is provided.
    34  	Port string `json:"port-id,omitempty"`
    35  
    36  	// V4FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
    37  	V4FixedIP string `json:"v4-fixed-ip,omitempty"`
    38  
    39  	// V6FixedIP [optional] specifies a fixed IPv6 address to be used on this network.
    40  	V6FixedIP string `json:"v6-fixed-ip,omitempty"`
    41  }
    42  
    43  // ToMap converts a NetworkOpts to a map[string]string (for a request body)
    44  func (opts NetworkOpts) ToMap() (map[string]interface{}, error) {
    45  	return gophercloud.BuildRequestBody(opts, "")
    46  }
    47  
    48  // CreateOpts is the struct responsible for configuring a new database instance.
    49  type CreateOpts struct {
    50  	// Either the integer UUID (in string form) of the flavor, or its URI
    51  	// reference as specified in the response from the List() call. Required.
    52  	FlavorRef string
    53  	// Specifies the volume size in gigabytes (GB). The value must be between 1
    54  	// and 300. Required.
    55  	Size int
    56  	// Specifies the volume type.
    57  	VolumeType string
    58  	// Name of the instance to create. The length of the name is limited to
    59  	// 255 characters and any characters are permitted. Optional.
    60  	Name string
    61  	// A slice of database information options.
    62  	Databases db.CreateOptsBuilder
    63  	// A slice of user information options.
    64  	Users users.CreateOptsBuilder
    65  	// Options to configure the type of datastore the instance will use. This is
    66  	// optional, and if excluded will default to MySQL.
    67  	Datastore *DatastoreOpts
    68  	// Networks dictates how this server will be attached to available networks.
    69  	Networks []NetworkOpts
    70  }
    71  
    72  // ToInstanceCreateMap will render a JSON map.
    73  func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
    74  	if opts.Size > 300 || opts.Size < 1 {
    75  		err := gophercloud.ErrInvalidInput{}
    76  		err.Argument = "instances.CreateOpts.Size"
    77  		err.Value = opts.Size
    78  		err.Info = "Size (GB) must be between 1-300"
    79  		return nil, err
    80  	}
    81  
    82  	if opts.FlavorRef == "" {
    83  		return nil, gophercloud.ErrMissingInput{Argument: "instances.CreateOpts.FlavorRef"}
    84  	}
    85  
    86  	instance := map[string]interface{}{
    87  		"flavorRef": opts.FlavorRef,
    88  	}
    89  
    90  	if opts.Name != "" {
    91  		instance["name"] = opts.Name
    92  	}
    93  	if opts.Databases != nil {
    94  		dbs, err := opts.Databases.ToDBCreateMap()
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  		instance["databases"] = dbs["databases"]
    99  	}
   100  	if opts.Users != nil {
   101  		users, err := opts.Users.ToUserCreateMap()
   102  		if err != nil {
   103  			return nil, err
   104  		}
   105  		instance["users"] = users["users"]
   106  	}
   107  	if opts.Datastore != nil {
   108  		datastore, err := opts.Datastore.ToMap()
   109  		if err != nil {
   110  			return nil, err
   111  		}
   112  		instance["datastore"] = datastore
   113  	}
   114  
   115  	if len(opts.Networks) > 0 {
   116  		networks := make([]map[string]interface{}, len(opts.Networks))
   117  		for i, net := range opts.Networks {
   118  			var err error
   119  			networks[i], err = net.ToMap()
   120  			if err != nil {
   121  				return nil, err
   122  			}
   123  		}
   124  		instance["nics"] = networks
   125  	}
   126  
   127  	volume := map[string]interface{}{
   128  		"size": opts.Size,
   129  	}
   130  
   131  	if opts.VolumeType != "" {
   132  		volume["type"] = opts.VolumeType
   133  	}
   134  
   135  	instance["volume"] = volume
   136  
   137  	return map[string]interface{}{"instance": instance}, nil
   138  }
   139  
   140  // Create asynchronously provisions a new database instance. It requires the
   141  // user to specify a flavor and a volume size. The API service then provisions
   142  // the instance with the requested flavor and sets up a volume of the specified
   143  // size, which is the storage for the database instance.
   144  //
   145  // Although this call only allows the creation of 1 instance per request, you
   146  // can create an instance with multiple databases and users. The default
   147  // binding for a MySQL instance is port 3306.
   148  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   149  	b, err := opts.ToInstanceCreateMap()
   150  	if err != nil {
   151  		r.Err = err
   152  		return
   153  	}
   154  	resp, err := client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
   155  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   156  	return
   157  }
   158  
   159  // List retrieves the status and information for all database instances.
   160  func List(client *gophercloud.ServiceClient) pagination.Pager {
   161  	return pagination.NewPager(client, baseURL(client), func(r pagination.PageResult) pagination.Page {
   162  		return InstancePage{pagination.LinkedPageBase{PageResult: r}}
   163  	})
   164  }
   165  
   166  // Get retrieves the status and information for a specified database instance.
   167  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
   168  	resp, err := client.Get(resourceURL(client, id), &r.Body, nil)
   169  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   170  	return
   171  }
   172  
   173  // Delete permanently destroys the database instance.
   174  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   175  	resp, err := client.Delete(resourceURL(client, id), nil)
   176  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   177  	return
   178  }
   179  
   180  // EnableRootUser enables the login from any host for the root user and
   181  // provides the user with a generated root password.
   182  func EnableRootUser(client *gophercloud.ServiceClient, id string) (r EnableRootUserResult) {
   183  	resp, err := client.Post(userRootURL(client, id), nil, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
   184  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   185  	return
   186  }
   187  
   188  // IsRootEnabled checks an instance to see if root access is enabled. It returns
   189  // True if root user is enabled for the specified database instance or False
   190  // otherwise.
   191  func IsRootEnabled(client *gophercloud.ServiceClient, id string) (r IsRootEnabledResult) {
   192  	resp, err := client.Get(userRootURL(client, id), &r.Body, nil)
   193  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   194  	return
   195  }
   196  
   197  // Restart will restart only the MySQL Instance. Restarting MySQL will
   198  // erase any dynamic configuration settings that you have made within MySQL.
   199  // The MySQL service will be unavailable until the instance restarts.
   200  func Restart(client *gophercloud.ServiceClient, id string) (r ActionResult) {
   201  	b := map[string]interface{}{"restart": struct{}{}}
   202  	resp, err := client.Post(actionURL(client, id), &b, nil, nil)
   203  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   204  	return
   205  }
   206  
   207  // Resize changes the memory size of the instance, assuming a valid
   208  // flavorRef is provided. It will also restart the MySQL service.
   209  func Resize(client *gophercloud.ServiceClient, id, flavorRef string) (r ActionResult) {
   210  	b := map[string]interface{}{"resize": map[string]string{"flavorRef": flavorRef}}
   211  	resp, err := client.Post(actionURL(client, id), &b, nil, nil)
   212  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   213  	return
   214  }
   215  
   216  // ResizeVolume will resize the attached volume for an instance. It supports
   217  // only increasing the volume size and does not support decreasing the size.
   218  // The volume size is in gigabytes (GB) and must be an integer.
   219  func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) (r ActionResult) {
   220  	b := map[string]interface{}{"resize": map[string]interface{}{"volume": map[string]int{"size": size}}}
   221  	resp, err := client.Post(actionURL(client, id), &b, nil, nil)
   222  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   223  	return
   224  }
   225  
   226  // AttachConfigurationGroup will attach configuration group to the instance
   227  func AttachConfigurationGroup(client *gophercloud.ServiceClient, instanceID string, configID string) (r ConfigurationResult) {
   228  	b := map[string]interface{}{"instance": map[string]interface{}{"configuration": configID}}
   229  	resp, err := client.Put(resourceURL(client, instanceID), &b, nil, &gophercloud.RequestOpts{OkCodes: []int{202}})
   230  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   231  	return
   232  }
   233  
   234  // DetachConfigurationGroup will dettach configuration group from the instance
   235  func DetachConfigurationGroup(client *gophercloud.ServiceClient, instanceID string) (r ConfigurationResult) {
   236  	b := map[string]interface{}{"instance": map[string]interface{}{}}
   237  	resp, err := client.Put(resourceURL(client, instanceID), &b, nil, &gophercloud.RequestOpts{OkCodes: []int{202}})
   238  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   239  	return
   240  }