github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cbr/v3/vaults/requests.go (about) 1 package vaults 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/openstack/common/tags" 6 "github.com/chnsz/golangsdk/pagination" 7 ) 8 9 type CreateOpts struct { 10 Billing *BillingCreate `json:"billing" required:"true"` 11 Name string `json:"name" required:"true"` 12 Resources []ResourceCreate `json:"resources" required:"true"` 13 AutoBind bool `json:"auto_bind,omitempty"` 14 AutoExpand bool `json:"auto_expand,omitempty"` 15 BackupNamePrefix string `json:"backup_name_prefix"` 16 BackupPolicyID string `json:"backup_policy_id,omitempty"` 17 BindRules *VaultBindRules `json:"bind_rules,omitempty"` 18 DemandBilling *bool `json:"demand_billing,omitempty"` 19 Description string `json:"description,omitempty"` 20 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 21 SmnNotify *bool `json:"smn_notify,omitempty"` 22 Tags []tags.ResourceTag `json:"tags,omitempty"` 23 Threshold int `json:"threshold,omitempty"` 24 } 25 26 type BillingCreate struct { 27 ConsistentLevel string `json:"consistent_level" required:"true"` 28 ObjectType string `json:"object_type" required:"true"` 29 ProtectType string `json:"protect_type" required:"true"` 30 Size int `json:"size" required:"true"` 31 ChargingMode string `json:"charging_mode,omitempty"` 32 CloudType string `json:"cloud_type,omitempty"` 33 ConsoleURL string `json:"console_url,omitempty"` 34 ExtraInfo *BillingCreateExtraInfo `json:"extra_info,omitempty"` 35 PeriodNum int `json:"period_num,omitempty"` 36 PeriodType string `json:"period_type,omitempty"` 37 IsAutoRenew bool `json:"is_auto_renew,omitempty"` 38 IsAutoPay bool `json:"is_auto_pay,omitempty"` 39 IsMultiAz bool `json:"is_multi_az,omitempty"` 40 } 41 42 type BillingCreateExtraInfo struct { 43 CombinedOrderECSNum int `json:"combined_order_ecs_num,omitempty"` 44 CombinedOrderID string `json:"combined_order_id,omitempty"` 45 } 46 47 type ResourceCreate struct { 48 ID string `json:"id" required:"true"` 49 Type string `json:"type" required:"true"` 50 Name string `json:"name,omitempty"` 51 ExtraInfo *ResourceExtraInfo `json:"extra_info,omitempty"` 52 } 53 54 type ResourceExtraInfo struct { 55 ExcludeVolumes []string `json:"exclude_volumes,omitempty"` 56 IncludeVolumes []ResourceExtraInfoIncludeVolumes `json:"include_volumes,omitempty"` 57 } 58 59 type ResourceExtraInfoIncludeVolumes struct { 60 ID string `json:"id" required:"true"` 61 OSVersion string `json:"os_version,omitempty"` 62 } 63 64 type VaultBindRules struct { 65 Tags []tags.ResourceTag `json:"tags,omitempty"` 66 } 67 68 type CreateOptsBuilder interface { 69 ToVaultCreateMap() (map[string]interface{}, error) 70 } 71 72 func (opts CreateOpts) ToVaultCreateMap() (map[string]interface{}, error) { 73 return golangsdk.BuildRequestBody(opts, "vault") 74 } 75 76 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 77 reqBody, err := opts.ToVaultCreateMap() 78 if err != nil { 79 r.Err = err 80 return 81 } 82 _, err = client.Post(rootURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{ 83 OkCodes: []int{200}, 84 }) 85 r.Err = err 86 return 87 } 88 89 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 90 _, r.Err = client.Delete(resourceURL(client, id), nil) 91 return 92 } 93 94 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 95 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 96 return 97 } 98 99 type UpdateOpts struct { 100 Billing *BillingUpdate `json:"billing,omitempty"` 101 Name string `json:"name,omitempty"` 102 AutoBind *bool `json:"auto_bind,omitempty"` 103 BindRules *VaultBindRules `json:"bind_rules,omitempty"` 104 AutoExpand *bool `json:"auto_expand,omitempty"` 105 SmnNotify *bool `json:"smn_notify,omitempty"` 106 Threshold int `json:"threshold,omitempty"` 107 } 108 109 type BillingUpdate struct { 110 // Vault specifications 111 // The valid values are as follows: 112 // + app_consistent 113 // + crash_consistent 114 ConsistentLevel string `json:"consistent_level,omitempty"` 115 // Vault size, in GB. 116 Size int `json:"size,omitempty"` 117 } 118 119 type UpdateOptsBuilder interface { 120 ToVaultUpdateMap() (map[string]interface{}, error) 121 } 122 123 func (opts UpdateOpts) ToVaultUpdateMap() (map[string]interface{}, error) { 124 return golangsdk.BuildRequestBody(opts, "vault") 125 } 126 127 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 128 reqBody, err := opts.ToVaultUpdateMap() 129 if err != nil { 130 r.Err = err 131 return 132 } 133 _, r.Err = client.Put(resourceURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{ 134 OkCodes: []int{200}, 135 }) 136 return 137 } 138 139 type ListOpts struct { 140 CloudType string `q:"cloud_type"` 141 EnterpriseProjectID string `q:"enterprise_project_id"` 142 ID string `q:"id"` 143 Limit int `q:"limit"` 144 Name string `q:"name"` 145 ObjectType string `q:"object_type"` 146 Offset int `q:"offset"` 147 PolicyID string `q:"policy_id"` 148 ProtectType string `q:"protect_type"` 149 ResourceIDs string `q:"resource_ids"` 150 Status string `q:"status"` 151 } 152 153 func (opts ListOpts) ToPolicyListQuery() (string, error) { 154 q, err := golangsdk.BuildQueryString(opts) 155 if err != nil { 156 return "", err 157 } 158 return q.String(), err 159 } 160 161 type ListOptsBuilder interface { 162 ToPolicyListQuery() (string, error) 163 } 164 165 // List is a method to obtain the specified CBR vaults according to the vault ID, vault name and so on. 166 // This method can also obtain all the CBR vaults through the default parameter settings. 167 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 168 url := rootURL(client) 169 if opts != nil { 170 query, err := opts.ToPolicyListQuery() 171 if err != nil { 172 return pagination.Pager{Err: err} 173 } 174 url += query 175 } 176 177 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 178 return VaultPage{pagination.SinglePageBase(r)} 179 }) 180 } 181 182 type BindPolicyOpts struct { 183 // The destination vault ID, only required if associate replication policy. 184 DestinationVaultId string `json:"destination_vault_id,omitempty"` 185 // The policy ID. 186 PolicyID string `json:"policy_id,omitempty"` 187 // The policy ID list. 188 PolicyIDs []string `json:"add_policy_ids,omitempty"` 189 } 190 191 func (opts BindPolicyOpts) ToBindPolicyMap() (map[string]interface{}, error) { 192 return golangsdk.BuildRequestBody(opts, "") 193 } 194 195 type BindPolicyOptsBuilder interface { 196 ToBindPolicyMap() (map[string]interface{}, error) 197 } 198 199 func BindPolicy(client *golangsdk.ServiceClient, vaultID string, opts BindPolicyOptsBuilder) (r BindPolicyResult) { 200 reqBody, err := opts.ToBindPolicyMap() 201 if err != nil { 202 r.Err = err 203 return 204 } 205 _, r.Err = client.Post(bindPolicyURL(client, vaultID), reqBody, &r.Body, &golangsdk.RequestOpts{ 206 OkCodes: []int{200}, 207 }) 208 return 209 } 210 211 func UnbindPolicy(client *golangsdk.ServiceClient, vaultID string, opts BindPolicyOptsBuilder) (r UnbindPolicyResult) { 212 reqBody, err := opts.ToBindPolicyMap() 213 if err != nil { 214 r.Err = err 215 return 216 } 217 _, r.Err = client.Post(unbindPolicyURL(client, vaultID), reqBody, &r.Body, &golangsdk.RequestOpts{ 218 OkCodes: []int{200}, 219 }) 220 return 221 } 222 223 type AssociateResourcesOpts struct { 224 Resources []ResourceCreate `json:"resources" required:"true"` 225 } 226 227 func (opts AssociateResourcesOpts) ToAssociateResourcesMap() (map[string]interface{}, error) { 228 return golangsdk.BuildRequestBody(opts, "") 229 } 230 231 type AssociateResourcesOptsBuilder interface { 232 ToAssociateResourcesMap() (map[string]interface{}, error) 233 } 234 235 func AssociateResources(client *golangsdk.ServiceClient, vaultID string, opts AssociateResourcesOptsBuilder) (r AssociateResourcesResult) { 236 reqBody, err := opts.ToAssociateResourcesMap() 237 if err != nil { 238 r.Err = err 239 return 240 } 241 _, r.Err = client.Post(addResourcesURL(client, vaultID), reqBody, &r.Body, &golangsdk.RequestOpts{ 242 OkCodes: []int{200}, 243 }) 244 return 245 } 246 247 type DissociateResourcesOpts struct { 248 ResourceIDs []string `json:"resource_ids" required:"true"` 249 } 250 251 func (opts DissociateResourcesOpts) ToDissociateResourcesMap() (map[string]interface{}, error) { 252 return golangsdk.BuildRequestBody(opts, "") 253 } 254 255 type DissociateResourcesOptsBuilder interface { 256 ToDissociateResourcesMap() (map[string]interface{}, error) 257 } 258 259 func DissociateResources(client *golangsdk.ServiceClient, vaultID string, opts DissociateResourcesOptsBuilder) (r DissociateResourcesResult) { 260 reqBody, err := opts.ToDissociateResourcesMap() 261 if err != nil { 262 r.Err = err 263 return 264 } 265 _, r.Err = client.Post(removeResourcesURL(client, vaultID), reqBody, &r.Body, &golangsdk.RequestOpts{ 266 OkCodes: []int{200}, 267 }) 268 return 269 }