github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/bandwidths/requests.go (about) 1 package bandwidths 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 ) 6 7 type UpdateOpts struct { 8 Bandwidth Bandwidth `json:"bandwidth" required:"true"` 9 ExtendParam *ExtendParam `json:"extendParam,omitempty"` 10 } 11 type Bandwidth struct { 12 Name string `json:"name,omitempty"` 13 Size int `json:"size,omitempty"` 14 } 15 type ExtendParam struct { 16 IsAutoPay string `json:"is_auto_pay,omitempty"` 17 } 18 19 func (opts UpdateOpts) ToBandWidthUpdateMap() (map[string]interface{}, error) { 20 return golangsdk.BuildRequestBody(opts, "") 21 } 22 23 type CreateOptsBuilder interface { 24 ToBandWidthCreateMap() (map[string]interface{}, error) 25 } 26 27 type CreateOpts struct { 28 Name string `json:"name" required:"true"` 29 Size *int `json:"size" required:"true"` 30 EnterpriseProjectId string `json:"enterprise_project_id,omitempty"` 31 } 32 33 func (opts CreateOpts) ToBandWidthCreateMap() (map[string]interface{}, error) { 34 return golangsdk.BuildRequestBody(opts, "bandwidth") 35 } 36 37 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 38 b, err := opts.ToBandWidthCreateMap() 39 if err != nil { 40 r.Err = err 41 return 42 } 43 44 _, r.Err = client.Post(PostURL(client), b, &r.Body, &golangsdk.RequestOpts{ 45 OkCodes: []int{200, 201}, 46 }) 47 return 48 } 49 50 type BatchCreateOptsBuilder interface { 51 ToBandWidthBatchCreateMap() (map[string]interface{}, error) 52 } 53 54 type BatchCreateOpts struct { 55 Name string `json:"name" required:"true"` 56 Size *int `json:"size" required:"true"` 57 Count *int `json:"count" required:"true"` 58 } 59 60 func (opts BatchCreateOpts) ToBandWidthBatchCreateMap() (map[string]interface{}, error) { 61 return golangsdk.BuildRequestBody(opts, "bandwidth") 62 } 63 64 type BandWidthInsertOptsBuilder interface { 65 ToBandWidthInsertMap() (map[string]interface{}, error) 66 } 67 68 type BandWidthRemoveOptsBuilder interface { 69 ToBandWidthBatchRemoveMap() (map[string]interface{}, error) 70 } 71 72 type BandWidthInsertOpts struct { 73 PublicipInfo []PublicIpInfoID `json:"publicip_info" required:"true"` 74 } 75 76 func (opts BandWidthInsertOpts) ToBandWidthInsertMap() (map[string]interface{}, error) { 77 return golangsdk.BuildRequestBody(opts, "bandwidth") 78 } 79 80 type BandWidthRemoveOpts struct { 81 ChargeMode string `json:"charge_mode" required:"true"` 82 Size *int `json:"size" required:"true"` 83 PublicipInfo []PublicIpInfoID `json:"publicip_info" required:"true"` 84 } 85 86 func (opts BandWidthRemoveOpts) ToBandWidthBatchRemoveMap() (map[string]interface{}, error) { 87 return golangsdk.BuildRequestBody(opts, "bandwidth") 88 } 89 90 type PublicIpInfoID struct { 91 PublicIPID string `json:"publicip_id" required:"true"` 92 } 93 94 func Insert(client *golangsdk.ServiceClient, bandwidthID string, opts BandWidthInsertOptsBuilder) (r CreateResult) { 95 b, err := opts.ToBandWidthInsertMap() 96 if err != nil { 97 r.Err = err 98 return 99 } 100 101 _, r.Err = client.Post(InsertURL(client, bandwidthID), b, &r.Body, &golangsdk.RequestOpts{ 102 OkCodes: []int{200, 201}, 103 }) 104 return 105 } 106 107 func Remove(client *golangsdk.ServiceClient, bandwidthID string, opts BandWidthRemoveOptsBuilder) (r DeleteResult) { 108 b, err := opts.ToBandWidthBatchRemoveMap() 109 if err != nil { 110 r.Err = err 111 return 112 } 113 114 _, r.Err = client.Post(RemoveURL(client, bandwidthID), b, nil, &golangsdk.RequestOpts{ 115 OkCodes: []int{200, 204}, 116 }) 117 return 118 } 119 120 func BatchCreate(client *golangsdk.ServiceClient, opts BatchCreateOptsBuilder) (r BatchCreateResult) { 121 b, err := opts.ToBandWidthBatchCreateMap() 122 if err != nil { 123 r.Err = err 124 return 125 } 126 127 _, r.Err = client.Post(BatchPostURL(client), b, &r.Body, &golangsdk.RequestOpts{ 128 OkCodes: []int{200, 201}, 129 }) 130 return 131 } 132 133 func Delete(client *golangsdk.ServiceClient, bandwidthID string) (r DeleteResult) { 134 url := DeleteURL(client, bandwidthID) 135 _, r.Err = client.Delete(url, nil) 136 return 137 } 138 139 func Update(c *golangsdk.ServiceClient, bandwidthID string, opts UpdateOpts) (interface{}, error) { 140 var r UpdateResult 141 body, err := opts.ToBandWidthUpdateMap() 142 if err != nil { 143 return nil, err 144 } 145 146 _, r.Err = c.Put(UpdateURL(c, bandwidthID), body, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 147 148 onDemandData, onDemandErr := r.Extract() 149 orderData, orderErr := r.ExtractOrderID() 150 151 if orderData.OrderID != "" { 152 return orderData, orderErr 153 } 154 155 return onDemandData, onDemandErr 156 157 }