github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/db/v1/configurations/requests.go (about)

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