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

     1  package configurations
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/openstack/db/v1/instances"
     6  	"github.com/gophercloud/gophercloud/pagination"
     7  )
     8  
     9  // List will list all of the available configurations.
    10  func List(client *gophercloud.ServiceClient) pagination.Pager {
    11  	return pagination.NewPager(client, baseURL(client), func(r pagination.PageResult) pagination.Page {
    12  		return ConfigPage{pagination.SinglePageBase(r)}
    13  	})
    14  }
    15  
    16  // CreateOptsBuilder is a top-level interface which renders a JSON map.
    17  type CreateOptsBuilder interface {
    18  	ToConfigCreateMap() (map[string]interface{}, error)
    19  }
    20  
    21  // DatastoreOpts is the primary options struct for creating and modifying
    22  // how configuration resources are associated with datastores.
    23  type DatastoreOpts struct {
    24  	// The type of datastore. Defaults to "MySQL".
    25  	Type string `json:"type,omitempty"`
    26  	// The specific version of a datastore. Defaults to "5.6".
    27  	Version string `json:"version,omitempty"`
    28  }
    29  
    30  // CreateOpts is the struct responsible for configuring new configurations.
    31  type CreateOpts struct {
    32  	// The configuration group name
    33  	Name string `json:"name" required:"true"`
    34  	// A map of user-defined configuration settings that will define
    35  	// how each associated datastore works. Each key/value pair is specific to a
    36  	// datastore type.
    37  	Values map[string]interface{} `json:"values" required:"true"`
    38  	// Associates the configuration group with a particular datastore.
    39  	Datastore *DatastoreOpts `json:"datastore,omitempty"`
    40  	// A human-readable explanation for the group.
    41  	Description string `json:"description,omitempty"`
    42  }
    43  
    44  // ToConfigCreateMap casts a CreateOpts struct into a JSON map.
    45  func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
    46  	return gophercloud.BuildRequestBody(opts, "configuration")
    47  }
    48  
    49  // Create will create a new configuration group.
    50  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    51  	b, err := opts.ToConfigCreateMap()
    52  	if err != nil {
    53  		r.Err = err
    54  		return
    55  	}
    56  	resp, err := client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
    57  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    58  	return
    59  }
    60  
    61  // Get will retrieve the details for a specified configuration group.
    62  func Get(client *gophercloud.ServiceClient, configID string) (r GetResult) {
    63  	resp, err := client.Get(resourceURL(client, configID), &r.Body, nil)
    64  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    65  	return
    66  }
    67  
    68  // UpdateOptsBuilder is the top-level interface for casting update options into
    69  // JSON maps.
    70  type UpdateOptsBuilder interface {
    71  	ToConfigUpdateMap() (map[string]interface{}, error)
    72  }
    73  
    74  // UpdateOpts is the struct responsible for modifying existing configurations.
    75  type UpdateOpts struct {
    76  	// The configuration group name
    77  	Name string `json:"name,omitempty"`
    78  	// A map of user-defined configuration settings that will define
    79  	// how each associated datastore works. Each key/value pair is specific to a
    80  	// datastore type.
    81  	Values map[string]interface{} `json:"values,omitempty"`
    82  	// Associates the configuration group with a particular datastore.
    83  	Datastore *DatastoreOpts `json:"datastore,omitempty"`
    84  	// A human-readable explanation for the group.
    85  	Description *string `json:"description,omitempty"`
    86  }
    87  
    88  // ToConfigUpdateMap will cast an UpdateOpts struct into a JSON map.
    89  func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
    90  	return gophercloud.BuildRequestBody(opts, "configuration")
    91  }
    92  
    93  // Update will modify an existing configuration group by performing a merge
    94  // between new and existing values. If the key already exists, the new value
    95  // will overwrite. All other keys will remain unaffected.
    96  func Update(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r UpdateResult) {
    97  	b, err := opts.ToConfigUpdateMap()
    98  	if err != nil {
    99  		r.Err = err
   100  		return
   101  	}
   102  	resp, err := client.Patch(resourceURL(client, configID), &b, nil, nil)
   103  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   104  	return
   105  }
   106  
   107  // Replace will modify an existing configuration group by overwriting the
   108  // entire parameter group with the new values provided. Any existing keys not
   109  // included in UpdateOptsBuilder will be deleted.
   110  func Replace(client *gophercloud.ServiceClient, configID string, opts UpdateOptsBuilder) (r ReplaceResult) {
   111  	b, err := opts.ToConfigUpdateMap()
   112  	if err != nil {
   113  		r.Err = err
   114  		return
   115  	}
   116  	resp, err := client.Put(resourceURL(client, configID), &b, nil, nil)
   117  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   118  	return
   119  }
   120  
   121  // Delete will permanently delete a configuration group. Please note that
   122  // config groups cannot be deleted whilst still attached to running instances -
   123  // you must detach and then delete them.
   124  func Delete(client *gophercloud.ServiceClient, configID string) (r DeleteResult) {
   125  	resp, err := client.Delete(resourceURL(client, configID), nil)
   126  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   127  	return
   128  }
   129  
   130  // ListInstances will list all the instances associated with a particular
   131  // configuration group.
   132  func ListInstances(client *gophercloud.ServiceClient, configID string) pagination.Pager {
   133  	return pagination.NewPager(client, instancesURL(client, configID), func(r pagination.PageResult) pagination.Page {
   134  		return instances.InstancePage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}}
   135  	})
   136  }
   137  
   138  // ListDatastoreParams will list all the available and supported parameters
   139  // that can be used for a particular datastore ID and a particular version.
   140  // For example, if you are wondering how you can configure a MySQL 5.6 instance,
   141  // you can use this operation (you will need to retrieve the MySQL datastore ID
   142  // by using the datastores API).
   143  func ListDatastoreParams(client *gophercloud.ServiceClient, datastoreID, versionID string) pagination.Pager {
   144  	return pagination.NewPager(client, listDSParamsURL(client, datastoreID, versionID), func(r pagination.PageResult) pagination.Page {
   145  		return ParamPage{pagination.SinglePageBase(r)}
   146  	})
   147  }
   148  
   149  // GetDatastoreParam will retrieve information about a specific configuration
   150  // parameter. For example, you can use this operation to understand more about
   151  // "innodb_file_per_table" configuration param for MySQL datastores. You will
   152  // need the param's ID first, which can be attained by using the ListDatastoreParams
   153  // operation.
   154  func GetDatastoreParam(client *gophercloud.ServiceClient, datastoreID, versionID, paramID string) (r ParamResult) {
   155  	resp, err := client.Get(getDSParamURL(client, datastoreID, versionID, paramID), &r.Body, nil)
   156  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   157  	return
   158  }
   159  
   160  // ListGlobalParams is similar to ListDatastoreParams but does not require a
   161  // DatastoreID.
   162  func ListGlobalParams(client *gophercloud.ServiceClient, versionID string) pagination.Pager {
   163  	return pagination.NewPager(client, listGlobalParamsURL(client, versionID), func(r pagination.PageResult) pagination.Page {
   164  		return ParamPage{pagination.SinglePageBase(r)}
   165  	})
   166  }
   167  
   168  // GetGlobalParam is similar to GetDatastoreParam but does not require a
   169  // DatastoreID.
   170  func GetGlobalParam(client *gophercloud.ServiceClient, versionID, paramID string) (r ParamResult) {
   171  	resp, err := client.Get(getGlobalParamURL(client, versionID, paramID), &r.Body, nil)
   172  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   173  	return
   174  }