github.com/gophercloud/gophercloud@v1.11.0/openstack/orchestration/v1/stacks/results.go (about)

     1  package stacks
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // CreatedStack represents the object extracted from a Create operation.
    12  type CreatedStack struct {
    13  	ID    string             `json:"id"`
    14  	Links []gophercloud.Link `json:"links"`
    15  }
    16  
    17  // CreateResult represents the result of a Create operation.
    18  type CreateResult struct {
    19  	gophercloud.Result
    20  }
    21  
    22  // Extract returns a pointer to a CreatedStack object and is called after a
    23  // Create operation.
    24  func (r CreateResult) Extract() (*CreatedStack, error) {
    25  	var s struct {
    26  		CreatedStack *CreatedStack `json:"stack"`
    27  	}
    28  	err := r.ExtractInto(&s)
    29  	return s.CreatedStack, err
    30  }
    31  
    32  // AdoptResult represents the result of an Adopt operation. AdoptResult has the
    33  // same form as CreateResult.
    34  type AdoptResult struct {
    35  	CreateResult
    36  }
    37  
    38  // StackPage is a pagination.Pager that is returned from a call to the List function.
    39  type StackPage struct {
    40  	pagination.SinglePageBase
    41  }
    42  
    43  // IsEmpty returns true if a ListResult contains no Stacks.
    44  func (r StackPage) IsEmpty() (bool, error) {
    45  	if r.StatusCode == 204 {
    46  		return true, nil
    47  	}
    48  
    49  	stacks, err := ExtractStacks(r)
    50  	return len(stacks) == 0, err
    51  }
    52  
    53  // ListedStack represents an element in the slice extracted from a List operation.
    54  type ListedStack struct {
    55  	CreationTime time.Time          `json:"-"`
    56  	Description  string             `json:"description"`
    57  	ID           string             `json:"id"`
    58  	Links        []gophercloud.Link `json:"links"`
    59  	Name         string             `json:"stack_name"`
    60  	Status       string             `json:"stack_status"`
    61  	StatusReason string             `json:"stack_status_reason"`
    62  	Tags         []string           `json:"tags"`
    63  	UpdatedTime  time.Time          `json:"-"`
    64  }
    65  
    66  func (r *ListedStack) UnmarshalJSON(b []byte) error {
    67  	type tmp ListedStack
    68  	var s struct {
    69  		tmp
    70  		CreationTime string `json:"creation_time"`
    71  		UpdatedTime  string `json:"updated_time"`
    72  	}
    73  
    74  	err := json.Unmarshal(b, &s)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	*r = ListedStack(s.tmp)
    80  
    81  	if s.CreationTime != "" {
    82  		t, err := time.Parse(time.RFC3339, s.CreationTime)
    83  		if err != nil {
    84  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime)
    85  			if err != nil {
    86  				return err
    87  			}
    88  		}
    89  		r.CreationTime = t
    90  	}
    91  
    92  	if s.UpdatedTime != "" {
    93  		t, err := time.Parse(time.RFC3339, s.UpdatedTime)
    94  		if err != nil {
    95  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime)
    96  			if err != nil {
    97  				return err
    98  			}
    99  		}
   100  		r.UpdatedTime = t
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  // ExtractStacks extracts and returns a slice of ListedStack. It is used while iterating
   107  // over a stacks.List call.
   108  func ExtractStacks(r pagination.Page) ([]ListedStack, error) {
   109  	var s struct {
   110  		ListedStacks []ListedStack `json:"stacks"`
   111  	}
   112  	err := (r.(StackPage)).ExtractInto(&s)
   113  	return s.ListedStacks, err
   114  }
   115  
   116  // RetrievedStack represents the object extracted from a Get operation.
   117  type RetrievedStack struct {
   118  	Capabilities        []interface{}            `json:"capabilities"`
   119  	CreationTime        time.Time                `json:"-"`
   120  	Description         string                   `json:"description"`
   121  	DisableRollback     bool                     `json:"disable_rollback"`
   122  	ID                  string                   `json:"id"`
   123  	Links               []gophercloud.Link       `json:"links"`
   124  	NotificationTopics  []interface{}            `json:"notification_topics"`
   125  	Outputs             []map[string]interface{} `json:"outputs"`
   126  	Parameters          map[string]string        `json:"parameters"`
   127  	Name                string                   `json:"stack_name"`
   128  	Status              string                   `json:"stack_status"`
   129  	StatusReason        string                   `json:"stack_status_reason"`
   130  	Tags                []string                 `json:"tags"`
   131  	TemplateDescription string                   `json:"template_description"`
   132  	Timeout             int                      `json:"timeout_mins"`
   133  	UpdatedTime         time.Time                `json:"-"`
   134  }
   135  
   136  func (r *RetrievedStack) UnmarshalJSON(b []byte) error {
   137  	type tmp RetrievedStack
   138  	var s struct {
   139  		tmp
   140  		CreationTime string `json:"creation_time"`
   141  		UpdatedTime  string `json:"updated_time"`
   142  	}
   143  
   144  	err := json.Unmarshal(b, &s)
   145  	if err != nil {
   146  		return err
   147  	}
   148  
   149  	*r = RetrievedStack(s.tmp)
   150  
   151  	if s.CreationTime != "" {
   152  		t, err := time.Parse(time.RFC3339, s.CreationTime)
   153  		if err != nil {
   154  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime)
   155  			if err != nil {
   156  				return err
   157  			}
   158  		}
   159  		r.CreationTime = t
   160  	}
   161  
   162  	if s.UpdatedTime != "" {
   163  		t, err := time.Parse(time.RFC3339, s.UpdatedTime)
   164  		if err != nil {
   165  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime)
   166  			if err != nil {
   167  				return err
   168  			}
   169  		}
   170  		r.UpdatedTime = t
   171  	}
   172  
   173  	return nil
   174  }
   175  
   176  // GetResult represents the result of a Get operation.
   177  type GetResult struct {
   178  	gophercloud.Result
   179  }
   180  
   181  // Extract returns a pointer to a RetrievedStack object and is called after a
   182  // Get operation.
   183  func (r GetResult) Extract() (*RetrievedStack, error) {
   184  	var s struct {
   185  		Stack *RetrievedStack `json:"stack"`
   186  	}
   187  	err := r.ExtractInto(&s)
   188  	return s.Stack, err
   189  }
   190  
   191  // UpdateResult represents the result of a Update operation.
   192  type UpdateResult struct {
   193  	gophercloud.ErrResult
   194  }
   195  
   196  // DeleteResult represents the result of a Delete operation.
   197  type DeleteResult struct {
   198  	gophercloud.ErrResult
   199  }
   200  
   201  // PreviewedStack represents the result of a Preview operation.
   202  type PreviewedStack struct {
   203  	Capabilities        []interface{}      `json:"capabilities"`
   204  	CreationTime        time.Time          `json:"-"`
   205  	Description         string             `json:"description"`
   206  	DisableRollback     bool               `json:"disable_rollback"`
   207  	ID                  string             `json:"id"`
   208  	Links               []gophercloud.Link `json:"links"`
   209  	Name                string             `json:"stack_name"`
   210  	NotificationTopics  []interface{}      `json:"notification_topics"`
   211  	Parameters          map[string]string  `json:"parameters"`
   212  	Resources           []interface{}      `json:"resources"`
   213  	TemplateDescription string             `json:"template_description"`
   214  	Timeout             int                `json:"timeout_mins"`
   215  	UpdatedTime         time.Time          `json:"-"`
   216  }
   217  
   218  func (r *PreviewedStack) UnmarshalJSON(b []byte) error {
   219  	type tmp PreviewedStack
   220  	var s struct {
   221  		tmp
   222  		CreationTime string `json:"creation_time"`
   223  		UpdatedTime  string `json:"updated_time"`
   224  	}
   225  
   226  	err := json.Unmarshal(b, &s)
   227  	if err != nil {
   228  		return err
   229  	}
   230  
   231  	*r = PreviewedStack(s.tmp)
   232  
   233  	if s.CreationTime != "" {
   234  		t, err := time.Parse(time.RFC3339, s.CreationTime)
   235  		if err != nil {
   236  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.CreationTime)
   237  			if err != nil {
   238  				return err
   239  			}
   240  		}
   241  		r.CreationTime = t
   242  	}
   243  
   244  	if s.UpdatedTime != "" {
   245  		t, err := time.Parse(time.RFC3339, s.UpdatedTime)
   246  		if err != nil {
   247  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.UpdatedTime)
   248  			if err != nil {
   249  				return err
   250  			}
   251  		}
   252  		r.UpdatedTime = t
   253  	}
   254  
   255  	return nil
   256  }
   257  
   258  // PreviewResult represents the result of a Preview operation.
   259  type PreviewResult struct {
   260  	gophercloud.Result
   261  }
   262  
   263  // Extract returns a pointer to a PreviewedStack object and is called after a
   264  // Preview operation.
   265  func (r PreviewResult) Extract() (*PreviewedStack, error) {
   266  	var s struct {
   267  		PreviewedStack *PreviewedStack `json:"stack"`
   268  	}
   269  	err := r.ExtractInto(&s)
   270  	return s.PreviewedStack, err
   271  }
   272  
   273  // AbandonedStack represents the result of an Abandon operation.
   274  type AbandonedStack struct {
   275  	Status             string                 `json:"status"`
   276  	Name               string                 `json:"name"`
   277  	Template           map[string]interface{} `json:"template"`
   278  	Action             string                 `json:"action"`
   279  	ID                 string                 `json:"id"`
   280  	Resources          map[string]interface{} `json:"resources"`
   281  	Files              map[string]string      `json:"files"`
   282  	StackUserProjectID string                 `json:"stack_user_project_id"`
   283  	ProjectID          string                 `json:"project_id"`
   284  	Environment        map[string]interface{} `json:"environment"`
   285  }
   286  
   287  // AbandonResult represents the result of an Abandon operation.
   288  type AbandonResult struct {
   289  	gophercloud.Result
   290  }
   291  
   292  // Extract returns a pointer to an AbandonedStack object and is called after an
   293  // Abandon operation.
   294  func (r AbandonResult) Extract() (*AbandonedStack, error) {
   295  	var s *AbandonedStack
   296  	err := r.ExtractInto(&s)
   297  	return s, err
   298  }
   299  
   300  // String converts an AbandonResult to a string. This is useful to when passing
   301  // the result of an Abandon operation to an AdoptOpts AdoptStackData field.
   302  func (r AbandonResult) String() (string, error) {
   303  	out, err := json.Marshal(r)
   304  	return string(out), err
   305  }