github.com/gophercloud/gophercloud@v1.11.0/openstack/db/v1/instances/requests.go (about)

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