github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dcs/v1/instances/requests.go (about) 1 package instances 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 // CreateOpsBuilder is used for creating instance parameters. 9 // any struct providing the parameters should implement this interface 10 type CreateOpsBuilder interface { 11 ToInstanceCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOps is a struct that contains all the parameters. 15 type CreateOps struct { 16 // DCS instance name. 17 // An instance name is a string of 4–64 characters 18 // that contain letters, digits, underscores (_), and hyphens (-). 19 // An instance name must start with letters. 20 Name string `json:"name" required:"true"` 21 22 // Brief description of the DCS instance. 23 // A brief description supports up to 1024 characters. 24 Description string `json:"description,omitempty"` 25 26 // Cache engine, which is Redis or Memcached. 27 Engine string `json:"engine" required:"true"` 28 29 // Cache engine version, When the cache engine is Redis, the value is 3.0, 4.0 or 5.0. 30 EngineVersion string `json:"engine_version"` 31 32 // Cache capacity. Unit: GB. 33 // Indicates the message storage space. 34 // 35 // Redis 3.0: Stand-alone and active/standby type instance values: 2, 4, 8, 16, 32, 64. 36 // Proxy cluster instance specifications support 64, 128, 256, 512, and 1024. 37 // Redis4.0 and 5.0: Stand-alone and active/standby type instance values: 0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 32, 64. 38 // Cluster instance specifications support 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024. 39 // Memcached: Stand-alone and active/standby type instance values: 2, 4, 8, 16, 32, 64. 40 Capacity float64 `json:"capacity" required:"true"` 41 42 // Indicate if no password visit cache instance is allowed. 43 NoPasswordAccess string `json:"no_password_access,omitempty"` 44 45 // Indicates the password of an instance. 46 // An instance password must meet the following complexity requirements: 47 48 // Password of a DCS instance. 49 // The password of a DCS Redis instance must meet 50 // the following complexity requirements: 51 // A string of 6–32 characters. 52 // Contains at least two of the following character types: 53 // Uppercase letters 54 // Lowercase letters 55 // Digits 56 // Special characters, such as `~!@#$%^&*()-_=+\|[{}]:'",<.>/? 57 Password string `json:"password,omitempty"` 58 59 // When NoPasswordAccess is flase, the AccessUser is enabled. 60 AccessUser string `json:"access_user,omitempty"` 61 62 // Tenant's VPC ID. 63 VPCID string `json:"vpc_id" required:"true"` 64 65 // Tenant's security group ID. 66 SecurityGroupID string `json:"security_group_id,omitempty"` 67 68 // Subnet ID. 69 SubnetID string `json:"subnet_id" required:"true"` 70 71 // IDs of the AZs where cache nodes reside. 72 // In the current version, only one AZ ID can be set in the request. 73 AvailableZones []string `json:"available_zones" required:"true"` 74 75 // Product ID used to differentiate DCS instance types. 76 ProductID string `json:"product_id" required:"true"` 77 78 // Backup policy. 79 // This parameter is available for master/standby DCS instances. 80 InstanceBackupPolicy *InstanceBackupPolicy `json:"instance_backup_policy,omitempty"` 81 82 // Indicates the time at which a maintenance time window starts. 83 // Format: HH:mm:ss 84 MaintainBegin string `json:"maintain_begin,omitempty"` 85 86 // Indicates the time at which a maintenance time window ends. 87 // Format: HH:mm:ss 88 MaintainEnd string `json:"maintain_end,omitempty"` 89 90 // Enterprise project id. 91 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 92 93 // Enterprise project name. 94 EnterpriseProjectName string `json:"enterprise_project_name,omitempty"` 95 96 // Port customization. 97 Port int `json:"port,omitempty"` 98 99 // DCS instance specification code. 100 SpecCode string `json:"spec_code,omitempty"` 101 } 102 103 // InstanceBackupPolicy for dcs 104 type InstanceBackupPolicy struct { 105 // Retention time. 106 // Unit: day. 107 // Range: 1–7. 108 SaveDays int `json:"save_days" required:"true"` 109 110 // Backup type. Options: 111 // auto: automatic backup. 112 // manual: manual backup. 113 BackupType string `json:"backup_type" required:"true"` 114 115 // Backup plan. 116 PeriodicalBackupPlan PeriodicalBackupPlan `json:"periodical_backup_plan" required:"true"` 117 } 118 119 // PeriodicalBackupPlan for dcs 120 type PeriodicalBackupPlan struct { 121 // Time at which backup starts. 122 // "00:00-01:00" indicates that backup starts at 00:00:00. 123 BeginAt string `json:"begin_at" required:"true"` 124 125 // Interval at which backup is performed. 126 // Currently, only weekly backup is supported. 127 PeriodType string `json:"period_type" required:"true"` 128 129 // Day in a week on which backup starts. 130 // Range: 1–7. Where: 1 indicates Monday; 7 indicates Sunday. 131 BackupAt []int `json:"backup_at" required:"true"` 132 } 133 134 type ListDcsInstanceOpts struct { 135 Id string `q:"id"` 136 Name string `q:"name"` 137 Type string `q:"type"` 138 DataStoreType string `q:"datastore_type"` 139 VpcId string `q:"vpc_id"` 140 SubnetId string `q:"subnet_id"` 141 Offset int `q:"offset"` 142 Limit int `q:"limit"` 143 } 144 145 type ListDcsBuilder interface { 146 ToDcsListDetailQuery() (string, error) 147 } 148 149 func (opts ListDcsInstanceOpts) ToDcsListDetailQuery() (string, error) { 150 q, err := golangsdk.BuildQueryString(opts) 151 if err != nil { 152 return "", err 153 } 154 return q.String(), err 155 } 156 157 // ToInstanceCreateMap is used for type convert 158 func (ops CreateOps) ToInstanceCreateMap() (map[string]interface{}, error) { 159 return golangsdk.BuildRequestBody(ops, "") 160 } 161 162 // Create an instance with given parameters. 163 func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) { 164 b, err := ops.ToInstanceCreateMap() 165 if err != nil { 166 r.Err = err 167 return 168 } 169 170 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 171 OkCodes: []int{200}, 172 }) 173 return 174 } 175 176 // Delete an instance by id 177 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 178 _, r.Err = client.Delete(deleteURL(client, id), nil) 179 return 180 } 181 182 // UpdateOptsBuilder is an interface which can build the map paramter of update function 183 type UpdateOptsBuilder interface { 184 ToInstanceUpdateMap() (map[string]interface{}, error) 185 } 186 187 // UpdateOpts is a struct which represents the parameters of update function 188 type UpdateOpts struct { 189 // DCS instance name. 190 // An instance name is a string of 4–64 characters 191 // that contain letters, digits, underscores (_), and hyphens (-). 192 // An instance name must start with letters. 193 Name string `json:"name,omitempty"` 194 195 // Brief description of the DCS instance. 196 // A brief description supports up to 1024 characters. 197 Description *string `json:"description,omitempty"` 198 199 // Backup policy. 200 // This parameter is available for master/standby DCS instances. 201 InstanceBackupPolicy *InstanceBackupPolicy `json:"instance_backup_policy,omitempty"` 202 203 // Time at which the maintenance time window starts. 204 // Format: HH:mm:ss 205 MaintainBegin string `json:"maintain_begin,omitempty"` 206 207 // Time at which the maintenance time window ends. 208 // Format: HH:mm:ss 209 MaintainEnd string `json:"maintain_end,omitempty"` 210 211 // Security group ID. 212 SecurityGroupID string `json:"security_group_id,omitempty"` 213 } 214 215 // ToInstanceUpdateMap is used for type convert 216 func (opts UpdateOpts) ToInstanceUpdateMap() (map[string]interface{}, error) { 217 return golangsdk.BuildRequestBody(opts, "") 218 } 219 220 // Update is a method which can be able to update the instance 221 // via accessing to the service with Put method and parameters 222 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 223 body, err := opts.ToInstanceUpdateMap() 224 if err != nil { 225 r.Err = err 226 return 227 } 228 229 _, r.Err = client.Put(updateURL(client, id), body, nil, &golangsdk.RequestOpts{ 230 OkCodes: []int{204}, 231 }) 232 return 233 } 234 235 // Get a instance with detailed information by id 236 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 237 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 238 return 239 } 240 241 // UpdatePasswordOptsBuilder is an interface which can build the map paramter of update password function 242 type UpdatePasswordOptsBuilder interface { 243 ToPasswordUpdateMap() (map[string]interface{}, error) 244 } 245 246 // UpdatePasswordOpts is a struct which represents the parameters of update function 247 type UpdatePasswordOpts struct { 248 // Old password. It may be empty. 249 OldPassword string `json:"old_password" required:"true"` 250 251 // New password. 252 // Password complexity requirements: 253 // A string of 6–32 characters. 254 // Must be different from the old password. 255 // Contains at least two types of the following characters: 256 // Uppercase letters 257 // Lowercase letters 258 // Digits 259 // Special characters `~!@#$%^&*()-_=+\|[{}]:'",<.>/? 260 NewPassword string `json:"new_password" required:"true"` 261 } 262 263 // ToPasswordUpdateMap is used for type convert 264 func (opts UpdatePasswordOpts) ToPasswordUpdateMap() (map[string]interface{}, error) { 265 return golangsdk.BuildRequestBody(opts, "") 266 } 267 268 // UpdatePassword is updating password for a dcs instance 269 func UpdatePassword(client *golangsdk.ServiceClient, id string, opts UpdatePasswordOptsBuilder) (r UpdatePasswordResult) { 270 271 body, err := opts.ToPasswordUpdateMap() 272 if err != nil { 273 r.Err = err 274 return 275 } 276 277 _, r.Err = client.Put(passwordURL(client, id), body, &r.Body, &golangsdk.RequestOpts{ 278 OkCodes: []int{200}, 279 }) 280 return 281 } 282 283 // ExtendOptsBuilder is an interface which can build the map paramter of extend function 284 type ExtendOptsBuilder interface { 285 ToExtendMap() (map[string]interface{}, error) 286 } 287 288 // ExtendOpts is a struct which represents the parameters of extend function 289 type ExtendOpts struct { 290 // New specifications (memory space) of the DCS instance. 291 // The new specification value to which the DCS instance 292 // will be scaled up must be greater than the current specification value. 293 // Unit: GB. 294 NewCapacity int `json:"new_capacity" required:"true"` 295 296 // New order ID. 297 OrderID string `json:"order_id,omitempty"` 298 } 299 300 // ToExtendMap is used for type convert 301 func (opts ExtendOpts) ToExtendMap() (map[string]interface{}, error) { 302 return golangsdk.BuildRequestBody(opts, "") 303 } 304 305 // Extend is extending for a dcs instance 306 func Extend(client *golangsdk.ServiceClient, id string, opts ExtendOptsBuilder) (r ExtendResult) { 307 308 body, err := opts.ToExtendMap() 309 if err != nil { 310 r.Err = err 311 return 312 } 313 314 _, r.Err = client.Post(extendURL(client, id), body, nil, &golangsdk.RequestOpts{ 315 OkCodes: []int{204}, 316 }) 317 return 318 } 319 320 func List(client *golangsdk.ServiceClient, opts ListDcsBuilder) pagination.Pager { 321 url := listURL(client) 322 if opts != nil { 323 query, err := opts.ToDcsListDetailQuery() 324 325 if err != nil { 326 return pagination.Pager{Err: err} 327 } 328 url += query 329 } 330 331 pageDcsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 332 return DcsPage{pagination.SinglePageBase(r)} 333 }) 334 335 dcsheader := map[string]string{"Content-Type": "application/json"} 336 pageDcsList.Headers = dcsheader 337 return pageDcsList 338 }