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

     1  package hosts
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     8  )
     9  
    10  // AllocateOptsBuilder allows extensions to add additional parameters to the
    11  // Allocate request.
    12  type AllocateOptsBuilder interface {
    13  	ToDeHAllocateMap() (map[string]interface{}, error)
    14  }
    15  
    16  // AllocateOpts contains all the values needed to allocate a new DeH.
    17  type AllocateOpts struct {
    18  	Name          string `json:"name" required:"true"`
    19  	Az            string `json:"availability_zone" required:"true"`
    20  	AutoPlacement string `json:"auto_placement,omitempty"`
    21  	HostType      string `json:"host_type" required:"true"`
    22  	Quantity      int    `json:"quantity" required:"true"`
    23  }
    24  
    25  // ToDeHAllocateMap builds a allocate request body from AllocateOpts.
    26  func (opts AllocateOpts) ToDeHAllocateMap() (map[string]interface{}, error) {
    27  	return golangsdk.BuildRequestBody(opts, "")
    28  }
    29  
    30  // Allocate accepts a AllocateOpts struct and uses the values to allocate a new DeH.
    31  func Allocate(c *golangsdk.ServiceClient, opts AllocateOptsBuilder) (r AllocateResult) {
    32  	b, err := opts.ToDeHAllocateMap()
    33  	if err != nil {
    34  		r.Err = err
    35  		return
    36  	}
    37  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200, 201}}
    38  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    39  	return
    40  }
    41  
    42  // UpdateOptsBuilder allows extensions to add additional parameters to the
    43  // Update request.
    44  type UpdateOptsBuilder interface {
    45  	ToDeHUpdateMap() (map[string]interface{}, error)
    46  }
    47  
    48  // UpdateOpts contains all the values needed to update a DeH.
    49  type UpdateOpts struct {
    50  	Name          string `json:"name,omitempty"`
    51  	AutoPlacement string `json:"auto_placement,omitempty"`
    52  }
    53  
    54  // ToDeHUpdateMap builds a update request body from UpdateOpts.
    55  func (opts UpdateOpts) ToDeHUpdateMap() (map[string]interface{}, error) {
    56  	return golangsdk.BuildRequestBody(opts, "dedicated_host")
    57  }
    58  
    59  // Update accepts a UpdateOpts struct and uses the values to update a DeH.The response code from api is 204
    60  func Update(c *golangsdk.ServiceClient, hostID string, opts UpdateOptsBuilder) (r UpdateResult) {
    61  	b, err := opts.ToDeHUpdateMap()
    62  	if err != nil {
    63  		r.Err = err
    64  		return
    65  	}
    66  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
    67  	_, r.Err = c.Put(resourceURL(c, hostID), b, nil, reqOpt)
    68  	return
    69  }
    70  
    71  // Deletes the DeH using the specified hostID.
    72  func Delete(c *golangsdk.ServiceClient, hostid string) (r DeleteResult) {
    73  	_, r.Err = c.Delete(resourceURL(c, hostid), nil)
    74  	return
    75  }
    76  
    77  // ListOpts allows the filtering and sorting of paginated collections through
    78  // the API.
    79  type ListOpts struct {
    80  	// Specifies Dedicated Host ID.
    81  	ID string `q:"dedicated_host_id"`
    82  	// Specifies the Dedicated Host name.
    83  	Name string `q:"name"`
    84  	// Specifes the Dedicated Host type.
    85  	HostType string `q:"host_type"`
    86  	// Specifes the Dedicated Host name of type.
    87  	HostTypeName string `q:"host_type_name"`
    88  	// Specifies flavor ID.
    89  	Flavor string `q:"flavor"`
    90  	// Specifies the Dedicated Host status.
    91  	// The value can be available, fault or released.
    92  	State string `q:"state"`
    93  	// Specifies the AZ to which the Dedicated Host belongs.
    94  	Az string `q:"availability_zone"`
    95  	// Specifies the number of entries displayed on each page.
    96  	Limit string `q:"limit"`
    97  	// 	The value is the ID of the last record on the previous page.
    98  	Marker string `q:"marker"`
    99  	// Filters the response by a date and time stamp when the dedicated host last changed status.
   100  	ChangesSince string `q:"changes-since"`
   101  	// Specifies the UUID of the tenant in a multi-tenancy cloud.
   102  	TenantId string `q:"tenant"`
   103  }
   104  
   105  // ListOptsBuilder allows extensions to add parameters to the List request.
   106  type ListOptsBuilder interface {
   107  	ToHostListQuery() (string, error)
   108  }
   109  
   110  // ToRegionListQuery formats a ListOpts into a query string.
   111  func (opts ListOpts) ToHostListQuery() (string, error) {
   112  	q, err := golangsdk.BuildQueryString(opts)
   113  	if err != nil {
   114  		return "", err
   115  	}
   116  	return q.String(), err
   117  }
   118  
   119  // List returns a Pager which allows you to iterate over a collection of
   120  // dedicated hosts resources. It accepts a ListOpts struct, which allows you to
   121  // filter the returned collection for greater efficiency.
   122  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   123  	url := rootURL(c)
   124  	if opts != nil {
   125  		query, err := opts.ToHostListQuery()
   126  		if err != nil {
   127  			return pagination.Pager{Err: err}
   128  		}
   129  		url += query
   130  	}
   131  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   132  		return HostPage{pagination.LinkedPageBase{PageResult: r}}
   133  	})
   134  
   135  }
   136  
   137  // Get retrieves a particular host based on its unique ID.
   138  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   139  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   140  	return
   141  }
   142  
   143  // ListServerOpts allows the filtering and sorting of paginated collections through
   144  // the API. Filtering is achieved by passing in struct field values that map to
   145  // the server attributes you want to see returned. Marker and Limit are used
   146  // for pagination.
   147  type ListServerOpts struct {
   148  	// Specifies the number of entries displayed on each page.
   149  	Limit int `q:"limit"`
   150  	// The value is the ID of the last record on the previous page.
   151  	// If the marker value is invalid, error code 400 will be returned.
   152  	Marker string `q:"marker"`
   153  	// ID uniquely identifies this server amongst all other servers,
   154  	// including those not accessible to the current tenant.
   155  	ID string `json:"id"`
   156  	// Name contains the human-readable name for the server.
   157  	Name string `json:"name"`
   158  	// Status contains the current operational status of the server,
   159  	// such as IN_PROGRESS or ACTIVE.
   160  	Status string `json:"status"`
   161  	// UserID uniquely identifies the user account owning the tenant.
   162  	UserID string `json:"user_id"`
   163  }
   164  
   165  // ListServer returns a Pager which allows you to iterate over a collection of
   166  // dedicated hosts Server resources. It accepts a ListServerOpts struct, which allows you to
   167  // filter the returned collection for greater efficiency.
   168  func ListServer(c *golangsdk.ServiceClient, id string, opts ListServerOpts) ([]Server, error) {
   169  	q, err := golangsdk.BuildQueryString(&opts)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	u := listServerURL(c, id) + q.String()
   174  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
   175  		return ServerPage{pagination.LinkedPageBase{PageResult: r}}
   176  	}).AllPages()
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  
   181  	allservers, err := ExtractServers(pages)
   182  	if err != nil {
   183  		return nil, err
   184  	}
   185  
   186  	return FilterServers(allservers, opts)
   187  }
   188  
   189  func FilterServers(servers []Server, opts ListServerOpts) ([]Server, error) {
   190  
   191  	var refinedServers []Server
   192  	var matched bool
   193  	m := map[string]interface{}{}
   194  
   195  	if opts.ID != "" {
   196  		m["ID"] = opts.ID
   197  	}
   198  	if opts.Name != "" {
   199  		m["Name"] = opts.Name
   200  	}
   201  	if opts.Status != "" {
   202  		m["Status"] = opts.Status
   203  	}
   204  	if opts.UserID != "" {
   205  		m["UserID"] = opts.UserID
   206  	}
   207  
   208  	if len(m) > 0 && len(servers) > 0 {
   209  		for _, server := range servers {
   210  			matched = true
   211  
   212  			for key, value := range m {
   213  				if sVal := getStructServerField(&server, key); !(sVal == value) {
   214  					matched = false
   215  				}
   216  			}
   217  
   218  			if matched {
   219  				refinedServers = append(refinedServers, server)
   220  			}
   221  		}
   222  
   223  	} else {
   224  		refinedServers = servers
   225  	}
   226  
   227  	return refinedServers, nil
   228  }
   229  
   230  func getStructServerField(v *Server, field string) string {
   231  	r := reflect.ValueOf(v)
   232  	f := reflect.Indirect(r).FieldByName(field)
   233  	return f.String()
   234  }