github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/iec/v1/servers/requests.go (about)

     1  package servers
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/ecs/v1/cloudservers"
     8  	"github.com/huaweicloud/golangsdk/openstack/iec/v1/common"
     9  )
    10  
    11  // GetServer get server detail
    12  func GetServer(client *golangsdk.ServiceClient, serverID string) (r GetResult) {
    13  	GetUrl := getURL(client, serverID)
    14  
    15  	var resp *http.Response
    16  	resp, r.Err = client.Get(GetUrl, &r.Body, &golangsdk.RequestOpts{
    17  		OkCodes: []int{http.StatusOK},
    18  	})
    19  	if r.Err != nil {
    20  		return
    21  	}
    22  	defer resp.Body.Close()
    23  
    24  	return
    25  }
    26  
    27  // UpdateOptsBuilder to handle Delete request's body
    28  type UpdateOptsBuilder interface {
    29  	ToUpdateBodyMap() (map[string]interface{}, error)
    30  }
    31  
    32  // UpdateInstance to update one server
    33  type UpdateInstance struct {
    34  	UpdateServer UpdateOpts `json:"server"`
    35  }
    36  
    37  // UpdateOpts to update one server options
    38  type UpdateOpts struct {
    39  	Name        *string `json:"name,omitempty"`
    40  	Description *string `json:"description,omitempty"`
    41  }
    42  
    43  // ToUpdateBodyMap converts UpdateInstance structures to map[string]interface{}
    44  func (opts UpdateInstance) ToUpdateBodyMap() (map[string]interface{}, error) {
    45  	body, err := golangsdk.BuildRequestBody(opts, "")
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return body, nil
    50  }
    51  
    52  // UpdateServer updates one server in UpdateOptsBuilder
    53  func UpdateServer(client *golangsdk.ServiceClient, opts UpdateOptsBuilder, serverID string) (r UpdateResult) {
    54  	body, err := opts.ToUpdateBodyMap()
    55  	if err != nil {
    56  		r.Err = err
    57  		return
    58  	}
    59  
    60  	updateURL := updateURL(client, serverID)
    61  
    62  	var resp *http.Response
    63  	resp, r.Err = client.Put(updateURL, body, &r.Body, &golangsdk.RequestOpts{
    64  		OkCodes: []int{http.StatusOK},
    65  	})
    66  	if r.Err != nil {
    67  		return
    68  	}
    69  	defer resp.Body.Close()
    70  
    71  	return
    72  }
    73  
    74  // CreateOptsBuilder allows extensions to add additional parameters to the
    75  // Create request.
    76  type CreateOptsBuilder interface {
    77  	ToServerCreateMap() (map[string]interface{}, error)
    78  }
    79  
    80  // ToServerCreateMap assembles a request body based on the contents of a
    81  // CreateOpts.
    82  func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
    83  	b, err := golangsdk.BuildRequestBody(opts, "")
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return map[string]interface{}{"server": b}, nil
    89  }
    90  
    91  type CreateOpts struct {
    92  	common.ResourceOpts
    93  	Coverage common.Coverage `json:"coverage" required:"true"`
    94  }
    95  
    96  // CreateServer requests a server to be provisioned to the user in the current tenant with response entity.
    97  func CreateServer(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (createResult CreateCloudServerResponse, err error) {
    98  	var r CreateResult
    99  	reqBody, err := opts.ToServerCreateMap()
   100  	if err != nil {
   101  		return
   102  	}
   103  
   104  	createURL := rootURL(client)
   105  	var resp *http.Response
   106  	resp, err = client.Post(createURL, reqBody, &r.Body, &golangsdk.RequestOpts{
   107  		OkCodes: []int{http.StatusAccepted},
   108  	})
   109  	if err != nil {
   110  		return
   111  	}
   112  
   113  	job, errJob := r.ExtractJob()
   114  	if errJob != nil {
   115  		err = errJob
   116  		return
   117  	}
   118  	server, errServer := r.ExtractServer()
   119  	if errServer != nil {
   120  		err = errServer
   121  		return
   122  	}
   123  	createResult = CreateCloudServerResponse{
   124  		Job:       job,
   125  		ServerIDs: server,
   126  	}
   127  
   128  	defer resp.Body.Close()
   129  	return
   130  }
   131  
   132  // DeleteOptsBuilder to handle Delete request's body
   133  type DeleteOptsBuilder interface {
   134  	ToDeleteBodyMap() (map[string]interface{}, error)
   135  }
   136  
   137  // DeleteOpts to delete all Servers in server array
   138  type DeleteOpts struct {
   139  	Servers []cloudservers.Server `json:"servers"`
   140  }
   141  
   142  // ToDeleteBodyMap converts DeleteOpts structures to map[string]interface{}
   143  func (opts DeleteOpts) ToDeleteBodyMap() (map[string]interface{}, error) {
   144  	body, err := golangsdk.BuildRequestBody(opts, "")
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return body, nil
   150  }
   151  
   152  // DeleteServers deletes all server in DeleteOptsBuilder
   153  func DeleteServers(client *golangsdk.ServiceClient, opts DeleteOptsBuilder) (r DeleteResult) {
   154  	body, err := opts.ToDeleteBodyMap()
   155  	if err != nil {
   156  		r.Err = err
   157  		return
   158  	}
   159  	deleteURL := deleteAllServersURL(client)
   160  
   161  	var resp *http.Response
   162  	resp, r.Err = client.Post(deleteURL, body, nil, &golangsdk.RequestOpts{
   163  		OkCodes: []int{http.StatusAccepted},
   164  	})
   165  	if r.Err != nil {
   166  		return
   167  	}
   168  	defer resp.Body.Close()
   169  
   170  	return
   171  }
   172  
   173  type ListOpts struct {
   174  	Limit       int    `q:"limit"`
   175  	Offset      int    `q:"offset"`
   176  	Name        string `q:"name"`
   177  	Area        string `q:"area"`
   178  	City        string `q:"city"`
   179  	Province    string `q:"province"`
   180  	EdgeCloudID string `q:"edgecloud_id"`
   181  	Status      string `q:"status"`
   182  }
   183  
   184  type ListServersOptsBuilder interface {
   185  	ToListServersQuery() (string, error)
   186  }
   187  
   188  func (opts ListOpts) ToListServersQuery() (string, error) {
   189  	b, err := golangsdk.BuildQueryString(&opts)
   190  	if err != nil {
   191  		return "", err
   192  	}
   193  	return b.String(), nil
   194  }
   195  
   196  func List(client *golangsdk.ServiceClient, opts ListServersOptsBuilder) (r ListResult) {
   197  	listURL := rootURL(client)
   198  	if opts != nil {
   199  		query, err := opts.ToListServersQuery()
   200  		if err != nil {
   201  			r.Err = err
   202  			return r
   203  		}
   204  		listURL += query
   205  	}
   206  
   207  	_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
   208  		OkCodes: []int{http.StatusOK},
   209  	})
   210  	return
   211  }