github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/sdrs/v1/protectedinstances/requests.go (about)

     1  package protectedinstances
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/openstack"
     6  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     7  )
     8  
     9  // CreateOptsBuilder allows extensions to add additional parameters to the
    10  // Create request.
    11  type CreateOptsBuilder interface {
    12  	ToInstanceCreateMap() (map[string]interface{}, error)
    13  }
    14  
    15  // CreateOpts contains all the values needed to create a new instance.
    16  type CreateOpts struct {
    17  	// Group ID
    18  	GroupID string `json:"server_group_id" required:"true"`
    19  	// Server ID
    20  	ServerID string `json:"server_id" required:"true"`
    21  	// Instance Name
    22  	Name string `json:"name" required:"true"`
    23  	// Instance Description
    24  	Description string `json:"description,omitempty"`
    25  	// Cluster ID
    26  	ClusterID string `json:"cluster_id,omitempty"`
    27  	// Subnet ID
    28  	SubnetID string `json:"primary_subnet_id,omitempty"`
    29  	// IP Address
    30  	IpAddress string `json:"primary_ip_address,omitempty"`
    31  	// Flavor ID of the DR site server
    32  	Flavor string `json:"flavorRef,omitempty"`
    33  	// Tags list
    34  	Tags []Tags `json:"tags,omitempty"`
    35  }
    36  
    37  type Tags struct {
    38  	Key   string `json:"key,omitempty"`
    39  	Value string `json:"value,omitempty"`
    40  }
    41  
    42  // ToInstanceCreateMap builds a create request body from CreateOpts.
    43  func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
    44  	return golangsdk.BuildRequestBody(opts, "protected_instance")
    45  }
    46  
    47  // Create will create a new Instance based on the values in CreateOpts.
    48  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
    49  	b, err := opts.ToInstanceCreateMap()
    50  	if err != nil {
    51  		r.Err = err
    52  		return
    53  	}
    54  	_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
    55  		OkCodes: []int{200},
    56  	})
    57  	return
    58  }
    59  
    60  // UpdateOptsBuilder allows extensions to add additional parameters to the
    61  // Update request.
    62  type UpdateOptsBuilder interface {
    63  	ToInstanceUpdateMap() (map[string]interface{}, error)
    64  }
    65  
    66  // UpdateOpts contains all the values needed to update an Instance.
    67  type UpdateOpts struct {
    68  	// Instance name
    69  	Name string `json:"name" required:"true"`
    70  }
    71  
    72  // ToInstanceUpdateMap builds a update request body from UpdateOpts.
    73  func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) {
    74  	return golangsdk.BuildRequestBody(opts, "protected_instance")
    75  }
    76  
    77  // Update accepts a UpdateOpts struct and uses the values to update an Instance.The response code from api is 200
    78  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    79  	b, err := opts.ToInstanceUpdateMap()
    80  	if err != nil {
    81  		r.Err = err
    82  		return
    83  	}
    84  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
    85  		OkCodes: []int{200},
    86  	})
    87  	return
    88  }
    89  
    90  // Get retrieves a particular Instance based on its unique ID.
    91  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    92  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, openstack.StdRequestOpts())
    93  	return
    94  }
    95  
    96  // DeleteOptsBuilder allows extensions to add additional parameters to the
    97  // Delete request.
    98  type DeleteOptsBuilder interface {
    99  	ToInstanceDeleteMap() (map[string]interface{}, error)
   100  }
   101  
   102  // DeleteOpts contains all the values needed to delete an Instance.
   103  type DeleteOpts struct {
   104  	// Delete Target Server
   105  	DeleteTargetServer *bool `json:"delete_target_server,omitempty"`
   106  	// Delete Target Eip
   107  	DeleteTargetEip *bool `json:"delete_target_eip,omitempty"`
   108  }
   109  
   110  // ToInstanceDeleteMap builds a update request body from DeleteOpts.
   111  func (opts DeleteOpts) ToInstanceDeleteMap() (map[string]interface{}, error) {
   112  	return golangsdk.BuildRequestBody(opts, "")
   113  }
   114  
   115  // Delete will permanently delete a particular Instance based on its unique ID.
   116  func Delete(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r JobResult) {
   117  	b, err := opts.ToInstanceDeleteMap()
   118  	if err != nil {
   119  		r.Err = err
   120  		return
   121  	}
   122  	_, r.Err = c.DeleteWithBodyResp(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   123  		OkCodes: []int{200},
   124  	})
   125  	return
   126  }
   127  
   128  type ListOptsBuilder interface {
   129  	ToInstanceListQuery() (string, error)
   130  }
   131  
   132  type ListOpts struct {
   133  	ServerGroupID        string   `q:"server_group_id"`
   134  	ServerGroupIDs       []string `q:"server_group_ids"`
   135  	ProtectedInstanceIDs []string `q:"protected_instance_ids"`
   136  	Limit                int      `q:"limit"`
   137  	Offset               int      `q:"offset"`
   138  	Status               string   `q:"status"`
   139  	Name                 string   `q:"name"`
   140  	QueryType            string   `q:"query_type"`
   141  	AvailabilityZone     string   `q:"availability_zone"`
   142  }
   143  
   144  func (opts ListOpts) ToInstanceListQuery() (string, error) {
   145  	q, err := golangsdk.BuildQueryString(opts)
   146  	if err != nil {
   147  		return "", err
   148  	}
   149  	return q.String(), nil
   150  }
   151  
   152  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   153  	url := rootURL(c)
   154  	if opts != nil {
   155  		q, err := opts.ToInstanceListQuery()
   156  		if err != nil {
   157  			return pagination.Pager{Err: err}
   158  		}
   159  		url += q
   160  	}
   161  
   162  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   163  		return InstancePage{pagination.SinglePageBase(r)}
   164  	})
   165  }