github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/nodes/requests.go (about)

     1  package nodes
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // CreateOptsBuilder allows extensions to add additional parameters to the
     9  // Create request.
    10  type CreateOptsBuilder interface {
    11  	ToNodeCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts represents options used to create a Node.
    15  type CreateOpts struct {
    16  	Role      string                 `json:"role,omitempty"`
    17  	ProfileID string                 `json:"profile_id" required:"true"`
    18  	ClusterID string                 `json:"cluster_id,omitempty"`
    19  	Name      string                 `json:"name" required:"true"`
    20  	Metadata  map[string]interface{} `json:"metadata,omitempty"`
    21  }
    22  
    23  // ToNodeCreateMap constructs a request body from CreateOpts.
    24  func (opts CreateOpts) ToNodeCreateMap() (map[string]interface{}, error) {
    25  	return gophercloud.BuildRequestBody(opts, "node")
    26  }
    27  
    28  // Create requests the creation of a new node.
    29  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    30  	b, err := opts.ToNodeCreateMap()
    31  	if err != nil {
    32  		r.Err = err
    33  		return
    34  	}
    35  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    36  		OkCodes: []int{200, 201, 202},
    37  	})
    38  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    39  	return
    40  }
    41  
    42  // UpdateOptsBuilder allows extensions to add additional parameters to the
    43  // Update request.
    44  type UpdateOptsBuilder interface {
    45  	ToNodeUpdateMap() (map[string]interface{}, error)
    46  }
    47  
    48  // UpdateOpts represents options used to update a Node.
    49  type UpdateOpts struct {
    50  	Name      string                 `json:"name,omitempty"`
    51  	ProfileID string                 `json:"profile_id,omitempty"`
    52  	Role      string                 `json:"role,omitempty"`
    53  	Metadata  map[string]interface{} `json:"metadata,omitempty"`
    54  }
    55  
    56  // ToNodeUpdateMap constructs a request body from UpdateOpts.
    57  func (opts UpdateOpts) ToNodeUpdateMap() (map[string]interface{}, error) {
    58  	return gophercloud.BuildRequestBody(opts, "node")
    59  }
    60  
    61  // Update requests the update of a node.
    62  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    63  	b, err := opts.ToNodeUpdateMap()
    64  	if err != nil {
    65  		r.Err = err
    66  		return
    67  	}
    68  	resp, err := client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
    69  		OkCodes: []int{200, 201, 202},
    70  	})
    71  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    72  	return
    73  }
    74  
    75  // ListOptsBuilder allows extensions to add additional parmeters to the
    76  // List request.
    77  type ListOptsBuilder interface {
    78  	ToNodeListQuery() (string, error)
    79  }
    80  
    81  // ListOpts represents options used to list nodes.
    82  type ListOpts struct {
    83  	Limit         int    `q:"limit"`
    84  	Marker        string `q:"marker"`
    85  	Sort          string `q:"sort"`
    86  	GlobalProject *bool  `q:"global_project"`
    87  	ClusterID     string `q:"cluster_id"`
    88  	Name          string `q:"name"`
    89  	Status        string `q:"status"`
    90  }
    91  
    92  // ToNodeListQuery formats a ListOpts into a query string.
    93  func (opts ListOpts) ToNodeListQuery() (string, error) {
    94  	q, err := gophercloud.BuildQueryString(opts)
    95  	return q.String(), err
    96  }
    97  
    98  // List instructs OpenStack to provide a list of nodes.
    99  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   100  	url := listURL(client)
   101  	if opts != nil {
   102  		query, err := opts.ToNodeListQuery()
   103  		if err != nil {
   104  			return pagination.Pager{Err: err}
   105  		}
   106  		url += query
   107  	}
   108  
   109  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   110  		return NodePage{pagination.LinkedPageBase{PageResult: r}}
   111  	})
   112  }
   113  
   114  // Delete deletes the specified node.
   115  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   116  	resp, err := client.Delete(deleteURL(client, id), nil)
   117  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   118  	return
   119  }
   120  
   121  // Get makes a request against senlin to get a details of a node type
   122  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
   123  	resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
   124  		OkCodes: []int{200},
   125  	})
   126  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   127  	return
   128  }
   129  
   130  // OperationName represents valid values for node operation
   131  type OperationName string
   132  
   133  const (
   134  	// Nova Profile Op Names
   135  	RebootOperation         OperationName = "reboot"
   136  	RebuildOperation        OperationName = "rebuild"
   137  	ChangePasswordOperation OperationName = "change_password"
   138  	PauseOperation          OperationName = "pause"
   139  	UnpauseOperation        OperationName = "unpause"
   140  	SuspendOperation        OperationName = "suspend"
   141  	ResumeOperation         OperationName = "resume"
   142  	LockOperation           OperationName = "lock"
   143  	UnlockOperation         OperationName = "unlock"
   144  	StartOperation          OperationName = "start"
   145  	StopOperation           OperationName = "stop"
   146  	RescueOperation         OperationName = "rescue"
   147  	UnrescueOperation       OperationName = "unrescue"
   148  	EvacuateOperation       OperationName = "evacuate"
   149  
   150  	// Heat Pofile Op Names
   151  	AbandonOperation OperationName = "abandon"
   152  )
   153  
   154  // ToNodeOperationMap constructs a request body from OperationOpts.
   155  func (opts OperationOpts) ToNodeOperationMap() (map[string]interface{}, error) {
   156  	optsMap := map[string]interface{}{string(opts.Operation): opts.Params}
   157  	return optsMap, nil
   158  }
   159  
   160  // OperationOptsBuilder allows extensions to add additional parameters to the
   161  // Op request.
   162  type OperationOptsBuilder interface {
   163  	ToNodeOperationMap() (map[string]interface{}, error)
   164  }
   165  type OperationParams map[string]interface{}
   166  
   167  // OperationOpts represents options used to perform an operation on a node
   168  type OperationOpts struct {
   169  	Operation OperationName   `json:"operation" required:"true"`
   170  	Params    OperationParams `json:"params,omitempty"`
   171  }
   172  
   173  func Ops(client *gophercloud.ServiceClient, id string, opts OperationOptsBuilder) (r ActionResult) {
   174  	b, err := opts.ToNodeOperationMap()
   175  	if err != nil {
   176  		r.Err = err
   177  		return
   178  	}
   179  	resp, err := client.Post(opsURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   180  		OkCodes: []int{202},
   181  	})
   182  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   183  	return
   184  }
   185  
   186  func (opts RecoverOpts) ToNodeRecoverMap() (map[string]interface{}, error) {
   187  	return gophercloud.BuildRequestBody(opts, "recover")
   188  }
   189  
   190  // RecoverAction represents valid values for recovering a node.
   191  type RecoverAction string
   192  
   193  const (
   194  	RebootRecovery  RecoverAction = "REBOOT"
   195  	RebuildRecovery RecoverAction = "REBUILD"
   196  	// RECREATE currently is NOT supported. See https://github.com/openstack/senlin/blob/b30b2b8496b2b8af243ccd5292f38aec7a95664f/senlin/profiles/base.py#L533
   197  	RecreateRecovery RecoverAction = "RECREATE"
   198  )
   199  
   200  type RecoverOpts struct {
   201  	Operation RecoverAction `json:"operation,omitempty"`
   202  	Check     *bool         `json:"check,omitempty"`
   203  }
   204  
   205  func Recover(client *gophercloud.ServiceClient, id string, opts RecoverOpts) (r ActionResult) {
   206  	b, err := opts.ToNodeRecoverMap()
   207  	if err != nil {
   208  		r.Err = err
   209  		return
   210  	}
   211  	resp, err := client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   212  		OkCodes: []int{202},
   213  	})
   214  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   215  	return
   216  }
   217  
   218  func Check(client *gophercloud.ServiceClient, id string) (r ActionResult) {
   219  	b := map[string]interface{}{
   220  		"check": map[string]interface{}{},
   221  	}
   222  	resp, err := client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   223  		OkCodes: []int{202},
   224  	})
   225  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   226  	return
   227  }