github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dayu/v1/instances/requests.go (about) 1 package instances 2 3 import ( 4 "encoding/json" 5 6 "github.com/chnsz/golangsdk" 7 "github.com/chnsz/golangsdk/openstack/common/tags" 8 "github.com/chnsz/golangsdk/pagination" 9 ) 10 11 // CreateOptsBuilder allows extensions to add additional parameters to the 12 // Create request. 13 type CreateOptsBuilder interface { 14 ToInstanceCreateMap() (map[string]interface{}, error) 15 } 16 17 // CreateOpts contains all the values needed to create a new instance. 18 type CreateOpts struct { 19 Region string `json:"region_id" required:"true"` 20 AvailabilityZone string `json:"availability_zone" required:"true"` 21 Name string `json:"instance_name" required:"true"` 22 SpecCode string `json:"resource_spec_code" required:"true"` 23 VpcID string `json:"vpc_id" required:"true"` 24 SubnetID string `json:"net_id" required:"true"` 25 SecurityGroupID string `json:"security_group_id" required:"true"` 26 EnterpriseProjectID string `json:"eps_id,omitempty"` 27 28 // 2 - monthly; 3 - yearly 29 PeriodType int `json:"period_type" required:"true"` 30 // monthly: 1-9; yearly: 1-3 31 PeriodNum int `json:"period_num" required:"true"` 32 // 0 - false; 1 - true 33 IsAutoRenew *int `json:"is_auto_renew" required:"true"` 34 35 Tags []tags.ResourceTag `json:"tags,omitempty"` 36 37 // the following parameters are reserved 38 ProductID string `json:"product_id,omitempty"` 39 CommodityID string `json:"commodity_id,omitempty"` 40 PromotionInfo string `json:"promotion_info,omitempty"` 41 ExtPackageType string `json:"extesion_package_type,omitempty"` 42 BindingInstanceID string `json:"binding_instance_id,omitempty"` 43 CdmVersion string `json:"cdm_version,omitempty"` 44 CloudServiceType string `json:"cloud_service_type,omitempty"` 45 ResourceType string `json:"resource_type,omitempty"` 46 } 47 48 // ToInstanceCreateMap builds a create request body from CreateOpts. 49 func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) { 50 return golangsdk.BuildRequestBody(opts, "") 51 } 52 53 // Create accepts a CreateOpts struct and uses the values to create a new instance. 54 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 55 b, err := opts.ToInstanceCreateMap() 56 if err != nil { 57 r.Err = err 58 return 59 } 60 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 61 _, r.Err = c.Post(createURL(c), b, &r.Body, reqOpt) 62 return 63 } 64 65 // ListOpts allows the filtering collections through the API. 66 type ListOpts struct { 67 // Limit is the records count to be queried, the value ranges 1-100, the default value is 20 68 Limit int `q:"limit"` 69 70 // Offset number, the default value is 0 71 Offset int `q:"offset"` 72 } 73 74 // ToInstanceListQuery formats a ListOpts into a query string 75 func (opts ListOpts) ToInstanceListQuery() (string, error) { 76 q, err := golangsdk.BuildQueryString(opts) 77 if err != nil { 78 return "", err 79 } 80 return q.String(), nil 81 } 82 83 // List returns collection of instances. 84 func List(c *golangsdk.ServiceClient, opts *ListOpts) ([]Instance, error) { 85 url := listURL(c) 86 if opts != nil { 87 query, err := opts.ToInstanceListQuery() 88 if err != nil { 89 return nil, err 90 } 91 url += query 92 } 93 94 var s struct { 95 Instances []Instance `json:"commodity_orders"` 96 } 97 _, err := c.Get(url, &s, nil) 98 if err != nil { 99 return nil, err 100 } 101 102 return s.Instances, nil 103 } 104 105 // ListAll returns all of instances start with initURL. 106 func ListAll(c *golangsdk.ServiceClient, opts *ListOpts) ([]Instance, error) { 107 url := listURL(c) 108 if opts != nil { 109 query, err := opts.ToInstanceListQuery() 110 if err != nil { 111 return nil, err 112 } 113 url += query 114 } 115 116 resp, err := pagination.ListAllItems(c, pagination.Offset, url, nil) 117 if err != nil { 118 return nil, err 119 } 120 121 body, err := json.Marshal(resp) 122 if err != nil { 123 return nil, err 124 } 125 126 var s struct { 127 Instances []Instance `json:"commodity_orders"` 128 } 129 if err := json.Unmarshal(body, &s); err != nil { 130 return nil, err 131 } 132 133 return s.Instances, nil 134 }