1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
package stacks
import (
"strings"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// CreateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Create operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type CreateOptsBuilder interface {
ToStackCreateMap() (map[string]interface{}, error)
}
// CreateOpts is the common options struct used in this package's Create
// operation.
type CreateOpts struct {
// The name of the stack. It must start with an alphabetic character.
Name string `json:"stack_name" required:"true"`
// A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
TemplateOpts *Template `json:"-" required:"true"`
// Enables or disables deletion of all stack resources when a stack
// creation fails. Default is true, meaning all resources are not deleted when
// stack creation fails.
DisableRollback *bool `json:"disable_rollback,omitempty"`
// A structure that contains details for the environment of the stack.
EnvironmentOpts *Environment `json:"-"`
// User-defined parameters to pass to the template.
Parameters map[string]interface{} `json:"parameters,omitempty"`
// The timeout for stack creation in minutes.
Timeout int `json:"timeout_mins,omitempty"`
// A list of tags to assosciate with the Stack
Tags []string `json:"-"`
}
// ToStackCreateMap casts a CreateOpts struct to a map.
func (opts CreateOpts) ToStackCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if err := opts.TemplateOpts.Parse(); err != nil {
return nil, err
}
if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
return nil, err
}
opts.TemplateOpts.fixFileRefs()
b["template"] = string(opts.TemplateOpts.Bin)
files := make(map[string]string)
for k, v := range opts.TemplateOpts.Files {
files[k] = v
}
if opts.EnvironmentOpts != nil {
if err := opts.EnvironmentOpts.Parse(); err != nil {
return nil, err
}
if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
return nil, err
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
files[k] = v
}
b["environment"] = string(opts.EnvironmentOpts.Bin)
}
if len(files) > 0 {
b["files"] = files
}
if opts.Tags != nil {
b["tags"] = strings.Join(opts.Tags, ",")
}
return b, nil
}
// Create accepts a CreateOpts struct and creates a new stack using the values
// provided.
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToStackCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Post(createURL(c), b, &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// AdoptOptsBuilder is the interface options structs have to satisfy in order
// to be used in the Adopt function in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type AdoptOptsBuilder interface {
ToStackAdoptMap() (map[string]interface{}, error)
}
// AdoptOpts is the common options struct used in this package's Adopt
// operation.
type AdoptOpts struct {
// Existing resources data represented as a string to add to the
// new stack. Data returned by Abandon could be provided as AdoptsStackData.
AdoptStackData string `json:"adopt_stack_data" required:"true"`
// The name of the stack. It must start with an alphabetic character.
Name string `json:"stack_name" required:"true"`
// A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
TemplateOpts *Template `json:"-" required:"true"`
// The timeout for stack creation in minutes.
Timeout int `json:"timeout_mins,omitempty"`
// A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
//TemplateOpts *Template `json:"-" required:"true"`
// Enables or disables deletion of all stack resources when a stack
// creation fails. Default is true, meaning all resources are not deleted when
// stack creation fails.
DisableRollback *bool `json:"disable_rollback,omitempty"`
// A structure that contains details for the environment of the stack.
EnvironmentOpts *Environment `json:"-"`
// User-defined parameters to pass to the template.
Parameters map[string]interface{} `json:"parameters,omitempty"`
}
// ToStackAdoptMap casts a CreateOpts struct to a map.
func (opts AdoptOpts) ToStackAdoptMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if err := opts.TemplateOpts.Parse(); err != nil {
return nil, err
}
if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
return nil, err
}
opts.TemplateOpts.fixFileRefs()
b["template"] = string(opts.TemplateOpts.Bin)
files := make(map[string]string)
for k, v := range opts.TemplateOpts.Files {
files[k] = v
}
if opts.EnvironmentOpts != nil {
if err := opts.EnvironmentOpts.Parse(); err != nil {
return nil, err
}
if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
return nil, err
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
files[k] = v
}
b["environment"] = string(opts.EnvironmentOpts.Bin)
}
if len(files) > 0 {
b["files"] = files
}
return b, nil
}
// Adopt accepts an AdoptOpts struct and creates a new stack using the resources
// from another stack.
func Adopt(c *gophercloud.ServiceClient, opts AdoptOptsBuilder) (r AdoptResult) {
b, err := opts.ToStackAdoptMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Post(adoptURL(c), b, &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// SortDir is a type for specifying in which direction to sort a list of stacks.
type SortDir string
// SortKey is a type for specifying by which key to sort a list of stacks.
type SortKey string
var (
// SortAsc is used to sort a list of stacks in ascending order.
SortAsc SortDir = "asc"
// SortDesc is used to sort a list of stacks in descending order.
SortDesc SortDir = "desc"
// SortName is used to sort a list of stacks by name.
SortName SortKey = "name"
// SortStatus is used to sort a list of stacks by status.
SortStatus SortKey = "status"
// SortCreatedAt is used to sort a list of stacks by date created.
SortCreatedAt SortKey = "created_at"
// SortUpdatedAt is used to sort a list of stacks by date updated.
SortUpdatedAt SortKey = "updated_at"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToStackListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the network attributes you want to see returned.
type ListOpts struct {
// TenantID is the UUID of the tenant. A tenant is also known as
// a project.
TenantID string `q:"tenant_id"`
// ID filters the stack list by a stack ID
ID string `q:"id"`
// Status filters the stack list by a status.
Status string `q:"status"`
// Name filters the stack list by a name.
Name string `q:"name"`
// Marker is the ID of last-seen item.
Marker string `q:"marker"`
// Limit is an integer value for the limit of values to return.
Limit int `q:"limit"`
// SortKey allows you to sort by stack_name, stack_status, creation_time, or
// update_time key.
SortKey SortKey `q:"sort_keys"`
// SortDir sets the direction, and is either `asc` or `desc`.
SortDir SortDir `q:"sort_dir"`
// AllTenants is a bool to show all tenants.
AllTenants bool `q:"global_tenant"`
// ShowDeleted set to `true` to include deleted stacks in the list.
ShowDeleted bool `q:"show_deleted"`
// ShowNested set to `true` to include nested stacks in the list.
ShowNested bool `q:"show_nested"`
// ShowHidden set to `true` to include hiddened stacks in the list.
ShowHidden bool `q:"show_hidden"`
// Tags lists stacks that contain one or more simple string tags.
Tags string `q:"tags"`
// TagsAny lists stacks that contain one or more simple string tags.
TagsAny string `q:"tags_any"`
// NotTags lists stacks that do not contain one or more simple string tags.
NotTags string `q:"not_tags"`
// NotTagsAny lists stacks that do not contain one or more simple string tags.
NotTagsAny string `q:"not_tags_any"`
}
// ToStackListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToStackListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), nil
}
// List returns a Pager which allows you to iterate over a collection of
// stacks. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToStackListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
createPage := func(r pagination.PageResult) pagination.Page {
return StackPage{pagination.SinglePageBase(r)}
}
return pagination.NewPager(c, url, createPage)
}
// Get retreives a stack based on the stack name and stack ID.
func Get(c *gophercloud.ServiceClient, stackName, stackID string) (r GetResult) {
resp, err := c.Get(getURL(c, stackName, stackID), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Find retrieves a stack based on the stack name or stack ID.
func Find(c *gophercloud.ServiceClient, stackIdentity string) (r GetResult) {
resp, err := c.Get(findURL(c, stackIdentity), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// UpdateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the Update operation in this package.
type UpdateOptsBuilder interface {
ToStackUpdateMap() (map[string]interface{}, error)
}
// UpdatePatchOptsBuilder is the interface options structs have to satisfy in order
// to be used in the UpdatePatch operation in this package
type UpdatePatchOptsBuilder interface {
ToStackUpdatePatchMap() (map[string]interface{}, error)
}
// UpdateOpts contains the common options struct used in this package's Update
// and UpdatePatch operations.
type UpdateOpts struct {
// A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
TemplateOpts *Template `json:"-"`
// A structure that contains details for the environment of the stack.
EnvironmentOpts *Environment `json:"-"`
// User-defined parameters to pass to the template.
Parameters map[string]interface{} `json:"parameters,omitempty"`
// The timeout for stack creation in minutes.
Timeout int `json:"timeout_mins,omitempty"`
// A list of tags to associate with the Stack
Tags []string `json:"-"`
}
// ToStackUpdateMap validates that a template was supplied and calls
// the toStackUpdateMap private function.
func (opts UpdateOpts) ToStackUpdateMap() (map[string]interface{}, error) {
if opts.TemplateOpts == nil {
return nil, ErrTemplateRequired{}
}
return toStackUpdateMap(opts)
}
// ToStackUpdatePatchMap calls the private function toStackUpdateMap
// directly.
func (opts UpdateOpts) ToStackUpdatePatchMap() (map[string]interface{}, error) {
return toStackUpdateMap(opts)
}
// ToStackUpdateMap casts a CreateOpts struct to a map.
func toStackUpdateMap(opts UpdateOpts) (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
files := make(map[string]string)
if opts.TemplateOpts != nil {
if err := opts.TemplateOpts.Parse(); err != nil {
return nil, err
}
if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
return nil, err
}
opts.TemplateOpts.fixFileRefs()
b["template"] = string(opts.TemplateOpts.Bin)
for k, v := range opts.TemplateOpts.Files {
files[k] = v
}
}
if opts.EnvironmentOpts != nil {
if err := opts.EnvironmentOpts.Parse(); err != nil {
return nil, err
}
if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
return nil, err
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
files[k] = v
}
b["environment"] = string(opts.EnvironmentOpts.Bin)
}
if len(files) > 0 {
b["files"] = files
}
if opts.Tags != nil {
b["tags"] = strings.Join(opts.Tags, ",")
}
return b, nil
}
// Update accepts an UpdateOpts struct and updates an existing stack using the
//
// http PUT verb with the values provided. opts.TemplateOpts is required.
func Update(c *gophercloud.ServiceClient, stackName, stackID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToStackUpdateMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Put(updateURL(c, stackName, stackID), b, nil, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Update accepts an UpdateOpts struct and updates an existing stack using the
//
// http PATCH verb with the values provided. opts.TemplateOpts is not required.
func UpdatePatch(c *gophercloud.ServiceClient, stackName, stackID string, opts UpdatePatchOptsBuilder) (r UpdateResult) {
b, err := opts.ToStackUpdatePatchMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Patch(updateURL(c, stackName, stackID), b, nil, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete deletes a stack based on the stack name and stack ID.
func Delete(c *gophercloud.ServiceClient, stackName, stackID string) (r DeleteResult) {
resp, err := c.Delete(deleteURL(c, stackName, stackID), nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// PreviewOptsBuilder is the interface options structs have to satisfy in order
// to be used in the Preview operation in this package.
type PreviewOptsBuilder interface {
ToStackPreviewMap() (map[string]interface{}, error)
}
// PreviewOpts contains the common options struct used in this package's Preview
// operation.
type PreviewOpts struct {
// The name of the stack. It must start with an alphabetic character.
Name string `json:"stack_name" required:"true"`
// The timeout for stack creation in minutes.
Timeout int `json:"timeout_mins" required:"true"`
// A structure that contains either the template file or url. Call the
// associated methods to extract the information relevant to send in a create request.
TemplateOpts *Template `json:"-" required:"true"`
// Enables or disables deletion of all stack resources when a stack
// creation fails. Default is true, meaning all resources are not deleted when
// stack creation fails.
DisableRollback *bool `json:"disable_rollback,omitempty"`
// A structure that contains details for the environment of the stack.
EnvironmentOpts *Environment `json:"-"`
// User-defined parameters to pass to the template.
Parameters map[string]interface{} `json:"parameters,omitempty"`
}
// ToStackPreviewMap casts a PreviewOpts struct to a map.
func (opts PreviewOpts) ToStackPreviewMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if err := opts.TemplateOpts.Parse(); err != nil {
return nil, err
}
if err := opts.TemplateOpts.getFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
return nil, err
}
opts.TemplateOpts.fixFileRefs()
b["template"] = string(opts.TemplateOpts.Bin)
files := make(map[string]string)
for k, v := range opts.TemplateOpts.Files {
files[k] = v
}
if opts.EnvironmentOpts != nil {
if err := opts.EnvironmentOpts.Parse(); err != nil {
return nil, err
}
if err := opts.EnvironmentOpts.getRRFileContents(ignoreIfEnvironment); err != nil {
return nil, err
}
opts.EnvironmentOpts.fixFileRefs()
for k, v := range opts.EnvironmentOpts.Files {
files[k] = v
}
b["environment"] = string(opts.EnvironmentOpts.Bin)
}
if len(files) > 0 {
b["files"] = files
}
return b, nil
}
// Preview accepts a PreviewOptsBuilder interface and creates a preview of a stack using the values
// provided.
func Preview(c *gophercloud.ServiceClient, opts PreviewOptsBuilder) (r PreviewResult) {
b, err := opts.ToStackPreviewMap()
if err != nil {
r.Err = err
return
}
resp, err := c.Post(previewURL(c), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Abandon deletes the stack with the provided stackName and stackID, but leaves its
// resources intact, and returns data describing the stack and its resources.
func Abandon(c *gophercloud.ServiceClient, stackName, stackID string) (r AbandonResult) {
resp, err := c.Delete(abandonURL(c, stackName, stackID), &gophercloud.RequestOpts{
JSONResponse: &r.Body,
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
|