github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/servergroups/requests.go (about)

     1  package servergroups
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // List returns a Pager that allows you to iterate over a collection of
     9  // ServerGroups.
    10  func List(client *golangsdk.ServiceClient) pagination.Pager {
    11  	return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
    12  		return ServerGroupPage{pagination.SinglePageBase(r)}
    13  	})
    14  }
    15  
    16  // CreateOptsBuilder allows extensions to add additional parameters to the
    17  // Create request.
    18  type CreateOptsBuilder interface {
    19  	ToServerGroupCreateMap() (map[string]interface{}, error)
    20  }
    21  
    22  // CreateOpts specifies Server Group creation parameters.
    23  type CreateOpts struct {
    24  	// Name is the name of the server group
    25  	Name string `json:"name" required:"true"`
    26  
    27  	// Policies are the server group policies
    28  	Policies []string `json:"policies" required:"true"`
    29  }
    30  
    31  // ToServerGroupCreateMap constructs a request body from CreateOpts.
    32  func (opts CreateOpts) ToServerGroupCreateMap() (map[string]interface{}, error) {
    33  	return golangsdk.BuildRequestBody(opts, "server_group")
    34  }
    35  
    36  // Create requests the creation of a new Server Group.
    37  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    38  	b, err := opts.ToServerGroupCreateMap()
    39  	if err != nil {
    40  		r.Err = err
    41  		return
    42  	}
    43  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    44  		OkCodes: []int{200},
    45  	})
    46  	return
    47  }
    48  
    49  // Get returns data about a previously created ServerGroup.
    50  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    51  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    52  	return
    53  }
    54  
    55  // Delete requests the deletion of a previously allocated ServerGroup.
    56  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    57  	_, r.Err = client.Delete(deleteURL(client, id), nil)
    58  	return
    59  }
    60  
    61  type MemberOptsBuilder interface {
    62  	ToServerGroupUpdateMemberMap(string) (map[string]interface{}, error)
    63  }
    64  
    65  type MemberOpts struct {
    66  	InstanceUUid string `json:"instance_uuid" required:"true"`
    67  }
    68  
    69  func (opts MemberOpts) ToServerGroupUpdateMemberMap(optsType string) (map[string]interface{}, error) {
    70  	return golangsdk.BuildRequestBody(opts, optsType)
    71  }
    72  
    73  // UpdateMembers is used to add and delete members from Server Group.
    74  // (params)optsType: The opts type is title of block in request body.
    75  //                   Add options is add_memebr and remove options is remove_member.
    76  func UpdateMember(client *golangsdk.ServiceClient, opts MemberOptsBuilder, optsType, id string) (r MemberResult) {
    77  	b, err := opts.ToServerGroupUpdateMemberMap(optsType)
    78  	if err != nil {
    79  		r.Err = err
    80  		return
    81  	}
    82  	_, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{
    83  		OkCodes: []int{200, 202},
    84  	})
    85  	return
    86  }