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